KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > FormDataManager


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
// FormDataManager
13
// EV 18.11.2000
14
// POL 08.02.2001
15
//
16
// encode // encodes data from http form (' -> ')
17
// formDecode // encodes special chars for forms (" -> &quot; and '<' -> &lt;)
18
// decode // decodes everything
19
// checkIntegrity
20
// isTagClosed // check if a tag is closed
21
// removeTag
22
// removeTags
23
// removeTagWithContent
24
//
25

26 package org.jahia.data;
27
28 import org.jahia.engines.EngineMessage;
29 import org.jahia.engines.EngineMessages;
30 import org.jahia.utils.JahiaConsole;
31 import org.jahia.utils.JahiaTools;
32
33 /**
34  * <p>Title: HTML Form processing singleton</p>
35  * <p>Description: </p>
36  * <p>Copyright: Copyright (c) 2002</p>
37  * <p>Company: Jahia Ltd</p>
38  * @author Eric Vassalli
39  * @author Philippe Vollenweider
40  * @author Serge Huber
41  * @version 1.0
42  */

43 public class FormDataManager {
44
45     private static FormDataManager theObject = null;
46
47     /***
48      * constructor
49      * EV 18.11.2000
50      *
51      */

52     private FormDataManager () {
53         JahiaConsole.println("FormDataManager",
54                              "***** Starting up Form Data Manager *****");
55     } // end constructor
56

57     /***
58      * getInstance
59      * EV 18.11.2000
60      *
61      */

62     public static synchronized FormDataManager getInstance () {
63         if (theObject == null) {
64             theObject = new FormDataManager();
65         }
66         return theObject;
67     } // end getInstance
68

69     /***
70      * formDecode
71      * from the file to the form
72      * substitutions: < to &lt;
73      * " to &quot;
74      * EV 19.11.2000
75      * POL 09.02.2001
76      */

77     public String JavaDoc formEncode (String JavaDoc str) {
78         if (str == null) {
79             return null;
80         }
81         str = JahiaTools.replacePattern(str, "&", "&amp;");
82         str = JahiaTools.replacePattern(str, "<", "&lt;");
83         str = JahiaTools.text2html(str);
84         return str;
85     }
86
87     /***
88      * decode
89      * From the file to a web page
90      * 1. if we are not in a <html> session -> replace \n with <br/> tag
91      * 2. replace special char with html code
92      * POL 08.02.2001
93      *
94      */

95     public String JavaDoc formDecode (String JavaDoc str) {
96         if (str == null) {
97             return null;
98         }
99
100         str = JahiaTools.replacePattern(str, "&amp;", "&");
101         str = JahiaTools.replacePattern(str, "&lt;", "<");
102         str = JahiaTools.html2text(str);
103
104         return str;
105     } // end decode
106

107     /**
108      *
109      * @param str
110      * @return
111      */

112     public String JavaDoc htmlEncode (String JavaDoc str) {
113         if (str == null) {
114             return null;
115         }
116         StringBuffer JavaDoc result = new StringBuffer JavaDoc(str.length() + 500);
117         String JavaDoc strLower = str.toLowerCase();
118         int startOfIndex = 0;
119         int i = strLower.indexOf("<html>");
120         while (i != -1) {
121             result.append(JahiaTools.replacePattern(str.substring(startOfIndex,
122                 i), "\n", "\n<br/>"));
123             startOfIndex = i + 6; // 6 -> <html>
124
i = strLower.indexOf("</html>", startOfIndex);
125             if (i != -1) {
126                 result.append(str.substring(startOfIndex, i));
127                 startOfIndex = i + 7; // 7 -> </html>
128
}
129             i = strLower.indexOf("<html>", startOfIndex);
130         }
131         str = result.append(JahiaTools.replacePattern(str.substring(
132             startOfIndex, str.length()), "\n", "\n<br/>")).toString();
133         str = removeSpecialTags(str);
134         str = JahiaTools.text2html(str);
135         //str = JahiaTools.replacePattern(str, "&#64;", "@");
136
str = JahiaTools.replacePattern(str, "&quot;", "\"");
137         return str;
138     }
139
140     /**
141      * Remove tags <body>, <head>, <title>, <frame>, <frameset>
142      *
143      * @param str
144      * @return
145      */

146     public String JavaDoc removeSpecialTags (String JavaDoc str) {
147         if (str == null) {
148             return null;
149         }
150
151         str = removeTags(str, "body");
152         str = removeTagWithContent(str, "head");
153         str = removeTagWithContent(str, "title");
154         str = removeTagWithContent(str, "frame");
155         str = removeTagWithContent(str, "frameset");
156
157         return str;
158     }
159
160     /*
161      * Remove a start & end tag in a string
162      * For example removeTags(str,"title") will remove <title> and </title>
163      * @author POL
164      * @version 1.0 POL 23/01/2002
165      * @param str Input String
166      * @param tag Tag to remove
167      * @return str
168      **/

169     private String JavaDoc removeTags (String JavaDoc str, String JavaDoc tag) {
170         str = removeTag(str, tag);
171         str = removeTag(str, "/" + tag);
172         return str;
173     }
174
175     /*
176      * Remove a tag from a string
177      * Exemple: removeTag(str,"body") will remove <body bgcolor="#ffffff">
178      * @author POL
179      * @version 1.0 POL 23/01/2002
180      * @param str Input String
181      * @param tag Tag to remove
182      * @return str
183      **/

184     private String JavaDoc removeTag (String JavaDoc str, String JavaDoc tag) {
185         if (str == null) {
186             return null;
187         }
188         StringBuffer JavaDoc result = new StringBuffer JavaDoc(str.length());
189         String JavaDoc strLower = str.toLowerCase();
190         tag = tag.toLowerCase();
191         int startOfIndex = 0;
192         int i = strLower.indexOf("<" + tag);
193         while (i != -1) {
194             result.append(str.substring(startOfIndex, i));
195             i = strLower.indexOf(">", i);
196             if (i != -1) {
197                 startOfIndex = i + 1;
198             }
199             i = strLower.indexOf("<" + tag, startOfIndex);
200         }
201         str = result.append(str.substring(startOfIndex, str.length())).toString();
202         return str;
203     }
204
205     /*
206      * Remove a start and end tag with content from a string
207          * Exemple: removeTag(str,"title") will remove <title>thist is a title</title>
208      * @author POL
209      * @version 1.0 POL 23/01/2002
210      * @param str Input String
211      * @param tag Tag to remove
212      * @return str
213      **/
private String JavaDoc removeTagWithContent (String JavaDoc str, String JavaDoc tag) {
214         if (str == null) {
215             return null;
216         }
217         StringBuffer JavaDoc result = new StringBuffer JavaDoc(str.length());
218         String JavaDoc strLower = str.toLowerCase();
219         tag = tag.toLowerCase();
220         int startOfIndex = 0;
221         int i = strLower.indexOf("<" + tag);
222         while (i != -1) {
223             result.append(str.substring(startOfIndex, i));
224             startOfIndex = i + tag.length() + 1;
225             i = strLower.indexOf(tag + ">", startOfIndex);
226             if (i != -1) {
227                 startOfIndex = i + tag.length() + 1;
228             }
229             i = strLower.indexOf("<" + tag, startOfIndex);
230         }
231         str = result.append(str.substring(startOfIndex, str.length())).toString();
232         return str;
233     }
234
235     /***
236      * checkIntegrity
237      * EV 19.11.2000
238      * called by encode
239      *
240      */

241     public boolean checkIntegrity (String JavaDoc str, EngineMessages resultMessages) {
242         if (str == null) {
243             return false;
244         }
245
246         String JavaDoc testStr = str;
247         int pos = 0;
248         int pos2 = 0;
249         int qtCount = 0;
250         testStr = JahiaTools.replacePattern(str, "\\<", "");
251
252         // checks "<" and ">"
253
int searchIndex = 0;
254         pos = testStr.indexOf("<", searchIndex);
255         while (pos != -1) {
256             pos2 = testStr.indexOf(">", searchIndex);
257             pos = testStr.indexOf("<", pos2);
258             if ( (pos2 != -1) && ( (pos > pos2) || (pos == -1))) {
259                 // testStr = testStr.substring( pos2 + 1, testStr.length() );
260
searchIndex = pos2 + 1;
261             } else {
262                 EngineMessage errorMessage = new EngineMessage(
263                     "org.jahia.data.FormDataManager.tagMissingLtOrGt");
264                 resultMessages.add("htmlParser", errorMessage);
265                 return false;
266             }
267         }
268
269         // check some tags who should be closed
270
if (! (
271             // isTagClosed("a",str) && // problem with above & array
272

273             isTagClosed("b>", str, resultMessages) &&
274             isTagClosed("font", str, resultMessages) &&
275             isTagClosed("em>", str, resultMessages) &&
276             isTagClosed("i>", str, resultMessages) &&
277             isTagClosed("h1", str, resultMessages) &&
278             isTagClosed("h2", str, resultMessages) &&
279             isTagClosed("h3", str, resultMessages) &&
280             isTagClosed("table", str, resultMessages) &&
281             isTagClosed("script", str, resultMessages) &&
282             isTagClosed("span", str, resultMessages) &&
283             isTagClosed("div", str, resultMessages) &&
284             isTagClosed("layer", str, resultMessages) &&
285             isTagClosed("blockquote", str, resultMessages) &&
286             isTagClosed("ul", str, resultMessages) &&
287             isTagClosed("ol", str, resultMessages) &&
288             isTagClosed("form", str, resultMessages) &&
289             isTagClosed("comment", str, resultMessages) &&
290             isTagClosed("center", str, resultMessages) &&
291             isTagClosed("textarea", str, resultMessages) &&
292             isTagClosed("select", str, resultMessages) &&
293             isTagClosed("xmp", str, resultMessages) &&
294             isTagClosed("style", str, resultMessages) &&
295             isTagClosed("pre", str, resultMessages) &&
296             isTagClosed("u>", str, resultMessages) &&
297             isTagClosed("small", str, resultMessages) &&
298             isTagClosed("sub", str, resultMessages) &&
299             isTagClosed("sup", str, resultMessages) &&
300             isTagClosed("s>", str, resultMessages) &&
301             isTagClosed("strike", str, resultMessages) &&
302             isTagClosed("strong", str, resultMessages) &&
303             isTagClosed("tbody", str, resultMessages) &&
304             isTagClosed("caption", str, resultMessages) &&
305             isTagClosed("colgroup", str, resultMessages) &&
306             isTagClosed("col", str, resultMessages) &&
307             isTagClosed("tfoot", str, resultMessages) &&
308             isTagClosed("th", str, resultMessages) &&
309             isTagClosed("thread", str, resultMessages) &&
310             isTagClosed("del", str, resultMessages) &&
311             isTagClosed("dt", str, resultMessages) &&
312             isTagClosed("dd", str, resultMessages) &&
313             isTagClosed("dl", str, resultMessages) &&
314             isTagClosed("dir", str, resultMessages) &&
315             isTagClosed("cite", str, resultMessages) &&
316             isTagClosed("code", str, resultMessages) &&
317             isTagClosed("fieldset", str, resultMessages) &&
318             isTagClosed("label", str, resultMessages) &&
319             isTagClosed("object", str, resultMessages) &&
320             isTagClosed("map", str, resultMessages) &&
321             isTagClosed("dfn", str, resultMessages) &&
322             isTagClosed("listing", str, resultMessages) &&
323             isTagClosed("marquee", str, resultMessages) &&
324             isTagClosed("menu", str, resultMessages) &&
325             isTagClosed("multicol", str, resultMessages) &&
326             isTagClosed("nobr", str, resultMessages) &&
327             isTagClosed("note", str, resultMessages) &&
328             isTagClosed("optgroup", str, resultMessages) &&
329             isTagClosed("samp", str, resultMessages) &&
330             isTagClosed("tt", str, resultMessages) &&
331             isTagClosed("kbd", str, resultMessages) &&
332             isTagClosed("var", str, resultMessages) &&
333             isTagClosed("noembed", str, resultMessages) &&
334             isTagClosed("html", str, resultMessages)
335             )
336             ) {
337             return false;
338         }
339
340         // checks for '"' in tags
341
searchIndex = 0; // we re-use the above variable, why not ? :)
342
int endTagIndex = 0;
343         int startTagIndex = str.indexOf("<", searchIndex);
344         while (startTagIndex != -1) {
345             endTagIndex = str.indexOf(">", startTagIndex);
346             if (endTagIndex != -1) {
347                 String JavaDoc tag = str.substring(startTagIndex, endTagIndex);
348                 testStr = str;
349                 pos = testStr.indexOf("\"", searchIndex);
350                 while (pos != -1) {
351                     qtCount++;
352                     searchIndex = pos + 1;
353                     pos = testStr.indexOf("\"", searchIndex);
354                 }
355                 /*
356                  * Return false for a string like : <html>t'élé t"élé</html>
357                  * ????
358                  */

359                 /*
360                 if ( (qtCount % 2) != 0) {
361                     EngineMessage errorMessage = new EngineMessage(
362                         "org.jahia.data.FormDataManager.illegalTag", JahiaTools.text2html( tag ));
363                     resultMessages.add("htmlParser", errorMessage);
364                     JahiaConsole.println("FormDataManagerager",
365                                          "Illegal html tag : <" + tag + ">");
366                     return false;
367                 }*/

368             } else {
369                 String JavaDoc tag;
370                 if (startTagIndex + 5 > str.length()) {
371                     tag = str.substring(startTagIndex);
372                 } else {
373                     tag = str.substring(startTagIndex, startTagIndex+5) + "...";
374                 }
375                 EngineMessage errorMessage = new EngineMessage(
376                     "org.jahia.data.FormDataManager.tagNotClosed", tag);
377                 resultMessages.add("htmlParser", errorMessage);
378                 JahiaConsole.println("FormDataManagerager", "Tag not closed");
379                 return false;
380             }
381             startTagIndex = str.indexOf("<", endTagIndex);
382         }
383         // all checks passed ! -> return true
384
return true;
385
386     } // end checkIntegrity
387

388     /***
389      * Return true if tag is closed in a string.<br>
390      * For example, <code>&lt;table&gt;</code> should be closed by <code>&lt;/table&gt;</code><br>
391      * <code>isTagClosed("table","&lt;table&gt;&lt;tr&gt;&lt;td&gt;hello&lt;/td&gt;&lt;/tr&gt;")</code> will return false.<br>
392      * @author POL
393      * @version 1.0 POL 03/05/2001
394      * @param tag Tag to check without the <code>&lt; &gt;</code>
395      * @param str Input String
396      * @return Return true if tag is closed
397      */

398     public boolean isTagClosed (String JavaDoc tag, String JavaDoc inputStr,
399                                 EngineMessages resultMessages) {
400         tag = tag.toLowerCase();
401         String JavaDoc testStr = inputStr.toLowerCase();
402         String JavaDoc openTag = "<" + tag;
403         String JavaDoc closeTag = "</" + tag;
404         int pos1 = 0;
405         int pos2 = 0;
406         int searchIndex = 0;
407         pos1 = testStr.indexOf(openTag, searchIndex);
408         while (pos1 != -1) {
409             pos2 = testStr.indexOf(closeTag, searchIndex);
410             pos1 = testStr.indexOf(openTag, pos2);
411             if ( (pos2 != -1) && ( (pos1 > pos2) || (pos1 == -1))) {
412                 searchIndex = pos2 + tag.length() + 2;
413             } else {
414                 EngineMessage errorMessage = new EngineMessage(
415                     "org.jahia.data.FormDataManager.tagNotClosed", tag);
416                 resultMessages.add("htmlParser", errorMessage);
417                 JahiaConsole.println("FormDataManagerager",
418                                      "ERROR: the HTML tag '" + tag +
419                                      "' should be closed.");
420                 return false;
421             }
422         }
423         return true;
424     } // isTagClosed
425

426 } // end FormDataManager
427
Popular Tags