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     "&Uuml;",
043       "Ü",
044       "&Auml;",
045       "Ä",
046       "&Ouml;",
047       "Ö",
048       "&Euml;",
049       "Ë",
050       "&Ccedil;",
051       "Ç",
052       "&AElig;",
053       "Æ",
054       "&Aring;",
055       "Å",
056       "&Oslash;",
057       "Ø",
058 
059     //OTHERS -> ignorecase!
060     "&uuml;",
061       "ü",
062       "&auml;",
063       "ä",
064       "&ouml;",
065       "ö",
066       "&euml;",
067       "ë",
068       "&ccedil;",
069       "ç",
070       "&aring;",
071       "å",
072       "&oslash;",
073       "ø",
074       "&grave;",
075       "`",
076       "&agrave;",
077       "à",
078       "&egrave;",
079       "è",
080       "&igrave;",
081       "ì",
082       "&ograve;",
083       "ò",
084       "&ugrave;",
085       "ù",
086       "&amp;",
087       "&",
088       "&#34;",
089       "\"",
090     // same as &quot; - but &quot; is not part of HTML3.2!!!
091     "&szlig;",
092       "ß",
093       "&nbsp;",
094       " ",
095       "&gt;",
096       ">",
097       "&lt;",
098       "<",
099       "&copy;",
100       "(C)",
101       "&cent;",
102       "¢",
103       "&pound;",
104       "£",
105       "&laquo;",
106       "«",
107       "&raquo;",
108       "»",
109       "&reg;",
110       "(R)",
111       "&middot;",
112       " - ",
113       "&times;",
114       " x ",
115       "&acute;",
116       "'",
117       "&aacute;",
118       "á",
119       "&uacute;",
120       "ú",
121       "&oacute;",
122       "ó",
123       "&eacute;",
124       "é",
125       "&iacute;",
126       "í",
127       "&ntilde;",
128       "ñ",
129       "&sect;",
130       "§",
131       "&egrave;",
132       "è",
133       "&icirc;",
134       "î",
135       "&ocirc;",
136       "ô",
137       "&acirc;",
138       "â",
139       "&ucirc;",
140       "û",
141       "&ecirc;",
142       "ê",
143       "&aelig;",
144       "æ",
145       "&iexcl;",
146       "¡",
147       "&#151;",
148       "-",
149       "&#0151;",
150       "-",
151       "&#0146;",
152       "'",
153       "&#146;",
154       "'",
155       "&#0145;",
156       "'",
157       "&#145;",
158       "'",
159       "&quot;",
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 - start2);
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 = (StringentityTableEncode.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 }