com.gargoylesoftware.htmlunit.javascript
Class JavaScriptEngine

java.lang.Object
  extended by com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine

public class JavaScriptEngine
extends Object

A wrapper for the Rhino JavaScript engine that provides browser specific features.
Like all classes in this package, this class is not intended for direct use and may change without notice.

Version:
$Revision: 5864 $
Author:
Mike Bowler, Chen Jun, David K. Taylor, Chris Erskine, Ben Curren, David D. Kilzer, Marc Guillemot, Daniel Gredler, Ahmed Ashour, Amit Manjhi
See Also:
Rhino and Java Browser

Field Summary
static String KEY_STARTING_PAGE
          Key used to place the HtmlPage for which the JavaScript code is executed as thread local attribute in current context.
static String KEY_STARTING_SCOPE
          Key used to place the scope in which the execution of some JavaScript code started as thread local attribute in current context.
This is needed to resolve some relative locations relatively to the page in which the script is executed and not to the page which location is changed.
 
Constructor Summary
JavaScriptEngine(WebClient webClient)
          Creates an instance for the specified WebClient.
 
Method Summary
 void addPostponedAction(PostponedAction action)
          Adds an action that should be executed first when the script currently being executed has finished.
 Object callFunction(HtmlPage htmlPage, net.sourceforge.htmlunit.corejs.javascript.Function function, net.sourceforge.htmlunit.corejs.javascript.Context context, net.sourceforge.htmlunit.corejs.javascript.Scriptable scope, net.sourceforge.htmlunit.corejs.javascript.Scriptable thisObject, Object[] args)
          Calls the given function taking care of synchronization issues.
 Object callFunction(HtmlPage htmlPage, Object javaScriptFunction, Object thisObject, Object[] args, DomNode htmlElement)
          Calls a JavaScript function and return the result.
 net.sourceforge.htmlunit.corejs.javascript.Script compile(HtmlPage htmlPage, String sourceCode, String sourceName, int startLine)
          Compiles the specified JavaScript code in the context of a given HTML page.
protected  JavaScriptExecutor createJavaScriptExecutor()
          Creates the JavaScriptExecutor that will be used to handle JS.
 Object execute(HtmlPage htmlPage, net.sourceforge.htmlunit.corejs.javascript.Script script)
          Executes the specified JavaScript code in the context of a given HTML page.
 Object execute(HtmlPage htmlPage, String sourceCode, String sourceName, int startLine)
          Executes the specified JavaScript code in the context of a given HTML page.
 HtmlUnitContextFactory getContextFactory()
          Returns this JavaScript engine's Rhino ContextFactory.
 JavaScriptExecutor getJavaScriptExecutor()
          Returns the JavaScriptExecutor.
 WebClient getWebClient()
          Returns the web client that this engine is associated with.
protected  void handleJavaScriptException(ScriptException scriptException)
          Handles an exception that occurred during execution of JavaScript code.
 void holdPosponedActions()
          INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Indicates that no postponed action should be executed.
 void initialize(WebWindow webWindow)
          Performs initialization for the given webWindow.
 boolean isScriptRunning()
          Indicates if JavaScript is running in current thread.
This allows code to know if there own evaluation is has been triggered by some JS code.
 void processPostponedActions()
          INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Process postponed actions, if any.
 int pumpEventLoop(long timeoutMillis)
          Executes the jobs in the eventLoop till timeoutMillis expires or the eventLoop becomes empty.
 void registerWindowAndMaybeStartEventLoop(WebWindow webWindow)
          Register WebWindow with the JavaScriptExecutor.
 void shutdownJavaScriptExecutor()
          Shutdown JavaScriptExecutor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

KEY_STARTING_SCOPE

public static final String KEY_STARTING_SCOPE
Key used to place the scope in which the execution of some JavaScript code started as thread local attribute in current context.
This is needed to resolve some relative locations relatively to the page in which the script is executed and not to the page which location is changed.

See Also:
Constant Field Values

KEY_STARTING_PAGE

public static final String KEY_STARTING_PAGE
Key used to place the HtmlPage for which the JavaScript code is executed as thread local attribute in current context.

See Also:
Constant Field Values
Constructor Detail

JavaScriptEngine

public JavaScriptEngine(WebClient webClient)
Creates an instance for the specified WebClient.

Parameters:
webClient - the client that will own this engine
Method Detail

getWebClient

public final WebClient getWebClient()
Returns the web client that this engine is associated with.

Returns:
the web client

getContextFactory

