001 package de.java2html.util;
002
003 import java.util.Hashtable;
004
005 /**
006 * Some methods for converting text to valid HTML.
007 *
008 * For questions, suggestions, bug-reports, enhancement-requests etc.
009 * I may be contacted at:
010 * <a href="mailto:markus@jave.de">markus@jave.de</a>
011 *
012 * The Java2html home page is located at:
013 * <a href="http://www.java2html.de">http://www.java2html.de</a>
014 *
015 * @author <a href="mailto:markus@jave.de">Markus Gebhard</a>
016 * @version 2.0, 05/07/02
017 *
018 * Copyright (C) Markus Gebhard 2000-2002
019 *
020 * This program is free software; you can redistribute it and/or
021 * modify it under the terms of the GNU General Public License
022 * as published by the Free Software Foundation; either version 2
023 * of the License, or (at your option) any later version.
024 *
025 * This program is distributed in the hope that it will be useful,
026 * but WITHOUT ANY WARRANTY; without even the implied warranty of
027 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
028 * GNU General Public License for more details.
029 *
030 * You should have received a copy of the GNU General Public License
031 * along with this program; if not, write to the Free Software
032 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
033 */
034 public class HTMLTools {
035 protected static Hashtable entityTableEncode;
036
037 protected final static String[] ENTITIES = {
038 //IGNORE (during encoding!!)
039 " ", " ", "-", "-", "'", "'", "`", "`",
040
041 //UPPERCASE
042 "Ü",
043 "Ü",
044 "Ä",
045 "Ä",
046 "Ö",
047 "Ö",
048 "Ë",
049 "Ë",
050 "Ç",
051 "Ç",
052 "Æ",
053 "Æ",
054 "Å",
055 "Å",
056 "Ø",
057 "Ø",
058
059 //OTHERS -> ignorecase!
060 "ü",
061 "ü",
062 "ä",
063 "ä",
064 "ö",
065 "ö",
066 "ë",
067 "ë",
068 "ç",
069 "ç",
070 "å",
071 "å",
072 "ø",
073 "ø",
074 "`",
075 "`",
076 "à",
077 "à",
078 "è",
079 "è",
080 "ì",
081 "ì",
082 "ò",
083 "ò",
084 "ù",
085 "ù",
086 "&",
087 "&",
088 """,
089 "\"",
090 // same as " - but " is not part of HTML3.2!!!
091 "ß",
092 "ß",
093 " ",
094 " ",
095 ">",
096 ">",
097 "<",
098 "<",
099 "©",
100 "(C)",
101 "¢",
102 "¢",
103 "£",
104 "£",
105 "«",
106 "«",
107 "»",
108 "»",
109 "®",
110 "(R)",
111 "·",
112 " - ",
113 "×",
114 " x ",
115 "´",
116 "'",
117 "á",
118 "á",
119 "ú",
120 "ú",
121 "ó",
122 "ó",
123 "é",
124 "é",
125 "í",
126 "í",
127 "ñ",
128 "ñ",
129 "§",
130 "§",
131 "è",
132 "è",
133 "î",
134 "î",
135 "ô",
136 "ô",
137 "â",
138 "â",
139 "û",
140 "û",
141 "ê",
142 "ê",
143 "æ",
144 "æ",
145 "¡",
146 "¡",
147 "—",
148 "-",
149 "—",
150 "-",
151 "’",
152 "'",
153 "’",
154 "'",
155 "‘",
156 "'",
157 "‘",
158 "'",
159 """,
160 "\"",
161 };
162
163 private HTMLTools() {
164 //No instance available
165 }
166
167 protected static void buildEntityTables() {
168 entityTableEncode = new Hashtable(ENTITIES.length);
169
170 for (int i = 0; i < ENTITIES.length; i += 2) {
171 if (!entityTableEncode.containsKey(ENTITIES[i + 1]))
172 entityTableEncode.put(ENTITIES[i + 1], ENTITIES[i]);
173 }
174 }
175
176 /**
177 * Converts a String to HTML by converting all special characters to
178 * HTML-entities.
179 */
180 public final static String encode(String s, String ignore) {
181 return encode(s, 0, s.length(), ignore);
182 }
183
184 /**
185 * Converts a String to HTML by converting all special characters to
186 * HTML-entities. Only s,substring(start,end) will be encoded.
187 */
188 public final static String encode(String s, int start, int end, String ignore) {
189 if (entityTableEncode == null)
190 buildEntityTables();
191
192 StringBuffer sb = new StringBuffer((end - start) * 2);
193 char ch;
194 for (int i = start; i < end; ++i) {
195 ch = s.charAt(i);
196 if ((ch >= 63 && ch <= 90) || (ch >= 97 && ch <= 122) || ignore.indexOf(ch) != -1)
197 sb.append(ch);
198 else
199 sb.append(encodeSingleChar(String.valueOf(ch)));
200 }
201 return sb.toString();
202 }
203
204 /**
205 * Converts a single character to HTML
206 */
207 protected final static String encodeSingleChar(String ch) {
208 String s = (String) entityTableEncode.get(ch);
209 return (s == null) ? ch : s;
210 }
211
212 /**
213 * Converts the given Color object to a String contaning the html
214 * description of the color. E.g.: #FF8080.
215 */
216 public final static String toHTML(RGB color) {
217 String red = Integer.toHexString(color.getRed());
218 String green = Integer.toHexString(color.getGreen());
219 String blue = Integer.toHexString(color.getBlue());
220
221 if (red.length() == 1)
222 red = "0" + red;
223 if (green.length() == 1)
224 green = "0" + green;
225 if (blue.length() == 1)
226 blue = "0" + blue;
227
228 return "#" + red + green + blue;
229 }
230 }
|