001: package net.sf.regain.util.io;
002:
003: /* ====================================================================
004: * The Apache Software License, Version 1.1
005: *
006: * Copyright (c) 2001 The Apache Software Foundation. All rights
007: * reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution,
022: * if any, must include the following acknowledgment:
023: * "This product includes software developed by the
024: * Apache Software Foundation (http://www.apache.org/)."
025: * Alternately, this acknowledgment may appear in the software itself,
026: * if and wherever such third-party acknowledgments normally appear.
027: *
028: * 4. The names "Apache" and "Apache Software Foundation" and
029: * "Apache Lucene" must not be used to endorse or promote products
030: * derived from this software without prior written permission. For
031: * written permission, please contact apache@apache.org.
032: *
033: * 5. Products derived from this software may not be called "Apache",
034: * "Apache Lucene", nor may "Apache" appear in their name, without
035: * prior written permission of the Apache Software Foundation.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
041: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
042: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
043: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
044: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
045: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
046: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
047: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
048: * SUCH DAMAGE.
049: * ====================================================================
050: *
051: * This software consists of voluntary contributions made by many
052: * individuals on behalf of the Apache Software Foundation. For more
053: * information on the Apache Software Foundation, please see
054: * <http://www.apache.org/>.
055: *
056: * CVS information:
057: * $RCSfile$
058: * $Source$
059: * $Date: 2005-03-14 16:04:35 +0100 (Mo, 14 Mrz 2005) $
060: * $Author: til132 $
061: * $Revision: 82 $
062: */
063:
064: import java.util.Hashtable;
065:
066: /**
067: * Diese Klasse habe ich (praktisch) unverändert aus der Lucene Demo übernommen.
068: * <p>
069: * Sie kann HTML-Entitäten in ihre Entsprechungen übersetzten und umgekehrt.
070: * <p>
071: * Für's Hinterköpfchen: Eine HTML-Entität ist z.B. '&' oder 'Ü', diese
072: * wird dann vom Browser in z.B. '&' oder 'Ü' übersetzt.
073: */
074: public class HtmlEntities {
075:
076: /**
077: * Enthält für eine Entität (key als String) seine Entsprechung (value als
078: * Character).
079: */
080: static final Hashtable decoder = new Hashtable(300);
081:
082: /**
083: * Enthält für einen char-Wert (index) eine Entität (als String) oder
084: * <CODE>null</CODE>.
085: */
086: static final String[] encoder = new String[0x100];
087:
088: /**
089: * Dekodiert eine Entität in seine Klartext-Entsprechung.
090: * <p>
091: * Im Gegensatz zur Methode {@link #encode(String)}, nimmt diese Methode nur
092: * <I>eine</I> Entität entgegen. Der gegebene String darf keinen weiteren Text
093: * enthalten.
094: *
095: * @param entity Die zu übersetzende Entität.
096: *
097: * @return Die Klartext-Entsprechung.
098: */
099: public static final String decode(String entity) {
100: if (entity.charAt(entity.length() - 1) == ';') // remove trailing semicolon
101: entity = entity.substring(0, entity.length() - 1);
102: if (entity.charAt(1) == '#') {
103: int start = 2;
104: int radix = 10;
105: if (entity.charAt(2) == 'X' || entity.charAt(2) == 'x') {
106: start++;
107: radix = 16;
108: }
109: Character c = new Character((char) Integer.parseInt(entity
110: .substring(start), radix));
111: return c.toString();
112: } else {
113: String s = (String) decoder.get(entity);
114: if (s != null)
115: return s;
116: else
117: return "";
118: }
119: }
120:
121: /**
122: * Kodiert alle Sonderzeichen in einem String zu HTML-Entitäten.
123: * <p>
124: * Im Gegensatz zur Methode {@link #decode(String)}, dürfen im gegebenen String
125: * Klartext und Sonderzeichen beliebig gemischt und beliebig oft vorhanden sein.
126: *
127: * @param s Der zu kodierende String.
128: *
129: * @return Die Entsprechung zum gegebenen String, wobei alle Sonderzeichen
130: * durch entsprechende HTML-Entitäten ersetzt wurden.
131: */
132: static final public String encode(String s) {
133: int length = s.length();
134: StringBuffer buffer = new StringBuffer(length * 2);
135: for (int i = 0; i < length; i++) {
136: char c = s.charAt(i);
137: int j = c;
138: if (j < 0x100 && encoder[j] != null) {
139: buffer.append(encoder[j]); // have a named encoding
140: buffer.append(';');
141: } else if (j < 0x80) {
142: buffer.append(c); // use ASCII value
143: } else {
144: buffer.append("&#"); // use numeric encoding
145: buffer.append((int) c);
146: buffer.append(';');
147: }
148: }
149: return buffer.toString();
150: }
151:
152: /**
153: * Fügt eine Entität der internen Datenstruktur hinzu.
154: *
155: * @param entity Die Entität.
156: * @param value Der Unicode-Wert ihrer Klartext-Entsprechung.
157: */
158: static final void add(String entity, int value) {
159: decoder.put(entity, (new Character((char) value)).toString());
160: if (value < 0x100)
161: encoder[value] = entity;
162: }
163:
164: static {
165: add(" ", 160);
166: add("¡", 161);
167: add("¢", 162);
168: add("£", 163);
169: add("¤", 164);
170: add("¥", 165);
171: add("¦", 166);
172: add("§", 167);
173: add("¨", 168);
174: add("©", 169);
175: add("ª", 170);
176: add("«", 171);
177: add("¬", 172);
178: add("­", 173);
179: add("®", 174);
180: add("¯", 175);
181: add("°", 176);
182: add("±", 177);
183: add("²", 178);
184: add("³", 179);
185: add("´", 180);
186: add("µ", 181);
187: add("¶", 182);
188: add("·", 183);
189: add("¸", 184);
190: add("¹", 185);
191: add("º", 186);
192: add("»", 187);
193: add("¼", 188);
194: add("½", 189);
195: add("¾", 190);
196: add("¿", 191);
197: add("À", 192);
198: add("Á", 193);
199: add("Â", 194);
200: add("Ã", 195);
201: add("Ä", 196);
202: add("Å", 197);
203: add("Æ", 198);
204: add("Ç", 199);
205: add("È", 200);
206: add("É", 201);
207: add("Ê", 202);
208: add("Ë", 203);
209: add("Ì", 204);
210: add("Í", 205);
211: add("Î", 206);
212: add("Ï", 207);
213: add("Ð", 208);
214: add("Ñ", 209);
215: add("Ò", 210);
216: add("Ó", 211);
217: add("Ô", 212);
218: add("Õ", 213);
219: add("Ö", 214);
220: add("×", 215);
221: add("Ø", 216);
222: add("Ù", 217);
223: add("Ú", 218);
224: add("Û", 219);
225: add("Ü", 220);
226: add("Ý", 221);
227: add("Þ", 222);
228: add("ß", 223);
229: add("à", 224);
230: add("á", 225);
231: add("â", 226);
232: add("ã", 227);
233: add("ä", 228);
234: add("å", 229);
235: add("æ", 230);
236: add("ç", 231);
237: add("è", 232);
238: add("é", 233);
239: add("ê", 234);
240: add("ë", 235);
241: add("ì", 236);
242: add("í", 237);
243: add("î", 238);
244: add("ï", 239);
245: add("ð", 240);
246: add("ñ", 241);
247: add("ò", 242);
248: add("ó", 243);
249: add("ô", 244);
250: add("õ", 245);
251: add("ö", 246);
252: add("÷", 247);
253: add("ø", 248);
254: add("ù", 249);
255: add("ú", 250);
256: add("û", 251);
257: add("ü", 252);
258: add("ý", 253);
259: add("þ", 254);
260: add("ÿ", 255);
261: add("&fnof", 402);
262: add("&Alpha", 913);
263: add("&Beta", 914);
264: add("&Gamma", 915);
265: add("&Delta", 916);
266: add("&Epsilon", 917);
267: add("&Zeta", 918);
268: add("&Eta", 919);
269: add("&Theta", 920);
270: add("&Iota", 921);
271: add("&Kappa", 922);
272: add("&Lambda", 923);
273: add("&Mu", 924);
274: add("&Nu", 925);
275: add("&Xi", 926);
276: add("&Omicron", 927);
277: add("&Pi", 928);
278: add("&Rho", 929);
279: add("&Sigma", 931);
280: add("&Tau", 932);
281: add("&Upsilon", 933);
282: add("&Phi", 934);
283: add("&Chi", 935);
284: add("&Psi", 936);
285: add("&Omega", 937);
286: add("&alpha", 945);
287: add("&beta", 946);
288: add("&gamma", 947);
289: add("&delta", 948);
290: add("&epsilon", 949);
291: add("&zeta", 950);
292: add("&eta", 951);
293: add("&theta", 952);
294: add("&iota", 953);
295: add("&kappa", 954);
296: add("&lambda", 955);
297: add("&mu", 956);
298: add("&nu", 957);
299: add("&xi", 958);
300: add("&omicron", 959);
301: add("&pi", 960);
302: add("&rho", 961);
303: add("&sigmaf", 962);
304: add("&sigma", 963);
305: add("&tau", 964);
306: add("&upsilon", 965);
307: add("&phi", 966);
308: add("&chi", 967);
309: add("&psi", 968);
310: add("&omega", 969);
311: add("&thetasym", 977);
312: add("&upsih", 978);
313: add("&piv", 982);
314: add("&bull", 8226);
315: add("&hellip", 8230);
316: add("&prime", 8242);
317: add("&Prime", 8243);
318: add("&oline", 8254);
319: add("&frasl", 8260);
320: add("&weierp", 8472);
321: add("&image", 8465);
322: add("&real", 8476);
323: add("&trade", 8482);
324: add("&alefsym", 8501);
325: add("&larr", 8592);
326: add("&uarr", 8593);
327: add("&rarr", 8594);
328: add("&darr", 8595);
329: add("&harr", 8596);
330: add("&crarr", 8629);
331: add("&lArr", 8656);
332: add("&uArr", 8657);
333: add("&rArr", 8658);
334: add("&dArr", 8659);
335: add("&hArr", 8660);
336: add("&forall", 8704);
337: add("&part", 8706);
338: add("&exist", 8707);
339: add("&empty", 8709);
340: add("&nabla", 8711);
341: add("&isin", 8712);
342: add("¬in", 8713);
343: add("&ni", 8715);
344: add("&prod", 8719);
345: add("&sum", 8721);
346: add("&minus", 8722);
347: add("&lowast", 8727);
348: add("&radic", 8730);
349: add("&prop", 8733);
350: add("&infin", 8734);
351: add("&ang", 8736);
352: add("&and", 8743);
353: add("&or", 8744);
354: add("&cap", 8745);
355: add("&cup", 8746);
356: add("&int", 8747);
357: add("&there4", 8756);
358: add("&sim", 8764);
359: add("&cong", 8773);
360: add("&asymp", 8776);
361: add("&ne", 8800);
362: add("&equiv", 8801);
363: add("&le", 8804);
364: add("&ge", 8805);
365: add("&sub", 8834);
366: add("&sup", 8835);
367: add("&nsub", 8836);
368: add("&sube", 8838);
369: add("&supe", 8839);
370: add("&oplus", 8853);
371: add("&otimes", 8855);
372: add("&perp", 8869);
373: add("&sdot", 8901);
374: add("&lceil", 8968);
375: add("&rceil", 8969);
376: add("&lfloor", 8970);
377: add("&rfloor", 8971);
378: add("&lang", 9001);
379: add("&rang", 9002);
380: add("&loz", 9674);
381: add("&spades", 9824);
382: add("&clubs", 9827);
383: add("&hearts", 9829);
384: add("&diams", 9830);
385: add(""", 34);
386: add("&", 38);
387: add("<", 60);
388: add(">", 62);
389: add("&OElig", 338);
390: add("&oelig", 339);
391: add("&Scaron", 352);
392: add("&scaron", 353);
393: add("&Yuml", 376);
394: add("&circ", 710);
395: add("&tilde", 732);
396: add("&ensp", 8194);
397: add("&emsp", 8195);
398: add("&thinsp", 8201);
399: add("&zwnj", 8204);
400: add("&zwj", 8205);
401: add("&lrm", 8206);
402: add("&rlm", 8207);
403: add("&ndash", 8211);
404: add("&mdash", 8212);
405: add("&lsquo", 8216);
406: add("&rsquo", 8217);
407: add("&sbquo", 8218);
408: add("&ldquo", 8220);
409: add("&rdquo", 8221);
410: add("&bdquo", 8222);
411: add("&dagger", 8224);
412: add("&Dagger", 8225);
413: add("&permil", 8240);
414: add("&lsaquo", 8249);
415: add("&rsaquo", 8250);
416: add("&euro", 8364);
417:
418: }
419: }
|