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 package org.w3c.tidy.servlet.util;
56
57 import java.lang.reflect.Method;
58 import java.net.URLEncoder;
59 import java.util.Hashtable;
60 import java.util.Iterator;
61 import java.util.Map;
62
63
64 /***
65 * Converts a String to HTML by converting all special characters to HTML-entities.
66 * @author Vlad Skarzhevskyy <a href="mailto:skarzhevskyy@gmail.com">skarzhevskyy@gmail.com </a>
67 * @version $Revision: 1.5 $ ($Author: vlads $)
68 */
69
70 public class HTMLEncode
71 {
72
73 /***
74 * j2se 1.4 encode method, used by reflection if available.
75 */
76 private static Method encodeMethod14;
77
78 static
79 {
80
81
82 try
83 {
84 Class urlEncoderClass = Class.forName("java.net.URLEncoder");
85 encodeMethod14 = urlEncoderClass.getMethod("encode", new Class[]{String.class, String.class});
86 }
87 catch (Throwable ex)
88 {
89
90 }
91 }
92
93 /***
94 * Utility class, don't instantiate.
95 */
96 private HTMLEncode()
97 {
98
99 }
100
101 private static final String[] ENTITIES = {
102 ">",
103 ">",
104 "<",
105 "<",
106 "&",
107 "&",
108 "\"",
109 """,
110 "'",
111 "'",
112 "//",
113 "\",
114 "\u00a9",
115 "©",
116 "\u00ae",
117 "®"};
118
119 private static Hashtable entityTableEncode = null;
120
121 protected static synchronized void buildEntityTables()
122 {
123 entityTableEncode = new Hashtable(ENTITIES.length);
124
125 for (int i = 0; i < ENTITIES.length; i += 2)
126 {
127 if (!entityTableEncode.containsKey(ENTITIES[i]))
128 {
129 entityTableEncode.put(ENTITIES[i], ENTITIES[i + 1]);
130 }
131 }
132 }
133
134 /***
135 * Converts a String to HTML by converting all special characters to HTML-entities.
136 */
137 public final static String encode(String s)
138 {
139 return encode(s, "\n");
140 }
141
142 /***
143 * Converts a String to HTML by converting all special characters to HTML-entities.
144 */
145 public final static String encode(String s, String cr)
146 {
147 if (entityTableEncode == null)
148 {
149 buildEntityTables();
150 }
151 if (s == null)
152 {
153 return "";
154 }
155 StringBuffer sb = new StringBuffer(s.length() * 2);
156 char ch;
157 for (int i = 0; i < s.length(); ++i)
158 {
159 ch = s.charAt(i);
160 if ((ch >= 63 && ch <= 90) || (ch >= 97 && ch <= 122) || (ch == ' '))
161 {
162 sb.append(ch);
163 }
164 else if (ch == '\n')
165 {
166 sb.append(cr);
167 }
168 else
169 {
170 String chEnc = encodeSingleChar(String.valueOf(ch));
171 if (chEnc != null)
172 {
173 sb.append(chEnc);
174 }
175 else
176 {
177
178 sb.append("&#");
179 sb.append(new Integer(ch).toString());
180 sb.append(';');
181 }
182 }
183 }
184 return sb.toString();
185 }
186
187 /***
188 * Converts a single character to HTML
189 */
190 private static String encodeSingleChar(String ch)
191 {
192 return (String) entityTableEncode.get(ch);
193 }
194
195 /***
196 * Converts a String to valid HTML HREF by converting all special characters to HTML-entities.
197 * @param url url to be encoded
198 * @return encoded url.
199 */
200 protected static String encodeHREFParam(String url)
201 {
202 if (encodeMethod14 != null)
203 {
204 Object[] methodArgs = new Object[2];
205 methodArgs[0] = url;
206
207 methodArgs[1] = "UTF8";
208
209 try
210 {
211 return (String) encodeMethod14.invoke(null, methodArgs);
212 }
213 catch (Throwable e)
214 {
215 throw new RuntimeException("Error invoking 1.4 URLEncoder.encode with reflection: " + e.getMessage());
216 }
217 }
218
219
220 return URLEncoder.encode(url);
221
222 }
223
224 protected static String encodeHREFParamJava13(String value)
225 {
226 return URLEncoder.encode(value);
227 }
228
229 public static String encodeQuery(String url, String[] args)
230 {
231 return encodeHREFQuery(url, args, false);
232 }
233
234 public static String encodeHREFQuery(String url, String[] args)
235 {
236 return encodeHREFQuery(url, args, true);
237 }
238
239 public static String encodeHREFQuery(String url, String[] args, boolean forHtml)
240 {
241 StringBuffer out = new StringBuffer(128);
242 out.append(url);
243
244 if ((args != null) && (args.length > 0))
245 {
246 out.append("?");
247 for (int i = 0; i < (args.length + 1) / 2; i++)
248 {
249 int k = i * 2;
250 if (k != 0)
251 {
252 if (forHtml)
253 {
254 out.append("&");
255 }
256 else
257 {
258 out.append("&");
259 }
260 }
261 out.append(encodeHREFParam(args[k]));
262 if (k + 1 < args.length)
263 {
264 out.append("=");
265 out.append(encodeHREFParam(args[k + 1]));
266 }
267 }
268 }
269 return out.toString();
270 }
271
272 public static String encodeHREFQuery(String url, Map args, boolean forHtml)
273 {
274 StringBuffer out = new StringBuffer(128);
275 out.append(url);
276
277 if ((args != null) && (args.size() > 0))
278 {
279 out.append("?");
280 int k = 0;
281 for (Iterator i = args.keySet().iterator(); i.hasNext();)
282 {
283 if (k != 0)
284 {
285 if (forHtml)
286 {
287 out.append("&");
288 }
289 else
290 {
291 out.append("&");
292 }
293 }
294 String name = (String) i.next();
295 out.append(encodeHREFParam(name));
296 out.append("=");
297 out.append(encodeHREFParam((String) args.get(name)));
298 k++;
299 }
300 }
301 return out.toString();
302 }
303 }