KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > util > HTMLTools


1 /*
2  * ====================================================================
3  * Copyright (c) 1995-1999 Purple Technology, Inc. All rights
4  * reserved.
5  *
6  * PLAIN LANGUAGE LICENSE: Do whatever you like with this code, free
7  * of charge, just give credit where credit is due. If you improve it,
8  * please send your improvements to alex@purpletech.com. Check
9  * http://www.purpletech.com/code/ for the latest version and news.
10  *
11  * LEGAL LANGUAGE LICENSE: Redistribution and use in source and binary
12  * forms, with or without modification, are permitted provided that
13  * the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above
19  * copyright notice, this list of conditions and the following
20  * disclaimer in the documentation and/or other materials provided
21  * with the distribution.
22  *
23  * 3. The names of the authors and the names "Purple Technology,"
24  * "Purple Server" and "Purple Chat" must not be used to endorse or
25  * promote products derived from this software without prior written
26  * permission. For written permission, please contact
27  * server@purpletech.com.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND PURPLE TECHNOLOGY ``AS
30  * IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
32  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
33  * AUTHORS OR PURPLE TECHNOLOGY BE LIABLE FOR ANY DIRECT, INDIRECT,
34  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
40  * OF THE POSSIBILITY OF SUCH DAMAGE.
41  *
42  * ====================================================================
43  *
44  **/

