KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > script > rhino > WindowWrapper


1 /*
2
3    Copyright 1999-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17 */

18
19 package org.apache.batik.script.rhino;
20
21 import java.security.AccessControlContext JavaDoc;
22 import java.security.AccessController JavaDoc;
23 import java.security.PrivilegedAction JavaDoc;
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 JavaDoc;
36
37 /**
38  * This class wraps a Window object to expose it to the interpreter.
39  * This will be the Global Object of our interpreter.
40  *
41  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
42  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
43  * @version $Id: WindowWrapper.java,v 1.22 2004/11/18 01:47:01 deweese Exp $
44  */

45 public class WindowWrapper extends ImporterTopLevel {
46
47     private final static Object JavaDoc[] EMPTY_ARGUMENTS = new Object JavaDoc[0];
48
49     /**
50      * Creates a new WindowWrapper.
51      */

52     public WindowWrapper(Context JavaDoc context) {
53         super(context);
54         String JavaDoc[] 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 JavaDoc(); // should never happen
61
}
62     }
63
64     public String JavaDoc getClassName() {
65         return "Window";
66     }
67
68     public String JavaDoc toString() {
69         return "[object Window]";
70     }
71
72     /**
73      * Wraps the 'setInterval' methods of the Window interface.
74      */

75     public static Object JavaDoc setInterval(Context JavaDoc cx,
76                                      Scriptable thisObj,
77                                      Object JavaDoc[] 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 JavaDoc)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 JavaDoc script =
95       (String JavaDoc)Context.toType(args[0], String JavaDoc.class);
96         return Context.toObject(window.setInterval(script, to), thisObj);
97     }
98
99     /**
100      * Wraps the 'setTimeout' methods of the Window interface.
101      */

102     public static Object JavaDoc setTimeout(Context JavaDoc cx,
103                                     Scriptable thisObj,
104                                     Object JavaDoc[] 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 JavaDoc)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 JavaDoc script =
122             (String JavaDoc)Context.toType(args[0], String JavaDoc.class);
123         return Context.toObject(window.setTimeout(script, to), thisObj);
124     }
125
126     /**
127      * Wraps the 'clearInterval' method of the Window interface.
128      */

129     public static void clearInterval(Context JavaDoc cx,
130                                      Scriptable thisObj,
131                                      Object JavaDoc[] 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 JavaDoc.class));
138         }
139     }
140
141     /**
142      * Wraps the 'clearTimeout' method of the Window interface.
143      */

144     public static void clearTimeout(Context JavaDoc cx,
145                                     Scriptable thisObj,
146                                     Object JavaDoc[] 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 JavaDoc.class));
153         }
154     }
155
156     /**
157      * Wraps the 'parseXML' method of the Window interface.
158      */

159     public static Object JavaDoc parseXML(Context JavaDoc cx,
160                                   Scriptable thisObj,
161                                   final Object JavaDoc[] 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 JavaDoc acc =
172             ((RhinoInterpreter)window.getInterpreter()).getAccessControlContext();
173
174         Object JavaDoc ret = AccessController.doPrivileged( new PrivilegedAction JavaDoc() {
175                 public Object JavaDoc run() {
176                     return window.parseXML
177                         ((String JavaDoc)Context.toType(args[0], String JavaDoc.class),
178                          (Document JavaDoc)Context.toType(args[1], Document JavaDoc.class));
179                 }
180             }, acc);
181         return Context.toObject(ret, thisObj);
182     }
183
184     /**
185      * Wraps the 'getURL' method of the Window interface.
186      */

187     public static void getURL(Context JavaDoc cx,
188                               Scriptable thisObj,
189                               final Object JavaDoc[] 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 JavaDoc uri = (String JavaDoc)Context.toType(args[0], String JavaDoc.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 JavaDoc acc =
212             ((RhinoInterpreter)window.getInterpreter()).getAccessControlContext();
213
214         if (len == 2) {
215             AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
216                     public Object JavaDoc run(){
217                         window.getURL(uri, fw);
218                         return null;
219                     }
220                 }, acc);
221         } else {
222             AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
223                     public Object JavaDoc run() {
224                         window.getURL
225                             (uri, fw,
226                              (String JavaDoc)Context.toType(args[2], String JavaDoc.class));
227                         return null;
228                     }
229                 }, acc);
230         }
231     }
232
233     /**
234      * Wraps the 'postURL' method of the Window interface.
235      */

236     public static void postURL(Context JavaDoc cx,
237                                Scriptable thisObj,
238                                final Object JavaDoc[] 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 JavaDoc uri = (String JavaDoc)Context.toType(args[0], String JavaDoc.class);
250         final String JavaDoc content = (String JavaDoc)Context.toType(args[1], String JavaDoc.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 JavaDoc acc;
262         acc = interp.getAccessControlContext();
263
264         switch (len) {
265         case 3:
266             AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
267                     public Object JavaDoc run(){
268                         window.postURL(uri, content, fw);
269                         return null;
270                     }
271                 }, acc);
272         case 4:
273             AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
274                     public Object JavaDoc run() {
275                         window.postURL
276                             (uri, content, fw,
277                              (String JavaDoc)Context.toType(args[3], String JavaDoc.class));
278                         return null;
279                     }
280                 }, acc);
281         default:
282             AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
283                     public Object JavaDoc run() {
284                         window.postURL
285                             (uri, content, fw,
286                              (String JavaDoc)Context.toType(args[3], String JavaDoc.class),
287                              (String JavaDoc)Context.toType(args[4], String JavaDoc.class));
288                         return null;
289                     }
290                 }, acc);
291         }
292     }
293
294     /**
295      * Wraps the 'alert' method of the Window interface.
296      */

