001: /*
002: * Copyright 2002-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package net.sf.thingamablog.xml;
017:
018: import java.util.HashMap;
019: import java.util.Map;
020: import java.util.TreeMap;
021:
022: /**
023: * <p>
024: * Provides HTML and XML entity utilities.
025: * </p>
026: * <p>
027: * This class comes from the jakarta project's commons-lang package. It was made public
028: * and method unescapeUnknownEntities was added.
029: * </p>
030: * @see <a href="http://hotwired.lycos.com/webmonkey/reference/special_characters/">ISO Entities</a>
031: * @see </br><a href="http://www.w3.org/TR/REC-html32#latin1">HTML 3.2 Character Entities for ISO Latin-1</a>
032: * @see </br><a href="http://www.w3.org/TR/REC-html40/sgml/entities.html">HTML 4.0 Character entity references</a>
033: * @see </br><a href="http://www.w3.org/TR/html401/charset.html#h-5.3">HTML 4.01 Character References</a>
034: * @see </br><a href="http://www.w3.org/TR/html401/charset.html#code-position">HTML 4.01 Code positions</a>
035: * @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
036: * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
037: * @since 2.0
038: * @version $Id: Entities.java,v 1.2 2006/12/21 18:40:36 cvsbob Exp $
039: */
040: public class Entities {
041:
042: private static final String[][] BASIC_ARRAY = { { "quot", "34" }, // " - double-quote
043: { "amp", "38" }, // & - ampersand
044: { "lt", "60" }, // < - less-than
045: { "gt", "62" }, // > - greater-than
046: };
047:
048: private static final String[][] SPECIAL_CHARS = {
049: { "nbsp", "160" }, // non-breaking space
050: { "iexcl", "161" }, // inverted exclamation mark
051: { "cent", "162" }, // cent sign
052: { "pound", "163" }, // pound sign
053: { "curren", "164" }, // currency sign
054: { "yen", "165" }, // yen sign = yuan sign
055: { "brvbar", "166" }, // broken bar = broken vertical bar
056: { "sect", "167" }, // section sign
057: { "uml", "168" }, // diaeresis = spacing diaeresis
058: { "copy", "169" }, // - copyright sign
059: { "ordf", "170" }, // feminine ordinal indicator
060: { "laquo", "171" }, // left-pointing double angle quotation mark = left pointing guillemet
061: { "not", "172" }, // not sign
062: { "shy", "173" }, // soft hyphen = discretionary hyphen
063: { "reg", "174" }, // - registered trademark sign
064: { "macr", "175" }, // macron = spacing macron = overline = APL overbar
065: { "deg", "176" }, // degree sign
066: { "plusmn", "177" }, // plus-minus sign = plus-or-minus sign
067: { "sup2", "178" }, // superscript two = superscript digit two = squared
068: { "sup3", "179" }, // superscript three = superscript digit three = cubed
069: { "acute", "180" }, // acute accent = spacing acute
070: { "micro", "181" }, // micro sign
071: { "para", "182" }, // pilcrow sign = paragraph sign
072: { "middot", "183" }, // middle dot = Georgian comma = Greek middle dot
073: { "cedil", "184" }, // cedilla = spacing cedilla
074: { "sup1", "185" }, // superscript one = superscript digit one
075: { "ordm", "186" }, // masculine ordinal indicator
076: { "raquo", "187" }, // right-pointing double angle quotation mark = right pointing guillemet
077: };
078:
079: private static final String[][] APOS_ARRAY = { { "apos", "39" }, // XML apostrophe
080: };
081:
082: // package scoped for testing
083: static final String[][] ISO8859_1_ARRAY = { { "frac14", "188" }, // vulgar fraction one quarter = fraction one quarter
084: { "frac12", "189" }, // vulgar fraction one half = fraction one half
085: { "frac34", "190" }, // vulgar fraction three quarters = fraction three quarters
086: { "iquest", "191" }, // inverted question mark = turned question mark
087: { "Agrave", "192" }, // - uppercase A, grave accent
088: { "Aacute", "193" }, // - uppercase A, acute accent
089: { "Acirc", "194" }, // - uppercase A, circumflex accent
090: { "Atilde", "195" }, // - uppercase A, tilde
091: { "Auml", "196" }, // - uppercase A, umlaut
092: { "Aring", "197" }, // - uppercase A, ring
093: { "AElig", "198" }, // - uppercase AE
094: { "Ccedil", "199" }, // - uppercase C, cedilla
095: { "Egrave", "200" }, // - uppercase E, grave accent
096: { "Eacute", "201" }, // - uppercase E, acute accent
097: { "Ecirc", "202" }, // - uppercase E, circumflex accent
098: { "Euml", "203" }, // - uppercase E, umlaut
099: { "Igrave", "204" }, // - uppercase I, grave accent
100: { "Iacute", "205" }, // - uppercase I, acute accent
101: { "Icirc", "206" }, // - uppercase I, circumflex accent
102: { "Iuml", "207" }, // - uppercase I, umlaut
103: { "ETH", "208" }, // - uppercase Eth, Icelandic
104: { "Ntilde", "209" }, // - uppercase N, tilde
105: { "Ograve", "210" }, // - uppercase O, grave accent
106: { "Oacute", "211" }, // - uppercase O, acute accent
107: { "Ocirc", "212" }, // - uppercase O, circumflex accent
108: { "Otilde", "213" }, // - uppercase O, tilde
109: { "Ouml", "214" }, // - uppercase O, umlaut
110: { "times", "215" }, // multiplication sign
111: { "Oslash", "216" }, // - uppercase O, slash
112: { "Ugrave", "217" }, // - uppercase U, grave accent
113: { "Uacute", "218" }, // - uppercase U, acute accent
114: { "Ucirc", "219" }, // - uppercase U, circumflex accent
115: { "Uuml", "220" }, // - uppercase U, umlaut
116: { "Yacute", "221" }, // - uppercase Y, acute accent
117: { "THORN", "222" }, // - uppercase THORN, Icelandic
118: { "szlig", "223" }, // - lowercase sharps, German
119: { "agrave", "224" }, // - lowercase a, grave accent
120: { "aacute", "225" }, // - lowercase a, acute accent
121: { "acirc", "226" }, // - lowercase a, circumflex accent
122: { "atilde", "227" }, // - lowercase a, tilde
123: { "auml", "228" }, // - lowercase a, umlaut
124: { "aring", "229" }, // - lowercase a, ring
125: { "aelig", "230" }, // - lowercase ae
126: { "ccedil", "231" }, // - lowercase c, cedilla
127: { "egrave", "232" }, // - lowercase e, grave accent
128: { "eacute", "233" }, // - lowercase e, acute accent
129: { "ecirc", "234" }, // - lowercase e, circumflex accent
130: { "euml", "235" }, // - lowercase e, umlaut
131: { "igrave", "236" }, // - lowercase i, grave accent
132: { "iacute", "237" }, // - lowercase i, acute accent
133: { "icirc", "238" }, // - lowercase i, circumflex accent
134: { "iuml", "239" }, // - lowercase i, umlaut
135: { "eth", "240" }, // - lowercase eth, Icelandic
136: { "ntilde", "241" }, // - lowercase n, tilde
137: { "ograve", "242" }, // - lowercase o, grave accent
138: { "oacute", "243" }, // - lowercase o, acute accent
139: { "ocirc", "244" }, // - lowercase o, circumflex accent
140: { "otilde", "245" }, // - lowercase o, tilde
141: { "ouml", "246" }, // - lowercase o, umlaut
142: { "divide", "247" }, // division sign
143: { "oslash", "248" }, // - lowercase o, slash
144: { "ugrave", "249" }, // - lowercase u, grave accent
145: { "uacute", "250" }, // - lowercase u, acute accent
146: { "ucirc", "251" }, // - lowercase u, circumflex accent
147: { "uuml", "252" }, // - lowercase u, umlaut
148: { "yacute", "253" }, // - lowercase y, acute accent
149: { "thorn", "254" }, // - lowercase thorn, Icelandic
150: { "yuml", "255" }, // - lowercase y, umlaut
151: };
152:
153: static final String[][] HTML40_GREEK_ARRAY = {
154: // <!-- Greek -->
155: { "Alpha", "913" }, // greek capital letter alpha, U+0391 -->
156: { "Beta", "914" }, // greek capital letter beta, U+0392 -->
157: { "Gamma", "915" }, // greek capital letter gamma,U+0393 ISOgrk3 -->
158: { "Delta", "916" }, // greek capital letter delta,U+0394 ISOgrk3 -->
159: { "Epsilon", "917" }, // greek capital letter epsilon, U+0395 -->
160: { "Zeta", "918" }, // greek capital letter zeta, U+0396 -->
161: { "Eta", "919" }, // greek capital letter eta, U+0397 -->
162: { "Theta", "920" }, // greek capital letter theta,U+0398 ISOgrk3 -->
163: { "Iota", "921" }, // greek capital letter iota, U+0399 -->
164: { "Kappa", "922" }, // greek capital letter kappa, U+039A -->
165: { "Lambda", "923" }, // greek capital letter lambda,U+039B ISOgrk3 -->
166: { "Mu", "924" }, // greek capital letter mu, U+039C -->
167: { "Nu", "925" }, // greek capital letter nu, U+039D -->
168: { "Xi", "926" }, // greek capital letter xi, U+039E ISOgrk3 -->
169: { "Omicron", "927" }, // greek capital letter omicron, U+039F -->
170: { "Pi", "928" }, // greek capital letter pi, U+03A0 ISOgrk3 -->
171: { "Rho", "929" }, // greek capital letter rho, U+03A1 -->
172: // <!-- there is no Sigmaf, and no U+03A2 character either -->
173: { "Sigma", "931" }, // greek capital letter sigma,U+03A3 ISOgrk3 -->
174: { "Tau", "932" }, // greek capital letter tau, U+03A4 -->
175: { "Upsilon", "933" }, // greek capital letter upsilon,U+03A5 ISOgrk3 -->
176: { "Phi", "934" }, // greek capital letter phi,U+03A6 ISOgrk3 -->
177: { "Chi", "935" }, // greek capital letter chi, U+03A7 -->
178: { "Psi", "936" }, // greek capital letter psi,U+03A8 ISOgrk3 -->
179: { "Omega", "937" }, // greek capital letter omega,U+03A9 ISOgrk3 -->
180: { "alpha", "945" }, // greek small letter alpha,U+03B1 ISOgrk3 -->
181: { "beta", "946" }, // greek small letter beta, U+03B2 ISOgrk3 -->
182: { "gamma", "947" }, // greek small letter gamma,U+03B3 ISOgrk3 -->
183: { "delta", "948" }, // greek small letter delta,U+03B4 ISOgrk3 -->
184: { "epsilon", "949" }, // greek small letter epsilon,U+03B5 ISOgrk3 -->
185: { "zeta", "950" }, // greek small letter zeta, U+03B6 ISOgrk3 -->
186: { "eta", "951" }, // greek small letter eta, U+03B7 ISOgrk3 -->
187: { "theta", "952" }, // greek small letter theta,U+03B8 ISOgrk3 -->
188: { "iota", "953" }, // greek small letter iota, U+03B9 ISOgrk3 -->
189: { "kappa", "954" }, // greek small letter kappa,U+03BA ISOgrk3 -->
190: { "lambda", "955" }, // greek small letter lambda,U+03BB ISOgrk3 -->
191: { "mu", "956" }, // greek small letter mu, U+03BC ISOgrk3 -->
192: { "nu", "957" }, // greek small letter nu, U+03BD ISOgrk3 -->
193: { "xi", "958" }, // greek small letter xi, U+03BE ISOgrk3 -->
194: { "omicron", "959" }, // greek small letter omicron, U+03BF NEW -->
195: { "pi", "960" }, // greek small letter pi, U+03C0 ISOgrk3 -->
196: { "rho", "961" }, // greek small letter rho, U+03C1 ISOgrk3 -->
197: { "sigmaf", "962" }, // greek small letter final sigma,U+03C2 ISOgrk3 -->
198: { "sigma", "963" }, // greek small letter sigma,U+03C3 ISOgrk3 -->
199: { "tau", "964" }, // greek small letter tau, U+03C4 ISOgrk3 -->
200: { "upsilon", "965" }, // greek small letter upsilon,U+03C5 ISOgrk3 -->
201: { "phi", "966" }, // greek small letter phi, U+03C6 ISOgrk3 -->
202: { "chi", "967" }, // greek small letter chi, U+03C7 ISOgrk3 -->
203: { "psi", "968" }, // greek small letter psi, U+03C8 ISOgrk3 -->
204: { "omega", "969" }, // greek small letter omega,U+03C9 ISOgrk3 -->
205: { "thetasym", "977" }, // greek small letter theta symbol,U+03D1 NEW -->
206: { "upsih", "978" }, // greek upsilon with hook symbol,U+03D2 NEW -->
207: { "piv", "982" }, // greek pi symbol, U+03D6 ISOgrk3 -->
208: };
209:
210: // http://www.w3.org/TR/REC-html40/sgml/entities.html
211: // package scoped for testing
212: static final String[][] HTML40_ARRAY = {
213: // <!-- Latin Extended-B -->
214: { "fnof", "402" }, // latin small f with hook = function= florin, U+0192 ISOtech -->
215: // <!-- General Punctuation -->
216: { "bull", "8226" }, // bullet = black small circle,U+2022 ISOpub -->
217: // <!-- bullet is NOT the same as bullet operator, U+2219 -->
218: { "hellip", "8230" }, // horizontal ellipsis = three dot leader,U+2026 ISOpub -->
219: { "prime", "8242" }, // prime = minutes = feet, U+2032 ISOtech -->
220: { "Prime", "8243" }, // double prime = seconds = inches,U+2033 ISOtech -->
221: { "oline", "8254" }, // overline = spacing overscore,U+203E NEW -->
222: { "frasl", "8260" }, // fraction slash, U+2044 NEW -->
223: // <!-- Letterlike Symbols -->
224: { "weierp", "8472" }, // script capital P = power set= Weierstrass p, U+2118 ISOamso -->
225: { "image", "8465" }, // blackletter capital I = imaginary part,U+2111 ISOamso -->
226: { "real", "8476" }, // blackletter capital R = real part symbol,U+211C ISOamso -->
227: { "trade", "8482" }, // trade mark sign, U+2122 ISOnum -->
228: { "alefsym", "8501" }, // alef symbol = first transfinite cardinal,U+2135 NEW -->
229: // <!-- alef symbol is NOT the same as hebrew letter alef,U+05D0 although the same glyph could be used to depict
230: // both characters -->
231: // <!-- Arrows -->
232: { "larr", "8592" }, // leftwards arrow, U+2190 ISOnum -->
233: { "uarr", "8593" }, // upwards arrow, U+2191 ISOnum-->
234: { "rarr", "8594" }, // rightwards arrow, U+2192 ISOnum -->
235: { "darr", "8595" }, // downwards arrow, U+2193 ISOnum -->
236: { "harr", "8596" }, // left right arrow, U+2194 ISOamsa -->
237: { "crarr", "8629" }, // downwards arrow with corner leftwards= carriage return, U+21B5 NEW -->
238: { "lArr", "8656" }, // leftwards double arrow, U+21D0 ISOtech -->
239: // <!-- ISO 10646 does not say that lArr is the same as the 'is implied by' arrowbut also does not have any
240: // other character for that function. So ? lArr canbe used for 'is implied by' as ISOtech suggests -->
241: { "uArr", "8657" }, // upwards double arrow, U+21D1 ISOamsa -->
242: { "rArr", "8658" }, // rightwards double arrow,U+21D2 ISOtech -->
243: // <!-- ISO 10646 does not say this is the 'implies' character but does not have another character with this
244: // function so ?rArr can be used for 'implies' as ISOtech suggests -->
245: { "dArr", "8659" }, // downwards double arrow, U+21D3 ISOamsa -->
246: { "hArr", "8660" }, // left right double arrow,U+21D4 ISOamsa -->
247: // <!-- Mathematical Operators -->
248: { "forall", "8704" }, // for all, U+2200 ISOtech -->
249: { "part", "8706" }, // partial differential, U+2202 ISOtech -->
250: { "exist", "8707" }, // there exists, U+2203 ISOtech -->
251: { "empty", "8709" }, // empty set = null set = diameter,U+2205 ISOamso -->
252: { "nabla", "8711" }, // nabla = backward difference,U+2207 ISOtech -->
253: { "isin", "8712" }, // element of, U+2208 ISOtech -->
254: { "notin", "8713" }, // not an element of, U+2209 ISOtech -->
255: { "ni", "8715" }, // contains as member, U+220B ISOtech -->
256: // <!-- should there be a more memorable name than 'ni'? -->
257: { "prod", "8719" }, // n-ary product = product sign,U+220F ISOamsb -->
258: // <!-- prod is NOT the same character as U+03A0 'greek capital letter pi' though the same glyph might be used
259: // for both -->
260: { "sum", "8721" }, // n-ary summation, U+2211 ISOamsb -->
261: // <!-- sum is NOT the same character as U+03A3 'greek capital letter sigma' though the same glyph might be used
262: // for both -->
263: { "minus", "8722" }, // minus sign, U+2212 ISOtech -->
264: { "lowast", "8727" }, // asterisk operator, U+2217 ISOtech -->
265: { "radic", "8730" }, // square root = radical sign,U+221A ISOtech -->
266: { "prop", "8733" }, // proportional to, U+221D ISOtech -->
267: { "infin", "8734" }, // infinity, U+221E ISOtech -->
268: { "ang", "8736" }, // angle, U+2220 ISOamso -->
269: { "and", "8743" }, // logical and = wedge, U+2227 ISOtech -->
270: { "or", "8744" }, // logical or = vee, U+2228 ISOtech -->
271: { "cap", "8745" }, // intersection = cap, U+2229 ISOtech -->
272: { "cup", "8746" }, // union = cup, U+222A ISOtech -->
273: { "int", "8747" }, // integral, U+222B ISOtech -->
274: { "there4", "8756" }, // therefore, U+2234 ISOtech -->
275: { "sim", "8764" }, // tilde operator = varies with = similar to,U+223C ISOtech -->
276: // <!-- tilde operator is NOT the same character as the tilde, U+007E,although the same glyph might be used to
277: // represent both -->
278: { "cong", "8773" }, // approximately equal to, U+2245 ISOtech -->
279: { "asymp", "8776" }, // almost equal to = asymptotic to,U+2248 ISOamsr -->
280: { "ne", "8800" }, // not equal to, U+2260 ISOtech -->
281: { "equiv", "8801" }, // identical to, U+2261 ISOtech -->
282: { "le", "8804" }, // less-than or equal to, U+2264 ISOtech -->
283: { "ge", "8805" }, // greater-than or equal to,U+2265 ISOtech -->
284: { "sub", "8834" }, // subset of, U+2282 ISOtech -->
285: { "sup", "8835" }, // superset of, U+2283 ISOtech -->
286: // <!-- note that nsup, 'not a superset of, U+2283' is not covered by the Symbol font encoding and is not
287: // included. Should it be, for symmetry?It is in ISOamsn --> <!ENTITY nsub", "8836"}, //not a subset of, U+2284
288: // ISOamsn -->
289: { "sube", "8838" }, // subset of or equal to, U+2286 ISOtech -->
290: { "supe", "8839" }, // superset of or equal to,U+2287 ISOtech -->
291: { "oplus", "8853" }, // circled plus = direct sum,U+2295 ISOamsb -->
292: { "otimes", "8855" }, // circled times = vector product,U+2297 ISOamsb -->
293: { "perp", "8869" }, // up tack = orthogonal to = perpendicular,U+22A5 ISOtech -->
294: { "sdot", "8901" }, // dot operator, U+22C5 ISOamsb -->
295: // <!-- dot operator is NOT the same character as U+00B7 middle dot -->
296: // <!-- Miscellaneous Technical -->
297: { "lceil", "8968" }, // left ceiling = apl upstile,U+2308 ISOamsc -->
298: { "rceil", "8969" }, // right ceiling, U+2309 ISOamsc -->
299: { "lfloor", "8970" }, // left floor = apl downstile,U+230A ISOamsc -->
300: { "rfloor", "8971" }, // right floor, U+230B ISOamsc -->
301: { "lang", "9001" }, // left-pointing angle bracket = bra,U+2329 ISOtech -->
302: // <!-- lang is NOT the same character as U+003C 'less than' or U+2039 'single left-pointing angle quotation
303: // mark' -->
304: { "rang", "9002" }, // right-pointing angle bracket = ket,U+232A ISOtech -->
305: // <!-- rang is NOT the same character as U+003E 'greater than' or U+203A 'single right-pointing angle quotation
306: // mark' -->
307: // <!-- Geometric Shapes -->
308: { "loz", "9674" }, // lozenge, U+25CA ISOpub -->
309: // <!-- Miscellaneous Symbols -->
310: { "spades", "9824" }, // black spade suit, U+2660 ISOpub -->
311: // <!-- black here seems to mean filled as opposed to hollow -->
312: { "clubs", "9827" }, // black club suit = shamrock,U+2663 ISOpub -->
313: { "hearts", "9829" }, // black heart suit = valentine,U+2665 ISOpub -->
314: { "diams", "9830" }, // black diamond suit, U+2666 ISOpub -->
315:
316: // <!-- Latin Extended-A -->
317: { "OElig", "338" }, // -- latin capital ligature OE,U+0152 ISOlat2 -->
318: { "oelig", "339" }, // -- latin small ligature oe, U+0153 ISOlat2 -->
319: // <!-- ligature is a misnomer, this is a separate character in some languages -->
320: { "Scaron", "352" }, // -- latin capital letter S with caron,U+0160 ISOlat2 -->
321: { "scaron", "353" }, // -- latin small letter s with caron,U+0161 ISOlat2 -->
322: { "Yuml", "376" }, // -- latin capital letter Y with diaeresis,U+0178 ISOlat2 -->
323: // <!-- Spacing Modifier Letters -->
324: { "circ", "710" }, // -- modifier letter circumflex accent,U+02C6 ISOpub -->
325: { "tilde", "732" }, // small tilde, U+02DC ISOdia -->
326: // <!-- General Punctuation -->
327: { "ensp", "8194" }, // en space, U+2002 ISOpub -->
328: { "emsp", "8195" }, // em space, U+2003 ISOpub -->
329: { "thinsp", "8201" }, // thin space, U+2009 ISOpub -->
330: { "zwnj", "8204" }, // zero width non-joiner,U+200C NEW RFC 2070 -->
331: { "zwj", "8205" }, // zero width joiner, U+200D NEW RFC 2070 -->
332: { "lrm", "8206" }, // left-to-right mark, U+200E NEW RFC 2070 -->
333: { "rlm", "8207" }, // right-to-left mark, U+200F NEW RFC 2070 -->
334: { "ndash", "8211" }, // en dash, U+2013 ISOpub -->
335: { "mdash", "8212" }, // em dash, U+2014 ISOpub -->
336: { "lsquo", "8216" }, // left single quotation mark,U+2018 ISOnum -->
337: { "rsquo", "8217" }, // right single quotation mark,U+2019 ISOnum -->
338: { "sbquo", "8218" }, // single low-9 quotation mark, U+201A NEW -->
339: { "ldquo", "8220" }, // left double quotation mark,U+201C ISOnum -->
340: { "rdquo", "8221" }, // right double quotation mark,U+201D ISOnum -->
341: { "bdquo", "8222" }, // double low-9 quotation mark, U+201E NEW -->
342: { "dagger", "8224" }, // dagger, U+2020 ISOpub -->
343: { "Dagger", "8225" }, // double dagger, U+2021 ISOpub -->
344: { "permil", "8240" }, // per mille sign, U+2030 ISOtech -->
345: { "lsaquo", "8249" }, // single left-pointing angle quotation mark,U+2039 ISO proposed -->
346: // <!-- lsaquo is proposed but not yet ISO standardized -->
347: { "rsaquo", "8250" }, // single right-pointing angle quotation mark,U+203A ISO proposed -->
348: // <!-- rsaquo is proposed but not yet ISO standardized -->
349: { "euro", "8364" }, // -- euro sign, U+20AC NEW -->
350: };
351:
352: /**
353: * <p>
354: * The set of entities supported by standard XML.
355: * </p>
356: */
357: public static final Entities XML;
358:
359: /**
360: * <p>
361: * A set of common entities that should be escaped in HTML. This includes
362: * double-quote, ampersand, less-than, greater-than and chars 160 - 190
363: *
364: * </p>
365: */
366: public static final Entities HTML_BASIC;
367:
368: /**
369: * <p>
370: * The set of entities supported by HTML 3.2.
371: * </p>
372: */
373: public static final Entities HTML32;
374:
375: /**
376: * <p>
377: * The set of entities supported by HTML 4.0. excluding greek entities
378: * </p>
379: */
380: public static final Entities HTML40;
381:
382: /**
383: * <p>
384: * The set of entities supported by HTML 4.0. including greek entities
385: * </p>
386: */
387: public static final Entities HTML40_FULL;
388:
389: static {
390: XML = new Entities();
391: XML.addEntities(BASIC_ARRAY);
392: XML.addEntities(APOS_ARRAY);
393: }
394:
395: static {
396: HTML_BASIC = new Entities();
397: HTML_BASIC.addEntities(BASIC_ARRAY);
398: HTML_BASIC.addEntities(SPECIAL_CHARS);
399: }
400:
401: static {
402: HTML32 = new Entities();
403: HTML32.addEntities(BASIC_ARRAY);
404: HTML32.addEntities(SPECIAL_CHARS);
405: HTML32.addEntities(ISO8859_1_ARRAY);
406: }
407:
408: static {
409: HTML40 = new Entities();
410: HTML40.addEntities(BASIC_ARRAY);
411: HTML40.addEntities(SPECIAL_CHARS);
412: HTML40.addEntities(ISO8859_1_ARRAY);
413: HTML40.addEntities(HTML40_ARRAY);
414: }
415:
416: static {
417: HTML40_FULL = new Entities();
418: HTML40_FULL.addEntities(BASIC_ARRAY);
419: HTML40_FULL.addEntities(SPECIAL_CHARS);
420: HTML40_FULL.addEntities(ISO8859_1_ARRAY);
421: HTML40_FULL.addEntities(HTML40_ARRAY);
422: HTML40_FULL.addEntities(HTML40_GREEK_ARRAY);
423: }
424:
425: static interface EntityMap {
426:
427: void add(String name, int value);
428:
429: String name(int value);
430:
431: int value(String name);
432: }
433:
434: static class PrimitiveEntityMap implements EntityMap {
435:
436: private Map mapNameToValue = new HashMap();
437:
438: private IntHashMap mapValueToName = new IntHashMap();
439:
440: public void add(String name, int value) {
441: mapNameToValue.put(name, new Integer(value));
442: mapValueToName.put(value, name);
443: }
444:
445: public String name(int value) {
446: return (String) mapValueToName.get(value);
447: }
448:
449: public int value(String name) {
450: Object value = mapNameToValue.get(name);
451: if (value == null) {
452: return -1;
453: }
454: return ((Integer) value).intValue();
455: }
456: }
457:
458: static abstract class MapIntMap implements Entities.EntityMap {
459:
460: protected Map mapNameToValue;
461:
462: protected Map mapValueToName;
463:
464: public void add(String name, int value) {
465: mapNameToValue.put(name, new Integer(value));
466: mapValueToName.put(new Integer(value), name);
467: }
468:
469: public String name(int value) {
470: return (String) mapValueToName.get(new Integer(value));
471: }
472:
473: public int value(String name) {
474: Object value = mapNameToValue.get(name);
475: if (value == null) {
476: return -1;
477: }
478: return ((Integer) value).intValue();
479: }
480: }
481:
482: static class HashEntityMap extends MapIntMap {
483:
484: public HashEntityMap() {
485: mapNameToValue = new HashMap();
486: mapValueToName = new HashMap();
487: }
488: }
489:
490: static class TreeEntityMap extends MapIntMap {
491:
492: public TreeEntityMap() {
493: mapNameToValue = new TreeMap();
494: mapValueToName = new TreeMap();
495: }
496: }
497:
498: static class LookupEntityMap extends PrimitiveEntityMap {
499:
500: private String[] lookupTable;
501:
502: private int LOOKUP_TABLE_SIZE = 256;
503:
504: public String name(int value) {
505: if (value < LOOKUP_TABLE_SIZE) {
506: return lookupTable()[value];
507: }
508: return super .name(value);
509: }
510:
511: private String[] lookupTable() {
512: if (lookupTable == null) {
513: createLookupTable();
514: }
515: return lookupTable;
516: }
517:
518: private void createLookupTable() {
519: lookupTable = new String[LOOKUP_TABLE_SIZE];
520: for (int i = 0; i < LOOKUP_TABLE_SIZE; ++i) {
521: lookupTable[i] = super .name(i);
522: }
523: }
524: }
525:
526: static class ArrayEntityMap implements EntityMap {
527:
528: protected int growBy = 100;
529:
530: protected int size = 0;
531:
532: protected String[] names;
533:
534: protected int[] values;
535:
536: public ArrayEntityMap() {
537: names = new String[growBy];
538: values = new int[growBy];
539: }
540:
541: public ArrayEntityMap(int growBy) {
542: this .growBy = growBy;
543: names = new String[growBy];
544: values = new int[growBy];
545: }
546:
547: public void add(String name, int value) {
548: ensureCapacity(size + 1);
549: names[size] = name;
550: values[size] = value;
551: size++;
552: }
553:
554: protected void ensureCapacity(int capacity) {
555: if (capacity > names.length) {
556: int newSize = Math.max(capacity, size + growBy);
557: String[] newNames = new String[newSize];
558: System.arraycopy(names, 0, newNames, 0, size);
559: names = newNames;
560: int[] newValues = new int[newSize];
561: System.arraycopy(values, 0, newValues, 0, size);
562: values = newValues;
563: }
564: }
565:
566: public String name(int value) {
567: for (int i = 0; i < size; ++i) {
568: if (values[i] == value) {
569: return names[i];
570: }
571: }
572: return null;
573: }
574:
575: public int value(String name) {
576: for (int i = 0; i < size; ++i) {
577: if (names[i].equals(name)) {
578: return values[i];
579: }
580: }
581: return -1;
582: }
583: }
584:
585: static class BinaryEntityMap extends ArrayEntityMap {
586:
587: public BinaryEntityMap() {
588: }
589:
590: public BinaryEntityMap(int growBy) {
591: super (growBy);
592: }
593:
594: // based on code in java.util.Arrays
595: private int binarySearch(int key) {
596: int low = 0;
597: int high = size - 1;
598:
599: while (low <= high) {
600: int mid = (low + high) >> 1;
601: int midVal = values[mid];
602:
603: if (midVal < key) {
604: low = mid + 1;
605: } else if (midVal > key) {
606: high = mid - 1;
607: } else {
608: return mid; // key found
609: }
610: }
611: return -(low + 1); // key not found.
612: }
613:
614: public void add(String name, int value) {
615: ensureCapacity(size + 1);
616: int insertAt = binarySearch(value);
617: if (insertAt > 0) {
618: return; // note: this means you can't insert the same value twice
619: }
620: insertAt = -(insertAt + 1); // binarySearch returns it negative and off-by-one
621: System.arraycopy(values, insertAt, values, insertAt + 1,
622: size - insertAt);
623: values[insertAt] = value;
624: System.arraycopy(names, insertAt, names, insertAt + 1, size
625: - insertAt);
626: names[insertAt] = name;
627: size++;
628: }
629:
630: public String name(int value) {
631: int index = binarySearch(value);
632: if (index < 0) {
633: return null;
634: }
635: return names[index];
636: }
637: }
638:
639: // package scoped for testing
640: EntityMap map = new Entities.LookupEntityMap();
641:
642: public void addEntities(String[][] entityArray) {
643: for (int i = 0; i < entityArray.length; ++i) {
644: addEntity(entityArray[i][0], Integer
645: .parseInt(entityArray[i][1]));
646: }
647: }
648:
649: public void addEntity(String name, int value) {
650: map.add(name, value);
651: }
652:
653: public String entityName(int value) {
654: return map.name(value);
655: }
656:
657: public int entityValue(String name) {
658: return map.value(name);
659: }
660:
661: /**
662: * <p>
663: * Escapes the characters in a <code>String</code>.
664: * </p>
665: * <p>
666: * For example, if you have called addEntity("foo", 0xA1), escape("\u00A1") will return
667: * "&foo;"
668: * </p>
669: * @param str The <code>String</code> to escape.
670: * @return A new escaped <code>String</code>.
671: */
672: public String escape(String str) {
673: return escape(str, true);
674: }
675:
676: /**
677: * <p>
678: * Escapes the characters in a <code>String</code>.
679: * </p>
680: * <p>
681: * For example, if you have called addEntity("foo", 0xA1), escape("\u00A1") will return
682: * "&foo;"
683: * </p>
684: * @param str The <code>String</code> to escape.
685: * @param escapeNon7bit Indicate whether non-7-bit chars should be escaped as well
686: * @return A new escaped <code>String</code>.
687: */
688: public String escape(String str, boolean escapeNon7bit) {
689: // todo: rewrite to use a Writer
690: StringBuffer buf = new StringBuffer(str.length() * 2);
691: int i;
692: for (i = 0; i < str.length(); ++i) {
693: char ch = str.charAt(i);
694: String entityName = this .entityName(ch);
695: if (entityName == null) {
696: if (escapeNon7bit && ch > 0x7F) {
697: int intValue = ch;
698: buf.append("&#");
699: buf.append(intValue);
700: buf.append(';');
701: } else {
702: buf.append(ch);
703: }
704: } else {
705: buf.append('&');
706: buf.append(entityName);
707: buf.append(';');
708: }
709: }
710: return buf.toString();
711: }
712:
713: /**
714: * <p>
715: * Unescapes the entities in a <code>String</code>.
716: * </p>
717: * <p>
718: * For example, if you have called addEntity("foo", 0xA1), unescape("&foo;") will return
719: * "\u00A1"
720: * </p>
721: * @param str The <code>String</code> to escape.
722: * @return A new escaped <code>String</code>.
723: */
724: public String unescape(String str) {
725: return doUnescape(str, false);
726: }
727:
728: /**
729: * <p>
730: * Unescapes the unknown entities in a <code>String</code>.
731: * </p>
732: * <p>
733: * For example, if you have called
734: * addEntity("foo", 0xA1), unescape("&foo;") will return
735: * & quot;&foo& quot;
736: * </p>
737: * <p>
738: * This is useful if you only want to unescape Cryllic entities, for example.
739: * </p>
740: * @param str The <code>String</code> to escape.
741: * @return A new escaped <code>String</code>.
742: */
743: public String unescapeUnknownEntities(String str) {
744: return doUnescape(str, true);
745: }
746:
747: private String doUnescape(String str, boolean unknownEntitiesOnly) {
748: StringBuffer buf = new StringBuffer(str.length());
749: int i;
750: for (i = 0; i < str.length(); ++i) {
751: char ch = str.charAt(i);
752: if (ch == '&') {
753: int semi = str.indexOf(';', i + 1);
754: if (semi == -1) {
755: buf.append(ch);
756: continue;
757: }
758: String entName = str.substring(i + 1, semi);
759: int entValue;
760: if (entName.charAt(0) == '#') {
761: char charAt1 = entName.charAt(1);
762: if (charAt1 == 'x' || charAt1 == 'X') {
763: entValue = Integer.valueOf(
764: entName.substring(2), 16).intValue();
765: } else {
766: entValue = Integer.parseInt(entName
767: .substring(1));
768: }
769: } else {
770: entValue = this .entityValue(entName);
771: }
772:
773: if (entValue == -1 || unknownEntitiesOnly
774: && (this .entityName(entValue) != null)) {
775: buf.append('&');
776: buf.append(entName);
777: buf.append(';');
778: } else {
779: buf.append((char) (entValue));
780: }
781: i = semi;
782: } else {
783: buf.append(ch);
784: }
785: }
786: return buf.toString();
787: }
788: }
|