KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > java2html > util > HtmlUtilities


1 package de.java2html.util;
2
3 import java.util.Hashtable JavaDoc;
4
5 /**
6  * Some methods for converting text to valid HTML.
7  *
8  * For questions, suggestions, bug-reports, enhancement-requests etc.
9  * I may be contacted at:
10  * <a HREF="mailto:markus@jave.de">markus@jave.de</a>
11  *
12  * The Java2html home page is located at:
13  * <a HREF="http://www.java2html.de">http://www.java2html.de</a>
14  *
15  * @author <a HREF="mailto:markus@jave.de">Markus Gebhard</a>
16  * @version 2.0, 05/07/02
17  *
18  * Copyright (C) Markus Gebhard 2000-2002
19  *
20  * This program is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU General Public License
22  * as published by the Free Software Foundation; either version 2
23  * of the License, or (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program; if not, write to the Free Software
32  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33  */

34 public class HtmlUtilities {
35   private static Hashtable JavaDoc entityTableEncode;
36
37   private final static String JavaDoc[] ENTITIES = {
38       //IGNORE (during encoding!!)
39
" ", //$NON-NLS-1$
40
" ", //$NON-NLS-1$
41
"-", //$NON-NLS-1$
42
"-", //$NON-NLS-1$
43
"'", //$NON-NLS-1$
44
"'", //$NON-NLS-1$
45
"`", //$NON-NLS-1$
46
"`", //$NON-NLS-1$
47

48       //UPPERCASE
49
"&Uuml;", //$NON-NLS-1$
50
"", //$NON-NLS-1$
51
"&Auml;", //$NON-NLS-1$
52
"", //$NON-NLS-1$
53
"&Ouml;", //$NON-NLS-1$
54
"", //$NON-NLS-1$
55
"&Euml;", //$NON-NLS-1$
56
"", //$NON-NLS-1$
57
"&Ccedil;", //$NON-NLS-1$
58
"", //$NON-NLS-1$
59
"&AElig;", //$NON-NLS-1$
60
"", //$NON-NLS-1$
61
"&Aring;", //$NON-NLS-1$
62
"", //$NON-NLS-1$
63
"&Oslash;", //$NON-NLS-1$
64
"", //$NON-NLS-1$
65

66       //OTHERS -> ignorecase!
67
"&uuml;", //$NON-NLS-1$
68
"", //$NON-NLS-1$
69
"&auml;", //$NON-NLS-1$
70
"", //$NON-NLS-1$
71
"&ouml;", //$NON-NLS-1$
72
"", //$NON-NLS-1$
73
"&euml;", //$NON-NLS-1$
74
"", //$NON-NLS-1$
75
"&ccedil;", //$NON-NLS-1$
76
"", //$NON-NLS-1$
77
"&aring;", //$NON-NLS-1$
78
"", //$NON-NLS-1$
79
"&oslash;", //$NON-NLS-1$
80
"", //$NON-NLS-1$
81
"&grave;", //$NON-NLS-1$
82
"`", //$NON-NLS-1$
83
"&agrave;", //$NON-NLS-1$
84
"", //$NON-NLS-1$
85
"&egrave;", //$NON-NLS-1$
86
"", //$NON-NLS-1$
87
"&igrave;", //$NON-NLS-1$
88
"", //$NON-NLS-1$
89
"&ograve;", //$NON-NLS-1$
90
"", //$NON-NLS-1$
91
"&ugrave;", //$NON-NLS-1$
92
"", //$NON-NLS-1$
93
"&amp;", //$NON-NLS-1$
94
"&", //$NON-NLS-1$
95
"&#34;", //$NON-NLS-1$
96
"\"", //$NON-NLS-1$
97
// same as &quot; - but &quot; is not part of HTML3.2!!!
98
"&szlig;", //$NON-NLS-1$
99
"", //$NON-NLS-1$
100
"&nbsp;", //$NON-NLS-1$
101
" ", //$NON-NLS-1$
102
"&gt;", //$NON-NLS-1$
103
">", //$NON-NLS-1$
104
"&lt;", //$NON-NLS-1$
105
"<", //$NON-NLS-1$
106
"&copy;", //$NON-NLS-1$
107
"(C)", //$NON-NLS-1$
108
"&cent;", //$NON-NLS-1$
109
"", //$NON-NLS-1$
110
"&pound;", //$NON-NLS-1$
111
"", //$NON-NLS-1$
112
"&laquo;", //$NON-NLS-1$
113
"", //$NON-NLS-1$
114
"&raquo;", //$NON-NLS-1$
115
"", //$NON-NLS-1$
116
"&reg;", //$NON-NLS-1$
117
"(R)", //$NON-NLS-1$
118
"&middot;", //$NON-NLS-1$
119
" - ", //$NON-NLS-1$
120
"&times;", //$NON-NLS-1$
121
" x ", //$NON-NLS-1$
122
"&acute;", //$NON-NLS-1$
123
"'", //$NON-NLS-1$ //$NON-NLS-1$
124
"&aacute;", //$NON-NLS-1$
125
"", //$NON-NLS-1$
126
"&uacute;", //$NON-NLS-1$
127
"", //$NON-NLS-1$
128
"&oacute;", //$NON-NLS-1$
129
"", //$NON-NLS-1$
130
"&eacute;", //$NON-NLS-1$
131
"", //$NON-NLS-1$
132
"&iacute;", //$NON-NLS-1$
133
"", //$NON-NLS-1$
134
"&ntilde;", //$NON-NLS-1$
135
"", //$NON-NLS-1$
136
"&sect;", //$NON-NLS-1$
137
"", //$NON-NLS-1$
138
"&egrave;", //$NON-NLS-1$
139
"", //$NON-NLS-1$
140
"&icirc;", //$NON-NLS-1$
141
"", //$NON-NLS-1$
142
"&ocirc;", //$NON-NLS-1$
143
"", //$NON-NLS-1$
144
"&acirc;", //$NON-NLS-1$
145
"", //$NON-NLS-1$
146
"&ucirc;", //$NON-NLS-1$
147
"", //$NON-NLS-1$
148
"&ecirc;", //$NON-NLS-1$
149
"", //$NON-NLS-1$
150
"&aelig;", //$NON-NLS-1$
151
"", //$NON-NLS-1$
152
"&iexcl;", //$NON-NLS-1$
153
"", //$NON-NLS-1$
154
"&#151;", //$NON-NLS-1$
155
"-", //$NON-NLS-1$
156
"&#0151;", //$NON-NLS-1$
157
"-", //$NON-NLS-1$
158
"&#0146;", //$NON-NLS-1$
159
"'", //$NON-NLS-1$
160
"&#146;", //$NON-NLS-1$
161
"'", //$NON-NLS-1$
162
"&#0145;", //$NON-NLS-1$
163
"'", //$NON-NLS-1$
164
"&#145;", //$NON-NLS-1$
165
"'", //$NON-NLS-1$
166
"&quot;", //$NON-NLS-1$
167
"\"", }; //$NON-NLS-1$
168

169   private HtmlUtilities() {
170     //No instance available
171
}
172
173   protected static void buildEntityTables() {
174     entityTableEncode = new Hashtable JavaDoc(ENTITIES.length);
175
176     for (int i = 0; i < ENTITIES.length; i += 2) {
177       if (!entityTableEncode.containsKey(ENTITIES[i + 1]))
178         entityTableEncode.put(ENTITIES[i + 1], ENTITIES[i]);
179     }
180   }
181
182   /**
183    * Converts a String to HTML by converting all special characters to
184    * HTML-entities.
185    */

186   public final static String JavaDoc encode(String JavaDoc s, String JavaDoc ignore) {
187     return encode(s, 0, s.length(), ignore);
188   }
189
190   /**
191    * Converts a String to HTML by converting all special characters to
192    * HTML-entities. Only s,substring(start,end) will be encoded.
193    */

194   public final static String JavaDoc encode(String JavaDoc s, int start, int end, String JavaDoc ignore) {
195     if (entityTableEncode == null)
196       buildEntityTables();
197
198     StringBuffer JavaDoc sb = new StringBuffer JavaDoc((end - start) * 2);
199     char ch;
200     for (int i = start; i < end; ++i) {
201       ch = s.charAt(i);
202       if ((ch >= 63 && ch <= 90) || (ch >= 97 && ch <= 122) || ignore.indexOf(ch) != -1)
203         sb.append(ch);
204       else
205         sb.append(encodeSingleChar(String.valueOf(ch)));
206     }
207     return sb.toString();
208   }
209
210   /**
211    * Converts a single character to HTML
212    */

213   protected final static String JavaDoc encodeSingleChar(String JavaDoc ch) {
214     String JavaDoc s = (String JavaDoc) entityTableEncode.get(ch);
215     return (s == null) ? ch : s;
216   }
217
218   /**
219    * Converts the given Color object to a String contaning the html
220    * description of the color. E.g.: #FF8080.
221    */

222   public final static String JavaDoc toHTML(RGB color) {
223     String JavaDoc red = Integer.toHexString(color.getRed());
224     String JavaDoc green = Integer.toHexString(color.getGreen());
225     String JavaDoc blue = Integer.toHexString(color.getBlue());
226
227     if (red.length() == 1) {
228       red = "0" + red; //$NON-NLS-1$
229
}
230     if (green.length() == 1) {
231       green = "0" + green; //$NON-NLS-1$
232
}
233     if (blue.length() == 1) {
234       blue = "0" + blue; //$NON-NLS-1$
235
}
236
237     return "#" + red + green + blue; //$NON-NLS-1$
238
}
239 }
Popular Tags