1 64 65 package com.jcorporate.expresso.core.misc; 66 67 import com.jcorporate.expresso.core.security.filters.Filter; 68 69 import java.beans.Introspector ; 70 import java.beans.PropertyDescriptor ; 71 import java.lang.reflect.Method ; 72 import java.sql.Timestamp ; 73 import java.util.Date ; 74 import java.util.Enumeration ; 75 import java.util.Hashtable ; 76 import java.util.Vector ; 77 78 87 88 public class JavascriptUtil { 89 90 static Class synthetic; 91 static Filter htmlFilter = new Filter(new String []{"&", "\"", "<", ">"}, new String []{"&", """, "<", ">"}); 94 static Filter javascriptFilter = new Filter(new String []{"\\", "'", "\"", "\n", "%22", "%27", "\r"}, new String []{"\\\\", "\\'", "\\\"", "\\n", "\\%22", "\\%27", "\\r"}); 97 100 private JavascriptUtil() { 101 } 102 103 113 public static final String addToJS(Object obj, String s, int i, int j) 114 throws Throwable { 115 int k = 0; 116 String s1 = ""; 117 if ((obj instanceof String ) 118 || (obj instanceof Timestamp ) 119 || (obj instanceof Date )) { 120 s1 = s + " = " + "\"" + toJavaScript(obj.toString().trim()) + "\";\n"; 121 return s1; 122 } 123 if ((obj instanceof Number ) || (obj instanceof Boolean )) { 124 s1 = s + " = " + obj.toString() + ";\n"; 125 return s1; 126 } 127 if (i >= j) { 128 return ""; 129 } 130 if (obj instanceof Hashtable ) { 131 s1 = s + " = " + "new Object();\n"; 132 for (Enumeration enumeration = ((Hashtable ) obj).keys(); 133 enumeration.hasMoreElements(); 134 ) { 135 136 Object obj1 = enumeration.nextElement(); 137 String s3 = s + "." + obj1.toString(); 138 s1 = s1 + addToJS(((Hashtable ) obj).get(obj1), s3, i + 1, j); 139 } 140 141 return s1; 142 } 143 if (obj instanceof Vector ) { 144 s1 = s + " = " + "new Array();\n"; 145 for (Enumeration enumeration1 = ((Vector ) obj).elements(); 146 enumeration1.hasMoreElements(); 147 ) { 148 String 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 []) { 156 s1 = s + " = " + "new Array();\n"; 157 for (int l = 0; l < ((Object []) obj).length; l++) { 158 String s5 = s + "[" + k + "]"; 159 k++; 160 s1 = s1 + addToJS(((Object []) obj)[l], s5, i + 1, j); 161 } 162 163 return s1; 164 } 165 166 Class class1 = obj.getClass(); 167 Class class2 = class1.getSuperclass(); 168 if (class2 == null) { 169 class2 = java.lang.Object .class; 170 } 171 PropertyDescriptor apropertydescriptor[] = 172 Introspector.getBeanInfo(class1, class2).getPropertyDescriptors(); 173 s1 = s + " = " + "new Object();\n"; 174 for (int i1 = 0; i1 < apropertydescriptor.length; i1++) { 175 Method method = apropertydescriptor[i1].getReadMethod(); 176 if (method != null) { 177 String s6 = s + "." + apropertydescriptor[i1].getName(); 178 Object obj3 = method.invoke(obj, (Object []) 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 addToJS(Object obj, String s, int i, int j, Class class1) 191 throws Throwable { 192 int k = 0; 193 String s1 = ""; 194 if ((obj instanceof String ) 195 || (obj instanceof Timestamp ) 196 || (obj instanceof Date )) { 197 s1 = s + " = " + "\"" + toJavaScript(obj.toString().trim()) + "\";\n"; 198 return s1; 199 } 200 if ((obj instanceof Number ) || (obj instanceof Boolean )) { 201 s1 = s + " = " + obj.toString() + ";\n"; 202 return s1; 203 } 204 if (i >= j) { 205 return ""; 206 } 207 if (obj instanceof Hashtable ) { 208 s1 = s + " = " + "new Object();\n"; 209 for (Enumeration enumeration = ((Hashtable ) obj).keys(); 210 enumeration.hasMoreElements(); 211 ) { 212 Object obj1 = enumeration.nextElement(); 213 String s3 = s + "." + obj1.toString(); 214 s1 = s1 + addToJS(((Hashtable ) obj).get(obj1), s3, i + 1, j); 215 } 216 217 return s1; 218 } 219 if (obj instanceof Vector ) { 220 s1 = s + " = " + "new Array();\n"; 221 for (Enumeration enumeration1 = ((Vector ) obj).elements(); 222 enumeration1.hasMoreElements(); 223 ) { 224 String 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 []) { 232 s1 = s + " = " + "new Array();\n"; 233 for (int l = 0; l < ((Object []) obj).length; l++) { 234 String s5 = s + "[" + k + "]"; 235 k++; 236 s1 = s1 + addToJS(((Object []) obj)[l], s5, i + 1, j); 237 } 238 239 return s1; 240 } 241 242 Class class2 = obj.getClass(); 243 if (class1 == null) { 244 class1 = class2.getSuperclass(); 245 } 246 if (class1 == null) { 247 class1 = java.lang.Object .class; 248 } 249 PropertyDescriptor apropertydescriptor[] = 250 Introspector.getBeanInfo(class2, class1).getPropertyDescriptors(); 251 s1 = s + " = " + "new Object();\n"; 252 for (int i1 = 0; i1 < apropertydescriptor.length; i1++) { 253 Method method = apropertydescriptor[i1].getReadMethod(); 254 if (method != null) { 255 String s6 = s + "." + apropertydescriptor[i1].getName(); 256 Object obj3 = method.invoke(obj, (Object []) 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 277 public static final String addToJSVector(Object obj, String s, int i, int j) 278 throws Throwable { 279 int k = 0; 280 String s1 = ""; 281 if ((obj instanceof String ) 282 || (obj instanceof Timestamp ) 283 || (obj instanceof Date )) { 284 s1 = s + " = " + "\"" + toJavaScript(obj.toString().trim()) + "\";"; 285 return s1; 286 } 287 if ((obj instanceof Number ) || (obj instanceof Boolean )) { 288 s1 = s + " = " + obj.toString() + ";"; 289 return s1; 290 } 291 if (i >= j) { 292 return ""; 293 } 294 if (obj instanceof Hashtable ) { 295 s1 = s + " = " + "new Object();"; 296 for (Enumeration enumeration = ((Hashtable ) obj).keys(); 297 enumeration.hasMoreElements(); 298 ) { 299 Object obj1 = enumeration.nextElement(); 300 String s3 = s + "." + obj1.toString(); 301 s1 = s1 + addToJSVector(((Hashtable ) obj).get(obj1), s3, i + 1, j); 302 } 303 304 return s1; 305 } 306 if (obj instanceof Vector ) { 307 s1 = s + " = " + "new Vector();"; 308 for (Enumeration enumeration1 = ((Vector ) obj).elements(); 309 enumeration1.hasMoreElements(); 310 ) { 311 String 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 class1 = obj.getClass(); 321 Class class2 = class1.getSuperclass(); 322 if (class2 == null) { 323 class2 = java.lang.Object .class; 324 } 325 PropertyDescriptor apropertydescriptor[] = 326 Introspector.getBeanInfo(class1, class2).getPropertyDescriptors(); 327 s1 = s + " = " + "new Object();"; 328 for (int l = 0; l < apropertydescriptor.length; l++) { 329 Method method = apropertydescriptor[l].getReadMethod(); 330 if (method != null) { 331 String s5 = s + "." + apropertydescriptor[l].getName(); 332 Object obj3 = method.invoke(obj, (Object []) 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 352 public static String changeString(String s, String s1, String 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 371 public static String toHTML(String s) { 372 if (s == null) { 373 return ""; 374 } else { 375 return htmlFilter.standardFilter(s); 376 } 382 } 383 384 390 public static String toJavaScript(Object obj) { 391 return toJavaScript((String ) obj); 392 } 393 394 400 public static String toJavaScript(String s) { 401 if (s == null) { 402 return ""; 403 } else { 404 return javascriptFilter.standardFilter(s); 405 } 413 } 414 415 } 416 | Popular Tags |