KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > ScriptEngine


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit;
39
40 import com.gargoylesoftware.htmlunit.html.HtmlElement;
41 import com.gargoylesoftware.htmlunit.html.HtmlPage;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45 /**
46  * An abstract base class for scripting engines.
47  *
48  * @version $Revision: 100 $
49  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
50  * @author <a HREF="mailto:bcurren@esomnie.com">Ben Curren</a>
51  * @author Marc Guillemot
52  */

53 public abstract class ScriptEngine {
54     private final WebClient webClient_;
55     private final Log scriptEngineLog_ = LogFactory.getLog(ScriptEngine.class);
56
57     /**
58      * Create an instance for the specifed web client
59      * @param webClient The web client.
60      */

61     protected ScriptEngine( final WebClient webClient ) {
62         Assert.notNull("webClient", webClient);
63         webClient_ = webClient;
64     }
65
66
67     /**
68      * Return the web client that this engine is associated with.
69      * @return The web client.
70      */

71     public final WebClient getWebClient() {
72         return webClient_;
73     }
74
75
76     /**
77      * Throw a NullPointerException with the specified description if the object is null.
78      * @param description The description
79      * @param object The object to check for null.
80      * @deprecated Use {@link Assert#notNull(String,Object)}
81      */

82     protected final void assertNotNull( final String JavaDoc description, final Object JavaDoc object ) {
83         Assert.notNull(description, object);
84     }
85
86     /**
87      * perform initialization for the given page
88      * @param page the page to initialize for
89      */

90     public abstract void initialize(final HtmlPage page);
91
92     /**
93      * Execute the specified source code in the context of the given page.
94      * @param htmlPage The page
95      * @param sourceCode The code to execute.
96      * @param sourceName A name for the chunk of code that is going to be executed. This will be
97      * used in error messages.
98      * @return The result of executing the specified code
99      */

100     public Object JavaDoc execute(
101             final HtmlPage htmlPage, final String JavaDoc sourceCode, final String JavaDoc sourceName ) {
102
103         return execute(htmlPage, sourceCode, sourceName, null);
104     }
105
106
107     /**
108      * Execute the specified source code in the context of the given page.
109      * @param htmlPage The page
110      * @param sourceCode The code to execute.
111      * @param sourceName A name for the chunk of code that is going to be executed. This will be
112      * used in error messages.
113      * @param htmlElement The html element that will act as the context.
114      * @return The result of executing the specified code
115      */

116     public abstract Object JavaDoc execute(
117         final HtmlPage htmlPage, final String JavaDoc sourceCode, final String JavaDoc sourceName, final HtmlElement htmlElement );
118
119
120     /**
121      * Call a JavaScript function and return the result.
122      * @param htmlPage The page
123      * @param javaScriptFunction The function to call.
124      * @param thisObject The this object for class method calls.
125      * @param args The list of arguments to pass to the function.
126      * @param htmlElementScope The html element that will act as the context.
127      * @return The result of the function call.
128      */

129     public abstract Object JavaDoc callFunction(
130         final HtmlPage htmlPage,
131         final Object JavaDoc javaScriptFunction,
132         final Object JavaDoc thisObject,
133         final Object JavaDoc [] args,
134         final HtmlElement htmlElementScope );
135
136
137     /**
138      * Return the string representation of the JavaScript object in the context of the given page.
139      * @param htmlPage The page
140      * @param javaScriptObject The object to represent at a string.
141      * @return The result string.
142      */

143     public abstract String JavaDoc toString(
144         final HtmlPage htmlPage, final Object JavaDoc javaScriptObject );
145
146
147     /**
148      * Return the log object that is being used to log information about the script engine.
149      * @return The log
150      */

151     public Log getScriptEngineLog() {
152         return scriptEngineLog_;
153     }
154     
155     /**
156      * Pre process the specified source code in the context of the given page using the processor specified
157      * in the webclient. This method delegates to the pre processor handler specified in the
158      * <code>WebClient</code>. If no pre processor handler is defined, the original source code is returned
159      * unchanged.
160      * @param htmlPage The page
161      * @param sourceCode The code to process.
162      * @param sourceName A name for the chunk of code. This will be used in error messages.
163      * @param htmlElement The html element that will act as the context.
164      * @return The source code after being pre processed
165      * @see com.gargoylesoftware.htmlunit.ScriptPreProcessor
166      */

167     public String JavaDoc preProcess(
168         final HtmlPage htmlPage, final String JavaDoc sourceCode, final String JavaDoc sourceName, final HtmlElement htmlElement ) {
169
170         String JavaDoc newSourceCode = sourceCode;
171         final ScriptPreProcessor preProcessor = getWebClient().getScriptPreProcessor();
172         if ( preProcessor != null ) {
173             newSourceCode = preProcessor.preProcess(htmlPage, sourceCode, sourceName, htmlElement);
174             if ( newSourceCode == null ) {
175                 newSourceCode = "";
176             }
177         }
178         return newSourceCode;
179     }
180
181     /**
182      * Indicates if Script is running in current thread. <br/>
183      * This allows code to know if there own evaluation is has been triggered by some script code.
184      * @return <code>true</code> if script is running.
185      */

186     public abstract boolean isScriptRunning();
187 }
188
Popular Tags