org.tinyjee.maven.dim.utils
Class JavaScriptFacade

java.lang.Object
  extended by org.tinyjee.maven.dim.utils.JavaScriptFacade
Direct Known Subclasses:
HighlighterScriptFacade

public class JavaScriptFacade
extends Object

Is a common base class for script facades.

Note: Non of the facades is thread safe nor sharable between threads.

Author:
Juergen_Kellerer, 2010-09-03

Field Summary
protected  org.mozilla.javascript.Context context
           
protected  org.mozilla.javascript.ScriptableObject scope
           
protected static Charset SCRIPT_ENCODING
           
 
Constructor Summary
protected JavaScriptFacade()
          Constructs a new JavaScriptFacade hosing a thread-bound script Context.
 
Method Summary
protected  void close()
           
protected  org.mozilla.javascript.Script compileScript(String name, InputStream in, boolean close)
          Compiles the given JS script contained in the stream and returns the compiled version.
protected  void compileScript(URL resource, List<org.mozilla.javascript.Script> scripts)
          Compiles the given script or script bundle.
protected  void compileScripts(ZipInputStream zIn, List<org.mozilla.javascript.Script> scripts)
          Compiles all JS scripts contained in the given ZIP file.
protected  org.mozilla.javascript.Scriptable createObject(Map<String,Object> values)
          Creates a JavaScript object.
protected  Object execute(org.mozilla.javascript.Script script)
          Executes the script.
protected  Object getFromScope(String name)
          Returns the object from the current scope.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SCRIPT_ENCODING

protected static final Charset SCRIPT_ENCODING

context

protected final org.mozilla.javascript.Context context

scope

protected final org.mozilla.javascript.ScriptableObject scope
Constructor Detail

JavaScriptFacade

protected JavaScriptFacade()
Constructs a new JavaScriptFacade hosing a thread-bound script Context.

Method Detail

close

protected void close()

createObject

protected org.mozilla.javascript.Scriptable createObject(Map<String,Object> values)
Creates a JavaScript object.

Parameters:
values - the key, value pairs forming the JavaScript object.
Returns:
a JavaScript object.

compileScript

protected void compileScript(URL resource,
                             List<org.mozilla.javascript.Script> scripts)
                      throws IOException
Compiles the given script or script bundle.

Parameters:
resource - an URL pointing to the JS or ZIP file.
scripts - the list of scripts to add the compiled scripts to.
Throws:
IOException - in case of reading the URL content failed.

compileScripts

protected void compileScripts(ZipInputStream zIn,
                              List<org.mozilla.javascript.Script> scripts)
                       throws IOException
Compiles all JS scripts contained in the given ZIP file.

Parameters:
zIn - the ZipInputStream containing the scripts.
scripts - the list of scripts to add the compiled scripts to.
Throws:
IOException - in case of reading the ZIP content failed.

compileScript

protected org.mozilla.javascript.Script compileScript(String name,
                                                      InputStream in,
                                                      boolean close)
                                               throws IOException
Compiles the given JS script contained in the stream and returns the compiled version.

Parameters:
name - the name of the script source (used when compile errors are contained).
in - the input stream containing the script.
close - whether the stream is closed or not at the end.
Returns:
the compiled Script instance.
Throws:
IOException - In case of reading the stream failed.

execute

protected Object execute(org.mozilla.javascript.Script script)
Executes the script.

Parameters:
script - the script to execute.
Returns:
the return value of the script.

getFromScope

protected Object getFromScope(String name)
Returns the object from the current scope.

Parameters:
name - the name of the object to retrieve.
Returns:
the object from the current scope.