KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > util > TextUtil


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 /*
6 * WebWork, Web Application Framework
7 *
8 * Distributable under Apache license.
9 * See terms of license at opensource.org
10 */

11 package com.opensymphony.webwork.views.util;
12
13
14 /**
15  * This class handles HTML escaping of text.
16  * It was written and optimized to be as fast as possible.
17  *
18  * @author Dick Zetterberg (dick@transitor.se)
19  * @version $Revision: 1.2 $
20  */

21 public class TextUtil {
22     //~ Static fields/initializers /////////////////////////////////////////////
23

24     protected static final int MAX_LENGTH = 300;
25
26     /**
27      * We use arrays of char in the lookup table because it is faster
28      * appending this to a StringBuffer than appending a String
29      */

30     protected static final char[][] _stringChars = new char[MAX_LENGTH][];
31
32     static {
33         // Initialize the mapping table
34
initMapping();
35     }
36
37     //~ Methods ////////////////////////////////////////////////////////////////
38

39     /**
40      * Call escapeHTML(s, false)
41      */

42     public static final String JavaDoc escapeHTML(String JavaDoc s) {
43         return escapeHTML(s, false);
44     }
45
46     /**
47      * Escape HTML.
48      *
49      * @param s string to be escaped
50      * @param escapeEmpty if true, then empty string will be escaped.
51      */

52     public static final String JavaDoc escapeHTML(String JavaDoc s, boolean escapeEmpty) {
53         int len = s.length();
54
55         if (len == 0) {
56             return s;
57         }
58
59         if (!escapeEmpty) {
60             String JavaDoc trimmed = s.trim();
61
62             if ((trimmed.length() == 0) || ("\"\"").equals(trimmed)) {
63                 return s;
64             }
65         }
66
67         int i = 0;
68
69         // First loop through String and check if escaping is needed at all
70
// No buffers are copied at this time
71
do {
72             int index = s.charAt(i);
73
74             if (index >= MAX_LENGTH) {
75                 if (index != 0x20AC) { // If not euro symbol
76

77                     continue;
78                 }
79
80                 break;
81             } else if (_stringChars[index] != null) {
82                 break;
83             }
84         } while (++i < len);
85
86         // If the check went to the end with no escaping then i should be == len now
87
// otherwise we must continue escaping for real
88
if (i == len) {
89             return s;
90         }
91
92         // We found a character to escape and broke out at position i
93
// Now copy all characters before that to StringBuffer sb
94
// Since a char[] will be used for copying we might as well get
95
// a complete copy of it so that we can use array indexing instead of charAt
96
StringBuffer JavaDoc sb = new StringBuffer JavaDoc(len + 40);
97         char[] chars = new char[len];
98
99         // Copy all chars from the String s to the chars buffer
100
s.getChars(0, len, chars, 0);
101
102         // Append the first i characters that we have checked to the resulting StringBuffer
103
sb.append(chars, 0, i);
104
105         int last = i;
106         char[] subst;
107
108         for (; i < len; i++) {
109             char c = chars[i];
110             int index = c;
111
112             if (index < MAX_LENGTH) {
113                 subst = _stringChars[index];
114
115                 // It is faster to append a char[] than a String which is why we use this
116
if (subst != null) {
117                     if (i > last) {
118                         sb.append(chars, last, i - last);
119                     }
120
121                     sb.append(subst);
122                     last = i + 1;
123                 }
124             }
125             // Check if it is the euro symbol. This could be changed to check in a second lookup
126
// table in case one wants to convert more characters in that area
127
else if (index == 0x20AC) {
128                 if (i > last) {
129                     sb.append(chars, last, i - last);
130                 }
131
132                 sb.append("&euro;");
133                 last = i + 1;
134             }
135         }
136
137         if (i > last) {
138             sb.append(chars, last, i - last);
139         }
140
141         return sb.toString();
142     }
143
144     protected static void addMapping(int c, String JavaDoc txt, String JavaDoc[] strings) {
145         strings[c] = txt;
146     }
147
148     protected static void initMapping() {
149         String JavaDoc[] strings = new String JavaDoc[MAX_LENGTH];
150
151         addMapping(0x22, "&quot;", strings); // "
152
addMapping(0x26, "&amp;", strings); // &
153
addMapping(0x3c, "&lt;", strings); // <
154
addMapping(0x3e, "&gt;", strings); // >
155

156         addMapping(0xa1, "&iexcl;", strings); //
157
addMapping(0xa2, "&cent;", strings); //
158
addMapping(0xa3, "&pound;", strings); //
159
addMapping(0xa9, "&copy;", strings); // ©
160
addMapping(0xae, "&reg;", strings); // ®
161
addMapping(0xbf, "&iquest;", strings); //
162

163         addMapping(0xc0, "&Agrave;", strings); // À
164
addMapping(0xc1, "&Aacute;", strings); // Á
165
addMapping(0xc2, "&Acirc;", strings); // Â
166
addMapping(0xc3, "&Atilde;", strings); // Â
167
addMapping(0xc4, "&Auml;", strings); // Ä
168
addMapping(0xc5, "&Aring;", strings); // Å
169
addMapping(0xc6, "&AElig;", strings); // Æ
170
addMapping(0xc7, "&Ccedil;", strings); // Ç
171
addMapping(0xc8, "&Egrave;", strings); //
172
addMapping(0xc9, "&Eacute;", strings); //
173
addMapping(0xca, "&Ecirc;", strings); //
174
addMapping(0xcb, "&Euml;", strings); //
175
addMapping(0xcc, "&Igrave;", strings); //
176
addMapping(0xcd, "&Iacute;", strings); //
177
addMapping(0xce, "&Icirc;", strings); //
178
addMapping(0xcf, "&Iuml;", strings); //
179

180         addMapping(0xd0, "&ETH;", strings); //
181
addMapping(0xd1, "&Ntilde;", strings); //
182
addMapping(0xd2, "&Ograve;", strings); //
183
addMapping(0xd3, "&Oacute;", strings); //
184
addMapping(0xd4, "&Ocirc;", strings); //
185
addMapping(0xd5, "&Otilde;", strings); //
186
addMapping(0xd6, "&Ouml;", strings); // Ö
187
addMapping(0xd7, "&times;", strings); //
188
addMapping(0xd8, "&Oslash;", strings); //
189
addMapping(0xd9, "&Ugrave;", strings); //
190
addMapping(0xda, "&Uacute;", strings); //
191
addMapping(0xdb, "&Ucirc;", strings); //
192
addMapping(0xdc, "&Uuml;", strings); //
193
addMapping(0xdd, "&Yacute;", strings); //
194
addMapping(0xde, "&THORN;", strings); //
195
addMapping(0xdf, "&szlig;", strings); //
196

197         addMapping(0xe0, "&agrave;", strings); //
198
addMapping(0xe1, "&aacute;", strings); //
199
addMapping(0xe2, "&acirc;", strings); //
200
addMapping(0xe3, "&atilde;", strings); //
201
addMapping(0xe4, "&auml;", strings); // ä
202
addMapping(0xe5, "&aring;", strings); // å
203
addMapping(0xe6, "&aelig;", strings); //
204
addMapping(0xe7, "&ccedil;", strings); //
205
addMapping(0xe8, "&egrave;", strings); //
206
addMapping(0xe9, "&eacute;", strings); //
207
addMapping(0xea, "&ecirc;", strings); //
208
addMapping(0xeb, "&euml;", strings); //
209
addMapping(0xec, "&igrave;", strings); //
210
addMapping(0xed, "&iacute;", strings); //
211
addMapping(0xee, "&icirc;", strings); //
212
addMapping(0xef, "&iuml;", strings); //
213

214         addMapping(0xf0, "&eth;", strings); //
215
addMapping(0xf1, "&ntilde;", strings); //
216
addMapping(0xf2, "&ograve;", strings); //
217
addMapping(0xf3, "&oacute;", strings); //
218
addMapping(0xf4, "&ocirc;", strings); //
219
addMapping(0xf5, "&otilde;", strings); //
220
addMapping(0xf6, "&ouml;", strings); // ö
221
addMapping(0xf7, "&divide;", strings); //
222
addMapping(0xf8, "&oslash;", strings); //
223
addMapping(0xf9, "&ugrave;", strings); //
224
addMapping(0xfa, "&uacute;", strings); //
225
addMapping(0xfb, "&ucirc;", strings); //
226
addMapping(0xfc, "&uuml;", strings); //
227
addMapping(0xfd, "&yacute;", strings); //
228
addMapping(0xfe, "&thorn;", strings); //
229
addMapping(0xff, "&yuml;", strings); //
230

231         for (int i = 0; i < strings.length; i++) {
232             String JavaDoc str = strings[i];
233
234             if (str != null) {
235                 _stringChars[i] = str.toCharArray();
236             }
237         }
238     }
239 }
240
Popular Tags