1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowledgement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgement may appear in the software itself,
  24. * if and wherever such third-party acknowledgements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.commons.lang;
  55. import java.util.HashMap;
  56. import java.util.Map;
  57. import java.util.TreeMap;
  58. /**
  59. * <p>Provides HTML and XML entity utilities.</p>
  60. *
  61. * @see <a href="http://hotwired.lycos.com/webmonkey/reference/special_characters/">ISO Entities</a>
  62. * @see </br><a href="http://www.w3.org/TR/REC-html32#latin1">HTML 3.2 Character Entities for ISO Latin-1</a>
  63. * @see </br><a href="http://www.w3.org/TR/REC-html40/sgml/entities.html">HTML 4.0 Character entity references</a>
  64. * @see </br><a href="http://www.w3.org/TR/html401/charset.html#h-5.3">HTML 4.01 Character References</a>
  65. * @see </br><a href="http://www.w3.org/TR/html401/charset.html#code-position">HTML 4.01 Code positions</a>
  66. *
  67. * @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
  68. * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
  69. * @since 2.0
  70. * @version $Id: Entities.java,v 1.13 2003/08/18 02:22:22 bayard Exp $
  71. */
  72. class Entities {
  73. private static final String[][] BASIC_ARRAY = {
  74. {"quot", "34"}, // " - double-quote
  75. {"amp", "38"}, // & - ampersand
  76. {"lt", "60"}, // < - less-than
  77. {"gt", "62"}, // > - greater-than
  78. };
  79. private static final String[][] APOS_ARRAY = {
  80. {"apos", "39"}, // XML apostrophe
  81. };
  82. // package scoped for testing
  83. static final String[][] ISO8859_1_ARRAY = {
  84. {"nbsp", "160"}, // non-breaking space
  85. {"iexcl", "161"}, //inverted exclamation mark
  86. {"cent", "162"}, //cent sign
  87. {"pound", "163"}, //pound sign
  88. {"curren", "164"}, //currency sign
  89. {"yen", "165"}, //yen sign = yuan sign
  90. {"brvbar", "166"}, //broken bar = broken vertical bar
  91. {"sect", "167"}, //section sign
  92. {"uml", "168"}, //diaeresis = spacing diaeresis
  93. {"copy", "169"}, // ? - copyright sign
  94. {"ordf", "170"}, //feminine ordinal indicator
  95. {"laquo", "171"}, //left-pointing double angle quotation mark = left pointing guillemet
  96. {"not", "172"}, //not sign
  97. {"shy", "173"}, //soft hyphen = discretionary hyphen
  98. {"reg", "174"}, // ? - registered trademark sign
  99. {"macr", "175"}, //macron = spacing macron = overline = APL overbar
  100. {"deg", "176"}, //degree sign
  101. {"plusmn", "177"}, //plus-minus sign = plus-or-minus sign
  102. {"sup2", "178"}, //superscript two = superscript digit two = squared
  103. {"sup3", "179"}, //superscript three = superscript digit three = cubed
  104. {"acute", "180"}, //acute accent = spacing acute
  105. {"micro", "181"}, //micro sign
  106. {"para", "182"}, //pilcrow sign = paragraph sign
  107. {"middot", "183"}, //middle dot = Georgian comma = Greek middle dot
  108. {"cedil", "184"}, //cedilla = spacing cedilla
  109. {"sup1", "185"}, //superscript one = superscript digit one
  110. {"ordm", "186"}, //masculine ordinal indicator
  111. {"raquo", "187"}, //right-pointing double angle quotation mark = right pointing guillemet
  112. {"frac14", "188"}, //vulgar fraction one quarter = fraction one quarter
  113. {"frac12", "189"}, //vulgar fraction one half = fraction one half
  114. {"frac34", "190"}, //vulgar fraction three quarters = fraction three quarters
  115. {"iquest", "191"}, //inverted question mark = turned question mark
  116. {"Agrave", "192"}, // ? - uppercase A, grave accent
  117. {"Aacute", "193"}, // ? - uppercase A, acute accent
  118. {"Acirc", "194"}, // ? - uppercase A, circumflex accent
  119. {"Atilde", "195"}, // ? - uppercase A, tilde
  120. {"Auml", "196"}, // ? - uppercase A, umlaut
  121. {"Aring", "197"}, // ? - uppercase A, ring
  122. {"AElig", "198"}, // ? - uppercase AE
  123. {"Ccedil", "199"}, // ? - uppercase C, cedilla
  124. {"Egrave", "200"}, // ? - uppercase E, grave accent
  125. {"Eacute", "201"}, // ? - uppercase E, acute accent
  126. {"Ecirc", "202"}, // ? - uppercase E, circumflex accent
  127. {"Euml", "203"}, // ? - uppercase E, umlaut
  128. {"Igrave", "204"}, // ? - uppercase I, grave accent
  129. {"Iacute", "205"}, // ? - uppercase I, acute accent
  130. {"Icirc", "206"}, // ? - uppercase I, circumflex accent
  131. {"Iuml", "207"}, // ? - uppercase I, umlaut
  132. {"ETH", "208"}, // ? - uppercase Eth, Icelandic
  133. {"Ntilde", "209"}, // ? - uppercase N, tilde
  134. {"Ograve", "210"}, // ? - uppercase O, grave accent
  135. {"Oacute", "211"}, // ? - uppercase O, acute accent
  136. {"Ocirc", "212"}, // ? - uppercase O, circumflex accent
  137. {"Otilde", "213"}, // ? - uppercase O, tilde
  138. {"Ouml", "214"}, // ? - uppercase O, umlaut
  139. {"times", "215"}, //multiplication sign
  140. {"Oslash", "216"}, // ? - uppercase O, slash
  141. {"Ugrave", "217"}, // ? - uppercase U, grave accent
  142. {"Uacute", "218"}, // ? - uppercase U, acute accent
  143. {"Ucirc", "219"}, // ? - uppercase U, circumflex accent
  144. {"Uuml", "220"}, // ? - uppercase U, umlaut
  145. {"Yacute", "221"}, // ? - uppercase Y, acute accent
  146. {"THORN", "222"}, // ? - uppercase THORN, Icelandic
  147. {"szlig", "223"}, // ? - lowercase sharps, German
  148. {"agrave", "224"}, // ? - lowercase a, grave accent
  149. {"aacute", "225"}, // ? - lowercase a, acute accent
  150. {"acirc", "226"}, // ? - lowercase a, circumflex accent
  151. {"atilde", "227"}, // ? - lowercase a, tilde
  152. {"auml", "228"}, // ? - lowercase a, umlaut
  153. {"aring", "229"}, // ? - lowercase a, ring
  154. {"aelig", "230"}, // ? - lowercase ae
  155. {"ccedil", "231"}, // ? - lowercase c, cedilla
  156. {"egrave", "232"}, // ? - lowercase e, grave accent
  157. {"eacute", "233"}, // ? - lowercase e, acute accent
  158. {"ecirc", "234"}, // ? - lowercase e, circumflex accent
  159. {"euml", "235"}, // ? - lowercase e, umlaut
  160. {"igrave", "236"}, // ? - lowercase i, grave accent
  161. {"iacute", "237"}, // ? - lowercase i, acute accent
  162. {"icirc", "238"}, // ? - lowercase i, circumflex accent
  163. {"iuml", "239"}, // ? - lowercase i, umlaut
  164. {"eth", "240"}, // ? - lowercase eth, Icelandic
  165. {"ntilde", "241"}, // ? - lowercase n, tilde
  166. {"ograve", "242"}, // ? - lowercase o, grave accent
  167. {"oacute", "243"}, // ? - lowercase o, acute accent
  168. {"ocirc", "244"}, // ? - lowercase o, circumflex accent
  169. {"otilde", "245"}, // ? - lowercase o, tilde
  170. {"ouml", "246"}, // ? - lowercase o, umlaut
  171. {"divide", "247"}, // division sign
  172. {"oslash", "248"}, // ? - lowercase o, slash
  173. {"ugrave", "249"}, // ? - lowercase u, grave accent
  174. {"uacute", "250"}, // ? - lowercase u, acute accent
  175. {"ucirc", "251"}, // ? - lowercase u, circumflex accent
  176. {"uuml", "252"}, // ? - lowercase u, umlaut
  177. {"yacute", "253"}, // ? - lowercase y, acute accent
  178. {"thorn", "254"}, // ? - lowercase thorn, Icelandic
  179. {"yuml", "255"}, // ? - lowercase y, umlaut
  180. };
  181. // http://www.w3.org/TR/REC-html40/sgml/entities.html
  182. // package scoped for testing
  183. static final String[][] HTML40_ARRAY = {
  184. // <!-- Latin Extended-B -->
  185. {"fnof", "402"}, //latin small f with hook = function= florin, U+0192 ISOtech -->
  186. // <!-- Greek -->
  187. {"Alpha", "913"}, //greek capital letter alpha, U+0391 -->
  188. {"Beta", "914"}, //greek capital letter beta, U+0392 -->
  189. {"Gamma", "915"}, //greek capital letter gamma,U+0393 ISOgrk3 -->
  190. {"Delta", "916"}, //greek capital letter delta,U+0394 ISOgrk3 -->
  191. {"Epsilon", "917"}, //greek capital letter epsilon, U+0395 -->
  192. {"Zeta", "918"}, //greek capital letter zeta, U+0396 -->
  193. {"Eta", "919"}, //greek capital letter eta, U+0397 -->
  194. {"Theta", "920"}, //greek capital letter theta,U+0398 ISOgrk3 -->
  195. {"Iota", "921"}, //greek capital letter iota, U+0399 -->
  196. {"Kappa", "922"}, //greek capital letter kappa, U+039A -->
  197. {"Lambda", "923"}, //greek capital letter lambda,U+039B ISOgrk3 -->
  198. {"Mu", "924"}, //greek capital letter mu, U+039C -->
  199. {"Nu", "925"}, //greek capital letter nu, U+039D -->
  200. {"Xi", "926"}, //greek capital letter xi, U+039E ISOgrk3 -->
  201. {"Omicron", "927"}, //greek capital letter omicron, U+039F -->
  202. {"Pi", "928"}, //greek capital letter pi, U+03A0 ISOgrk3 -->
  203. {"Rho", "929"}, //greek capital letter rho, U+03A1 -->
  204. // <!-- there is no Sigmaf, and no U+03A2 character either -->
  205. {"Sigma", "931"}, //greek capital letter sigma,U+03A3 ISOgrk3 -->
  206. {"Tau", "932"}, //greek capital letter tau, U+03A4 -->
  207. {"Upsilon", "933"}, //greek capital letter upsilon,U+03A5 ISOgrk3 -->
  208. {"Phi", "934"}, //greek capital letter phi,U+03A6 ISOgrk3 -->
  209. {"Chi", "935"}, //greek capital letter chi, U+03A7 -->
  210. {"Psi", "936"}, //greek capital letter psi,U+03A8 ISOgrk3 -->
  211. {"Omega", "937"}, //greek capital letter omega,U+03A9 ISOgrk3 -->
  212. {"alpha", "945"}, //greek small letter alpha,U+03B1 ISOgrk3 -->
  213. {"beta", "946"}, //greek small letter beta, U+03B2 ISOgrk3 -->
  214. {"gamma", "947"}, //greek small letter gamma,U+03B3 ISOgrk3 -->
  215. {"delta", "948"}, //greek small letter delta,U+03B4 ISOgrk3 -->
  216. {"epsilon", "949"}, //greek small letter epsilon,U+03B5 ISOgrk3 -->
  217. {"zeta", "950"}, //greek small letter zeta, U+03B6 ISOgrk3 -->
  218. {"eta", "951"}, //greek small letter eta, U+03B7 ISOgrk3 -->
  219. {"theta", "952"}, //greek small letter theta,U+03B8 ISOgrk3 -->
  220. {"iota", "953"}, //greek small letter iota, U+03B9 ISOgrk3 -->
  221. {"kappa", "954"}, //greek small letter kappa,U+03BA ISOgrk3 -->
  222. {"lambda", "955"}, //greek small letter lambda,U+03BB ISOgrk3 -->
  223. {"mu", "956"}, //greek small letter mu, U+03BC ISOgrk3 -->
  224. {"nu", "957"}, //greek small letter nu, U+03BD ISOgrk3 -->
  225. {"xi", "958"}, //greek small letter xi, U+03BE ISOgrk3 -->
  226. {"omicron", "959"}, //greek small letter omicron, U+03BF NEW -->
  227. {"pi", "960"}, //greek small letter pi, U+03C0 ISOgrk3 -->
  228. {"rho", "961"}, //greek small letter rho, U+03C1 ISOgrk3 -->
  229. {"sigmaf", "962"}, //greek small letter final sigma,U+03C2 ISOgrk3 -->
  230. {"sigma", "963"}, //greek small letter sigma,U+03C3 ISOgrk3 -->
  231. {"tau", "964"}, //greek small letter tau, U+03C4 ISOgrk3 -->
  232. {"upsilon", "965"}, //greek small letter upsilon,U+03C5 ISOgrk3 -->
  233. {"phi", "966"}, //greek small letter phi, U+03C6 ISOgrk3 -->
  234. {"chi", "967"}, //greek small letter chi, U+03C7 ISOgrk3 -->
  235. {"psi", "968"}, //greek small letter psi, U+03C8 ISOgrk3 -->
  236. {"omega", "969"}, //greek small letter omega,U+03C9 ISOgrk3 -->
  237. {"thetasym", "977"}, //greek small letter theta symbol,U+03D1 NEW -->
  238. {"upsih", "978"}, //greek upsilon with hook symbol,U+03D2 NEW -->
  239. {"piv", "982"}, //greek pi symbol, U+03D6 ISOgrk3 -->
  240. // <!-- General Punctuation -->
  241. {"bull", "8226"}, //bullet = black small circle,U+2022 ISOpub -->
  242. // <!-- bullet is NOT the same as bullet operator, U+2219 -->
  243. {"hellip", "8230"}, //horizontal ellipsis = three dot leader,U+2026 ISOpub -->
  244. {"prime", "8242"}, //prime = minutes = feet, U+2032 ISOtech -->
  245. {"Prime", "8243"}, //double prime = seconds = inches,U+2033 ISOtech -->
  246. {"oline", "8254"}, //overline = spacing overscore,U+203E NEW -->
  247. {"frasl", "8260"}, //fraction slash, U+2044 NEW -->
  248. // <!-- Letterlike Symbols -->
  249. {"weierp", "8472"}, //script capital P = power set= Weierstrass p, U+2118 ISOamso -->
  250. {"image", "8465"}, //blackletter capital I = imaginary part,U+2111 ISOamso -->
  251. {"real", "8476"}, //blackletter capital R = real part symbol,U+211C ISOamso -->
  252. {"trade", "8482"}, //trade mark sign, U+2122 ISOnum -->
  253. {"alefsym", "8501"}, //alef symbol = first transfinite cardinal,U+2135 NEW -->
  254. // <!-- alef symbol is NOT the same as hebrew letter alef,U+05D0 although the same glyph could be used to depict both characters -->
  255. // <!-- Arrows -->
  256. {"larr", "8592"}, //leftwards arrow, U+2190 ISOnum -->
  257. {"uarr", "8593"}, //upwards arrow, U+2191 ISOnum-->
  258. {"rarr", "8594"}, //rightwards arrow, U+2192 ISOnum -->
  259. {"darr", "8595"}, //downwards arrow, U+2193 ISOnum -->
  260. {"harr", "8596"}, //left right arrow, U+2194 ISOamsa -->
  261. {"crarr", "8629"}, //downwards arrow with corner leftwards= carriage return, U+21B5 NEW -->
  262. {"lArr", "8656"}, //leftwards double arrow, U+21D0 ISOtech -->
  263. // <!-- ISO 10646 does not say that lArr is the same as the 'is implied by' arrowbut also does not have any other character for that function. So ? lArr canbe used for 'is implied by' as ISOtech suggests -->
  264. {"uArr", "8657"}, //upwards double arrow, U+21D1 ISOamsa -->
  265. {"rArr", "8658"}, //rightwards double arrow,U+21D2 ISOtech -->
  266. // <!-- ISO 10646 does not say this is the 'implies' character but does not have another character with this function so ?rArr can be used for 'implies' as ISOtech suggests -->
  267. {"dArr", "8659"}, //downwards double arrow, U+21D3 ISOamsa -->
  268. {"hArr", "8660"}, //left right double arrow,U+21D4 ISOamsa -->
  269. // <!-- Mathematical Operators -->
  270. {"forall", "8704"}, //for all, U+2200 ISOtech -->
  271. {"part", "8706"}, //partial differential, U+2202 ISOtech -->
  272. {"exist", "8707"}, //there exists, U+2203 ISOtech -->
  273. {"empty", "8709"}, //empty set = null set = diameter,U+2205 ISOamso -->
  274. {"nabla", "8711"}, //nabla = backward difference,U+2207 ISOtech -->
  275. {"isin", "8712"}, //element of, U+2208 ISOtech -->
  276. {"notin", "8713"}, //not an element of, U+2209 ISOtech -->
  277. {"ni", "8715"}, //contains as member, U+220B ISOtech -->
  278. // <!-- should there be a more memorable name than 'ni'? -->
  279. {"prod", "8719"}, //n-ary product = product sign,U+220F ISOamsb -->
  280. // <!-- prod is NOT the same character as U+03A0 'greek capital letter pi' thoughthe same glyph might be used for both -->
  281. {"sum", "8721"}, //n-ary sumation, U+2211 ISOamsb -->
  282. // <!-- sum is NOT the same character as U+03A3 'greek capital letter sigma'though the same glyph might be used for both -->
  283. {"minus", "8722"}, //minus sign, U+2212 ISOtech -->
  284. {"lowast", "8727"}, //asterisk operator, U+2217 ISOtech -->
  285. {"radic", "8730"}, //square root = radical sign,U+221A ISOtech -->
  286. {"prop", "8733"}, //proportional to, U+221D ISOtech -->
  287. {"infin", "8734"}, //infinity, U+221E ISOtech -->
  288. {"ang", "8736"}, //angle, U+2220 ISOamso -->
  289. {"and", "8743"}, //logical and = wedge, U+2227 ISOtech -->
  290. {"or", "8744"}, //logical or = vee, U+2228 ISOtech -->
  291. {"cap", "8745"}, //intersection = cap, U+2229 ISOtech -->
  292. {"cup", "8746"}, //union = cup, U+222A ISOtech -->
  293. {"int", "8747"}, //integral, U+222B ISOtech -->
  294. {"there4", "8756"}, //therefore, U+2234 ISOtech -->
  295. {"sim", "8764"}, //tilde operator = varies with = similar to,U+223C ISOtech -->
  296. // <!-- tilde operator is NOT the same character as the tilde, U+007E,although the same glyph might be used to represent both -->
  297. {"cong", "8773"}, //approximately equal to, U+2245 ISOtech -->
  298. {"asymp", "8776"}, //almost equal to = asymptotic to,U+2248 ISOamsr -->
  299. {"ne", "8800"}, //not equal to, U+2260 ISOtech -->
  300. {"equiv", "8801"}, //identical to, U+2261 ISOtech -->
  301. {"le", "8804"}, //less-than or equal to, U+2264 ISOtech -->
  302. {"ge", "8805"}, //greater-than or equal to,U+2265 ISOtech -->
  303. {"sub", "8834"}, //subset of, U+2282 ISOtech -->
  304. {"sup", "8835"}, //superset of, U+2283 ISOtech -->
  305. // <!-- note that nsup, 'not a superset of, U+2283' is not covered by the Symbol font encoding and is not included. Should it be, for symmetry?It is in ISOamsn --> <!ENTITY nsub", "8836"}, //not a subset of, U+2284 ISOamsn -->
  306. {"sube", "8838"}, //subset of or equal to, U+2286 ISOtech -->
  307. {"supe", "8839"}, //superset of or equal to,U+2287 ISOtech -->
  308. {"oplus", "8853"}, //circled plus = direct sum,U+2295 ISOamsb -->
  309. {"otimes", "8855"}, //circled times = vector product,U+2297 ISOamsb -->
  310. {"perp", "8869"}, //up tack = orthogonal to = perpendicular,U+22A5 ISOtech -->
  311. {"sdot", "8901"}, //dot operator, U+22C5 ISOamsb -->
  312. // <!-- dot operator is NOT the same character as U+00B7 middle dot -->
  313. // <!-- Miscellaneous Technical -->
  314. {"lceil", "8968"}, //left ceiling = apl upstile,U+2308 ISOamsc -->
  315. {"rceil", "8969"}, //right ceiling, U+2309 ISOamsc -->
  316. {"lfloor", "8970"}, //left floor = apl downstile,U+230A ISOamsc -->
  317. {"rfloor", "8971"}, //right floor, U+230B ISOamsc -->
  318. {"lang", "9001"}, //left-pointing angle bracket = bra,U+2329 ISOtech -->
  319. // <!-- lang is NOT the same character as U+003C 'less than' or U+2039 'single left-pointing angle quotation mark' -->
  320. {"rang", "9002"}, //right-pointing angle bracket = ket,U+232A ISOtech -->
  321. // <!-- rang is NOT the same character as U+003E 'greater than' or U+203A 'single right-pointing angle quotation mark' -->
  322. // <!-- Geometric Shapes -->
  323. {"loz", "9674"}, //lozenge, U+25CA ISOpub -->
  324. // <!-- Miscellaneous Symbols -->
  325. {"spades", "9824"}, //black spade suit, U+2660 ISOpub -->
  326. // <!-- black here seems to mean filled as opposed to hollow -->
  327. {"clubs", "9827"}, //black club suit = shamrock,U+2663 ISOpub -->
  328. {"hearts", "9829"}, //black heart suit = valentine,U+2665 ISOpub -->
  329. {"diams", "9830"}, //black diamond suit, U+2666 ISOpub -->
  330. // <!-- Latin Extended-A -->
  331. {"OElig", "338"}, // -- latin capital ligature OE,U+0152 ISOlat2 -->
  332. {"oelig", "339"}, // -- latin small ligature oe, U+0153 ISOlat2 -->
  333. // <!-- ligature is a misnomer, this is a separate character in some languages -->
  334. {"Scaron", "352"}, // -- latin capital letter S with caron,U+0160 ISOlat2 -->
  335. {"scaron", "353"}, // -- latin small letter s with caron,U+0161 ISOlat2 -->
  336. {"Yuml", "376"}, // -- latin capital letter Y with diaeresis,U+0178 ISOlat2 -->
  337. // <!-- Spacing Modifier Letters -->
  338. {"circ", "710"}, // -- modifier letter circumflex accent,U+02C6 ISOpub -->
  339. {"tilde", "732"}, //small tilde, U+02DC ISOdia -->
  340. // <!-- General Punctuation -->
  341. {"ensp", "8194"}, //en space, U+2002 ISOpub -->
  342. {"emsp", "8195"}, //em space, U+2003 ISOpub -->
  343. {"thinsp", "8201"}, //thin space, U+2009 ISOpub -->
  344. {"zwnj", "8204"}, //zero width non-joiner,U+200C NEW RFC 2070 -->
  345. {"zwj", "8205"}, //zero width joiner, U+200D NEW RFC 2070 -->
  346. {"lrm", "8206"}, //left-to-right mark, U+200E NEW RFC 2070 -->
  347. {"rlm", "8207"}, //right-to-left mark, U+200F NEW RFC 2070 -->
  348. {"ndash", "8211"}, //en dash, U+2013 ISOpub -->
  349. {"mdash", "8212"}, //em dash, U+2014 ISOpub -->
  350. {"lsquo", "8216"}, //left single quotation mark,U+2018 ISOnum -->
  351. {"rsquo", "8217"}, //right single quotation mark,U+2019 ISOnum -->
  352. {"sbquo", "8218"}, //single low-9 quotation mark, U+201A NEW -->
  353. {"ldquo", "8220"}, //left double quotation mark,U+201C ISOnum -->
  354. {"rdquo", "8221"}, //right double quotation mark,U+201D ISOnum -->
  355. {"bdquo", "8222"}, //double low-9 quotation mark, U+201E NEW -->
  356. {"dagger", "8224"}, //dagger, U+2020 ISOpub -->
  357. {"Dagger", "8225"}, //double dagger, U+2021 ISOpub -->
  358. {"permil", "8240"}, //per mille sign, U+2030 ISOtech -->
  359. {"lsaquo", "8249"}, //single left-pointing angle quotation mark,U+2039 ISO proposed -->
  360. // <!-- lsaquo is proposed but not yet ISO standardized -->
  361. {"rsaquo", "8250"}, //single right-pointing angle quotation mark,U+203A ISO proposed -->
  362. // <!-- rsaquo is proposed but not yet ISO standardized -->
  363. {"euro", "8364"}, // -- euro sign, U+20AC NEW -->
  364. };
  365. /**
  366. * <p>The set of entities supported by standard XML.</p>
  367. */
  368. public static final Entities XML;
  369. /**
  370. * <p>The set of entities supported by HTML 3.2.</p>
  371. */
  372. public static final Entities HTML32;
  373. /**
  374. * <p>The set of entities supported by HTML 4.0.</p>
  375. */
  376. public static final Entities HTML40;
  377. static {
  378. XML = new Entities();
  379. XML.addEntities(BASIC_ARRAY);
  380. XML.addEntities(APOS_ARRAY);
  381. }
  382. static {
  383. HTML32 = new Entities();
  384. HTML32.addEntities(BASIC_ARRAY);
  385. HTML32.addEntities(ISO8859_1_ARRAY);
  386. }
  387. static {
  388. HTML40 = new Entities();
  389. fillWithHtml40Entities(HTML40);
  390. }
  391. static void fillWithHtml40Entities(Entities entities) {
  392. entities.addEntities(BASIC_ARRAY);
  393. entities.addEntities(ISO8859_1_ARRAY);
  394. entities.addEntities(HTML40_ARRAY);
  395. }
  396. static interface EntityMap {
  397. void add(String name, int value);
  398. String name(int value);
  399. int value(String name);
  400. }
  401. static class PrimitiveEntityMap implements EntityMap {
  402. private Map mapNameToValue = new HashMap();
  403. private IntHashMap mapValueToName = new IntHashMap();
  404. public void add(String name, int value) {
  405. mapNameToValue.put(name, new Integer(value));
  406. mapValueToName.put(value, name);
  407. }
  408. public String name(int value) {
  409. return (String) mapValueToName.get(value);
  410. }
  411. public int value(String name) {
  412. Object value = mapNameToValue.get(name);
  413. if (value == null)
  414. return -1;
  415. return ((Integer) value).intValue();
  416. }
  417. }
  418. static abstract class MapIntMap implements Entities.EntityMap {
  419. protected Map mapNameToValue;
  420. protected Map mapValueToName;
  421. public void add(String name, int value) {
  422. mapNameToValue.put(name, new Integer(value));
  423. mapValueToName.put(new Integer(value), name);
  424. }
  425. public String name(int value) {
  426. return (String) mapValueToName.get(new Integer(value));
  427. }
  428. public int value(String name) {
  429. Object value = mapNameToValue.get(name);
  430. if (value == null)
  431. return -1;
  432. return ((Integer) value).intValue();
  433. }
  434. }
  435. static class HashEntityMap extends MapIntMap {
  436. public HashEntityMap() {
  437. mapNameToValue = new HashMap();
  438. mapValueToName = new HashMap();
  439. }
  440. }
  441. static class TreeEntityMap extends MapIntMap {
  442. public TreeEntityMap() {
  443. mapNameToValue = new TreeMap();
  444. mapValueToName = new TreeMap();
  445. }
  446. }
  447. static class LookupEntityMap extends PrimitiveEntityMap {
  448. private String[] lookupTable;
  449. private int LOOKUP_TABLE_SIZE = 256;
  450. public String name(int value) {
  451. if (value < LOOKUP_TABLE_SIZE) {
  452. return lookupTable()[value];
  453. }
  454. return super.name(value);
  455. }
  456. private String[] lookupTable() {
  457. if (lookupTable == null) {
  458. createLookupTable();
  459. }
  460. return lookupTable;
  461. }
  462. private void createLookupTable() {
  463. lookupTable = new String[LOOKUP_TABLE_SIZE];
  464. for (int i = 0; i < LOOKUP_TABLE_SIZE; ++i) {
  465. lookupTable[i] = super.name(i);
  466. }
  467. }
  468. }
  469. static class ArrayEntityMap implements EntityMap {
  470. protected int growBy = 100;
  471. protected int size = 0;
  472. protected String[] names;
  473. protected int[] values;
  474. public ArrayEntityMap() {
  475. names = new String[growBy];
  476. values = new int[growBy];
  477. }
  478. public ArrayEntityMap(int growBy) {
  479. this.growBy = growBy;
  480. names = new String[growBy];
  481. values = new int[growBy];
  482. }
  483. public void add(String name, int value) {
  484. ensureCapacity(size + 1);
  485. names[size] = name;
  486. values[size] = value;
  487. size++;
  488. }
  489. protected void ensureCapacity(int capacity) {
  490. if (capacity > names.length) {
  491. int newSize = Math.max(capacity, size + growBy);
  492. String[] newNames = new String[newSize];
  493. System.arraycopy(names, 0, newNames, 0, size);
  494. names = newNames;
  495. int[] newValues = new int[newSize];
  496. System.arraycopy(values, 0, newValues, 0, size);
  497. values = newValues;
  498. }
  499. }
  500. public String name(int value) {
  501. for (int i = 0; i < size; ++i) {
  502. if (values[i] == value) {
  503. return names[i];
  504. }
  505. }
  506. return null;
  507. }
  508. public int value(String name) {
  509. for (int i = 0; i < size; ++i) {
  510. if (names[i].equals(name)) {
  511. return values[i];
  512. }
  513. }
  514. return -1;
  515. }
  516. }
  517. static class BinaryEntityMap extends ArrayEntityMap {
  518. public BinaryEntityMap() {
  519. }
  520. public BinaryEntityMap(int growBy) {
  521. super(growBy);
  522. }
  523. // based on code in java.util.Arrays
  524. private int binarySearch(int key) {
  525. int low = 0;
  526. int high = size - 1;
  527. while (low <= high) {
  528. int mid = (low + high) >> 1;
  529. int midVal = values[mid];
  530. if (midVal < key)
  531. low = mid + 1;
  532. else if (midVal > key)
  533. high = mid - 1;
  534. else
  535. return mid; // key found
  536. }
  537. return -(low + 1); // key not found.
  538. }
  539. public void add(String name, int value) {
  540. ensureCapacity(size + 1);
  541. int insertAt = binarySearch(value);
  542. if (insertAt > 0) return; // note: this means you can't insert the same value twice
  543. insertAt = -(insertAt + 1); // binarySearch returns it negative and off-by-one
  544. System.arraycopy(values, insertAt, values, insertAt + 1, size - insertAt);
  545. values[insertAt] = value;
  546. System.arraycopy(names, insertAt, names, insertAt + 1, size - insertAt);
  547. names[insertAt] = name;
  548. size++;
  549. }
  550. public String name(int value) {
  551. int index = binarySearch(value);
  552. if (index < 0) return null;
  553. return names[index];
  554. }
  555. }
  556. // package scoped for testing
  557. EntityMap map = new Entities.LookupEntityMap();
  558. public void addEntities(String[][] entityArray) {
  559. for (int i = 0; i < entityArray.length; ++i) {
  560. addEntity(entityArray[i][0], Integer.parseInt(entityArray[i][1]));
  561. }
  562. }
  563. public void addEntity(String name, int value) {
  564. map.add(name, value);
  565. }
  566. public String entityName(int value) {
  567. return map.name(value);
  568. }
  569. public int entityValue(String name) {
  570. return map.value(name);
  571. }
  572. /**
  573. * <p>Escapes the characters in a <code>String</code>.</p>
  574. *
  575. * <p>For example, if you have called addEntity("foo", 0xA1),
  576. * escape("\u00A1") will return "&foo;"</p>
  577. *
  578. * @param str The <code>String</code> to escape.
  579. * @return A new escaped <code>String</code>.
  580. */
  581. public String escape(String str) {
  582. //todo: rewrite to use a Writer
  583. StringBuffer buf = new StringBuffer(str.length() * 2);
  584. int i;
  585. for (i = 0; i < str.length(); ++i) {
  586. char ch = str.charAt(i);
  587. String entityName = this.entityName(ch);
  588. if (entityName == null) {
  589. if (ch > 0x7F) {
  590. int intValue = ch;
  591. buf.append("&#");
  592. buf.append(intValue);
  593. buf.append(';');
  594. } else {
  595. buf.append(ch);
  596. }
  597. } else {
  598. buf.append('&');
  599. buf.append(entityName);
  600. buf.append(';');
  601. }
  602. }
  603. return buf.toString();
  604. }
  605. /**
  606. * <p>Unescapes the entities in a <code>String</code>.</p>
  607. *
  608. * <p>For example, if you have called addEntity("foo", 0xA1),
  609. * unescape("&foo;") will return "\u00A1"</p>
  610. *
  611. * @param str The <code>String</code> to escape.
  612. * @return A new escaped <code>String</code>.
  613. */
  614. public String unescape(String str) {
  615. StringBuffer buf = new StringBuffer(str.length());
  616. int i;
  617. for (i = 0; i < str.length(); ++i) {
  618. char ch = str.charAt(i);
  619. if (ch == '&') {
  620. int semi = str.indexOf(';', i + 1);
  621. if (semi == -1) {
  622. buf.append(ch);
  623. continue;
  624. }
  625. String entityName = str.substring(i + 1, semi);
  626. int entityValue;
  627. if (entityName.charAt(0) == '#') {
  628. entityValue = Integer.parseInt(entityName.substring(1));
  629. } else {
  630. entityValue = this.entityValue(entityName);
  631. }
  632. if (entityValue == -1) {
  633. buf.append('&');
  634. buf.append(entityName);
  635. buf.append(';');
  636. } else {
  637. buf.append((char) (entityValue));
  638. }
  639. i = semi;
  640. } else {
  641. buf.append(ch);
  642. }
  643. }
  644. return buf.toString();
  645. }
  646. }