public HtmlUnitContextFactory getContextFactory()
Returns this JavaScript engine's Rhino ContextFactory.

Returns:
this JavaScript engine's Rhino ContextFactory

initialize

public void initialize(WebWindow webWindow)
Performs initialization for the given webWindow.

Parameters:
webWindow - the web window to initialize for

getJavaScriptExecutor

public JavaScriptExecutor getJavaScriptExecutor()
Returns the JavaScriptExecutor.

Returns:
the JavaScriptExecutor.

registerWindowAndMaybeStartEventLoop

public void registerWindowAndMaybeStartEventLoop(WebWindow webWindow)
Register WebWindow with the JavaScriptExecutor.

Parameters:
webWindow - the WebWindow to be registered.

createJavaScriptExecutor

protected JavaScriptExecutor createJavaScriptExecutor()
Creates the JavaScriptExecutor that will be used to handle JS.

Returns:
the executor.

pumpEventLoop

public int pumpEventLoop(long timeoutMillis)
Executes the jobs in the eventLoop till timeoutMillis expires or the eventLoop becomes empty. No use in non-GAE mode (see GAEUtils.isGaeMode().

Parameters:
timeoutMillis - the timeout in milliseconds
Returns:
the number of jobs executed

shutdownJavaScriptExecutor

public void shutdownJavaScriptExecutor()
Shutdown JavaScriptExecutor.


compile

public net.sourceforge.htmlunit.corejs.javascript.Script compile(HtmlPage htmlPage,
                                                                 String sourceCode,
                                                                 String sourceName,
                                                                 int startLine)
Compiles the specified JavaScript code in the context of a given HTML page.

Parameters:
htmlPage - the page that the code will execute within
sourceCode - the JavaScript code to execute
sourceName - the name that will be displayed on error conditions
startLine - the line at which the script source starts
Returns:
the result of executing the specified code

execute

public Object execute(HtmlPage htmlPage,
                      String sourceCode,
                      String sourceName,
                      int startLine)
Executes the specified JavaScript code in the context of a given HTML page.

Parameters:
htmlPage - the page that the code will execute within
sourceCode - the JavaScript code to execute
sourceName - the name that will be displayed on error conditions
startLine - the line at which the script source starts
Returns:
the result of executing the specified code

execute

public Object execute(HtmlPage htmlPage,
                      net.sourceforge.htmlunit.corejs.javascript.Script script)
Executes the specified JavaScript code in the context of a given HTML page.

Parameters:
htmlPage - the page that the code will execute within
script - the script to execute
Returns:
the result of executing the specified code

callFunction

public Object callFunction(HtmlPage htmlPage,
                           Object javaScriptFunction,
                           Object thisObject,
                           Object[] args,
                           DomNode htmlElement)
Calls a JavaScript function and return the result.

Parameters:
htmlPage - the page
javaScriptFunction - the function to call
thisObject - the this object for class method calls
args - the list of arguments to pass to the function
htmlElement - the HTML element that will act as the context
Returns:
the result of the function call

callFunction

public Object callFunction(HtmlPage htmlPage,
                           net.sourceforge.htmlunit.corejs.javascript.Function function,
                           net.sourceforge.htmlunit.corejs.javascript.Context context,
                           net.sourceforge.htmlunit.corejs.javascript.Scriptable scope,
                           net.sourceforge.htmlunit.corejs.javascript.Scriptable thisObject,
                           Object[] args)
Calls the given function taking care of synchronization issues.

Parameters:
htmlPage - the HTML page that caused this script to executed
function - the JavaScript function to execute
context - the context in which execution should occur
scope - the execution scope
thisObject - the 'this' object
args - the function's arguments
Returns:
the function result

isScriptRunning

public boolean isScriptRunning()
Indicates if JavaScript is running in current thread.
This allows code to know if there own evaluation is has been triggered by some JS code.

Returns:
true if JavaScript is running

addPostponedAction

public void addPostponedAction(PostponedAction action)
Adds an action that should be executed first when the script currently being executed has finished.

Parameters:
action - the action

handleJavaScriptException

protected void handleJavaScriptException(ScriptException scriptException)
Handles an exception that occurred during execution of JavaScript code.

Parameters:
scriptException - the exception

holdPosponedActions

public void holdPosponedActions()
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Indicates that no postponed action should be executed.


processPostponedActions

public void processPostponedActions()
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Process postponed actions, if any.



Copyright © 2002-2011 Gargoyle Software Inc.. All Rights Reserved.