001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005:
006: /*
007: * Created on Feb 2, 2004
008: *
009: * To change the template for this generated file go to
010: * Window - Preferences - Java - Code Generation - Code and Comments
011: */
012: package org.vfny.geoserver.action;
013:
014: /**
015: * HTMLEncoder purpose.
016: *
017: * <p>
018: * This is a class taken from the java.sun.com Developer forums that can be
019: * used to encode/decode a String to/from HTML text.
020: * </p>
021: *
022: * <p>
023: * Capabilities:
024: * </p>
025: *
026: * <ul>
027: * <li>
028: * encode: Encodes a String into HTML text
029: * </li>
030: * <li>
031: * decode: Takes an HTML text and decodes it to a normal String
032: * </li>
033: * </ul>
034: *
035: * <p>
036: * Example Use:
037: * </p>
038: * <pre><code>
039: * HTMLEncoder.encode("K�nig"); //will return "König" (For JavaDocs: "K&ouml;nig")
040: * HTMLEncoder.decode("König"); //will return "K�nig"
041: * </code></pre>
042: *
043: * @author rgould, Refractions Research, Inc.
044: * @author $Author: dmzwiers $ (last modification)
045: * @version $Id: HTMLEncoder.java 7048 2007-06-19 16:34:53Z amanfredi $
046: */
047: public class HTMLEncoder {
048: private static java.util.Hashtable chars = null;
049:
050: /**
051: * Translate a String from HTML-text to a regular human-readable String
052: * @param val the String to be decoded
053: * @return
054: */
055: public static String decode(String val) {
056: if (val != null) {
057: StringBuffer buf = new StringBuffer(val.length() + 8);
058: char c;
059: char d;
060: String res;
061: int indice;
062:
063: for (int i = 0; i < val.length(); i++) {
064: c = val.charAt(i);
065:
066: switch (c) {
067: case '&':
068:
069: if ((i + 1) <= val.length()) {
070: res = val.substring(i);
071: /*
072: * drichard 2005-06-20 : indice should be relative to res :
073: indice = val.indexOf(';'); // Hasta el ;
074: */
075: indice = res.indexOf(';'); // Hasta el ;
076:
077: if (indice != -1) {
078: /*
079: * drichard 2005-06-20 : d should be relative to res :
080: d = val.charAt(1);
081: */
082: d = res.charAt(1);
083:
084: if (d == '#') {
085: res = res.substring(2, indice);
086:
087: try {
088: buf.append((char) Integer
089: .parseInt(res));
090: i = i + indice;
091:
092: break;
093: } catch (Throwable t) {
094: }
095:
096: // es un numero se coge hasta el ;
097: } else {
098: // No es un numero
099: res = res.substring(1, indice);
100:
101: try {
102: buf.append(getChar(res));
103: i = i + indice;
104:
105: break;
106: } catch (Throwable t) {
107: }
108: }
109: }
110: }
111:
112: buf.append(c);
113:
114: break;
115:
116: default:
117: buf.append(c);
118:
119: break;
120: }
121: }
122:
123: return buf.toString();
124: } else {
125: return new String("");
126: }
127: }
128:
129: /**
130: * Translate a String to an HTML-encoded version of itself
131: * @param val The String to be encoded
132: * @return
133: */
134: public static String encode(String val) {
135: if (val != null) {
136: StringBuffer buf = new StringBuffer(val.length() + 8);
137: char c;
138:
139: for (int i = 0; i < val.length(); i++) {
140: c = val.charAt(i);
141:
142: switch (c) {
143: case '<':
144: buf.append("<");
145:
146: break;
147:
148: case '>':
149: buf.append(">");
150:
151: break;
152:
153: case '&':
154: buf.append("&");
155:
156: break;
157:
158: case '\"':
159: buf.append(""");
160:
161: break;
162:
163: case '\'':
164: buf.append("'");
165:
166: break;
167:
168: // This shows up lower in the other numbered cases.
169:
170: /*case '�':
171: case '�':
172: case '�':
173: case '�':
174: case '�':
175: case '�':
176: case '�':
177: case '�':
178: case '�':
179: case '�':
180: buf.append(c + "´");
181:
182: break;*/
183: case 160:
184: case 161:
185: case 162:
186: case 163:
187: case 164:
188: case 165:
189: case 166:
190: case 167:
191: case 168:
192: case 169:
193: case 170:
194: case 171:
195: case 172:
196: case 173:
197: case 174:
198: case 175:
199: case 176:
200: case 177:
201: case 178:
202: case 179:
203: case 180:
204: case 181:
205: case 182:
206: case 183:
207: case 184:
208: case 185:
209: case 186:
210: case 187:
211: case 188:
212: case 189:
213: case 190:
214: case 191:
215: case 192:
216: case 194:
217: case 195:
218: case 196:
219: case 197:
220: case 198:
221: case 199:
222: case 200:
223: case 202:
224: case 203:
225: case 204:
226: case 206:
227: case 207:
228: case 208:
229: case 209:
230: case 210:
231: case 212:
232: case 213:
233: case 214:
234: case 215:
235: case 216:
236: case 217:
237: case 219:
238: case 220:
239: case 221:
240: case 222:
241: case 223:
242: case 224:
243: case 226:
244: case 227:
245: case 228:
246: case 229:
247: case 230:
248: case 231:
249: case 232:
250: case 234:
251: case 235:
252: case 236:
253: case 238:
254: case 239:
255: case 240:
256: case 241:
257: case 242:
258: case 244:
259: case 245:
260: case 246:
261: case 247:
262: case 248:
263: case 249:
264: case 251:
265: case 252:
266: case 253:
267: case 254:
268: case 255:
269: buf.append("&#" + ((int) c) + ";");
270:
271: break;
272:
273: default:
274: buf.append(c);
275:
276: break;
277: }
278: }
279:
280: return buf.toString();
281: } else {
282: return new String("");
283: }
284: }
285:
286: private static char getChar(String res)
287: throws IllegalArgumentException {
288: loadChar();
289:
290: Character o = (Character) chars.get(res);
291:
292: if (o == null) {
293: throw new IllegalArgumentException("Inexistant char: "
294: + res);
295: } else {
296: return o.charValue();
297: }
298: }
299:
300: private static void loadChar() {
301: if (chars == null) {
302: chars = new java.util.Hashtable();
303: chars.put("amp", new Character((char) 38));
304: chars.put("lt", new Character((char) 60));
305: chars.put("gt", new Character((char) 62));
306: chars.put("nbsp", new Character((char) 160));
307: chars.put("iexcl", new Character((char) 161));
308: chars.put("cent", new Character((char) 162));
309: chars.put("pound", new Character((char) 163));
310: chars.put("curren", new Character((char) 164));
311: chars.put("yen", new Character((char) 165));
312: chars.put("brvbar", new Character((char) 166));
313: chars.put("sect", new Character((char) 167));
314: chars.put("uml", new Character((char) 168));
315: chars.put("copy", new Character((char) 169));
316: chars.put("ordf", new Character((char) 170));
317: chars.put("laquo", new Character((char) 171));
318: chars.put("not", new Character((char) 172));
319: chars.put("shy", new Character((char) 173));
320: chars.put("reg", new Character((char) 174));
321: chars.put("macr", new Character((char) 175));
322: chars.put("deg", new Character((char) 176));
323: chars.put("plusmn", new Character((char) 177));
324: chars.put("sup2", new Character((char) 178));
325: chars.put("sup3", new Character((char) 179));
326: chars.put("acute", new Character((char) 180));
327: chars.put("micro", new Character((char) 181));
328: chars.put("para", new Character((char) 182));
329: chars.put("middot", new Character((char) 183));
330: chars.put("cedil", new Character((char) 184));
331: chars.put("sup1", new Character((char) 185));
332: chars.put("ordm", new Character((char) 186));
333: chars.put("raquo", new Character((char) 187));
334: chars.put("frac14", new Character((char) 188));
335: chars.put("frac12", new Character((char) 189));
336: chars.put("frac34", new Character((char) 190));
337: chars.put("iquest", new Character((char) 191));
338: chars.put("Agrave", new Character((char) 192));
339: chars.put("Aacute", new Character((char) 193));
340: chars.put("Acirc", new Character((char) 194));
341: chars.put("Atilde", new Character((char) 195));
342: chars.put("Auml", new Character((char) 196));
343: chars.put("Aring", new Character((char) 197));
344: chars.put("AElig", new Character((char) 198));
345: chars.put("Ccedil", new Character((char) 199));
346: chars.put("Egrave", new Character((char) 200));
347: chars.put("Eacute", new Character((char) 201));
348: chars.put("Ecirc", new Character((char) 202));
349: chars.put("Euml", new Character((char) 203));
350: chars.put("Igrave", new Character((char) 204));
351: chars.put("Iacute", new Character((char) 205));
352: chars.put("Icirc", new Character((char) 206));
353: chars.put("Iuml", new Character((char) 207));
354: chars.put("ETH", new Character((char) 208));
355: chars.put("Ntilde", new Character((char) 209));
356: chars.put("Ograve", new Character((char) 210));
357: chars.put("Oacute", new Character((char) 211));
358: chars.put("Ocirc", new Character((char) 212));
359: chars.put("Otilde", new Character((char) 213));
360: chars.put("Ouml", new Character((char) 214));
361: chars.put("times", new Character((char) 215));
362: chars.put("Oslash", new Character((char) 216));
363: chars.put("Ugrave", new Character((char) 217));
364: chars.put("Uacute", new Character((char) 218));
365: chars.put("Ucirc", new Character((char) 219));
366: chars.put("Uuml", new Character((char) 220));
367: chars.put("Yacute", new Character((char) 221));
368: chars.put("THORN", new Character((char) 222));
369: chars.put("szlig", new Character((char) 223));
370: chars.put("agrave", new Character((char) 224));
371: chars.put("aacute", new Character((char) 225));
372: chars.put("acirc", new Character((char) 226));
373: chars.put("atilde", new Character((char) 227));
374: chars.put("auml", new Character((char) 228));
375: chars.put("aring", new Character((char) 229));
376: chars.put("aelig", new Character((char) 230));
377: chars.put("ccedil", new Character((char) 231));
378: chars.put("egrave", new Character((char) 232));
379: chars.put("eacute", new Character((char) 233));
380: chars.put("ecirc", new Character((char) 234));
381: chars.put("euml", new Character((char) 235));
382: chars.put("igrave", new Character((char) 236));
383: chars.put("iacute", new Character((char) 237));
384: chars.put("icirc", new Character((char) 238));
385: chars.put("iuml", new Character((char) 239));
386: chars.put("eth", new Character((char) 240));
387: chars.put("ntilde", new Character((char) 241));
388: chars.put("ograve", new Character((char) 242));
389: chars.put("oacute", new Character((char) 243));
390: chars.put("ocirc", new Character((char) 244));
391: chars.put("otilde", new Character((char) 245));
392: chars.put("ouml", new Character((char) 246));
393: chars.put("divide", new Character((char) 247));
394: chars.put("oslash", new Character((char) 248));
395: chars.put("ugrave", new Character((char) 249));
396: chars.put("uacute", new Character((char) 250));
397: chars.put("ucirc", new Character((char) 251));
398: chars.put("uuml", new Character((char) 252));
399: chars.put("yacute", new Character((char) 253));
400: chars.put("thorn", new Character((char) 254));
401: chars.put("yuml", new Character((char) 255));
402: }
403: }
404: }
|