Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.commonfoundation.html;

import org.litesoft.commonfoundation.base.*;

public class HTMLize implements HTMLConstants
{
    /**
     * HTML Encodes any markup in input (Spaces are left as Spaces).
     *
     * @param pText String to be processed.
     *
     * @return Processed pText with any markup properly HTML Encoded.
     */
    public static String escape( String pText )
    {
        return INSTANCE.process( pText );
    }

    /**
     * HTML Encodes any markup in input (Spaces are converted to  ).
     *
     * @param pText String to be processed.
     *
     * @return Processed pText with any markup properly HTML Encoded.
     */
    public static String escapeNoWrap( String pText )
    {
        return INSTANCE_NO_WRAP.process( pText );
    }

    public static final HTMLize INSTANCE = new HTMLize();
    public static final HTMLize INSTANCE_NO_WRAP = new HTMLize()
    {
        @Override
        protected String space()
        {
            return NBSP;
        }
    };
    public static final HTMLize INSTANCE_NO_ESCAPE = new HTMLize()
    {
        @Override
        public String process( String pText )
        {
            return pText;
        }
    };

    public String process( String pText )
    {
        if ( (pText == null) || (pText.length() == 0) )
        {
            return "";
        }
        StringBuilder zSB = new StringBuilder();
        for ( int i = 0; i < pText.length(); i++ )
        {
            char c = pText.charAt( i );
            switch ( c )
            {
                case ' ':
                    zSB.append( space() );
                    break;
                case '&':
                    zSB.append( AMPERSAND );
                    break;
                case '<':
                    zSB.append( LESS_THAN );
                    break;
                case '>':
                    zSB.append( GREATER_THAN );
                    break;
                case '"':
                    zSB.append( DOUBLE_QUOTE );
                    break;
                case '\n':
                    zSB.append( HTML_BR );
                    break;
                default:
                    if ( (' ' < c) && (c < 127) )
                    {
                        zSB.append( c );
                    }
                    else
                    {
                        zSB.append( "&#x" );
                        char[] chars = Hex.to4Chars( c );
                        for ( char aChar : chars )
                        {
                            zSB.append( aChar );
                        }
                        zSB.append( ';' );
                    }
                    break;
            }
        }
        return zSB.toString();
    }

    protected HTMLize()
    {
    }

    protected String space()
    {
        return " ";
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/commonfoundation/html/HTMLize.java

Diff revisions: vs.
Revision Author Commited Message
942 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 23:41:46 +0000

Extracting commonfoundation

811 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 13:45:18 +0000
712 Diff Diff GeorgeS picture GeorgeS Sat 09 Jun, 2012 22:46:04 +0000

Move PAV stuff into LiteSoft

517 Diff Diff GeorgeS picture GeorgeS Mon 26 Sep, 2011 22:36:28 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000