Source code: org/javahispano/canyamo/util/html/validator/HTMLParser.java
1 /*
2 Cañamo, portal framework
3 Copyright (c) 2002
4 Alberto Molpeceres, javaHispano (http://www.javahispano.org)
5 Martin Pérez, javaHispano (http://www.javahispano.org)
6 All rights reserved.
7 .
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15 Neither the name of Alberto Molpeceres, javaHispano nor the names of its
16 contributors may be used to endorse or promote products derived from
17 this software without specific prior written permission.
18 .
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 package org.javahispano.canyamo.util.html.validator;
32
33 import javax.swing.text.*;
34 import javax.swing.text.html.*;
35 import javax.swing.text.html.parser.*;
36 import javax.swing.*;
37 import java.io.*;
38 import java.net.*;
39 import java.util.*;
40
41 /**
42 * This class is an HTML callback used when parsing HTML code. It has callback
43 * methods that are called through the parsing process.
44 *
45 *@author <a href="mailto:martin AT javahispano DOT org">Martín Pérez
46 * Mariñán</a>
47 *@created 08 September 2002
48 *@version 1.0
49 */
50 class HTMLParser extends HTMLEditorKit.ParserCallback {
51
52 /*
53 array with smilies
54 keep in mind that these are regexp expressions
55 */
56 /** Description of the Field */
57 private final static String[] strSmilies = new String[]{
58 ":-)", ":)", ":-(", ":(", ":-/", ":-P",
59 ":P", "X-D", "XD", ":-D", ":D", ";-)", ";)", ":-o", ":O",
60 ":-O", ":'("
61 };
62
63 // array with smilies urls
64 /** Description of the Field */
65 private final static String[] urlSmilies = new String[]{
66 "<IMG src=\"/smilies/smile.gif\">",
67 "<IMG src=\"/smilies/smile.gif\">",
68 "<IMG src=\"/smilies/sad.gif\">",
69 "<IMG src=\"/smilies/sad.gif\">",
70 "<IMG src=\"/smilies/nono.gif\">",
71 "<IMG src=\"/smilies/tongue.gif\">",
72 "<IMG src=\"/smilies/tongue.gif\">",
73 "<IMG src=\"/smilies/biggrin.gif\">",
74 "<IMG src=\"/smilies/biggrin.gif\">",
75 "<IMG src=\"/smilies/biggrin.gif\">",
76 "<IMG src=\"/smilies/biggrin.gif\">",
77 "<IMG src=\"/smilies/wink.gif\">",
78 "<IMG src=\"/smilies/wink.gif\">",
79 "<IMG src=\"/smilies/shocked.gif\">",
80 "<IMG src=\"/smilies/shocked.gif\">",
81 "<IMG src=\"/smilies/shocked.gif\">",
82 "<IMG src=\"/smilies/cry.gif\">"
83 };
84
85 // Saves the HTML code while parsing
86 /** Description of the Field */
87 private StringBuffer result = new StringBuffer();
88
89 // flag that indicates if smilies images are allowed, default:true
90 /** Description of the Field */
91 private boolean smilies = true;
92
93
94 /** Constructor */
95 HTMLParser() { }
96
97
98 /**
99 * Sets if smilies are allowed
100 *
101 *@param b <code>true</code> if smilies are allowed, <code>false</code> in
102 * another case
103 */
104 public void setSmiliesAllowed(boolean b) {
105 this.smilies = b;
106 }
107
108
109 /**
110 * Gets the HTML code parsed
111 *
112 *@return String HTML code parsed
113 */
114 public String getResult() {
115 return result.toString().trim();
116 }
117
118
119 /**
120 * Gets an attributes set string representation
121 *
122 *@param attributes Set of attributes
123 *@return String String representation of those attributes
124 */
125 public String getAttributes(AttributeSet attributes) {
126
127 if (attributes == null) {
128 return "";
129 }
130 StringBuffer atts = new StringBuffer();
131 Enumeration e = attributes.getAttributeNames();
132 while (e.hasMoreElements()) {
133 Object name = e.nextElement();
134 if (attributes.getAttribute(name) != null) {
135 String stValue = attributes.getAttribute(name).toString();
136 atts.append(" ");
137 atts.append(name);
138 atts.append("=\"");
139 atts.append(stValue.trim());
140 atts.append("\"");
141 }
142 }
143 return atts.toString();
144 }
145
146
147 /**
148 * ¿Are smilies allowed?
149 *
150 *@return boolean <code>true</code> if smilies are allowed, <code>false</code>
151 * in another case
152 */
153 public boolean isSmiliesAllowed() {
154 return smilies;
155 }
156
157
158 /**
159 * Callback method that is called when a start tag is found. Example of start
160 * tags are <B>,<I>,...
161 *
162 *@param tag HTML tag
163 *@param attributes Tag attributes
164 *@param position Tag anidation
165 */
166 public void handleStartTag(HTML.Tag tag,
167 MutableAttributeSet attributes, int position) {
168 if (isAllowed(tag)) {
169 result.append("<" + tag + getAttributes(attributes) + ">");
170 // } else {
171 // result.append("(((" + tag.toString() + getAttributes(attributes) + ">");
172 }
173 }
174
175
176 /**
177 * Callback method that is called when a simple HTML tag is found. Simple
178 * HTML tags are those like <BR>
179 * ,<P>
180 *
181 * , <IMG>, etc.
182 *
183 *@param tag HTML tag found
184 *@param attributes Tag attributes
185 *@param position Tag anidation
186 */
187 public void handleSimpleTag(HTML.Tag tag,
188 MutableAttributeSet attributes, int position) {
189 if (isAllowed(tag)) {
190 result.append("<" + tag + getAttributes(attributes) + ">");
191 // } else {
192 // result.append("(((" + tag.toString() + getAttributes(attributes) + ">");
193 }
194 }
195
196
197 /**
198 * Callback method that is called when an end tag is found. Example of end
199 * tags are </B> ,</I> ,etc.
200 *
201 *@param tag Tag found
202 *@param position Tag anidation
203 */
204 public void handleEndTag(HTML.Tag tag, int position) {
205 if (isAllowed(tag)) {
206 result.append("</" + tag + ">");
207 // } else {
208 // result.append("(((/" + tag.toString() + ">");
209 }
210 }
211
212
213 /**
214 * Callback method that is called when some text is found
215 *
216 *@param text Text found
217 *@param position Anidation of the text found
218 */
219 public void handleText(char[] text, int position) {
220
221 String texto = new String(text);
222
223 if (smilies) {
224 texto = addSmilies(texto);
225 }
226 result.append(texto);
227 }
228
229
230 /**
231 * Callback method that is called when an eol tag is found
232 *
233 *@param eol End of line marker
234 */
235 public void handleEndOfLineString(String eol) {
236 result.append("\n");
237 }
238
239
240 /**
241 * Tells us if one HTML tag is allowed. Be careful, only allowed HTML tags
242 * will be parsed <br>
243 * <br>
244 * Allowed tags: <br>
245 * <br>
246 * B, I, U, OL, UL, LI, A, BR
247 *
248 *@param tag HTML tag
249 *@return boolean <code>true</code> if the tag is allowed <code>false</code>
250 * instead
251 */
252 protected boolean isAllowed(HTML.Tag tag) {
253
254 boolean allowed =
255 (tag == HTML.Tag.B) ||
256 (tag == HTML.Tag.I) ||
257 (tag == HTML.Tag.U) ||
258 (tag == HTML.Tag.OL) ||
259 (tag == HTML.Tag.UL) ||
260 (tag == HTML.Tag.LI) ||
261 (tag == HTML.Tag.A) ||
262 (tag == HTML.Tag.BR);
263
264 return allowed;
265 }
266
267
268 /**
269 * Detects if some text have smilies and in that case add the corresponding
270 * images
271 *
272 *@param texto The feature to be added to the Smilies attribute
273 *@return String Text with smilies images ( if it had smilies within )
274 */
275 protected String addSmilies(String texto) {
276
277 // System.out.println("[HTMLParser.addSmilies] Texto: " + texto);
278 for (int i = 0; i < strSmilies.length; i++) {
279 //jdk1.4
280 //texto = texto.replaceAll(strSmilies[i], urlSmilies[i]);
281 //jdk1.3
282 StringBuffer buffer = new StringBuffer(texto);
283 int from = texto.length() - 1;
284 int pos = 0;
285 // System.out.println("[HTMLParser.addSmilies] Smiley: " + strSmilies[i]);
286 while (pos >= 0) {
287 pos = texto.lastIndexOf(strSmilies[i], from);
288 if (pos >= 0) {
289 buffer.replace(pos, pos + strSmilies[i].length(), urlSmilies[i]);
290 from = pos - 1;
291 // System.out.println("[HTMLParser.addSmilies] encontrado: " + pos);
292 } else {
293 // System.out.println("[HTMLParser.addSmilies] no encontrado");
294 break;
295 }
296 }
297 texto = buffer.toString();
298 // System.out.println("[HTMLParser.addSmilies] Texto: " + texto);
299 }
300
301 return texto;
302 }
303 }
304