1 18 19 package org.apache.batik.script.rhino; 20 21 import java.security.AccessControlContext ; 22 import java.security.AccessController ; 23 import java.security.PrivilegedAction ; 24 25 import org.apache.batik.script.Window; 26 import org.mozilla.javascript.Context; 27 import org.mozilla.javascript.Function; 28 import org.mozilla.javascript.ImporterTopLevel; 29 import org.mozilla.javascript.JavaScriptException; 30 import org.mozilla.javascript.NativeObject; 31 import org.mozilla.javascript.PropertyException; 32 import org.mozilla.javascript.Scriptable; 33 import org.mozilla.javascript.ScriptableObject; 34 import org.mozilla.javascript.WrappedException; 35 import org.w3c.dom.Document ; 36 37 45 public class WindowWrapper extends ImporterTopLevel { 46 47 private final static Object [] EMPTY_ARGUMENTS = new Object [0]; 48 49 52 public WindowWrapper(Context context) { 53 super(context); 54 String [] names = { "setInterval", "setTimeout", "clearInterval", "clearTimeout", 55 "parseXML", "getURL", "alert", "confirm", "prompt" }; 56 try { 57 this.defineFunctionProperties(names, WindowWrapper.class, 58 ScriptableObject.DONTENUM); 59 } catch (PropertyException e) { 60 throw new Error (); } 62 } 63 64 public String getClassName() { 65 return "Window"; 66 } 67 68 public String toString() { 69 return "[object Window]"; 70 } 71 72 75 public static Object setInterval(Context cx, 76 Scriptable thisObj, 77 Object [] args, 78 Function funObj) 79 throws JavaScriptException { 80 int len = args.length; 81 Window window = ((RhinoInterpreter.ExtendedContext)cx).getWindow(); 82 if (len < 2) { 83 throw Context.reportRuntimeError("invalid argument count"); 84 } 85 long to = ((Long )Context.toType(args[1], Long.TYPE)).longValue(); 86 if (args[0] instanceof Function) { 87 RhinoInterpreter interp = 88 (RhinoInterpreter)window.getInterpreter(); 89 FunctionWrapper fw; 90 fw = new FunctionWrapper(interp, (Function)args[0], 91 EMPTY_ARGUMENTS); 92 return Context.toObject(window.setInterval(fw, to), thisObj); 93 } 94 String script = 95 (String )Context.toType(args[0], String .class); 96 return Context.toObject(window.setInterval(script, to), thisObj); 97 } 98 99 102 public static Object setTimeout(Context cx, 103 Scriptable thisObj, 104 Object [] args, 105 Function funObj) 106 throws JavaScriptException { 107 int len = args.length; 108 Window window = ((RhinoInterpreter.ExtendedContext)cx).getWindow(); 109 if (len < 2) { 110 throw Context.reportRuntimeError("invalid argument count"); 111 } 112 long to = ((Long )Context.toType(args[1], Long.TYPE)).longValue(); 113 if (args[0] instanceof Function) { 114 RhinoInterpreter interp = 115 (RhinoInterpreter)window.getInterpreter(); 116 FunctionWrapper fw; 117 fw = new FunctionWrapper(interp, (Function)args[0], 118 EMPTY_ARGUMENTS); 119 return Context.toObject(window.setTimeout(fw, to), thisObj); 120 } 121 String script = 122 (String )Context.toType(args[0], String .class); 123 return Context.toObject(window.setTimeout(script, to), thisObj); 124 } 125 126 129 public static void clearInterval(Context cx, 130 Scriptable thisObj, 131 Object [] args, 132 Function funObj) 133 throws JavaScriptException { 134 int len = args.length; 135 Window window = ((RhinoInterpreter.ExtendedContext)cx).getWindow(); 136 if (len >= 1) { 137 window.clearInterval(Context.toType(args[0], Object .class)); 138 } 139 } 140 141 144 public static void clearTimeout(Context cx, 145 Scriptable thisObj, 146 Object [] args, 147 Function funObj) 148 throws JavaScriptException { 149 int len = args.length; 150 Window window = ((RhinoInterpreter.ExtendedContext)cx).getWindow(); 151 if (len >= 1) { 152 window.clearTimeout(Context.toType(args[0], Object .class)); 153 } 154 } 155 156 159 public static Object parseXML(Context cx, 160 Scriptable thisObj, 161 final Object [] args, 162 Function funObj) 163 throws JavaScriptException { 164 int len = args.length; 165 final Window window = 166 ((RhinoInterpreter.ExtendedContext)cx).getWindow(); 167 if (len < 2) { 168 throw Context.reportRuntimeError("invalid argument count"); 169 } 170 171 AccessControlContext acc = 172 ((RhinoInterpreter)window.getInterpreter()).getAccessControlContext(); 173 174 Object ret = AccessController.doPrivileged( new PrivilegedAction () { 175 public Object run() { 176 return window.parseXML 177 ((String )Context.toType(args[0], String .class), 178 (Document )Context.toType(args[1], Document .class)); 179 } 180 }, acc); 181 return Context.toObject(ret, thisObj); 182 } 183 184 187 public static void getURL(Context cx, 188 Scriptable thisObj, 189 final Object [] args, 190 Function funObj) 191 throws JavaScriptException { 192 int len = args.length; 193 final Window window = ((RhinoInterpreter.ExtendedContext)cx).getWindow(); 194 final ScriptableObject go = ((RhinoInterpreter.ExtendedContext)cx).getGlobalObject(); 195 if (len < 2) { 196 throw Context.reportRuntimeError("invalid argument count"); 197 } 198 RhinoInterpreter interp = 199 (RhinoInterpreter)window.getInterpreter(); 200 final String uri = (String )Context.toType(args[0], String .class); 201 Window.URLResponseHandler urlHandler = null; 202 if (args[1] instanceof Function) { 203 urlHandler = new GetURLFunctionWrapper 204 (interp, (Function)args[1], go); 205 } else { 206 urlHandler = new GetURLObjectWrapper 207 (interp, (NativeObject)args[1], go); 208 } 209 final Window.URLResponseHandler fw = urlHandler; 210 211 AccessControlContext acc = 212 ((RhinoInterpreter)window.getInterpreter()).getAccessControlContext(); 213 214 if (len == 2) { 215 AccessController.doPrivileged(new PrivilegedAction () { 216 public Object run(){ 217 window.getURL(uri, fw); 218 return null; 219 } 220 }, acc); 221 } else { 222 AccessController.doPrivileged(new PrivilegedAction () { 223 public Object run() { 224 window.getURL 225 (uri, fw, 226 (String )Context.toType(args[2], String .class)); 227 return null; 228 } 229 }, acc); 230 } 231 } 232 233 236 public static void postURL(Context cx, 237 Scriptable thisObj, 238 final Object [] args, 239 Function funObj) 240 throws JavaScriptException { 241 int len = args.length; 242 final Window window = ((RhinoInterpreter.ExtendedContext)cx).getWindow(); 243 final ScriptableObject go = ((RhinoInterpreter.ExtendedContext)cx).getGlobalObject(); 244 if (len < 3) { 245 throw Context.reportRuntimeError("invalid argument count"); 246 } 247 RhinoInterpreter interp = 248 (RhinoInterpreter)window.getInterpreter(); 249 final String uri = (String )Context.toType(args[0], String .class); 250 final String content = (String )Context.toType(args[1], String .class); 251 Window.URLResponseHandler urlHandler = null; 252 if (args[2] instanceof Function) { 253 urlHandler = new GetURLFunctionWrapper 254 (interp, (Function)args[2], go); 255 } else { 256 urlHandler = new GetURLObjectWrapper 257 (interp, (NativeObject)args[2], go); 258 } 259 final Window.URLResponseHandler fw = urlHandler; 260 261 AccessControlContext acc; 262 acc = interp.getAccessControlContext(); 263 264 switch (len) { 265 case 3: 266 AccessController.doPrivileged(new PrivilegedAction () { 267 public Object run(){ 268 window.postURL(uri, content, fw); 269 return null; 270 } 271 }, acc); 272 case 4: 273 AccessController.doPrivileged(new PrivilegedAction () { 274 public Object run() { 275 window.postURL 276 (uri, content, fw, 277 (String )Context.toType(args[3], String .class)); 278 return null; 279 } 280 }, acc); 281 default: 282 AccessController.doPrivileged(new PrivilegedAction () { 283 public Object run() { 284 window.postURL 285 (uri, content, fw, 286 (String )Context.toType(args[3], String .class), 287 (String )Context.toType(args[4], String .class)); 288 return null; 289 } 290 }, acc); 291 } 292 } 293 294 297 public static void alert(Context cx, 298 Scriptable thisObj, 299 Object [] args, 300 Function funObj) 301 throws JavaScriptException { 302 int len = args.length; 303 Window window = ((RhinoInterpreter.ExtendedContext)cx).getWindow(); 304 if (len >= 1) { 305 String message = 306 (String )Context.toType(args[0], String .class); 307 window.alert(message); 308 } 309 } 310 311 314 public static Object confirm(Context cx, 315 Scriptable thisObj, 316 Object [] args, 317 Function funObj) 318 throws JavaScriptException { 319 int len = args.length; 320 Window window = ((RhinoInterpreter.ExtendedContext)cx).getWindow(); 321 if (len >= 1) { 322 String message = 323 (String )Context.toType(args[0], String .class); 324 if (window.confirm(message)) 325 return Context.toObject(Boolean.TRUE, thisObj); 326 else 327 return Context.toObject(Boolean.FALSE, thisObj); 328 } 329 return Context.toObject(Boolean.FALSE, thisObj); 330 } 331 332 335 public static Object prompt(Context cx, 336 Scriptable thisObj, 337 Object [] args, 338 Function funObj) 339 throws JavaScriptException { 340 int len = args.length; 341 Window window = ((RhinoInterpreter.ExtendedContext)cx).getWindow(); 342 switch (len) { 343 case 0: 344 return Context.toObject("", thisObj); 345 346 case 1: 347 String message = 348 (String )Context.toType(args[0], String .class); 349 return Context.toObject(window.prompt(message), thisObj); 350 351 default: 352 message = 353 (String )Context.toType(args[0], String .class); 354 String defVal = 355 (String )Context.toType(args[1], String .class); 356 return Context.toObject(window.prompt(message, defVal), thisObj); 357 } 358 } 359 360 363 protected static class FunctionWrapper implements Runnable { 364 365 368 protected RhinoInterpreter interpreter; 369 370 373 protected Function function; 374 375 378 protected Object [] arguments; 379 380 383 public FunctionWrapper(RhinoInterpreter ri, 384 Function f, 385 Object [] args) { 386 interpreter = ri; 387 function = f; 388 arguments = args; 389 } 390 391 394 public void run() { 395 try { 396 interpreter.callHandler(function, arguments); 397 } catch (JavaScriptException e) { 398 throw new WrappedException(e); 399 } 400 } 401 } 402 403 406 protected static class GetURLFunctionWrapper 407 implements Window.URLResponseHandler { 408 409 412 protected RhinoInterpreter interpreter; 413 414 417 protected Function function; 418 419 422 protected ScriptableObject scope; 423 424 427 public GetURLFunctionWrapper(RhinoInterpreter ri, Function fct, 428 ScriptableObject sc) { 429 interpreter = ri; 430 function = fct; 431 scope = sc; 432 } 433 434 440 public void getURLDone(final boolean success, 441 final String mime, 442 final String content) { 443 try { 444 interpreter.callHandler 445 (function, 446 new GetURLDoneArgBuilder(success, mime, content, scope)); 447 } catch (JavaScriptException e) { 448 throw new WrappedException(e); 449 } 450 } 451 } 452 453 456 private static class GetURLObjectWrapper 457 implements Window.URLResponseHandler { 458 459 462 private RhinoInterpreter interpreter; 463 464 467 private ScriptableObject object; 468 469 472 private ScriptableObject scope; 473 474 private static final String COMPLETE = "operationComplete"; 475 476 479 public GetURLObjectWrapper(RhinoInterpreter ri, 480 ScriptableObject obj, 481 ScriptableObject sc) { 482 interpreter = ri; 483 object = obj; 484 scope = sc; 485 } 486 487 493 public void getURLDone(final boolean success, 494 final String mime, 495 final String content) { 496 try { 497 interpreter.callMethod 498 (object, COMPLETE, 499 new GetURLDoneArgBuilder(success, mime, content, scope)); 500 } catch (JavaScriptException e) { 501 Context.exit(); 502 throw new WrappedException(e); 503 } 504 } 505 } 506 507 static class GetURLDoneArgBuilder 508 implements RhinoInterpreter.ArgumentsBuilder { 509 boolean success; 510 String mime, content; 511 ScriptableObject scope; 512 public GetURLDoneArgBuilder(boolean success, 513 String mime, String content, 514 ScriptableObject scope) { 515 this.success = success; 516 this.mime = mime; 517 this.content = content; 518 this.scope = scope; 519 } 520 521 public Object [] buildArguments() { 522 ScriptableObject so = new NativeObject(); 523 so.put("success", so, 524 (success) ? Boolean.TRUE : Boolean.FALSE); 525 if (mime != null) { 526 so.put("contentType", so, 527 Context.toObject(mime, scope)); 528 } 529 if (content != null) { 530 so.put("content", so, 531 Context.toObject(content, scope)); 532 } 533 return new Object [] { so }; 534 } 535 } 536 537 } 538 | Popular Tags |