KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > Entities


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.TreeMap JavaDoc;
21
22 /**
23  * <p>Provides HTML and XML entity utilities.</p>
24  *
25  * @see <a HREF="http://hotwired.lycos.com/webmonkey/reference/special_characters/">ISO Entities</a>
26  * @see <br/><a HREF="http://www.w3.org/TR/REC-html32#latin1">HTML 3.2 Character Entities for ISO Latin-1</a>
27  * @see <br/><a HREF="http://www.w3.org/TR/REC-html40/sgml/entities.html">HTML 4.0 Character entity references</a>
28  * @see <br/><a HREF="http://www.w3.org/TR/html401/charset.html#h-5.3">HTML 4.01 Character References</a>
29  * @see <br/><a HREF="http://www.w3.org/TR/html401/charset.html#code-position">HTML 4.01 Code positions</a>
30  *
31  * @author <a HREF="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
32  * @author <a HREF="mailto:ggregory@seagullsw.com">Gary Gregory</a>
33  * @since 2.0
34  * @version $Id: Entities.java 161243 2005-04-14 04:30:28Z ggregory $
35  */

36 class Entities {
37
38     private static final String JavaDoc[][] BASIC_ARRAY = {
39         {"quot", "34"}, // " - double-quote
40
{"amp", "38"}, // & - ampersand
41
{"lt", "60"}, // < - less-than
42
{"gt", "62"}, // > - greater-than
43
};
44
45     private static final String JavaDoc[][] APOS_ARRAY = {
46         {"apos", "39"}, // XML apostrophe
47
};
48
49     // package scoped for testing
50
static final String JavaDoc[][] ISO8859_1_ARRAY = {
51         {"nbsp", "160"}, // non-breaking space
52
{"iexcl", "161"}, //inverted exclamation mark
53
{"cent", "162"}, //cent sign
54
{"pound", "163"}, //pound sign
55
{"curren", "164"}, //currency sign
56
{"yen", "165"}, //yen sign = yuan sign
57
{"brvbar", "166"}, //broken bar = broken vertical bar
58
{"sect", "167"}, //section sign
59
{"uml", "168"}, //diaeresis = spacing diaeresis
60
{"copy", "169"}, // - copyright sign
61
{"ordf", "170"}, //feminine ordinal indicator
62
{"laquo", "171"}, //left-pointing double angle quotation mark = left pointing guillemet
63
{"not", "172"}, //not sign
64
{"shy", "173"}, //soft hyphen = discretionary hyphen
65
{"reg", "174"}, // - registered trademark sign
66
{"macr", "175"}, //macron = spacing macron = overline = APL overbar
67
{"deg", "176"}, //degree sign
68
{"plusmn", "177"}, //plus-minus sign = plus-or-minus sign
69
{"sup2", "178"}, //superscript two = superscript digit two = squared
70
{"sup3", "179"}, //superscript three = superscript digit three = cubed
71
{"acute", "180"}, //acute accent = spacing acute
72
{"micro", "181"}, //micro sign
73
{"para", "182"}, //pilcrow sign = paragraph sign
74
{"middot", "183"}, //middle dot = Georgian comma = Greek middle dot
75
{"cedil", "184"}, //cedilla = spacing cedilla
76
{"sup1", "185"}, //superscript one = superscript digit one
77
{"ordm", "186"}, //masculine ordinal indicator
78
{"raquo", "187"}, //right-pointing double angle quotation mark = right pointing guillemet
79
{"frac14", "188"}, //vulgar fraction one quarter = fraction one quarter
80
{"frac12", "189"}, //vulgar fraction one half = fraction one half
81
{"frac34", "190"}, //vulgar fraction three quarters = fraction three quarters
82
{"iquest", "191"}, //inverted question mark = turned question mark
83
{"Agrave", "192"}, // - uppercase A, grave accent
84
{"Aacute", "193"}, // - uppercase A, acute accent
85
{"Acirc", "194"}, // - uppercase A, circumflex accent
86
{"Atilde", "195"}, // - uppercase A, tilde
87
{"Auml", "196"}, // - uppercase A, umlaut
88
{"Aring", "197"}, // - uppercase A, ring
89
{"AElig", "198"}, // - uppercase AE
90
{"Ccedil", "199"}, // - uppercase C, cedilla
91
{"Egrave", "200"}, // - uppercase E, grave accent
92
{"Eacute", "201"}, // - uppercase E, acute accent
93
{"Ecirc", "202"}, // - uppercase E, circumflex accent
94
{"Euml", "203"}, // - uppercase E, umlaut
95
{"Igrave", "204"}, // - uppercase I, grave accent
96
{"Iacute", "205"}, // - uppercase I, acute accent
97
{"Icirc", "206"}, // - uppercase I, circumflex accent
98
{"Iuml", "207"}, // - uppercase I, umlaut
99
{"ETH", "208"}, // - uppercase Eth, Icelandic
100
{"Ntilde", "209"}, // - uppercase N, tilde
101
{"Ograve", "210"}, // - uppercase O, grave accent
102
{"Oacute", "211"}, // - uppercase O, acute accent
103
{"Ocirc", "212"}, // - uppercase O, circumflex accent
104
{"Otilde", "213"}, // - uppercase O, tilde
105
{"Ouml", "214"}, // - uppercase O, umlaut
106
{"times", "215"}, //multiplication sign
107
{"Oslash", "216"}, // - uppercase O, slash
108
{"Ugrave", "217"}, // - uppercase U, grave accent
109
{"Uacute", "218"}, // - uppercase U, acute accent
110
{"Ucirc", "219"}, // - uppercase U, circumflex accent
111
{"Uuml", "220"}, // - uppercase U, umlaut
112
{"Yacute", "221"}, // - uppercase Y, acute accent
113
{"THORN", "222"}, // - uppercase THORN, Icelandic
114
{"szlig", "223"}, // - lowercase sharps, German
115
{"agrave", "224"}, // - lowercase a, grave accent
116
{"aacute", "225"}, // - lowercase a, acute accent
117
{"acirc", "226"}, // - lowercase a, circumflex accent
118
{"atilde", "227"}, // - lowercase a, tilde
119
{"auml", "228"}, // - lowercase a, umlaut
120
{"aring", "229"}, // - lowercase a, ring
121
{"aelig", "230"}, // - lowercase ae
122
{"ccedil", "231"}, // - lowercase c, cedilla
123
{"egrave", "232"}, // - lowercase e, grave accent
124
{"eacute", "233"}, // - lowercase e, acute accent
125
{"ecirc", "234"}, // - lowercase e, circumflex accent
126
{"euml", "235"}, // - lowercase e, umlaut
127
{"igrave", "236"}, // - lowercase i, grave accent
128
{"iacute", "237"}, // - lowercase i, acute accent
129
{"icirc", "238"}, // - lowercase i, circumflex accent
130
{"iuml", "239"}, // - lowercase i, umlaut
131
{"eth", "240"}, // - lowercase eth, Icelandic
132
{"ntilde", "241"}, // - lowercase n, tilde
133
{"ograve", "242"}, // - lowercase o, grave accent
134
{"oacute", "243"}, // - lowercase o, acute accent
135
{"ocirc", "244"}, // - lowercase o, circumflex accent
136
{"otilde", "245"}, // - lowercase o, tilde
137
{"ouml", "246"}, // - lowercase o, umlaut
138
{"divide", "247"}, // division sign
139
{"oslash", "248"}, // - lowercase o, slash
140
{"ugrave", "249"}, // - lowercase u, grave accent
141
{"uacute", "250"}, // - lowercase u, acute accent
142
{"ucirc", "251"}, // - lowercase u, circumflex accent
143
{"uuml", "252"}, // - lowercase u, umlaut
144
{"yacute", "253"}, // - lowercase y, acute accent
145
{"thorn", "254"}, // - lowercase thorn, Icelandic
146
{"yuml", "255"}, // - lowercase y, umlaut
147
};
148
149     // http://www.w3.org/TR/REC-html40/sgml/entities.html
150
// package scoped for testing
151
static final String JavaDoc[][] 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 same glyph could be used to depict both characters -->
223
// <!-- Arrows -->
224
{"larr", "8592"}, //leftwards arrow, U+2190 ISOnum -->
225
{"uarr", "8593"}, //upwards arrow, U+2191 ISOnum-->
226
{"rarr", "8594"}, //rightwards arrow, U+2192 ISOnum -->
227
{"darr", "8595"}, //downwards arrow, U+2193 ISOnum -->
228
{"harr", "8596"}, //left right arrow, U+2194 ISOamsa -->
229
{"crarr", "8629"}, //downwards arrow with corner leftwards= carriage return, U+21B5 NEW -->
230
{"lArr", "8656"}, //leftwards double arrow, U+21D0 ISOtech -->
231
// <!-- 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 -->
232
{"uArr", "8657"}, //upwards double arrow, U+21D1 ISOamsa -->
233
{"rArr", "8658"}, //rightwards double arrow,U+21D2 ISOtech -->
234
// <!-- 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 -->
235
{"dArr", "8659"}, //downwards double arrow, U+21D3 ISOamsa -->
236
{"hArr", "8660"}, //left right double arrow,U+21D4 ISOamsa -->
237
// <!-- Mathematical Operators -->
238
{"forall", "8704"}, //for all, U+2200 ISOtech -->
239
{"part", "8706"}, //partial differential, U+2202 ISOtech -->
240
{"exist", "8707"}, //there exists, U+2203 ISOtech -->
241
{"empty", "8709"}, //empty set = null set = diameter,U+2205 ISOamso -->
242
{"nabla", "8711"}, //nabla = backward difference,U+2207 ISOtech -->
243
{"isin", "8712"}, //element of, U+2208 ISOtech -->
244
{"notin", "8713"}, //not an element of, U+2209 ISOtech -->
245
{"ni", "8715"}, //contains as member, U+220B ISOtech -->
246
// <!-- should there be a more memorable name than 'ni'? -->
247
{"prod", "8719"}, //n-ary product = product sign,U+220F ISOamsb -->
248
// <!-- prod is NOT the same character as U+03A0 'greek capital letter pi' though the same glyph might be used for both -->
249
{"sum", "8721"}, //n-ary summation, U+2211 ISOamsb -->
250
// <!-- sum is NOT the same character as U+03A3 'greek capital letter sigma' though the same glyph might be used for both -->
251
{"minus", "8722"}, //minus sign, U+2212 ISOtech -->
252
{"lowast", "8727"}, //asterisk operator, U+2217 ISOtech -->
253
{"radic", "8730"}, //square root = radical sign,U+221A ISOtech -->
254
{"prop", "8733"}, //proportional to, U+221D ISOtech -->
255
{"infin", "8734"}, //infinity, U+221E ISOtech -->
256
{"ang", "8736"}, //angle, U+2220 ISOamso -->
257
{"and", "8743"}, //logical and = wedge, U+2227 ISOtech -->
258
{"or", "8744"}, //logical or = vee, U+2228 ISOtech -->
259
{"cap", "8745"}, //intersection = cap, U+2229 ISOtech -->
260
{"cup", "8746"}, //union = cup, U+222A ISOtech -->
261
{"int", "8747"}, //integral, U+222B ISOtech -->
262
{"there4", "8756"}, //therefore, U+2234 ISOtech -->
263
{"sim", "8764"}, //tilde operator = varies with = similar to,U+223C ISOtech -->
264
// <!-- tilde operator is NOT the same character as the tilde, U+007E,although the same glyph might be used to represent both -->
265
{"cong", "8773"}, //approximately equal to, U+2245 ISOtech -->
266
{"asymp", "8776"}, //almost equal to = asymptotic to,U+2248 ISOamsr -->
267
{"ne", "8800"}, //not equal to, U+2260 ISOtech -->
268
{"equiv", "8801"}, //identical to, U+2261 ISOtech -->
269
{"le", "8804"}, //less-than or equal to, U+2264 ISOtech -->
270
{"ge", "8805"}, //greater-than or equal to,U+2265 ISOtech -->
271
{"sub", "8834"}, //subset of, U+2282 ISOtech -->
272
{"sup", "8835"}, //superset of, U+2283 ISOtech -->
273
// <!-- 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 -->
274
{"sube", "8838"}, //subset of or equal to, U+2286 ISOtech -->
275
{"supe", "8839"}, //superset of or equal to,U+2287 ISOtech -->
276
{"oplus", "8853"}, //circled plus = direct sum,U+2295 ISOamsb -->
277
{"otimes", "8855"}, //circled times = vector product,U+2297 ISOamsb -->
278
{"perp", "8869"}, //up tack = orthogonal to = perpendicular,U+22A5 ISOtech -->
279
{"sdot", "8901"}, //dot operator, U+22C5 ISOamsb -->
280
// <!-- dot operator is NOT the same character as U+00B7 middle dot -->
281
// <!-- Miscellaneous Technical -->
282
{"lceil", "8968"}, //left ceiling = apl upstile,U+2308 ISOamsc -->
283
{"rceil", "8969"}, //right ceiling, U+2309 ISOamsc -->
284
{"lfloor", "8970"}, //left floor = apl downstile,U+230A ISOamsc -->
285
{"rfloor", "8971"}, //right floor, U+230B ISOamsc -->
286
{"lang", "9001"}, //left-pointing angle bracket = bra,U+2329 ISOtech -->
287
// <!-- lang is NOT the same character as U+003C 'less than' or U+2039 'single left-pointing angle quotation mark' -->
288
{"rang", "9002"}, //right-pointing angle bracket = ket,U+232A ISOtech -->
289
// <!-- rang is NOT the same character as U+003E 'greater than' or U+203A 'single right-pointing angle quotation mark' -->
290
// <!-- Geometric Shapes -->
291
{"loz", "9674"}, //lozenge, U+25CA ISOpub -->
292
// <!-- Miscellaneous Symbols -->
293
{"spades", "9824"}, //black spade suit, U+2660 ISOpub -->
294
// <!-- black here seems to mean filled as opposed to hollow -->
295
{"clubs", "9827"}, //black club suit = shamrock,U+2663 ISOpub -->
296
{"hearts", "9829"}, //black heart suit = valentine,U+2665 ISOpub -->
297
{"diams", "9830"}, //black diamond suit, U+2666 ISOpub -->
298

299 // <!-- Latin Extended-A -->
300
{"OElig", "338"}, // -- latin capital ligature OE,U+0152 ISOlat2 -->
301
{"oelig", "339"}, // -- latin small ligature oe, U+0153 ISOlat2 -->
302
// <!-- ligature is a misnomer, this is a separate character in some languages -->
303
{"Scaron", "352"}, // -- latin capital letter S with caron,U+0160 ISOlat2 -->
304
{"scaron", "353"}, // -- latin small letter s with caron,U+0161 ISOlat2 -->
305
{"Yuml", "376"}, // -- latin capital letter Y with diaeresis,U+0178 ISOlat2 -->
306
// <!-- Spacing Modifier Letters -->
307
{"circ", "710"}, // -- modifier letter circumflex accent,U+02C6 ISOpub -->
308
{"tilde", "732"}, //small tilde, U+02DC ISOdia -->
309
// <!-- General Punctuation -->
310
{"ensp", "8194"}, //en space, U+2002 ISOpub -->
311
{"emsp", "8195"}, //em space, U+2003 ISOpub -->
312
{"thinsp", "8201"}, //thin space, U+2009 ISOpub -->
313
{"zwnj", "8204"}, //zero width non-joiner,U+200C NEW RFC 2070 -->
314
{"zwj", "8205"}, //zero width joiner, U+200D NEW RFC 2070 -->
315
{"lrm", "8206"}, //left-to-right mark, U+200E NEW RFC 2070 -->
316
{"rlm", "8207"}, //right-to-left mark, U+200F NEW RFC 2070 -->
317
{"ndash", "8211"}, //en dash, U+2013 ISOpub -->
318
{"mdash", "8212"}, //em dash, U+2014 ISOpub -->
319
{"lsquo", "8216"}, //left single quotation mark,U+2018 ISOnum -->
320
{"rsquo", "8217"}, //right single quotation mark,U+2019 ISOnum -->
321
{"sbquo", "8218"}, //single low-9 quotation mark, U+201A NEW -->
322
{"ldquo", "8220"}, //left double quotation mark,U+201C ISOnum -->
323
{"rdquo", "8221"}, //right double quotation mark,U+201D ISOnum -->
324
{"bdquo", "8222"}, //double low-9 quotation mark, U+201E NEW -->
325
{"dagger", "8224"}, //dagger, U+2020 ISOpub -->
326
{"Dagger", "8225"}, //double dagger, U+2021 ISOpub -->
327
{"permil", "8240"}, //per mille sign, U+2030 ISOtech -->
328
{"lsaquo", "8249"}, //single left-pointing angle quotation mark,U+2039 ISO proposed -->
329
// <!-- lsaquo is proposed but not yet ISO standardized -->
330
{"rsaquo", "8250"}, //single right-pointing angle quotation mark,U+203A ISO proposed -->
331
// <!-- rsaquo is proposed but not yet ISO standardized -->
332
{"euro", "8364"}, // -- euro sign, U+20AC NEW -->
333
};
334
335     /**
336      * <p>The set of entities supported by standard XML.</p>
337      */

