org.apache.batik.script.rhino
Class RhinoInterpreter

java.lang.Object
  extended byorg.apache.batik.script.rhino.RhinoInterpreter
All Implemented Interfaces:
Interpreter, Localizable

public class RhinoInterpreter
extends Object
implements Interpreter

A simple implementation of Interpreter interface to use Rhino ECMAScript interpreter.


Nested Class Summary
static interface RhinoInterpreter.ArgumentsBuilder
          To build an argument list.
 class RhinoInterpreter.ExtendedContext
           
static class RhinoInterpreter.RhinoGetDelegate
          Class to act as 'get' delegate for Rhino.
 
Field Summary
static String BIND_NAME_WINDOW
          Name of the "window" object when referenced by scripts
protected  RhinoClassLoader rhinoClassLoader
          The Rhino 'security domain'.
static String SOURCE_NAME_SVG
          Constant used to describe an SVG source
protected  Window window
          The window object
 
Constructor Summary
RhinoInterpreter(URL documentURL)
          Build a Interpreter for ECMAScript using Rhino.
 
Method Summary
 void bindObject(String name, Object object)
          This method registers a particular Java Object in the environment of the interpreter.
 void dispose()
          For RhinoInterpreter this method flushes the Rhino caches to avoid memory leaks.
 Context enterContext()
          Implementation helper.
 Object evaluate(Reader scriptreader)
          This method evaluates a piece of ECMAScript.
 Object evaluate(Reader scriptreader, String description)
          This method evaluates a piece of ECMAScript.
 Object evaluate(String scriptstr)
          This method evaluates a piece of ECMA script.
 String formatMessage(String key, Object[] args)
          Creates and returns a localized message, given the key of the message, 0, data.length in the resource bundle and the message parameters.
 AccessControlContext getAccessControlContext()
          Returns the AccessControlContext associated with this Interpreter.
protected  ScriptableObject getGlobalObject()
          This method returns the ECMAScript global object used by this interpreter.
 Locale getLocale()
          Returns the current locale or null if the locale currently used is the default one.
 Window getWindow()
           
 void setLocale(Locale locale)
          Provides a way to the user to specify a locale which override the default one.
 void setOut(Writer out)
          By default Rhino has no output method in its language.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

window

protected Window window
The window object


SOURCE_NAME_SVG

public static final String SOURCE_NAME_SVG
Constant used to describe an SVG source

See Also:
Constant Field Values

BIND_NAME_WINDOW

public static final String BIND_NAME_WINDOW
Name of the "window" object when referenced by scripts

See Also:
Constant Field Values

rhinoClassLoader

protected RhinoClassLoader rhinoClassLoader
The Rhino 'security domain'. We use the RhinoClassLoader which will grant permissions to connect to the document URL.

Constructor Detail

RhinoInterpreter

public RhinoInterpreter(URL documentURL)
Build a Interpreter for ECMAScript using Rhino.

Parameters:
documentURL - the URL for the document which references
See Also:
Interpreter, InterpreterPool
Method Detail

getWindow

public Window getWindow()

getAccessControlContext

public AccessControlContext getAccessControlContext()
Returns the AccessControlContext associated with this Interpreter.

See Also:
RhinoClassLoader

enterContext

public Context enterContext()
Implementation helper. Makes sure the proper security is set on the context.


getGlobalObject

protected ScriptableObject getGlobalObject()
This method returns the ECMAScript global object used by this interpreter.


evaluate

public Object evaluate(Reader scriptreader)
                throws InterpreterException,
                       IOException
This method evaluates a piece of ECMAScript.

Specified by:
evaluate in interface Interpreter
Parameters:
scriptreader - a java.io.Reader on the piece of script
Returns:
if no exception is thrown during the call, should return the value of the last expression evaluated in the script.
Throws:
InterpreterException
IOException

evaluate

public Object evaluate(Reader scriptreader,
                       String description)
                throws InterpreterException,
                       IOException
This method evaluates a piece of ECMAScript.

Specified by:
evaluate in interface Interpreter
Parameters:
scriptreader - a java.io.Reader on the piece of script
description - description which can be later used (e.g., for error messages).
Returns:
if no exception is thrown during the call, should return the value of the last expression evaluated in the script.
Throws:
InterpreterException
IOException

evaluate

public Object evaluate(String scriptstr)
                throws InterpreterException
This method evaluates a piece of ECMA script. The first time a String is passed, it is compiled and evaluated. At next call, the piece of script will only be evaluated to prevent from recompiling it.

Specified by:
evaluate in interface Interpreter
Parameters:
scriptstr - the piece of script
Returns:
if no exception is thrown during the call, should return the value of the last expression evaluated in the script.
Throws:
InterpreterException

dispose

public void dispose()
For RhinoInterpreter this method flushes the Rhino caches to avoid memory leaks.

Specified by:
dispose in interface Interpreter

bindObject

public void bindObject(String name,
                       Object object)
This method registers a particular Java Object in the environment of the interpreter.

Specified by:
bindObject in interface Interpreter
Parameters:
name - the name of the script object to create
object - the Java object

setOut

public void setOut(Writer out)
By default Rhino has no output method in its language. That's why this method does nothing.

Specified by:
setOut in interface Interpreter
Parameters:
out - the new out Writer.

getLocale

public Locale getLocale()
Returns the current locale or null if the locale currently used is the default one.

Specified by:
getLocale in interface Localizable

setLocale

public void setLocale(Locale locale)
Provides a way to the user to specify a locale which override the default one. If null is passed to this method, the used locale becomes the global one.

Specified by:
setLocale in interface Localizable
Parameters:
locale - The locale to set.

formatMessage

public String formatMessage(String key,
                            Object[] args)
Creates and returns a localized message, given the key of the message, 0, data.length in the resource bundle and the message parameters. The messages in the resource bundle must have the syntax described in the java.text.MessageFormat class documentation.

Specified by:
formatMessage in interface Localizable
Parameters:
key - The key used to retreive the message from the resource bundle.
args - The objects that compose the message.
Throws:
MissingResourceException - if the key is not in the bundle.


Copyright ? 2005 Apache Software Foundation. All Rights Reserved.