297     public static void alert(Context JavaDoc cx,
298                              Scriptable thisObj,
299                              Object JavaDoc[] 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 JavaDoc message =
306                 (String JavaDoc)Context.toType(args[0], String JavaDoc.class);
307             window.alert(message);
308         }
309     }
310
311     /**
312      * Wraps the 'confirm' method of the Window interface.
313      */

314     public static Object JavaDoc confirm(Context JavaDoc cx,
315                                   Scriptable thisObj,
316                                   Object JavaDoc[] 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 JavaDoc message =
323                 (String JavaDoc)Context.toType(args[0], String JavaDoc.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     /**
333      * Wraps the 'prompt' method of the Window interface.
334      */

335     public static Object JavaDoc prompt(Context JavaDoc cx,
336                                 Scriptable thisObj,
337                                 Object JavaDoc[] 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 JavaDoc message =
348                 (String JavaDoc)Context.toType(args[0], String JavaDoc.class);
349             return Context.toObject(window.prompt(message), thisObj);
350
351         default:
352             message =
353                 (String JavaDoc)Context.toType(args[0], String JavaDoc.class);
354             String JavaDoc defVal =
355                 (String JavaDoc)Context.toType(args[1], String JavaDoc.class);
356             return Context.toObject(window.prompt(message, defVal), thisObj);
357         }
358     }
359
360     /**
361      * To wrap a function in an handler.
362      */

363     protected static class FunctionWrapper implements Runnable JavaDoc {
364
365         /**
366          * The current interpreter.
367          */

368         protected RhinoInterpreter interpreter;
369
370         /**
371          * The function wrapper.
372          */

373         protected Function function;
374
375         /**
376          * The arguments.
377          */

378         protected Object JavaDoc[] arguments;
379
380         /**
381          * Creates a function wrapper.
382          */

383         public FunctionWrapper(RhinoInterpreter ri,
384                                Function f,
385                                Object JavaDoc[] args) {
386             interpreter = ri;
387             function = f;
388             arguments = args;
389         }
390
391         /**
392          * Calls the function.
393          */

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     /**
404      * To wrap a function passed to getURL().
405      */

406     protected static class GetURLFunctionWrapper
407         implements Window.URLResponseHandler {
408
409         /**
410          * The current interpreter.
411          */

412         protected RhinoInterpreter interpreter;
413
414         /**
415          * The function wrapper.
416          */

417         protected Function function;
418
419         /**
420          * The Scope for callback
421          */

422         protected ScriptableObject scope;
423
424         /**
425          * Creates a wrapper.
426          */

427         public GetURLFunctionWrapper(RhinoInterpreter ri, Function fct,
428                                      ScriptableObject sc) {
429             interpreter = ri;
430             function = fct;
431             scope = sc;
432         }
433
434         /**
435          * Called before 'getURL()' returns.
436          * @param success Whether the data was successfully retreived.
437          * @param mime The data MIME type.
438          * @param content The data.
439          */

440         public void getURLDone(final boolean success,
441                                final String JavaDoc mime,
442                                final String JavaDoc 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     /**
454      * To wrap an object passed to getURL().
455      */

456     private static class GetURLObjectWrapper
457         implements Window.URLResponseHandler {
458
459         /**
460          * The current interpreter.
461          */

462         private RhinoInterpreter interpreter;
463
464         /**
465          * The object wrapper.
466          */

467         private ScriptableObject object;
468
469         /**
470          * The Scope for the callback.
471          */

472         private ScriptableObject scope;
473
474         private static final String JavaDoc COMPLETE = "operationComplete";
475
476         /**
477          * Creates a wrapper.
478          */

479         public GetURLObjectWrapper(RhinoInterpreter ri,
480                                    ScriptableObject obj,
481                                    ScriptableObject sc) {
482             interpreter = ri;
483             object = obj;
484             scope = sc;
485         }
486
487         /**
488          * Called before 'getURL()' returns.
489          * @param success Whether the data was successfully retreived.
490          * @param mime The data MIME type.
491          * @param content The data.
492          */

493         public void getURLDone(final boolean success,
494                                final String JavaDoc mime,
495                                final String JavaDoc 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 JavaDoc mime, content;
511         ScriptableObject scope;
512         public GetURLDoneArgBuilder(boolean success,
513                                     String JavaDoc mime, String JavaDoc 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 JavaDoc[] 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 JavaDoc [] { so };
534         }
535     }
536     
537 }
538
Popular Tags