338     public static final Entities XML;
339
340     /**
341      * <p>The set of entities supported by HTML 3.2.</p>
342      */

343     public static final Entities HTML32;
344
345     /**
346      * <p>The set of entities supported by HTML 4.0.</p>
347      */

348     public static final Entities HTML40;
349
350     static {
351         XML = new Entities();
352         XML.addEntities(BASIC_ARRAY);
353         XML.addEntities(APOS_ARRAY);
354     }
355
356     static {
357         HTML32 = new Entities();
358         HTML32.addEntities(BASIC_ARRAY);
359         HTML32.addEntities(ISO8859_1_ARRAY);
360     }
361
362     static {
363         HTML40 = new Entities();
364         fillWithHtml40Entities(HTML40);
365     }
366
367     static void fillWithHtml40Entities(Entities entities) {
368         entities.addEntities(BASIC_ARRAY);
369         entities.addEntities(ISO8859_1_ARRAY);
370         entities.addEntities(HTML40_ARRAY);
371     }
372
373     static interface EntityMap {
374         void add(String JavaDoc name, int value);
375
376         String JavaDoc name(int value);
377
378         int value(String JavaDoc name);
379     }
380
381     static class PrimitiveEntityMap implements EntityMap {
382         private Map JavaDoc mapNameToValue = new HashMap JavaDoc();
383         private IntHashMap mapValueToName = new IntHashMap();
384
385         public void add(String JavaDoc name, int value) {
386             mapNameToValue.put(name, new Integer JavaDoc(value));
387             mapValueToName.put(value, name);
388         }
389
390         public String JavaDoc name(int value) {
391             return (String JavaDoc) mapValueToName.get(value);
392         }
393
394         public int value(String JavaDoc name) {
395             Object JavaDoc value = mapNameToValue.get(name);
396             if (value == null) {
397                 return -1;
398             }
399             return ((Integer JavaDoc) value).intValue();
400         }
401     }
402
403
404     static abstract class MapIntMap implements Entities.EntityMap {
405         protected Map JavaDoc mapNameToValue;
406         protected Map JavaDoc mapValueToName;
407
408         public void add(String JavaDoc name, int value) {
409             mapNameToValue.put(name, new Integer JavaDoc(value));
410             mapValueToName.put(new Integer JavaDoc(value), name);
411         }
412
413         public String JavaDoc name(int value) {
414             return (String JavaDoc) mapValueToName.get(new Integer JavaDoc(value));
415         }
416
417         public int value(String JavaDoc name) {
418             Object JavaDoc value = mapNameToValue.get(name);
419             if (value == null) {
420                 return -1;
421             }
422             return ((Integer JavaDoc) value).intValue();
423         }
424     }
425
426     static class HashEntityMap extends MapIntMap {
427         public HashEntityMap() {
428             mapNameToValue = new HashMap JavaDoc();
429             mapValueToName = new HashMap JavaDoc();
430         }
431     }
432
433     static class TreeEntityMap extends MapIntMap {
434         public TreeEntityMap() {
435             mapNameToValue = new TreeMap JavaDoc();
436             mapValueToName = new TreeMap JavaDoc();
437         }
438     }
439
440     static class LookupEntityMap extends PrimitiveEntityMap {
441         private String JavaDoc[] lookupTable;
442         private int LOOKUP_TABLE_SIZE = 256;
443
444         public String JavaDoc name(int value) {
445             if (value < LOOKUP_TABLE_SIZE) {
446                 return lookupTable()[value];
447             }
448             return super.name(value);
449         }
450
451         private String JavaDoc[] lookupTable() {
452             if (lookupTable == null) {
453                 createLookupTable();
454             }
455             return lookupTable;
456         }
457
458         private void createLookupTable() {
459             lookupTable = new String JavaDoc[LOOKUP_TABLE_SIZE];
460             for (int i = 0; i < LOOKUP_TABLE_SIZE; ++i) {
461                 lookupTable[i] = super.name(i);
462             }
463         }
464     }
465
466     static class ArrayEntityMap implements EntityMap {
467         protected int growBy = 100;
468         protected int size = 0;
469         protected String JavaDoc[] names;
470         protected int[] values;
471
472         public ArrayEntityMap() {
473             names = new String JavaDoc[growBy];
474             values = new int[growBy];
475         }
476
477         public ArrayEntityMap(int growBy) {
478             this.growBy = growBy;
479             names = new String JavaDoc[growBy];
480             values = new int[growBy];
481         }
482
483         public void add(String JavaDoc name, int value) {
484             ensureCapacity(size + 1);
485             names[size] = name;
486             values[size] = value;
487             size++;
488         }
489
490         protected void ensureCapacity(int capacity) {
491             if (capacity > names.length) {
492                 int newSize = Math.max(capacity, size + growBy);
493                 String JavaDoc[] newNames = new String JavaDoc[newSize];
494                 System.arraycopy(names, 0, newNames, 0, size);
495                 names = newNames;
496                 int[] newValues = new int[newSize];
497                 System.arraycopy(values, 0, newValues, 0, size);
498                 values = newValues;
499             }
500         }
501
502         public String JavaDoc name(int value) {
503             for (int i = 0; i < size; ++i) {
504                 if (values[i] == value) {
505                     return names[i];
506                 }
507             }
508             return null;
509         }
510
511         public int value(String JavaDoc name) {
512             for (int i = 0; i < size; ++i) {
513                 if (names[i].equals(name)) {
514                     return values[i];
515                 }
516             }
517             return -1;
518         }
519     }
520
521     static class BinaryEntityMap extends ArrayEntityMap {
522
523         public BinaryEntityMap() {
524         }
525
526         public BinaryEntityMap(int growBy) {
527             super(growBy);
528         }
529
530         // based on code in java.util.Arrays
531
private int binarySearch(int key) {
532             int low = 0;
533             int high = size - 1;
534
535             while (low <= high) {
536                 int mid = (low + high) >> 1;
537                 int midVal = values[mid];
538
539                 if (midVal < key) {
540                     low = mid + 1;
541                 } else if (midVal > key) {
542                     high = mid - 1;
543                 } else {
544                     return mid; // key found
545
}
546             }
547             return -(low + 1); // key not found.
548
}
549
550         public void add(String JavaDoc name, int value) {
551             ensureCapacity(size + 1);
552             int insertAt = binarySearch(value);
553             if (insertAt > 0) {
554                 return; // note: this means you can't insert the same value twice
555
}
556             insertAt = -(insertAt + 1); // binarySearch returns it negative and off-by-one
557
System.arraycopy(values, insertAt, values, insertAt + 1, size - insertAt);
558             values[insertAt] = value;
559             System.arraycopy(names, insertAt, names, insertAt + 1, size - insertAt);
560             names[insertAt] = name;
561             size++;
562         }
563
564         public String JavaDoc name(int value) {
565             int index = binarySearch(value);
566             if (index < 0) {
567                 return null;
568             }
569             return names[index];
570         }
571     }
572
573     // package scoped for testing
574
EntityMap map = new Entities.LookupEntityMap();
575
576     public void addEntities(String JavaDoc[][] entityArray) {
577         for (int i = 0; i < entityArray.length; ++i) {
578             addEntity(entityArray[i][0], Integer.parseInt(entityArray[i][1]));
579         }
580     }
581
582     public void addEntity(String JavaDoc name, int value) {
583         map.add(name, value);
584     }
585
586     public String JavaDoc entityName(int value) {
587         return map.name(value);
588     }
589
590
591     public int entityValue(String JavaDoc name) {
592         return map.value(name);
593     }
594
595     /**
596      * <p>Escapes the characters in a <code>String</code>.</p>
597      *
598      * <p>For example, if you have called addEntity(&quot;foo&quot;, 0xA1),
599      * escape(&quot;&quot;) will return &quot;&amp;foo;&quot;</p>
600      *
601      * @param str The <code>String</code> to escape.
602      * @return A new escaped <code>String</code>.
603      */