45 package org.ejbca.util;
46
47 import java.util.HashMap JavaDoc;
48 import java.util.Map JavaDoc;
49
50 public class HTMLTools {
51 // see http://hotwired.lycos.com/webmonkey/reference/special_characters/
52
static Object JavaDoc[][] entities = {
53        // {"#39", new Integer(39)}, // ' - apostrophe
54
{"quot", new Integer JavaDoc(34)}, // " - double-quote
55
{"amp", new Integer JavaDoc(38)}, // & - ampersand
56
{"lt", new Integer JavaDoc(60)}, // < - less-than
57
{"gt", new Integer JavaDoc(62)}, // > - greater-than
58
{"nbsp", new Integer JavaDoc(160)}, // non-breaking space
59
{"copy", new Integer JavaDoc(169)}, // © - copyright
60
{"reg", new Integer JavaDoc(174)}, // ® - registered trademark
61
{"Agrave", new Integer JavaDoc(192)}, // À - uppercase A, grave accent
62
{"Aacute", new Integer JavaDoc(193)}, // Ã? - uppercase A, acute accent
63
{"Acirc", new Integer JavaDoc(194)}, // Â - uppercase A, circumflex accent
64
{"Atilde", new Integer JavaDoc(195)}, // Ã - uppercase A, tilde
65
{"Auml", new Integer JavaDoc(196)}, // Ä - uppercase A, umlaut
66
{"Aring", new Integer JavaDoc(197)}, // Ã… - uppercase A, ring
67
{"AElig", new Integer JavaDoc(198)}, // Æ - uppercase AE
68
{"Ccedil", new Integer JavaDoc(199)}, // Ç - uppercase C, cedilla
69
{"Egrave", new Integer JavaDoc(200)}, // È - uppercase E, grave accent
70
{"Eacute", new Integer JavaDoc(201)}, // É - uppercase E, acute accent
71
{"Ecirc", new Integer JavaDoc(202)}, // Ê - uppercase E, circumflex accent
72
{"Euml", new Integer JavaDoc(203)}, // Ë - uppercase E, umlaut
73
{"Igrave", new Integer JavaDoc(204)}, // Ì - uppercase I, grave accent
74
{"Iacute", new Integer JavaDoc(205)}, // Ã? - uppercase I, acute accent
75
{"Icirc", new Integer JavaDoc(206)}, // ÃŽ - uppercase I, circumflex accent
76
{"Iuml", new Integer JavaDoc(207)}, // Ã? - uppercase I, umlaut
77
{"ETH", new Integer JavaDoc(208)}, // Ã? - uppercase Eth, Icelandic
78
{"Ntilde", new Integer JavaDoc(209)}, // Ñ - uppercase N, tilde
79
{"Ograve", new Integer JavaDoc(210)}, // Ã’ - uppercase O, grave accent
80
{"Oacute", new Integer JavaDoc(211)}, // Ó - uppercase O, acute accent
81
{"Ocirc", new Integer JavaDoc(212)}, // Ô - uppercase O, circumflex accent
82
{"Otilde", new Integer JavaDoc(213)}, // Õ - uppercase O, tilde
83
{"Ouml", new Integer JavaDoc(214)}, // Ö - uppercase O, umlaut
84
{"Oslash", new Integer JavaDoc(216)}, // Ø - uppercase O, slash
85
{"Ugrave", new Integer JavaDoc(217)}, // Ù - uppercase U, grave accent
86
{"Uacute", new Integer JavaDoc(218)}, // Ú - uppercase U, acute accent
87
{"Ucirc", new Integer JavaDoc(219)}, // Û - uppercase U, circumflex accent
88
{"Uuml", new Integer JavaDoc(220)}, // Ü - uppercase U, umlaut
89
{"Yacute", new Integer JavaDoc(221)}, // Ã? - uppercase Y, acute accent
90
{"THORN", new Integer JavaDoc(222)}, // Þ - uppercase THORN, Icelandic
91
{"szlig", new Integer JavaDoc(223)}, // ß - lowercase sharps, German
92
{"agrave", new Integer JavaDoc(224)}, // à - lowercase a, grave accent
93
{"aacute", new Integer JavaDoc(225)}, // á - lowercase a, acute accent
94
{"acirc", new Integer JavaDoc(226)}, // â - lowercase a, circumflex accent
95
{"atilde", new Integer JavaDoc(227)}, // ã - lowercase a, tilde
96
{"auml", new Integer JavaDoc(228)}, // ä - lowercase a, umlaut
97
{"aring", new Integer JavaDoc(229)}, // å - lowercase a, ring
98
{"aelig", new Integer JavaDoc(230)}, // æ - lowercase ae
99
{"ccedil", new Integer JavaDoc(231)}, // ç - lowercase c, cedilla
100
{"egrave", new Integer JavaDoc(232)}, // è - lowercase e, grave accent
101
{"eacute", new Integer JavaDoc(233)}, // é - lowercase e, acute accent
102
{"ecirc", new Integer JavaDoc(234)}, // ê - lowercase e, circumflex accent
103
{"euml", new Integer JavaDoc(235)}, // ë - lowercase e, umlaut
104
{"igrave", new Integer JavaDoc(236)}, // ì - lowercase i, grave accent
105
{"iacute", new Integer JavaDoc(237)}, // í - lowercase i, acute accent
106
{"icirc", new Integer JavaDoc(238)}, // î - lowercase i, circumflex accent
107
{"iuml", new Integer JavaDoc(239)}, // ï - lowercase i, umlaut
108
{"igrave", new Integer JavaDoc(236)}, // ì - lowercase i, grave accent
109
{"iacute", new Integer JavaDoc(237)}, // í - lowercase i, acute accent
110
{"icirc", new Integer JavaDoc(238)}, // î - lowercase i, circumflex accent
111
{"iuml", new Integer JavaDoc(239)}, // ï - lowercase i, umlaut
112
{"eth", new Integer JavaDoc(240)}, // ð - lowercase eth, Icelandic
113
{"ntilde", new Integer JavaDoc(241)}, // ñ - lowercase n, tilde
114
{"ograve", new Integer JavaDoc(242)}, // ò - lowercase o, grave accent
115
{"oacute", new Integer JavaDoc(243)}, // ó - lowercase o, acute accent
116
{"ocirc", new Integer JavaDoc(244)}, // ô - lowercase o, circumflex accent
117
{"otilde", new Integer JavaDoc(245)}, // õ - lowercase o, tilde
118
{"ouml", new Integer JavaDoc(246)}, // ö - lowercase o, umlaut
119
{"oslash", new Integer JavaDoc(248)}, // ø - lowercase o, slash
120
{"ugrave", new Integer JavaDoc(249)}, // ù - lowercase u, grave accent
121
{"uacute", new Integer JavaDoc(250)}, // ú - lowercase u, acute accent
122
{"ucirc", new Integer JavaDoc(251)}, // û - lowercase u, circumflex accent
123
{"uuml", new Integer JavaDoc(252)}, // ü - lowercase u, umlaut
124
{"yacute", new Integer JavaDoc(253)}, // ý - lowercase y, acute accent
125
{"thorn", new Integer JavaDoc(254)}, // þ - lowercase thorn, Icelandic
126
{"yuml", new Integer JavaDoc(255)}, // ÿ - lowercase y, umlaut
127
{"euro", new Integer JavaDoc(8364)}, // Euro symbol
128
};
129     static Map JavaDoc e2i = new HashMap JavaDoc();
130     static Map JavaDoc i2e = new HashMap JavaDoc();
131     static {
132         for (int i=0; i<entities.length; ++i) {
133             e2i.put(entities[i][0], entities[i][1]);
134             i2e.put(entities[i][1], entities[i][0]);
135         }
136     }
137
138     /**
139      * Turns funky characters into HTML entity equivalents<p>
140      * e.g. <tt>"bread" & "butter"</tt> => <tt>&amp;quot;bread&amp;quot; &amp;amp; &amp;quot;butter&amp;quot;</tt>.
141      * Update: supports nearly all HTML entities, including funky accents. See the source code for more detail.
142      * @see #htmlunescape(String)
143      **/

144     public static String JavaDoc htmlescape(String JavaDoc s1)
145     {
146         if (s1 == null) return null;
147         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
148         int i;
149         for (i=0; i<s1.length(); ++i) {
150             char ch = s1.charAt(i);
151             String JavaDoc entity = (String JavaDoc)i2e.get( new Integer JavaDoc(ch) );
152             if (entity == null) {
153                 if ((ch) > 128) {
154                     buf.append("&#" + ((int)ch) + ";");
155                 }
156                 else {
157                     buf.append(ch);
158                 }
159             }
160             else {
161                 buf.append("&" + entity + ";");
162             }
163         }
164         return buf.toString();
165     }
166
167     /**
168      * Given a string containing entity escapes, returns a string
169      * containing the actual Unicode characters corresponding to the
170      * escapes.
171      *
172      * Note: nasty bug fixed by Helge Tesgaard (and, in parallel, by
173      * Alex, but Helge deserves major props for emailing me the fix).
174      * 15-Feb-2002 Another bug fixed by Sean Brown <sean@boohai.com>
175      *
176      * @see #htmlescape(String)
177      **/

178     public static String JavaDoc htmlunescape(String JavaDoc s1) {
179         if (s1 == null) return null;
180         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
181         int i;
182         for (i=0; i<s1.length(); ++i) {
183             char ch = s1.charAt(i);
184             if (ch == '&') {
185                 int semi = s1.indexOf(';', i+1);
186                 if (semi == -1) {
187                     buf.append(ch);
188                     continue;
189                 }
190                 String JavaDoc entity = s1.substring(i+1, semi);
191                 Integer JavaDoc iso;
192                 if (entity.charAt(0) == '#') {
193                     iso = new Integer JavaDoc(entity.substring(1));
194                 }
195                 else {
196                     iso = (Integer JavaDoc)e2i.get(entity);
197                 }
198                 if (iso == null) {
199                     buf.append("&" + entity + ";");
200                 }
201                 else {
202                     buf.append((char)(iso.intValue()));
203                 }
204                 i = semi;
205             }
206             else {
207                 buf.append(ch);
208             }
209         }
210         return buf.toString();
211     }
212 }
213
Popular Tags