KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > misc > JavascriptUtil


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.misc;
66
67 import com.jcorporate.expresso.core.security.filters.Filter;
68
69 import java.beans.Introspector JavaDoc;
70 import java.beans.PropertyDescriptor JavaDoc;
71 import java.lang.reflect.Method JavaDoc;
72 import java.sql.Timestamp JavaDoc;
73 import java.util.Date JavaDoc;
74 import java.util.Enumeration JavaDoc;
75 import java.util.Hashtable JavaDoc;
76 import java.util.Vector JavaDoc;
77
78 /*
79  * JavascriptUtil.java
80  *
81  * Copyright 1999, 2002, 2002 Yves Henri AMAIZO.
82  * amy_amaizo@compuserve.com
83  * Class to help encode string for use in javascript.
84  *
85  * @author Yves Henri AMAIZO
86  */

87
88 public class JavascriptUtil {
89
90     static Class JavaDoc synthetic; /* synthetic field */
91     static Filter htmlFilter = new Filter(new String JavaDoc[]{"&", "\"", "<", ">"}, //Strings to replace
92
new String JavaDoc[]{"&amp;", "&quot;", "&lt;", "&gt;"}); //Strings replaced with
93

94     static Filter javascriptFilter = new Filter(new String JavaDoc[]{"\\", "'", "\"", "\n", "%22", "%27", "\r"}, //Strings to replace
95
new String JavaDoc[]{"\\\\", "\\'", "\\\"", "\\n", "\\%22", "\\%27", "\\r"}); //Strings replaced with.
96

97     /**
98      * Constructor.
99      */

100     private JavascriptUtil() {
101     }
102
103     /**
104      * Adds given object to a javascript string
105      *
106      * @param obj the object to add
107      * @param s ?
108      * @param i ?
109      * @param j ?
110      * @return java.lang.String
111      * @todo Change all the string allocations to use the pooled FastStringBuffer
112      */

113     public static final String JavaDoc addToJS(Object JavaDoc obj, String JavaDoc s, int i, int j)
114             throws Throwable JavaDoc {
115         int k = 0;
116         String JavaDoc s1 = "";
117         if ((obj instanceof String JavaDoc)
118                 || (obj instanceof Timestamp JavaDoc)
119                 || (obj instanceof Date JavaDoc)) {
120             s1 = s + " = " + "\"" + toJavaScript(obj.toString().trim()) + "\";\n";
121             return s1;
122         }
123         if ((obj instanceof Number JavaDoc) || (obj instanceof Boolean JavaDoc)) {
124             s1 = s + " = " + obj.toString() + ";\n";
125             return s1;
126         }
127         if (i >= j) {
128             return "";
129         }
130         if (obj instanceof Hashtable JavaDoc) {
131             s1 = s + " = " + "new Object();\n";
132             for (Enumeration JavaDoc enumeration = ((Hashtable JavaDoc) obj).keys();
133                  enumeration.hasMoreElements();
134                     ) {
135
136                 Object JavaDoc obj1 = enumeration.nextElement();
137                 String JavaDoc s3 = s + "." + obj1.toString();
138                 s1 = s1 + addToJS(((Hashtable JavaDoc) obj).get(obj1), s3, i + 1, j);
139             }
140
141             return s1;
142         }
143         if (obj instanceof Vector JavaDoc) {
144             s1 = s + " = " + "new Array();\n";
145             for (Enumeration JavaDoc enumeration1 = ((Vector JavaDoc) obj).elements();
146                  enumeration1.hasMoreElements();
147                     ) {
148                 String JavaDoc s4 = s + "[" + k + "]";
149                 k++;
150                 s1 = s1 + addToJS(enumeration1.nextElement(), s4, i + 1, j);
151             }
152
153             return s1;
154         }
155         if (obj instanceof Object JavaDoc[]) {
156             s1 = s + " = " + "new Array();\n";
157             for (int l = 0; l < ((Object JavaDoc[]) obj).length; l++) {
158                 String JavaDoc s5 = s + "[" + k + "]";
159                 k++;
160                 s1 = s1 + addToJS(((Object JavaDoc[]) obj)[l], s5, i + 1, j);
161             }
162
163             return s1;
164         }
165
166         Class JavaDoc class1 = obj.getClass();
167         Class JavaDoc class2 = class1.getSuperclass();
168         if (class2 == null) {
169             class2 = java.lang.Object JavaDoc.class;
170         }
171         PropertyDescriptor JavaDoc apropertydescriptor[] =
172                 Introspector.getBeanInfo(class1, class2).getPropertyDescriptors();
173         s1 = s + " = " + "new Object();\n";
174         for (int i1 = 0; i1 < apropertydescriptor.length; i1++) {
175             Method JavaDoc method = apropertydescriptor[i1].getReadMethod();
176             if (method != null) {
177                 String JavaDoc s6 = s + "." + apropertydescriptor[i1].getName();
178                 Object JavaDoc obj3 = method.invoke(obj, (Object JavaDoc[]) null);
179                 if (obj3 == null) {
180                     s1 = s1 + s6 + " = \"\";\n";
181                 } else {
182                     s1 = s1 + addToJS(obj3, s6, i + 1, j);
183                 }
184             }
185         }
186
187         return s1;
188     }
189
190     public static final String JavaDoc addToJS(Object JavaDoc obj, String JavaDoc s, int i, int j, Class JavaDoc class1)
191             throws Throwable JavaDoc {
192         int k = 0;
193         String JavaDoc s1 = "";
194         if ((obj instanceof String JavaDoc)
195                 || (obj instanceof Timestamp JavaDoc)
196                 || (obj instanceof Date JavaDoc)) {
197             s1 = s + " = " + "\"" + toJavaScript(obj.toString().trim()) + "\";\n";
198             return s1;
199         }
200         if ((obj instanceof Number JavaDoc) || (obj instanceof Boolean JavaDoc)) {
201             s1 = s + " = " + obj.toString() + ";\n";
202             return s1;
203         }
204         if (i >= j) {
205             return "";
206         }
207         if (obj instanceof Hashtable JavaDoc) {
208             s1 = s + " = " + "new Object();\n";
209             for (Enumeration JavaDoc enumeration = ((Hashtable JavaDoc) obj).keys();
210                  enumeration.hasMoreElements();
211                     ) {
212                 Object JavaDoc obj1 = enumeration.nextElement();
213                 String JavaDoc s3 = s + "." + obj1.toString();
214                 s1 = s1 + addToJS(((Hashtable JavaDoc) obj).get(obj1), s3, i + 1, j);
215             }
216
217             return s1;
218         }
219         if (obj instanceof Vector JavaDoc) {
220             s1 = s + " = " + "new Array();\n";
221             for (Enumeration JavaDoc enumeration1 = ((Vector JavaDoc) obj).elements();
222                  enumeration1.hasMoreElements();
223                     ) {
224                 String JavaDoc s4 = s + "[" + k + "]";
225                 k++;
226                 s1 = s1 + addToJS(enumeration1.nextElement(), s4, i + 1, j);
227             }
228
229             return s1;
230         }
231         if (obj instanceof Object JavaDoc[]) {
232             s1 = s + " = " + "new Array();\n";
233             for (int l = 0; l < ((Object JavaDoc[]) obj).length; l++) {
234                 String JavaDoc s5 = s + "[" + k + "]";
235                 k++;
236                 s1 = s1 + addToJS(((Object JavaDoc[]) obj)[l], s5, i + 1, j);
237             }
238
239             return s1;
240         }
241
242         Class JavaDoc class2 = obj.getClass();
243         if (class1 == null) {
244             class1 = class2.getSuperclass();
245         }
246         if (class1 == null) {
247             class1 = java.lang.Object JavaDoc.class;
248         }
249         PropertyDescriptor JavaDoc apropertydescriptor[] =
250                 Introspector.getBeanInfo(class2, class1).getPropertyDescriptors();
251         s1 = s + " = " + "new Object();\n";
252         for (int i1 = 0; i1 < apropertydescriptor.length; i1++) {
253             Method JavaDoc method = apropertydescriptor[i1].getReadMethod();
254             if (method != null) {
255                 String JavaDoc s6 = s + "." + apropertydescriptor[i1].getName();
256                 Object JavaDoc obj3 = method.invoke(obj, (Object JavaDoc[]) null);
257                 if (obj3 == null) {
258                     s1 = s1 + s6 + " = \"\";\n";
259                 } else {
260                     s1 = s1 + addToJS(obj3, s6, i + 1, j);
261                 }
262             }
263         }
264
265         return s1;
266     }
267
268     /**
269      * Add object to string and return it for use in javascript.
270      *
271      * @param Object
272      * @param String
273      * @param int
274      * @param int
275      * @return String
276      */

277     public static final String JavaDoc addToJSVector(Object JavaDoc obj, String JavaDoc s, int i, int j)
278             throws Throwable JavaDoc {
279         int k = 0;
280         String JavaDoc s1 = "";
281         if ((obj instanceof String JavaDoc)
282                 || (obj instanceof Timestamp JavaDoc)
283                 || (obj instanceof Date JavaDoc)) {
284             s1 = s + " = " + "\"" + toJavaScript(obj.toString().trim()) + "\";";
285             return s1;
286         }
287         if ((obj instanceof Number JavaDoc) || (obj instanceof Boolean JavaDoc)) {
288             s1 = s + " = " + obj.toString() + ";";
289             return s1;
290         }
291         if (i >= j) {
292             return "";
293         }
294         if (obj instanceof Hashtable JavaDoc) {
295             s1 = s + " = " + "new Object();";
296             for (Enumeration JavaDoc enumeration = ((Hashtable JavaDoc) obj).keys();
297                  enumeration.hasMoreElements();
298                     ) {
299                 Object JavaDoc obj1 = enumeration.nextElement();
300                 String JavaDoc s3 = s + "." + obj1.toString();
301                 s1 = s1 + addToJSVector(((Hashtable JavaDoc) obj).get(obj1), s3, i + 1, j);
302             }
303
304             return s1;
305         }
306         if (obj instanceof Vector JavaDoc) {
307             s1 = s + " = " + "new Vector();";
308             for (Enumeration JavaDoc enumeration1 = ((Vector JavaDoc) obj).elements();
309                  enumeration1.hasMoreElements();
310                     ) {
311                 String JavaDoc s4 = "temp";
312                 k++;
313                 s1 = s1 + addToJSVector(enumeration1.nextElement(), s4, i + 1, j);
314                 s1 = s1 + s + ".addElement(temp);";
315             }
316
317             return s1;
318         }
319
320         Class JavaDoc class1 = obj.getClass();
321         Class JavaDoc class2 = class1.getSuperclass();
322         if (class2 == null) {
323             class2 = java.lang.Object JavaDoc.class;
324         }
325         PropertyDescriptor JavaDoc apropertydescriptor[] =
326                 Introspector.getBeanInfo(class1, class2).getPropertyDescriptors();
327         s1 = s + " = " + "new Object();";
328         for (int l = 0; l < apropertydescriptor.length; l++) {
329             Method JavaDoc method = apropertydescriptor[l].getReadMethod();
330             if (method != null) {
331                 String JavaDoc s5 = s + "." + apropertydescriptor[l].getName();
332                 Object JavaDoc obj3 = method.invoke(obj, (Object JavaDoc[]) null);
333                 if (obj3 == null) {
334                     s1 = s1 + s5 + " = \"\";";
335                 } else {
336                     s1 = s1 + addToJSVector(obj3, s5, i + 1, j);
337                 }
338             }
339         }
340
341         return s1;
342     }
343
344     /**
345      * Substituate string s2 for s1 in s.
346      *
347      * @param String
348      * @param String
349      * @param String
350      * @return String
351      */

352     public static String JavaDoc changeString(String JavaDoc s, String JavaDoc s1, String JavaDoc s2) {
353         if (s == null) {
354             return s;
355         }
356         int i = -1;
357         for (int j = s.indexOf(s1); j != -1 && i != 0; i--) {
358             s = s.substring(0, j) + s2 + s.substring(j + s1.length());
359             j = s.indexOf(s1, j + s2.length());
360         }
361
362         return s;
363     }
364
365     /**
366      * encode string in HTML
367      *
368      * @param String
369      * @return String
370      */

371     public static String JavaDoc toHTML(String JavaDoc s) {
372         if (s == null) {
373             return "";
374         } else {
375             return htmlFilter.standardFilter(s);
376 // s = StringUtil.replaceAll(s, "&", "&amp;");
377
// s = StringUtil.replaceAll(s, "\"", "&quot;");
378
// s = StringUtil.replaceAll(s, "<", "&lt;");
379
// s = StringUtil.replaceAll(s, ">", "&gt;");
380
// return s;
381
}
382     }
383
384     /**
385      * Encode string object for use in javascript.
386      *
387      * @param Object
388      * @return String
389      */

390     public static String JavaDoc toJavaScript(Object JavaDoc obj) {
391         return toJavaScript((String JavaDoc) obj);
392     }
393
394     /**
395      * Encode string for use in javascript.
396      *
397      * @param String
398      * @return String
399      */

400     public static String JavaDoc toJavaScript(String JavaDoc s) {
401         if (s == null) {
402             return "";
403         } else {
404             return javascriptFilter.standardFilter(s);
405 // s = StringUtil.replaceAll(s, "\\", "\\\\");
406
// s = StringUtil.replaceAll(s, "'", "\\'");
407
// s = StringUtil.replaceAll(s, "\"", "\\\"");
408
// s = StringUtil.replaceAll(s, "\n", "\\n");
409
// s = StringUtil.replaceAll(s, "%22", "\\%22");
410
// s = StringUtil.replaceAll(s, "%27", "\\%27");
411
// return s;
412
}
413     }
414
415 }
416
Popular Tags