KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > js > Executor


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 package org.lobobrowser.html.js;
22
23 import org.lobobrowser.html.*;
24 import org.lobobrowser.html.domimpl.*;
25 import org.lobobrowser.js.JavaScript;
26 import org.mozilla.javascript.*;
27 import org.w3c.dom.Document JavaDoc;
28 import java.util.logging.*;
29
30 public class Executor {
31     private static final Logger logger = Logger.getLogger(Executor.class.getName());
32
33     /**
34      * This method should be invoked instead of <code>Context.enter</code>.
35      * @param codeSource
36      * @param ucontext
37      */

38     public static Context createContext(java.net.URL JavaDoc codeSource, UserAgentContext ucontext) {
39         Context prev = Context.getCurrentContext();
40         Context ctx = Context.enter();
41         ctx.setOptimizationLevel(ucontext.getScriptingOptimizationLevel());
42         if(prev == null) {
43             // If there was a previous context, this one must be nested.
44
// We still need to create a context because of exit() but
45
// we cannot set a new security controller.
46
ctx.setSecurityController(new SecurityControllerImpl(codeSource, ucontext.getSecurityPolicy()));
47         }
48         return ctx;
49     }
50     
51     public static boolean executeFunction(NodeImpl element, Function f, Event event) {
52         return Executor.executeFunction(element, element, f, event);
53     }
54     
55     public static boolean executeFunction(NodeImpl element, Object JavaDoc thisObject, Function f, Event event) {
56         Document JavaDoc doc = element.getOwnerDocument();
57         if(doc == null) {
58             throw new IllegalStateException JavaDoc("Element does not belong to a document.");
59         }
60         Context ctx = createContext(element.getDocumentURL(), element.getUserAgentContext());
61         try {
62             Scriptable scope = (Scriptable) doc.getUserData(Executor.SCOPE_KEY);
63             if(scope == null) {
64                 throw new IllegalStateException JavaDoc("Scriptable (scope) instance was expected to be keyed as UserData to document using " + Executor.SCOPE_KEY);
65             }
66             JavaScript js = JavaScript.getInstance();
67             Scriptable thisScope = (Scriptable) js.getJavascriptObject(thisObject, scope);
68             try {
69                 Scriptable eventScriptable = (Scriptable) js.getJavascriptObject(event, thisScope);
70                 ScriptableObject.defineProperty(thisScope, "event", eventScriptable, ScriptableObject.READONLY);
71                 Object JavaDoc result = f.call(ctx, thisScope, thisScope, new Object JavaDoc[0]);
72                 if(!(result instanceof Boolean JavaDoc)) {
73                     return true;
74                 }
75                 return ((Boolean JavaDoc) result).booleanValue();
76             } catch(Exception JavaDoc thrown) {
77                 logger.log(Level.WARNING, "executeFunction(): There was an error in Javascript code.", thrown);
78                 return true;
79             }
80         } finally {
81             Context.exit();
82         }
83     }
84
85     public static boolean executeFunction(Scriptable thisScope, Function f, java.net.URL JavaDoc codeSource, UserAgentContext ucontext) {
86         Context ctx = createContext(codeSource, ucontext);
87         try {
88             try {
89                 Object JavaDoc result = f.call(ctx, thisScope, thisScope, new Object JavaDoc[0]);
90                 if(!(result instanceof Boolean JavaDoc)) {
91                     return true;
92                 }
93                 return ((Boolean JavaDoc) result).booleanValue();
94             } catch(Exception JavaDoc err) {
95                 logger.log(Level.WARNING, "executeFunction(): Unable to execute Javascript function " + f.getClassName() + ".", err);
96                 return true;
97             }
98         } finally {
99             Context.exit();
100         }
101     }
102
103     /**
104      * A document <code>UserData</code> key used
105      * to map Javascript scope in the HTML document.
106      */

107     public static final String JavaDoc SCOPE_KEY = "cobra.js.scope";
108 }
109
Popular Tags