pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / feeds / demohtml / Entities.java
1 package org.apache.lucene.benchmark.byTask.feeds.demohtml;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.util.HashMap;
21 import java.util.Map;
22
23 public class Entities {
24   static final Map<String,String> decoder = new HashMap<String,String>(300);
25   static final String[]  encoder = new String[0x100];
26
27   static final String decode(String entity) {
28     if (entity.charAt(entity.length()-1) == ';')  // remove trailing semicolon
29       entity = entity.substring(0, entity.length()-1);
30     if (entity.charAt(1) == '#') {
31       int start = 2;
32       int radix = 10;
33       if (entity.charAt(2) == 'X' || entity.charAt(2) == 'x') {
34         start++;
35         radix = 16;
36       }
37       Character c =
38         new Character((char)Integer.parseInt(entity.substring(start), radix));
39       return c.toString();
40     } else {
41       String s = decoder.get(entity);
42       if (s != null)
43         return s;
44       else return "";
45     }
46   }
47
48   public static final String encode(String s) {
49     int length = s.length();
50     StringBuffer buffer = new StringBuffer(length * 2);
51     for (int i = 0; i < length; i++) {
52       int j = s.charAt(i);
53       if (j < 0x100 && encoder[j] != null) {
54         buffer.append(encoder[j]);                // have a named encoding
55         buffer.append(';');
56       } else if (j < 0x80) {
57         buffer.append((char) j);                          // use ASCII value
58       } else {
59         buffer.append("&#");                      // use numeric encoding
60         buffer.append(j).append(';');
61       }
62     }
63     return buffer.toString();
64   }
65
66   static final void add(String entity, int value) {
67     decoder.put(entity, (new Character((char)value)).toString());
68     if (value < 0x100)
69       encoder[value] = entity;
70   }
71
72   static {
73     add("&nbsp",   160);
74     add("&iexcl",  161);
75     add("&cent",   162);
76     add("&pound",  163);
77     add("&curren", 164);
78     add("&yen",    165);
79     add("&brvbar", 166);
80     add("&sect",   167);
81     add("&uml",    168);
82     add("&copy",   169);
83     add("&ordf",   170);
84     add("&laquo",  171);
85     add("&not",    172);
86     add("&shy",    173);
87     add("&reg",    174);
88     add("&macr",   175);
89     add("&deg",    176);
90     add("&plusmn", 177);
91     add("&sup2",   178);
92     add("&sup3",   179);
93     add("&acute",  180);
94     add("&micro",  181);
95     add("&para",   182);
96     add("&middot", 183);
97     add("&cedil",  184);
98     add("&sup1",   185);
99     add("&ordm",   186);
100     add("&raquo",  187);
101     add("&frac14", 188);
102     add("&frac12", 189);
103     add("&frac34", 190);
104     add("&iquest", 191);
105     add("&Agrave", 192);
106     add("&Aacute", 193);
107     add("&Acirc",  194);
108     add("&Atilde", 195);
109     add("&Auml",   196);
110     add("&Aring",  197);
111     add("&AElig",  198);
112     add("&Ccedil", 199);
113     add("&Egrave", 200);
114     add("&Eacute", 201);
115     add("&Ecirc",  202);
116     add("&Euml",   203);
117     add("&Igrave", 204);
118     add("&Iacute", 205);
119     add("&Icirc",  206);
120     add("&Iuml",   207);
121     add("&ETH",    208);
122     add("&Ntilde", 209);
123     add("&Ograve", 210);
124     add("&Oacute", 211);
125     add("&Ocirc",  212);
126     add("&Otilde", 213);
127     add("&Ouml",   214);
128     add("&times",  215);
129     add("&Oslash", 216);
130     add("&Ugrave", 217);
131     add("&Uacute", 218);
132     add("&Ucirc",  219);
133     add("&Uuml",   220);
134     add("&Yacute", 221);
135     add("&THORN",  222);
136     add("&szlig",  223);
137     add("&agrave", 224);
138     add("&aacute", 225);
139     add("&acirc",  226);
140     add("&atilde", 227);
141     add("&auml",   228);
142     add("&aring",  229);
143     add("&aelig",  230);
144     add("&ccedil", 231);
145     add("&egrave", 232);
146     add("&eacute", 233);
147     add("&ecirc",  234);
148     add("&euml",   235);
149     add("&igrave", 236);
150     add("&iacute", 237);
151     add("&icirc",  238);
152     add("&iuml",   239);
153     add("&eth",    240);
154     add("&ntilde", 241);
155     add("&ograve", 242);
156     add("&oacute", 243);
157     add("&ocirc",  244);
158     add("&otilde", 245);
159     add("&ouml",   246);
160     add("&divide", 247);
161     add("&oslash", 248);
162     add("&ugrave", 249);
163     add("&uacute", 250);
164     add("&ucirc",  251);
165     add("&uuml",   252);
166     add("&yacute", 253);
167     add("&thorn",  254);
168     add("&yuml",   255);
169     add("&fnof",   402);
170     add("&Alpha",  913);
171     add("&Beta",   914);
172     add("&Gamma",  915);
173     add("&Delta",  916);
174     add("&Epsilon",917);
175     add("&Zeta",   918);
176     add("&Eta",    919);
177     add("&Theta",  920);
178     add("&Iota",   921);
179     add("&Kappa",  922);
180     add("&Lambda", 923);
181     add("&Mu",     924);
182     add("&Nu",     925);
183     add("&Xi",     926);
184     add("&Omicron",927);
185     add("&Pi",     928);
186     add("&Rho",    929);
187     add("&Sigma",  931);
188     add("&Tau",    932);
189     add("&Upsilon",933);
190     add("&Phi",    934);
191     add("&Chi",    935);
192     add("&Psi",    936);
193     add("&Omega",  937);
194     add("&alpha",  945);
195     add("&beta",   946);
196     add("&gamma",  947);
197     add("&delta",  948);
198     add("&epsilon",949);
199     add("&zeta",   950);
200     add("&eta",    951);
201     add("&theta",  952);
202     add("&iota",   953);
203     add("&kappa",  954);
204     add("&lambda", 955);
205     add("&mu",     956);
206     add("&nu",     957);
207     add("&xi",     958);
208     add("&omicron",959);
209     add("&pi",     960);
210     add("&rho",    961);
211     add("&sigmaf", 962);
212     add("&sigma",  963);
213     add("&tau",    964);
214     add("&upsilon",965);
215     add("&phi",    966);
216     add("&chi",    967);
217     add("&psi",    968);
218     add("&omega",  969);
219     add("&thetasym",977);
220     add("&upsih",  978);
221     add("&piv",    982);
222     add("&bull",   8226);
223     add("&hellip", 8230);
224     add("&prime",  8242);
225     add("&Prime",  8243);
226     add("&oline",  8254);
227     add("&frasl",  8260);
228     add("&weierp", 8472);
229     add("&image",  8465);
230     add("&real",   8476);
231     add("&trade",  8482);
232     add("&alefsym",8501);
233     add("&larr",   8592);
234     add("&uarr",   8593);
235     add("&rarr",   8594);
236     add("&darr",   8595);
237     add("&harr",   8596);
238     add("&crarr",  8629);
239     add("&lArr",   8656);
240     add("&uArr",   8657);
241     add("&rArr",   8658);
242     add("&dArr",   8659);
243     add("&hArr",   8660);
244     add("&forall", 8704);
245     add("&part",   8706);
246     add("&exist",  8707);
247     add("&empty",  8709);
248     add("&nabla",  8711);
249     add("&isin",   8712);
250     add("&notin",  8713);
251     add("&ni",     8715);
252     add("&prod",   8719);
253     add("&sum",    8721);
254     add("&minus",  8722);
255     add("&lowast", 8727);
256     add("&radic",  8730);
257     add("&prop",   8733);
258     add("&infin",  8734);
259     add("&ang",    8736);
260     add("&and",    8743);
261     add("&or",     8744);
262     add("&cap",    8745);
263     add("&cup",    8746);
264     add("&int",    8747);
265     add("&there4", 8756);
266     add("&sim",    8764);
267     add("&cong",   8773);
268     add("&asymp",  8776);
269     add("&ne",     8800);
270     add("&equiv",  8801);
271     add("&le",     8804);
272     add("&ge",     8805);
273     add("&sub",    8834);
274     add("&sup",    8835);
275     add("&nsub",   8836);
276     add("&sube",   8838);
277     add("&supe",   8839);
278     add("&oplus",  8853);
279     add("&otimes", 8855);
280     add("&perp",   8869);
281     add("&sdot",   8901);
282     add("&lceil",  8968);
283     add("&rceil",  8969);
284     add("&lfloor", 8970);
285     add("&rfloor", 8971);
286     add("&lang",   9001);
287     add("&rang",   9002);
288     add("&loz",    9674);
289     add("&spades", 9824);
290     add("&clubs",  9827);
291     add("&hearts", 9829);
292     add("&diams",  9830);
293     add("&quot",   34);
294     add("&amp",    38);
295     add("&lt",     60);
296     add("&gt",     62);
297     add("&OElig",  338);
298     add("&oelig",  339);
299     add("&Scaron", 352);
300     add("&scaron", 353);
301     add("&Yuml",   376);
302     add("&circ",   710);
303     add("&tilde",  732);
304     add("&ensp",   8194);
305     add("&emsp",   8195);
306     add("&thinsp", 8201);
307     add("&zwnj",   8204);
308     add("&zwj",    8205);
309     add("&lrm",    8206);
310     add("&rlm",    8207);
311     add("&ndash",  8211);
312     add("&mdash",  8212);
313     add("&lsquo",  8216);
314     add("&rsquo",  8217);
315     add("&sbquo",  8218);
316     add("&ldquo",  8220);
317     add("&rdquo",  8221);
318     add("&bdquo",  8222);
319     add("&dagger", 8224);
320     add("&Dagger", 8225);
321     add("&permil", 8240);
322     add("&lsaquo", 8249);
323     add("&rsaquo", 8250);
324     add("&euro",   8364);
325
326   }
327 }