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 = { //IGNORE (during encoding!!)
038: " ", //$NON-NLS-1$
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:
047: //UPPERCASE
048: "Ü", //$NON-NLS-1$
049: "Ü", //$NON-NLS-1$
050: "Ä", //$NON-NLS-1$
051: "Ä", //$NON-NLS-1$
052: "Ö", //$NON-NLS-1$
053: "Ö", //$NON-NLS-1$
054: "Ë", //$NON-NLS-1$
055: "Ë", //$NON-NLS-1$
056: "Ç", //$NON-NLS-1$
057: "Ç", //$NON-NLS-1$
058: "Æ", //$NON-NLS-1$
059: "Æ", //$NON-NLS-1$
060: "Å", //$NON-NLS-1$
061: "Å", //$NON-NLS-1$
062: "Ø", //$NON-NLS-1$
063: "Ø", //$NON-NLS-1$
064:
065: //OTHERS -> ignorecase!
066: "ü", //$NON-NLS-1$
067: "ü", //$NON-NLS-1$
068: "ä", //$NON-NLS-1$
069: "ä", //$NON-NLS-1$
070: "ö", //$NON-NLS-1$
071: "ö", //$NON-NLS-1$
072: "ë", //$NON-NLS-1$
073: "ë", //$NON-NLS-1$
074: "ç", //$NON-NLS-1$
075: "ç", //$NON-NLS-1$
076: "å", //$NON-NLS-1$
077: "å", //$NON-NLS-1$
078: "ø", //$NON-NLS-1$
079: "ø", //$NON-NLS-1$
080: "`", //$NON-NLS-1$
081: "`", //$NON-NLS-1$
082: "à", //$NON-NLS-1$
083: "à", //$NON-NLS-1$
084: "è", //$NON-NLS-1$
085: "è", //$NON-NLS-1$
086: "ì", //$NON-NLS-1$
087: "ì", //$NON-NLS-1$
088: "ò", //$NON-NLS-1$
089: "ò", //$NON-NLS-1$
090: "ù", //$NON-NLS-1$
091: "ù", //$NON-NLS-1$
092: "&", //$NON-NLS-1$
093: "&", //$NON-NLS-1$
094: """, //$NON-NLS-1$
095: "\"", //$NON-NLS-1$
096: // same as " - but " is not part of HTML3.2!!!
097: "ß", //$NON-NLS-1$
098: "ß", //$NON-NLS-1$
099: " ", //$NON-NLS-1$
100: " ", //$NON-NLS-1$
101: ">", //$NON-NLS-1$
102: ">", //$NON-NLS-1$
103: "<", //$NON-NLS-1$
104: "<", //$NON-NLS-1$
105: "©", //$NON-NLS-1$
106: "(C)", //$NON-NLS-1$
107: "¢", //$NON-NLS-1$
108: "¢", //$NON-NLS-1$
109: "£", //$NON-NLS-1$
110: "£", //$NON-NLS-1$
111: "«", //$NON-NLS-1$
112: "«", //$NON-NLS-1$
113: "»", //$NON-NLS-1$
114: "»", //$NON-NLS-1$
115: "®", //$NON-NLS-1$
116: "(R)", //$NON-NLS-1$
117: "·", //$NON-NLS-1$
118: " - ", //$NON-NLS-1$
119: "×", //$NON-NLS-1$
120: " x ", //$NON-NLS-1$
121: "´", //$NON-NLS-1$
122: "'", //$NON-NLS-1$ //$NON-NLS-1$
123: "á", //$NON-NLS-1$
124: "á", //$NON-NLS-1$
125: "ú", //$NON-NLS-1$
126: "ú", //$NON-NLS-1$
127: "ó", //$NON-NLS-1$
128: "ó", //$NON-NLS-1$
129: "é", //$NON-NLS-1$
130: "é", //$NON-NLS-1$
131: "í", //$NON-NLS-1$
132: "í", //$NON-NLS-1$
133: "ñ", //$NON-NLS-1$
134: "ñ", //$NON-NLS-1$
135: "§", //$NON-NLS-1$
136: "§", //$NON-NLS-1$
137: "è", //$NON-NLS-1$
138: "è", //$NON-NLS-1$
139: "î", //$NON-NLS-1$
140: "î", //$NON-NLS-1$
141: "ô", //$NON-NLS-1$
142: "ô", //$NON-NLS-1$
143: "â", //$NON-NLS-1$
144: "â", //$NON-NLS-1$
145: "û", //$NON-NLS-1$
146: "û", //$NON-NLS-1$
147: "ê", //$NON-NLS-1$
148: "ê", //$NON-NLS-1$
149: "æ", //$NON-NLS-1$
150: "æ", //$NON-NLS-1$
151: "¡", //$NON-NLS-1$
152: "¡", //$NON-NLS-1$
153: "—", //$NON-NLS-1$
154: "-", //$NON-NLS-1$
155: "—", //$NON-NLS-1$
156: "-", //$NON-NLS-1$
157: "’", //$NON-NLS-1$
158: "'", //$NON-NLS-1$
159: "’", //$NON-NLS-1$
160: "'", //$NON-NLS-1$
161: "‘", //$NON-NLS-1$
162: "'", //$NON-NLS-1$
163: "‘", //$NON-NLS-1$
164: "'", //$NON-NLS-1$
165: """, //$NON-NLS-1$
166: "\"", }; //$NON-NLS-1$
167:
168: private HtmlUtilities() {
169: //No instance available
170: }
171:
172: protected static void buildEntityTables() {
173: entityTableEncode = new Hashtable(ENTITIES.length);
174:
175: for (int i = 0; i < ENTITIES.length; i += 2) {
176: if (!entityTableEncode.containsKey(ENTITIES[i + 1]))
177: entityTableEncode.put(ENTITIES[i + 1], ENTITIES[i]);
178: }
179: }
180:
181: /**
182: * Converts a String to HTML by converting all special characters to
183: * HTML-entities.
184: */
185: public final static String encode(String s, String ignore) {
186: return encode(s, 0, s.length(), ignore);
187: }
188:
189: /**
190: * Converts a String to HTML by converting all special characters to
191: * HTML-entities. Only s,substring(start,end) will be encoded.
192: */
193: public final static String encode(String s, int start, int end,
194: String ignore) {
195: if (entityTableEncode == null)
196: buildEntityTables();
197:
198: StringBuffer sb = new StringBuffer((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)
203: || ignore.indexOf(ch) != -1)
204: sb.append(ch);
205: else
206: sb.append(encodeSingleChar(String.valueOf(ch)));
207: }
208: return sb.toString();
209: }
210:
211: /**
212: * Converts a single character to HTML
213: */
214: protected final static String encodeSingleChar(String ch) {
215: String s = (String) entityTableEncode.get(ch);
216: return (s == null) ? ch : s;
217: }
218:
219: /**
220: * Converts the given Color object to a String contaning the html
221: * description of the color. E.g.: #FF8080.
222: */
223: public final static String toHTML(RGB color) {
224: String red = Integer.toHexString(color.getRed());
225: String green = Integer.toHexString(color.getGreen());
226: String blue = Integer.toHexString(color.getBlue());
227:
228: if (red.length() == 1) {
229: red = "0" + red; //$NON-NLS-1$
230: }
231: if (green.length() == 1) {
232: green = "0" + green; //$NON-NLS-1$
233: }
234: if (blue.length() == 1) {
235: blue = "0" + blue; //$NON-NLS-1$
236: }
237:
238: return "#" + red + green + blue; //$NON-NLS-1$
239: }
240: }
|