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 HtmlUtilities {
035   private static Hashtable entityTableEncode;
036 
037   private final static String[] ENTITIES = {
038       //IGNORE (during encoding!!)
039       " "//$NON-NLS-1$
040       " "//$NON-NLS-1$
041       "-"//$NON-NLS-1$
042       "-"//$NON-NLS-1$
043       "'"//$NON-NLS-1$
044       "'"//$NON-NLS-1$
045       "`"//$NON-NLS-1$
046       "`"//$NON-NLS-1$
047 
048       //UPPERCASE
049       "&Uuml;"//$NON-NLS-1$
050       "Ü"//$NON-NLS-1$
051       "&Auml;"//$NON-NLS-1$
052       "Ä"//$NON-NLS-1$
053       "&Ouml;"//$NON-NLS-1$
054       "Ö"//$NON-NLS-1$
055       "&Euml;"//$NON-NLS-1$
056       "Ë"//$NON-NLS-1$
057       "&Ccedil;"//$NON-NLS-1$
058       "Ç"//$NON-NLS-1$
059       "&AElig;"//$NON-NLS-1$
060       "Æ"//$NON-NLS-1$
061       "&Aring;"//$NON-NLS-1$
062       "Å"//$NON-NLS-1$
063       "&Oslash;"//$NON-NLS-1$
064       "Ø"//$NON-NLS-1$
065 
066       //OTHERS -> ignorecase!
067       "&uuml;"//$NON-NLS-1$
068       "ü"//$NON-NLS-1$
069       "&auml;"//$NON-NLS-1$
070       "ä"//$NON-NLS-1$
071       "&ouml;"//$NON-NLS-1$
072       "ö"//$NON-NLS-1$
073       "&euml;"//$NON-NLS-1$
074       "ë"//$NON-NLS-1$
075       "&ccedil;"//$NON-NLS-1$
076       "ç"//$NON-NLS-1$
077       "&aring;"//$NON-NLS-1$
078       "å"//$NON-NLS-1$
079       "&oslash;"//$NON-NLS-1$
080       "ø"//$NON-NLS-1$
081       "&grave;"//$NON-NLS-1$
082       "`"//$NON-NLS-1$
083       "&agrave;"//$NON-NLS-1$
084       "à"//$NON-NLS-1$
085       "&egrave;"//$NON-NLS-1$
086       "è"//$NON-NLS-1$
087       "&igrave;"//$NON-NLS-1$
088       "ì"//$NON-NLS-1$
089       "&ograve;"//$NON-NLS-1$
090       "ò"//$NON-NLS-1$
091       "&ugrave;"//$NON-NLS-1$
092       "ù"//$NON-NLS-1$
093       "&amp;"//$NON-NLS-1$
094       "&"//$NON-NLS-1$
095       "&#34;"//$NON-NLS-1$
096       "\""//$NON-NLS-1$
097       // same as &quot; - but &quot; is not part of HTML3.2!!!
098       "&szlig;"//$NON-NLS-1$
099       "ß"//$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$ 
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(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   /**
184    * Converts a String to HTML by converting all special characters to
185    * HTML-entities.
186    */
187   public final static String encode(String s, String ignore) {
188     return encode(s, 0, s.length(), ignore);
189   }
190 
191   /**
192    * Converts a String to HTML by converting all special characters to
193    * HTML-entities. Only s,substring(start,end) will be encoded.
194    */
195   public final static String encode(String s, int start, int end, String ignore) {
196     if (entityTableEncode == null) {
197       buildEntityTables();
198     }
199 
200     final StringBuffer sb = new StringBuffer((end - start2);
201     char ch;
202     for (int i = start; i < end; ++i) {
203       ch = s.charAt(i);
204       if ((ch >= 63 && ch <= 90|| (ch >= 97 && ch <= 122|| ignore.indexOf(ch!= -1) {
205         sb.append(ch);
206       }
207       else {
208         sb.append(encodeSingleChar(String.valueOf(ch)));
209       }
210     }
211     return sb.toString();
212   }
213 
214   /**
215    * Converts a single character to HTML
216    */
217   protected final static String encodeSingleChar(String ch) {
218     final String s = (StringentityTableEncode.get(ch);
219     return (s == null? ch : s;
220   }
221 
222   /**
223    * Converts the given Color object to a String contaning the html 
224    * description of the color. E.g.: #FF8080.
225    */
226   public final static String toHTML(RGB color) {
227     String red = Integer.toHexString(color.getRed());
228     String green = Integer.toHexString(color.getGreen());
229     String blue = Integer.toHexString(color.getBlue());
230 
231     if (red.length() == 1) {
232       red = "0" + red; //$NON-NLS-1$
233     }
234     if (green.length() == 1) {
235       green = "0" + green; //$NON-NLS-1$
236     }
237     if (blue.length() == 1) {
238       blue = "0" + blue; //$NON-NLS-1$
239     }
240 
241     return "#" + red + green + blue; //$NON-NLS-1$
242   }
243 }