604     public String JavaDoc escape(String JavaDoc str) {
605         //todo: rewrite to use a Writer
606
StringBuffer JavaDoc buf = new StringBuffer JavaDoc(str.length() * 2);
607         int i;
608         for (i = 0; i < str.length(); ++i) {
609             char ch = str.charAt(i);
610             String JavaDoc entityName = this.entityName(ch);
611             if (entityName == null) {
612                 if (ch > 0x7F) {
613                     int intValue = ch;
614                     buf.append("&#");
615                     buf.append(intValue);
616                     buf.append(';');
617                 } else {
618                     buf.append(ch);
619                 }
620             } else {
621                 buf.append('&');
622                 buf.append(entityName);
623                 buf.append(';');
624             }
625         }
626         return buf.toString();
627     }
628
629     /**
630      * <p>Unescapes the entities in a <code>String</code>.</p>
631      *
632      * <p>For example, if you have called addEntity(&quot;foo&quot;, 0xA1),
633      * unescape(&quot;&amp;foo;&quot;) will return &quot;&quot;</p>
634      *
635      * @param str The <code>String</code> to escape.
636      * @return A new escaped <code>String</code>.
637      */

638     public String JavaDoc unescape(String JavaDoc str) {
639         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(str.length());
640         int i;
641         for (i = 0; i < str.length(); ++i) {
642             char ch = str.charAt(i);
643             if (ch == '&') {
644                 int semi = str.indexOf(';', i + 1);
645                 if (semi == -1) {
646                     buf.append(ch);
647                     continue;
648                 }
649                 String JavaDoc entityName = str.substring(i + 1, semi);
650                 int entityValue;
651                 if (entityName.length() == 0) {
652                     entityValue = -1;
653                 } else if (entityName.charAt(0) == '#') {
654                     if (entityName.length() == 1) {
655                         entityValue = -1;
656                     } else {
657                         char charAt1 = entityName.charAt(1);
658                         try {
659                             if (charAt1 == 'x' || charAt1=='X') {
660                                 entityValue = Integer.valueOf(entityName.substring(2), 16).intValue();
661                             } else {
662                                 entityValue = Integer.parseInt(entityName.substring(1));
663                             }
664                         } catch (NumberFormatException JavaDoc ex) {
665                             entityValue = -1;
666                         }
667                     }
668                 } else {
669                     entityValue = this.entityValue(entityName);
670                 }
671                 if (entityValue == -1) {
672                     buf.append('&');
673                     buf.append(entityName);
674                     buf.append(';');
675                 } else {
676                     buf.append((char) (entityValue));
677                 }
678                 i = semi;
679             } else {
680                 buf.append(ch);
681             }
682         }
683         return buf.toString();
684     }
685
686 }
687
Popular Tags