You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Entities.java 40KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.dmdirc.addons.freedesktop_notifications.commons;
  18. import java.io.IOException;
  19. import java.io.StringWriter;
  20. import java.io.Writer;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.TreeMap;
  24. /**
  25. * <p>
  26. * Provides HTML and XML entity utilities.
  27. * </p>
  28. *
  29. * @see <a href="http://hotwired.lycos.com/webmonkey/reference/special_characters/">ISO Entities</a>
  30. * @see <a href="http://www.w3.org/TR/REC-html32#latin1">HTML 3.2 Character Entities for ISO Latin-1</a>
  31. * @see <a href="http://www.w3.org/TR/REC-html40/sgml/entities.html">HTML 4.0 Character entity references</a>
  32. * @see <a href="http://www.w3.org/TR/html401/charset.html#h-5.3">HTML 4.01 Character References</a>
  33. * @see <a href="http://www.w3.org/TR/html401/charset.html#code-position">HTML 4.01 Code positions</a>
  34. *
  35. * @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
  36. * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
  37. * @since 2.0
  38. * @version $Id$
  39. */
  40. class Entities {
  41. private static final String[][] BASIC_ARRAY = {{"quot", "34"}, // " - double-quote
  42. {"amp", "38"}, // & - ampersand
  43. {"lt", "60"}, // < - less-than
  44. {"gt", "62"}, // > - greater-than
  45. };
  46. private static final String[][] APOS_ARRAY = {{"apos", "39"}, // XML apostrophe
  47. };
  48. private static final String[][] ISO8859_1_ARRAY = {{"nbsp", "160"}, // non-breaking space
  49. {"iexcl", "161"}, // inverted exclamation mark
  50. {"cent", "162"}, // cent sign
  51. {"pound", "163"}, // pound sign
  52. {"curren", "164"}, // currency sign
  53. {"yen", "165"}, // yen sign = yuan sign
  54. {"brvbar", "166"}, // broken bar = broken vertical bar
  55. {"sect", "167"}, // section sign
  56. {"uml", "168"}, // diaeresis = spacing diaeresis
  57. {"copy", "169"}, // © - copyright sign
  58. {"ordf", "170"}, // feminine ordinal indicator
  59. {"laquo", "171"}, // left-pointing double angle quotation mark = left pointing guillemet
  60. {"not", "172"}, // not sign
  61. {"shy", "173"}, // soft hyphen = discretionary hyphen
  62. {"reg", "174"}, // ® - registered trademark sign
  63. {"macr", "175"}, // macron = spacing macron = overline = APL overbar
  64. {"deg", "176"}, // degree sign
  65. {"plusmn", "177"}, // plus-minus sign = plus-or-minus sign
  66. {"sup2", "178"}, // superscript two = superscript digit two = squared
  67. {"sup3", "179"}, // superscript three = superscript digit three = cubed
  68. {"acute", "180"}, // acute accent = spacing acute
  69. {"micro", "181"}, // micro sign
  70. {"para", "182"}, // pilcrow sign = paragraph sign
  71. {"middot", "183"}, // middle dot = Georgian comma = Greek middle dot
  72. {"cedil", "184"}, // cedilla = spacing cedilla
  73. {"sup1", "185"}, // superscript one = superscript digit one
  74. {"ordm", "186"}, // masculine ordinal indicator
  75. {"raquo", "187"}, // right-pointing double angle quotation mark = right pointing guillemet
  76. {"frac14", "188"}, // vulgar fraction one quarter = fraction one quarter
  77. {"frac12", "189"}, // vulgar fraction one half = fraction one half
  78. {"frac34", "190"}, // vulgar fraction three quarters = fraction three quarters
  79. {"iquest", "191"}, // inverted question mark = turned question mark
  80. {"Agrave", "192"}, // À - uppercase A, grave accent
  81. {"Aacute", "193"}, // Á - uppercase A, acute accent
  82. {"Acirc", "194"}, // Â - uppercase A, circumflex accent
  83. {"Atilde", "195"}, // Ã - uppercase A, tilde
  84. {"Auml", "196"}, // Ä - uppercase A, umlaut
  85. {"Aring", "197"}, // Å - uppercase A, ring
  86. {"AElig", "198"}, // Æ - uppercase AE
  87. {"Ccedil", "199"}, // Ç - uppercase C, cedilla
  88. {"Egrave", "200"}, // È - uppercase E, grave accent
  89. {"Eacute", "201"}, // É - uppercase E, acute accent
  90. {"Ecirc", "202"}, // Ê - uppercase E, circumflex accent
  91. {"Euml", "203"}, // Ë - uppercase E, umlaut
  92. {"Igrave", "204"}, // Ì - uppercase I, grave accent
  93. {"Iacute", "205"}, // Í - uppercase I, acute accent
  94. {"Icirc", "206"}, // Î - uppercase I, circumflex accent
  95. {"Iuml", "207"}, // Ï - uppercase I, umlaut
  96. {"ETH", "208"}, // Ð - uppercase Eth, Icelandic
  97. {"Ntilde", "209"}, // Ñ - uppercase N, tilde
  98. {"Ograve", "210"}, // Ò - uppercase O, grave accent
  99. {"Oacute", "211"}, // Ó - uppercase O, acute accent
  100. {"Ocirc", "212"}, // Ô - uppercase O, circumflex accent
  101. {"Otilde", "213"}, // Õ - uppercase O, tilde
  102. {"Ouml", "214"}, // Ö - uppercase O, umlaut
  103. {"times", "215"}, // multiplication sign
  104. {"Oslash", "216"}, // Ø - uppercase O, slash
  105. {"Ugrave", "217"}, // Ù - uppercase U, grave accent
  106. {"Uacute", "218"}, // Ú - uppercase U, acute accent
  107. {"Ucirc", "219"}, // Û - uppercase U, circumflex accent
  108. {"Uuml", "220"}, // Ü - uppercase U, umlaut
  109. {"Yacute", "221"}, // Ý - uppercase Y, acute accent
  110. {"THORN", "222"}, // Þ - uppercase THORN, Icelandic
  111. {"szlig", "223"}, // ß - lowercase sharps, German
  112. {"agrave", "224"}, // à - lowercase a, grave accent
  113. {"aacute", "225"}, // á - lowercase a, acute accent
  114. {"acirc", "226"}, // â - lowercase a, circumflex accent
  115. {"atilde", "227"}, // ã - lowercase a, tilde
  116. {"auml", "228"}, // ä - lowercase a, umlaut
  117. {"aring", "229"}, // å - lowercase a, ring
  118. {"aelig", "230"}, // æ - lowercase ae
  119. {"ccedil", "231"}, // ç - lowercase c, cedilla
  120. {"egrave", "232"}, // è - lowercase e, grave accent
  121. {"eacute", "233"}, // é - lowercase e, acute accent
  122. {"ecirc", "234"}, // ê - lowercase e, circumflex accent
  123. {"euml", "235"}, // ë - lowercase e, umlaut
  124. {"igrave", "236"}, // ì - lowercase i, grave accent
  125. {"iacute", "237"}, // í - lowercase i, acute accent
  126. {"icirc", "238"}, // î - lowercase i, circumflex accent
  127. {"iuml", "239"}, // ï - lowercase i, umlaut
  128. {"eth", "240"}, // ð - lowercase eth, Icelandic
  129. {"ntilde", "241"}, // ñ - lowercase n, tilde
  130. {"ograve", "242"}, // ò - lowercase o, grave accent
  131. {"oacute", "243"}, // ó - lowercase o, acute accent
  132. {"ocirc", "244"}, // ô - lowercase o, circumflex accent
  133. {"otilde", "245"}, // õ - lowercase o, tilde
  134. {"ouml", "246"}, // ö - lowercase o, umlaut
  135. {"divide", "247"}, // division sign
  136. {"oslash", "248"}, // ø - lowercase o, slash
  137. {"ugrave", "249"}, // ù - lowercase u, grave accent
  138. {"uacute", "250"}, // ú - lowercase u, acute accent
  139. {"ucirc", "251"}, // û - lowercase u, circumflex accent
  140. {"uuml", "252"}, // ü - lowercase u, umlaut
  141. {"yacute", "253"}, // ý - lowercase y, acute accent
  142. {"thorn", "254"}, // þ - lowercase thorn, Icelandic
  143. {"yuml", "255"}, // ÿ - lowercase y, umlaut
  144. };
  145. // package scoped for testing
  146. static final int ISO8859_1_ARRAY_LENGTH = ISO8859_1_ARRAY.length;
  147. static String getISO88591(int i, int j) {
  148. return ISO8859_1_ARRAY[i][j];
  149. }
  150. // http://www.w3.org/TR/REC-html40/sgml/entities.html
  151. private static final String[][] HTML40_ARRAY = {
  152. // <!-- Latin Extended-B -->
  153. {"fnof", "402"}, // latin small f with hook = function= florin, U+0192 ISOtech -->
  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. // <!-- General Punctuation -->
  209. {"bull", "8226"}, // bullet = black small circle,U+2022 ISOpub -->
  210. // <!-- bullet is NOT the same as bullet operator, U+2219 -->
  211. {"hellip", "8230"}, // horizontal ellipsis = three dot leader,U+2026 ISOpub -->
  212. {"prime", "8242"}, // prime = minutes = feet, U+2032 ISOtech -->
  213. {"Prime", "8243"}, // double prime = seconds = inches,U+2033 ISOtech -->
  214. {"oline", "8254"}, // overline = spacing overscore,U+203E NEW -->
  215. {"frasl", "8260"}, // fraction slash, U+2044 NEW -->
  216. // <!-- Letterlike Symbols -->
  217. {"weierp", "8472"}, // script capital P = power set= Weierstrass p, U+2118 ISOamso -->
  218. {"image", "8465"}, // blackletter capital I = imaginary part,U+2111 ISOamso -->
  219. {"real", "8476"}, // blackletter capital R = real part symbol,U+211C ISOamso -->
  220. {"trade", "8482"}, // trade mark sign, U+2122 ISOnum -->
  221. {"alefsym", "8501"}, // alef symbol = first transfinite cardinal,U+2135 NEW -->
  222. // <!-- alef symbol is NOT the same as hebrew letter alef,U+05D0 although the
  223. // same glyph could be used to depict both characters -->
  224. // <!-- Arrows -->
  225. {"larr", "8592"}, // leftwards arrow, U+2190 ISOnum -->
  226. {"uarr", "8593"}, // upwards arrow, U+2191 ISOnum-->
  227. {"rarr", "8594"}, // rightwards arrow, U+2192 ISOnum -->
  228. {"darr", "8595"}, // downwards arrow, U+2193 ISOnum -->
  229. {"harr", "8596"}, // left right arrow, U+2194 ISOamsa -->
  230. {"crarr", "8629"}, // downwards arrow with corner leftwards= carriage return, U+21B5 NEW -->
  231. {"lArr", "8656"}, // leftwards double arrow, U+21D0 ISOtech -->
  232. // <!-- ISO 10646 does not say that lArr is the same as the 'is implied by'
  233. // arrow but also does not have any other character for that function.
  234. // So ? lArr canbe used for 'is implied by' as ISOtech suggests -->
  235. {"uArr", "8657"}, // upwards double arrow, U+21D1 ISOamsa -->
  236. {"rArr", "8658"}, // rightwards double arrow,U+21D2 ISOtech -->
  237. // <!-- ISO 10646 does not say this is the 'implies' character but does not
  238. // have another character with this function so ?rArr can be used for
  239. // 'implies' as ISOtech suggests -->
  240. {"dArr", "8659"}, // downwards double arrow, U+21D3 ISOamsa -->
  241. {"hArr", "8660"}, // left right double arrow,U+21D4 ISOamsa -->
  242. // <!-- Mathematical Operators -->
  243. {"forall", "8704"}, // for all, U+2200 ISOtech -->
  244. {"part", "8706"}, // partial differential, U+2202 ISOtech -->
  245. {"exist", "8707"}, // there exists, U+2203 ISOtech -->
  246. {"empty", "8709"}, // empty set = null set = diameter,U+2205 ISOamso -->
  247. {"nabla", "8711"}, // nabla = backward difference,U+2207 ISOtech -->
  248. {"isin", "8712"}, // element of, U+2208 ISOtech -->
  249. {"notin", "8713"}, // not an element of, U+2209 ISOtech -->
  250. {"ni", "8715"}, // contains as member, U+220B ISOtech -->
  251. // <!-- should there be a more memorable name than 'ni'? -->
  252. {"prod", "8719"}, // n-ary product = product sign,U+220F ISOamsb -->
  253. // <!-- prod is NOT the same character as U+03A0 'greek capital letter pi'
  254. // though the same glyph might be used for both -->
  255. {"sum", "8721"}, // n-ary summation, U+2211 ISOamsb -->
  256. // <!-- sum is NOT the same character as U+03A3 'greek capital letter sigma'
  257. // though the same glyph might be used for both -->
  258. {"minus", "8722"}, // minus sign, U+2212 ISOtech -->
  259. {"lowast", "8727"}, // asterisk operator, U+2217 ISOtech -->
  260. {"radic", "8730"}, // square root = radical sign,U+221A ISOtech -->
  261. {"prop", "8733"}, // proportional to, U+221D ISOtech -->
  262. {"infin", "8734"}, // infinity, U+221E ISOtech -->
  263. {"ang", "8736"}, // angle, U+2220 ISOamso -->
  264. {"and", "8743"}, // logical and = wedge, U+2227 ISOtech -->
  265. {"or", "8744"}, // logical or = vee, U+2228 ISOtech -->
  266. {"cap", "8745"}, // intersection = cap, U+2229 ISOtech -->
  267. {"cup", "8746"}, // union = cup, U+222A ISOtech -->
  268. {"int", "8747"}, // integral, U+222B ISOtech -->
  269. {"there4", "8756"}, // therefore, U+2234 ISOtech -->
  270. {"sim", "8764"}, // tilde operator = varies with = similar to,U+223C ISOtech -->
  271. // <!-- tilde operator is NOT the same character as the tilde, U+007E,although
  272. // the same glyph might be used to represent both -->
  273. {"cong", "8773"}, // approximately equal to, U+2245 ISOtech -->
  274. {"asymp", "8776"}, // almost equal to = asymptotic to,U+2248 ISOamsr -->
  275. {"ne", "8800"}, // not equal to, U+2260 ISOtech -->
  276. {"equiv", "8801"}, // identical to, U+2261 ISOtech -->
  277. {"le", "8804"}, // less-than or equal to, U+2264 ISOtech -->
  278. {"ge", "8805"}, // greater-than or equal to,U+2265 ISOtech -->
  279. {"sub", "8834"}, // subset of, U+2282 ISOtech -->
  280. {"sup", "8835"}, // superset of, U+2283 ISOtech -->
  281. // <!-- note that nsup, 'not a superset of, U+2283' is not covered by the
  282. // Symbol font encoding and is not included. Should it be, for symmetry?
  283. // It is in ISOamsn --> <!ENTITY nsub", "8836"},
  284. // not a subset of, U+2284 ISOamsn -->
  285. {"sube", "8838"}, // subset of or equal to, U+2286 ISOtech -->
  286. {"supe", "8839"}, // superset of or equal to,U+2287 ISOtech -->
  287. {"oplus", "8853"}, // circled plus = direct sum,U+2295 ISOamsb -->
  288. {"otimes", "8855"}, // circled times = vector product,U+2297 ISOamsb -->
  289. {"perp", "8869"}, // up tack = orthogonal to = perpendicular,U+22A5 ISOtech -->
  290. {"sdot", "8901"}, // dot operator, U+22C5 ISOamsb -->
  291. // <!-- dot operator is NOT the same character as U+00B7 middle dot -->
  292. // <!-- Miscellaneous Technical -->
  293. {"lceil", "8968"}, // left ceiling = apl upstile,U+2308 ISOamsc -->
  294. {"rceil", "8969"}, // right ceiling, U+2309 ISOamsc -->
  295. {"lfloor", "8970"}, // left floor = apl downstile,U+230A ISOamsc -->
  296. {"rfloor", "8971"}, // right floor, U+230B ISOamsc -->
  297. {"lang", "9001"}, // left-pointing angle bracket = bra,U+2329 ISOtech -->
  298. // <!-- lang is NOT the same character as U+003C 'less than' or U+2039 'single left-pointing angle quotation
  299. // mark' -->
  300. {"rang", "9002"}, // right-pointing angle bracket = ket,U+232A ISOtech -->
  301. // <!-- rang is NOT the same character as U+003E 'greater than' or U+203A
  302. // 'single right-pointing angle quotation mark' -->
  303. // <!-- Geometric Shapes -->
  304. {"loz", "9674"}, // lozenge, U+25CA ISOpub -->
  305. // <!-- Miscellaneous Symbols -->
  306. {"spades", "9824"}, // black spade suit, U+2660 ISOpub -->
  307. // <!-- black here seems to mean filled as opposed to hollow -->
  308. {"clubs", "9827"}, // black club suit = shamrock,U+2663 ISOpub -->
  309. {"hearts", "9829"}, // black heart suit = valentine,U+2665 ISOpub -->
  310. {"diams", "9830"}, // black diamond suit, U+2666 ISOpub -->
  311. // <!-- Latin Extended-A -->
  312. {"OElig", "338"}, // -- latin capital ligature OE,U+0152 ISOlat2 -->
  313. {"oelig", "339"}, // -- latin small ligature oe, U+0153 ISOlat2 -->
  314. // <!-- ligature is a misnomer, this is a separate character in some languages -->
  315. {"Scaron", "352"}, // -- latin capital letter S with caron,U+0160 ISOlat2 -->
  316. {"scaron", "353"}, // -- latin small letter s with caron,U+0161 ISOlat2 -->
  317. {"Yuml", "376"}, // -- latin capital letter Y with diaeresis,U+0178 ISOlat2 -->
  318. // <!-- Spacing Modifier Letters -->
  319. {"circ", "710"}, // -- modifier letter circumflex accent,U+02C6 ISOpub -->
  320. {"tilde", "732"}, // small tilde, U+02DC ISOdia -->
  321. // <!-- General Punctuation -->
  322. {"ensp", "8194"}, // en space, U+2002 ISOpub -->
  323. {"emsp", "8195"}, // em space, U+2003 ISOpub -->
  324. {"thinsp", "8201"}, // thin space, U+2009 ISOpub -->
  325. {"zwnj", "8204"}, // zero width non-joiner,U+200C NEW RFC 2070 -->
  326. {"zwj", "8205"}, // zero width joiner, U+200D NEW RFC 2070 -->
  327. {"lrm", "8206"}, // left-to-right mark, U+200E NEW RFC 2070 -->
  328. {"rlm", "8207"}, // right-to-left mark, U+200F NEW RFC 2070 -->
  329. {"ndash", "8211"}, // en dash, U+2013 ISOpub -->
  330. {"mdash", "8212"}, // em dash, U+2014 ISOpub -->
  331. {"lsquo", "8216"}, // left single quotation mark,U+2018 ISOnum -->
  332. {"rsquo", "8217"}, // right single quotation mark,U+2019 ISOnum -->
  333. {"sbquo", "8218"}, // single low-9 quotation mark, U+201A NEW -->
  334. {"ldquo", "8220"}, // left double quotation mark,U+201C ISOnum -->
  335. {"rdquo", "8221"}, // right double quotation mark,U+201D ISOnum -->
  336. {"bdquo", "8222"}, // double low-9 quotation mark, U+201E NEW -->
  337. {"dagger", "8224"}, // dagger, U+2020 ISOpub -->
  338. {"Dagger", "8225"}, // double dagger, U+2021 ISOpub -->
  339. {"permil", "8240"}, // per mille sign, U+2030 ISOtech -->
  340. {"lsaquo", "8249"}, // single left-pointing angle quotation mark,U+2039 ISO proposed -->
  341. // <!-- lsaquo is proposed but not yet ISO standardized -->
  342. {"rsaquo", "8250"}, // single right-pointing angle quotation mark,U+203A ISO proposed -->
  343. // <!-- rsaquo is proposed but not yet ISO standardized -->
  344. {"euro", "8364"}, // -- euro sign, U+20AC NEW -->
  345. };
  346. // package scoped for testing
  347. static final int HTML40_ARRAY_LENGTH = HTML40_ARRAY.length;
  348. static String getHTML40(int i, int j) {
  349. return HTML40_ARRAY[i][j];
  350. }
  351. /**
  352. * <p>
  353. * The set of entities supported by standard XML.
  354. * </p>
  355. */
  356. public static final Entities XML;
  357. /**
  358. * <p>
  359. * The set of entities supported by HTML 3.2.
  360. * </p>
  361. */
  362. public static final Entities HTML32;
  363. /**
  364. * <p>
  365. * The set of entities supported by HTML 4.0.
  366. * </p>
  367. */
  368. public static final Entities HTML40;
  369. static {
  370. XML = new Entities();
  371. XML.addEntities(BASIC_ARRAY);
  372. XML.addEntities(APOS_ARRAY);
  373. }
  374. static {
  375. HTML32 = new Entities();
  376. HTML32.addEntities(BASIC_ARRAY);
  377. HTML32.addEntities(ISO8859_1_ARRAY);
  378. }
  379. static {
  380. HTML40 = new Entities();
  381. fillWithHtml40Entities(HTML40);
  382. }
  383. /**
  384. * <p>
  385. * Fills the specified entities instance with HTML 40 entities.
  386. * </p>
  387. *
  388. * @param entities
  389. * the instance to be filled.
  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. /**
  398. * <p>
  399. * Add an entry to this entity map.
  400. * </p>
  401. *
  402. * @param name
  403. * the entity name
  404. * @param value
  405. * the entity value
  406. */
  407. void add(String name, int value);
  408. /**
  409. * <p>
  410. * Returns the name of the entity identified by the specified value.
  411. * </p>
  412. *
  413. * @param value
  414. * the value to locate
  415. * @return entity name associated with the specified value
  416. */
  417. String name(int value);
  418. /**
  419. * <p>
  420. * Returns the value of the entity identified by the specified name.
  421. * </p>
  422. *
  423. * @param name
  424. * the name to locate
  425. * @return entity value associated with the specified name
  426. */
  427. int value(String name);
  428. }
  429. static class PrimitiveEntityMap implements EntityMap {
  430. private final Map<String, Integer> mapNameToValue = new HashMap<String, Integer>();
  431. private final IntHashMap mapValueToName = new IntHashMap();
  432. /**
  433. * {@inheritDoc}
  434. */
  435. public void add(String name, int value) {
  436. mapNameToValue.put(name, value);
  437. mapValueToName.put(value, name);
  438. }
  439. /**
  440. * {@inheritDoc}
  441. */
  442. public String name(int value) {
  443. return (String) mapValueToName.get(value);
  444. }
  445. /**
  446. * {@inheritDoc}
  447. */
  448. public int value(String name) {
  449. Object value = mapNameToValue.get(name);
  450. if (value == null) {
  451. return -1;
  452. }
  453. return ((Integer) value).intValue();
  454. }
  455. }
  456. abstract static class MapIntMap implements Entities.EntityMap {
  457. protected Map<String, Integer> mapNameToValue;
  458. protected Map<Integer, String> mapValueToName;
  459. /**
  460. * {@inheritDoc}
  461. */
  462. public void add(String name, int value) {
  463. mapNameToValue.put(name, value);
  464. mapValueToName.put(value, name);
  465. }
  466. /**
  467. * {@inheritDoc}
  468. */
  469. public String name(int value) {
  470. return mapValueToName.get(value);
  471. }
  472. /**
  473. * {@inheritDoc}
  474. */
  475. public int value(String name) {
  476. Object value = mapNameToValue.get(name);
  477. if (value == null) {
  478. return -1;
  479. }
  480. return ((Integer) value).intValue();
  481. }
  482. }
  483. static class HashEntityMap extends MapIntMap {
  484. /**
  485. * Constructs a new instance of <code>HashEntityMap</code>.
  486. */
  487. public HashEntityMap() {
  488. mapNameToValue = new HashMap<String, Integer>();
  489. mapValueToName = new HashMap<Integer, String>();
  490. }
  491. }
  492. static class TreeEntityMap extends MapIntMap {
  493. /**
  494. * Constructs a new instance of <code>TreeEntityMap</code>.
  495. */
  496. public TreeEntityMap() {
  497. mapNameToValue = new TreeMap<String, Integer>();
  498. mapValueToName = new TreeMap<Integer, String>();
  499. }
  500. }
  501. static class LookupEntityMap extends PrimitiveEntityMap {
  502. private String[] lookupTable;
  503. private static final int LOOKUP_TABLE_SIZE = 256;
  504. /**
  505. * {@inheritDoc}
  506. */
  507. @Override
  508. public String name(int value) {
  509. if (value < LOOKUP_TABLE_SIZE) {
  510. return lookupTable()[value];
  511. }
  512. return super.name(value);
  513. }
  514. /**
  515. * <p>
  516. * Returns the lookup table for this entity map. The lookup table is created if it has not been previously.
  517. * </p>
  518. *
  519. * @return the lookup table
  520. */
  521. private String[] lookupTable() {
  522. if (lookupTable == null) {
  523. createLookupTable();
  524. }
  525. return lookupTable;
  526. }
  527. /**
  528. * <p>
  529. * Creates an entity lookup table of LOOKUP_TABLE_SIZE elements, initialized with entity names.
  530. * </p>
  531. */
  532. private void createLookupTable() {
  533. lookupTable = new String[LOOKUP_TABLE_SIZE];
  534. for (int i = 0; i < LOOKUP_TABLE_SIZE; ++i) {
  535. lookupTable[i] = super.name(i);
  536. }
  537. }
  538. }
  539. static class ArrayEntityMap implements EntityMap {
  540. protected int growBy = 100;
  541. protected int size = 0;
  542. protected String[] names;
  543. protected int[] values;
  544. /**
  545. * Constructs a new instance of <code>ArrayEntityMap</code>.
  546. */
  547. public ArrayEntityMap() {
  548. names = new String[growBy];
  549. values = new int[growBy];
  550. }
  551. /**
  552. * Constructs a new instance of <code>ArrayEntityMap</code> specifying the size by which the array should
  553. * grow.
  554. *
  555. * @param growBy
  556. * array will be initialized to and will grow by this amount
  557. */
  558. public ArrayEntityMap(int growBy) {
  559. this.growBy = growBy;
  560. names = new String[growBy];
  561. values = new int[growBy];
  562. }
  563. /**
  564. * {@inheritDoc}
  565. */
  566. public void add(String name, int value) {
  567. ensureCapacity(size + 1);
  568. names[size] = name;
  569. values[size] = value;
  570. size++;
  571. }
  572. /**
  573. * Verifies the capacity of the entity array, adjusting the size if necessary.
  574. *
  575. * @param capacity
  576. * size the array should be
  577. */
  578. protected void ensureCapacity(int capacity) {
  579. if (capacity > names.length) {
  580. int newSize = Math.max(capacity, size + growBy);
  581. String[] newNames = new String[newSize];
  582. System.arraycopy(names, 0, newNames, 0, size);
  583. names = newNames;
  584. int[] newValues = new int[newSize];
  585. System.arraycopy(values, 0, newValues, 0, size);
  586. values = newValues;
  587. }
  588. }
  589. /**
  590. * {@inheritDoc}
  591. */
  592. public String name(int value) {
  593. for (int i = 0; i < size; ++i) {
  594. if (values[i] == value) {
  595. return names[i];
  596. }
  597. }
  598. return null;
  599. }
  600. /**
  601. * {@inheritDoc}
  602. */
  603. public int value(String name) {
  604. for (int i = 0; i < size; ++i) {
  605. if (names[i].equals(name)) {
  606. return values[i];
  607. }
  608. }
  609. return -1;
  610. }
  611. }
  612. static class BinaryEntityMap extends ArrayEntityMap {
  613. /**
  614. * Constructs a new instance of <code>BinaryEntityMap</code>.
  615. */
  616. public BinaryEntityMap() {
  617. super();
  618. }
  619. /**
  620. * Constructs a new instance of <code>ArrayEntityMap</code> specifying the size by which the underlying array
  621. * should grow.
  622. *
  623. * @param growBy
  624. * array will be initialized to and will grow by this amount
  625. */
  626. public BinaryEntityMap(int growBy) {
  627. super(growBy);
  628. }
  629. /**
  630. * Performs a binary search of the entity array for the specified key. This method is based on code in
  631. * {@link java.util.Arrays}.
  632. *
  633. * @param key
  634. * the key to be found
  635. * @return the index of the entity array matching the specified key
  636. */
  637. private int binarySearch(int key) {
  638. int low = 0;
  639. int high = size - 1;
  640. while (low <= high) {
  641. int mid = (low + high) >>> 1;
  642. int midVal = values[mid];
  643. if (midVal < key) {
  644. low = mid + 1;
  645. } else if (midVal > key) {
  646. high = mid - 1;
  647. } else {
  648. return mid; // key found
  649. }
  650. }
  651. return -(low + 1); // key not found.
  652. }
  653. /**
  654. * {@inheritDoc}
  655. */
  656. @Override
  657. public void add(String name, int value) {
  658. ensureCapacity(size + 1);
  659. int insertAt = binarySearch(value);
  660. if (insertAt > 0) {
  661. return; // note: this means you can't insert the same value twice
  662. }
  663. insertAt = -(insertAt + 1); // binarySearch returns it negative and off-by-one
  664. System.arraycopy(values, insertAt, values, insertAt + 1, size - insertAt);
  665. values[insertAt] = value;
  666. System.arraycopy(names, insertAt, names, insertAt + 1, size - insertAt);
  667. names[insertAt] = name;
  668. size++;
  669. }
  670. /**
  671. * {@inheritDoc}
  672. */
  673. @Override
  674. public String name(int value) {
  675. int index = binarySearch(value);
  676. if (index < 0) {
  677. return null;
  678. }
  679. return names[index];
  680. }
  681. }
  682. // package scoped for testing
  683. EntityMap map = new Entities.LookupEntityMap();
  684. /**
  685. * <p>
  686. * Adds entities to this entity.
  687. * </p>
  688. *
  689. * @param entityArray
  690. * array of entities to be added
  691. */
  692. public void addEntities(String[][] entityArray) {
  693. for (String[] element : entityArray) {
  694. addEntity(element[0], Integer.parseInt(element[1]));
  695. }
  696. }
  697. /**
  698. * <p>
  699. * Add an entity to this entity.
  700. * </p>
  701. *
  702. * @param name
  703. * name of the entity
  704. * @param value
  705. * vale of the entity
  706. */
  707. public void addEntity(String name, int value) {
  708. map.add(name, value);
  709. }
  710. /**
  711. * <p>
  712. * Returns the name of the entity identified by the specified value.
  713. * </p>
  714. *
  715. * @param value
  716. * the value to locate
  717. * @return entity name associated with the specified value
  718. */
  719. public String entityName(int value) {
  720. return map.name(value);
  721. }
  722. /**
  723. * <p>
  724. * Returns the value of the entity identified by the specified name.
  725. * </p>
  726. *
  727. * @param name
  728. * the name to locate
  729. * @return entity value associated with the specified name
  730. */
  731. public int entityValue(String name) {
  732. return map.value(name);
  733. }
  734. /**
  735. * <p>
  736. * Escapes the characters in a <code>String</code>.
  737. * </p>
  738. *
  739. * <p>
  740. * For example, if you have called addEntity(&quot;foo&quot;, 0xA1), escape(&quot;\u00A1&quot;) will return
  741. * &quot;&amp;foo;&quot;
  742. * </p>
  743. *
  744. * @param str
  745. * The <code>String</code> to escape.
  746. * @return A new escaped <code>String</code>.
  747. */
  748. public String escape(String str) {
  749. StringWriter stringWriter = createStringWriter(str);
  750. try {
  751. this.escape(stringWriter, str);
  752. } catch (IOException e) {
  753. // This should never happen because ALL the StringWriter methods called by #escape(Writer, String) do not
  754. // throw IOExceptions.
  755. throw new UnhandledException(e);
  756. }
  757. return stringWriter.toString();
  758. }
  759. /**
  760. * <p>
  761. * Escapes the characters in the <code>String</code> passed and writes the result to the <code>Writer</code>
  762. * passed.
  763. * </p>
  764. *
  765. * @param writer
  766. * The <code>Writer</code> to write the results of the escaping to. Assumed to be a non-null value.
  767. * @param str
  768. * The <code>String</code> to escape. Assumed to be a non-null value.
  769. * @throws IOException
  770. * when <code>Writer</code> passed throws the exception from calls to the {@link Writer#write(int)}
  771. * methods.
  772. *
  773. * @see #escape(String)
  774. * @see Writer
  775. */
  776. public void escape(Writer writer, String str) throws IOException {
  777. int len = str.length();
  778. for (int i = 0; i < len; i++) {
  779. int c = Character.codePointAt(str, i);
  780. String entityName = this.entityName(c);
  781. if (entityName == null) {
  782. if (c >= 0x010000 && i < len - 1) {
  783. writer.write("&#");
  784. writer.write(Integer.toString(c, 10));
  785. writer.write(';');
  786. i++;
  787. } else if (c > 0x7F) {
  788. writer.write("&#");
  789. writer.write(Integer.toString(c, 10));
  790. writer.write(';');
  791. } else {
  792. writer.write(c);
  793. }
  794. } else {
  795. writer.write('&');
  796. writer.write(entityName);
  797. writer.write(';');
  798. }
  799. }
  800. }
  801. /**
  802. * <p>
  803. * Unescapes the entities in a <code>String</code>.
  804. * </p>
  805. *
  806. * <p>
  807. * For example, if you have called addEntity(&quot;foo&quot;, 0xA1), unescape(&quot;&amp;foo;&quot;) will return
  808. * &quot;\u00A1&quot;
  809. * </p>
  810. *
  811. * @param str
  812. * The <code>String</code> to escape.
  813. * @return A new escaped <code>String</code>.
  814. */
  815. public String unescape(String str) {
  816. int firstAmp = str.indexOf('&');
  817. if (firstAmp < 0) {
  818. return str;
  819. } else {
  820. StringWriter stringWriter = createStringWriter(str);
  821. try {
  822. this.doUnescape(stringWriter, str, firstAmp);
  823. } catch (IOException e) {
  824. // This should never happen because ALL the StringWriter methods called by #escape(Writer, String)
  825. // do not throw IOExceptions.
  826. throw new UnhandledException(e);
  827. }
  828. return stringWriter.toString();
  829. }
  830. }
  831. /**
  832. * Make the StringWriter 10% larger than the source String to avoid growing the writer
  833. *
  834. * @param str The source string
  835. * @return A newly created StringWriter
  836. */
  837. private StringWriter createStringWriter(String str) {
  838. return new StringWriter((int) (str.length() + (str.length() * 0.1)));
  839. }
  840. /**
  841. * <p>
  842. * Unescapes the escaped entities in the <code>String</code> passed and writes the result to the
  843. * <code>Writer</code> passed.
  844. * </p>
  845. *
  846. * @param writer
  847. * The <code>Writer</code> to write the results to; assumed to be non-null.
  848. * @param str
  849. * The source <code>String</code> to unescape; assumed to be non-null.
  850. * @throws IOException
  851. * when <code>Writer</code> passed throws the exception from calls to the {@link Writer#write(int)}
  852. * methods.
  853. *
  854. * @see #escape(String)
  855. * @see Writer
  856. */
  857. public void unescape(Writer writer, String str) throws IOException {
  858. int firstAmp = str.indexOf('&');
  859. if (firstAmp < 0) {
  860. writer.write(str);
  861. return;
  862. } else {
  863. doUnescape(writer, str, firstAmp);
  864. }
  865. }
  866. /**
  867. * Underlying unescape method that allows the optimisation of not starting from the 0 index again.
  868. *
  869. * @param writer
  870. * The <code>Writer</code> to write the results to; assumed to be non-null.
  871. * @param str
  872. * The source <code>String</code> to unescape; assumed to be non-null.
  873. * @param firstAmp
  874. * The <code>int</code> index of the first ampersand in the source String.
  875. * @throws IOException
  876. * when <code>Writer</code> passed throws the exception from calls to the {@link Writer#write(int)}
  877. * methods.
  878. */
  879. private void doUnescape(Writer writer, String str, int firstAmp) throws IOException {
  880. writer.write(str, 0, firstAmp);
  881. int len = str.length();
  882. for (int i = firstAmp; i < len; i++) {
  883. char c = str.charAt(i);
  884. if (c == '&') {
  885. int nextIdx = i + 1;
  886. int semiColonIdx = str.indexOf(';', nextIdx);
  887. if (semiColonIdx == -1) {
  888. writer.write(c);
  889. continue;
  890. }
  891. int amphersandIdx = str.indexOf('&', i + 1);
  892. if (amphersandIdx != -1 && amphersandIdx < semiColonIdx) {
  893. // Then the text looks like &...&...;
  894. writer.write(c);
  895. continue;
  896. }
  897. String entityContent = str.substring(nextIdx, semiColonIdx);
  898. int entityValue = -1;
  899. int entityContentLen = entityContent.length();
  900. if (entityContentLen > 0) {
  901. if (entityContent.charAt(0) == '#') { // escaped value content is an integer (decimal or
  902. // hexidecimal)
  903. if (entityContentLen > 1) {
  904. char isHexChar = entityContent.charAt(1);
  905. try {
  906. switch (isHexChar) {
  907. case 'X':
  908. case 'x':
  909. entityValue = Integer.parseInt(entityContent.substring(2), 16);
  910. break;
  911. default:
  912. entityValue = Integer.parseInt(entityContent.substring(1), 10);
  913. break;
  914. }
  915. if (entityValue > 0xFFFF) {
  916. entityValue = -1;
  917. }
  918. } catch (NumberFormatException e) {
  919. entityValue = -1;
  920. }
  921. }
  922. } else { // escaped value content is an entity name
  923. entityValue = this.entityValue(entityContent);
  924. }
  925. }
  926. if (entityValue == -1) {
  927. writer.write('&');
  928. writer.write(entityContent);
  929. writer.write(';');
  930. } else {
  931. writer.write(entityValue);
  932. }
  933. i = semiColonIdx; // move index up to the semi-colon
  934. } else {
  935. writer.write(c);
  936. }
  937. }
  938. }
  939. }