Package org.apache.batik.bridge
Class RhinoInterpreter
- java.lang.Object
-
- org.apache.batik.bridge.RhinoInterpreter
-
- All Implemented Interfaces:
Localizable
,Interpreter
- Direct Known Subclasses:
SVG12RhinoInterpreter
public class RhinoInterpreter extends java.lang.Object implements Interpreter
A simple implementation ofInterpreter
interface to use Rhino ECMAScript interpreter.- Version:
- $Id: RhinoInterpreter.java 1808888 2017-09-19 14:22:11Z ssteiner $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
RhinoInterpreter.ArgumentsBuilder
To build an argument list.protected static class
RhinoInterpreter.Entry
Class to store cached compiled scripts.protected class
RhinoInterpreter.Factory
Factory for Context objects.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
BIND_NAME_WINDOW
Name of the "window" object when referenced by scriptsprotected org.mozilla.javascript.ClassShutter
classShutter
Class shutter.protected java.util.LinkedList
compiledScripts
List of cached compiled scripts.protected org.mozilla.javascript.ContextFactory
contextFactory
Factory object for creating Contexts.protected static java.util.List
contexts
Context vector, to make sure we are not setting the security context too many timesprotected org.mozilla.javascript.Context
defaultContext
Default Context for scripts.protected org.mozilla.javascript.ScriptableObject
globalObject
The global object.private static int
MAX_CACHED_SCRIPTS
The number of cached compiled scripts to store.protected RhinoClassLoader
rhinoClassLoader
The Rhino 'security domain'.protected org.mozilla.javascript.SecurityController
securityController
The SecurityController implementation for Batik, which ensures scripts have access to the server they were downloaded fromstatic java.lang.String
SOURCE_NAME_SVG
Constant used to describe an SVG sourceprotected Window
window
The window object.protected org.mozilla.javascript.WrapFactory
wrapFactory
Factory for Java wrapper objects.
-
Constructor Summary
Constructors Constructor Description RhinoInterpreter(java.net.URL documentURL)
Build aInterpreter
for ECMAScript using Rhino.RhinoInterpreter(java.net.URL documentURL, ImportInfo imports)
Build aInterpreter
for ECMAScript using Rhino.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
bindObject(java.lang.String name, java.lang.Object object)
This method registers a particular JavaObject
in the environment of the interpreter.(package private) org.mozilla.javascript.Scriptable
buildEventTargetWrapper(org.w3c.dom.events.EventTarget obj)
Build the wrapper for objects implementEventTarget
.(package private) java.lang.Object
call(org.mozilla.javascript.ContextAction action)
To be used byEventTargetWrapper
.(package private) void
callHandler(org.mozilla.javascript.Function handler, java.lang.Object arg)
To be used byEventTargetWrapper
.(package private) void
callHandler(org.mozilla.javascript.Function handler, java.lang.Object[] args)
To be used byWindowWrapper
.(package private) void
callHandler(org.mozilla.javascript.Function handler, RhinoInterpreter.ArgumentsBuilder ab)
To be used byWindowWrapper
.(package private) void
callMethod(org.mozilla.javascript.ScriptableObject obj, java.lang.String methodName, RhinoInterpreter.ArgumentsBuilder ab)
To be used byWindowWrapper
.protected org.mozilla.javascript.ScriptableObject
createGlobalObject(org.mozilla.javascript.Context ctx)
Creates the global object.protected void
defineGlobalWrapperClass(org.mozilla.javascript.Scriptable global)
Defines the class for the global object.void
dispose()
ForRhinoInterpreter
this method flushes the Rhino caches to avoid memory leaks.java.lang.Object
evaluate(java.io.Reader scriptreader)
This method evaluates a piece of ECMAScript.java.lang.Object
evaluate(java.io.Reader scriptReader, java.lang.String description)
This method evaluates a piece of ECMAScript.java.lang.Object
evaluate(java.lang.String scriptStr)
This method evaluates a piece of ECMA script.java.lang.String
formatMessage(java.lang.String key, java.lang.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.java.security.AccessControlContext
getAccessControlContext()
Returns the AccessControlContext associated with this Interpreter.org.mozilla.javascript.ContextFactory
getContextFactory()
Returns the ContextFactory for this interpreter.protected org.mozilla.javascript.ScriptableObject
getGlobalObject()
This method returns the ECMAScript global object used by this interpreter.java.util.Locale
getLocale()
Returns the current locale or null if the locale currently used is the default one.java.lang.String[]
getMimeTypes()
Returns the content types of the scripting languages this interpreter handles.Window
getWindow()
Returns the window object for this interpreter.protected void
init(java.net.URL documentURL, ImportInfo imports)
void
setLocale(java.util.Locale locale)
Provides a way to the user to specify a locale which override the default one.void
setOut(java.io.Writer out)
By default Rhino has no output method in its language.
-
-
-
Field Detail
-
MAX_CACHED_SCRIPTS
private static final int MAX_CACHED_SCRIPTS
The number of cached compiled scripts to store.- See Also:
- Constant Field Values
-
SOURCE_NAME_SVG
public static final java.lang.String SOURCE_NAME_SVG
Constant used to describe an SVG source- See Also:
- Constant Field Values
-
BIND_NAME_WINDOW
public static final java.lang.String BIND_NAME_WINDOW
Name of the "window" object when referenced by scripts- See Also:
- Constant Field Values
-
contexts
protected static java.util.List contexts
Context vector, to make sure we are not setting the security context too many times
-
window
protected Window window
The window object.
-
globalObject
protected org.mozilla.javascript.ScriptableObject globalObject
The global object.
-
compiledScripts
protected java.util.LinkedList compiledScripts
List of cached compiled scripts.
-
wrapFactory
protected org.mozilla.javascript.WrapFactory wrapFactory
Factory for Java wrapper objects.
-
classShutter
protected org.mozilla.javascript.ClassShutter classShutter
Class shutter.
-
rhinoClassLoader
protected RhinoClassLoader rhinoClassLoader
The Rhino 'security domain'. We use the RhinoClassLoader which will grant permissions to connect to the document URL.
-
securityController
protected org.mozilla.javascript.SecurityController securityController
The SecurityController implementation for Batik, which ensures scripts have access to the server they were downloaded from
-
contextFactory
protected org.mozilla.javascript.ContextFactory contextFactory
Factory object for creating Contexts.
-
defaultContext
protected org.mozilla.javascript.Context defaultContext
Default Context for scripts. This is used only for efficiency reasons.
-
-
Constructor Detail
-
RhinoInterpreter
public RhinoInterpreter(java.net.URL documentURL)
Build aInterpreter
for ECMAScript using Rhino.- Parameters:
documentURL
- the URL for the document which references- See Also:
Interpreter
,InterpreterPool
-
RhinoInterpreter
public RhinoInterpreter(java.net.URL documentURL, ImportInfo imports)
Build aInterpreter
for ECMAScript using Rhino.- Parameters:
documentURL
- the URL for the document which referencesimports
- the set of Java classes/packages to import into the scripting enviornment.- See Also:
Interpreter
,InterpreterPool
-
-
Method Detail
-
init
protected void init(java.net.URL documentURL, ImportInfo imports)
-
getMimeTypes
public java.lang.String[] getMimeTypes()
Returns the content types of the scripting languages this interpreter handles.- Specified by:
getMimeTypes
in interfaceInterpreter
-
getWindow
public Window getWindow()
Returns the window object for this interpreter.
-
getContextFactory
public org.mozilla.javascript.ContextFactory getContextFactory()
Returns the ContextFactory for this interpreter.
-
defineGlobalWrapperClass
protected void defineGlobalWrapperClass(org.mozilla.javascript.Scriptable global)
Defines the class for the global object.
-
createGlobalObject
protected org.mozilla.javascript.ScriptableObject createGlobalObject(org.mozilla.javascript.Context ctx)
Creates the global object.
-
getAccessControlContext
public java.security.AccessControlContext getAccessControlContext()
Returns the AccessControlContext associated with this Interpreter.- See Also:
RhinoClassLoader
-
getGlobalObject
protected org.mozilla.javascript.ScriptableObject getGlobalObject()
This method returns the ECMAScript global object used by this interpreter.
-
evaluate
public java.lang.Object evaluate(java.io.Reader scriptreader) throws java.io.IOException
This method evaluates a piece of ECMAScript.- Specified by:
evaluate
in interfaceInterpreter
- Parameters:
scriptreader
- ajava.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:
java.io.IOException
-
evaluate
public java.lang.Object evaluate(java.io.Reader scriptReader, java.lang.String description) throws java.io.IOException
This method evaluates a piece of ECMAScript.- Specified by:
evaluate
in interfaceInterpreter
- Parameters:
scriptReader
- ajava.io.Reader
on the piece of scriptdescription
- 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:
java.io.IOException
-
evaluate
public java.lang.Object evaluate(java.lang.String scriptStr)
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 interfaceInterpreter
- 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.
-
dispose
public void dispose()
ForRhinoInterpreter
this method flushes the Rhino caches to avoid memory leaks.- Specified by:
dispose
in interfaceInterpreter
-
bindObject
public void bindObject(java.lang.String name, java.lang.Object object)
This method registers a particular JavaObject
in the environment of the interpreter.- Specified by:
bindObject
in interfaceInterpreter
- Parameters:
name
- the name of the script object to createobject
- the Java object
-
callHandler
void callHandler(org.mozilla.javascript.Function handler, java.lang.Object arg)
To be used byEventTargetWrapper
.
-
callMethod
void callMethod(org.mozilla.javascript.ScriptableObject obj, java.lang.String methodName, RhinoInterpreter.ArgumentsBuilder ab)
To be used byWindowWrapper
.
-
callHandler
void callHandler(org.mozilla.javascript.Function handler, java.lang.Object[] args)
To be used byWindowWrapper
.
-
callHandler
void callHandler(org.mozilla.javascript.Function handler, RhinoInterpreter.ArgumentsBuilder ab)
To be used byWindowWrapper
.
-
call
java.lang.Object call(org.mozilla.javascript.ContextAction action)
To be used byEventTargetWrapper
.
-
buildEventTargetWrapper
org.mozilla.javascript.Scriptable buildEventTargetWrapper(org.w3c.dom.events.EventTarget obj)
Build the wrapper for objects implementEventTarget
.
-
setOut
public void setOut(java.io.Writer out)
By default Rhino has no output method in its language. That's why this method does nothing.- Specified by:
setOut
in interfaceInterpreter
- Parameters:
out
- the new outWriter
.
-
getLocale
public java.util.Locale getLocale()
Returns the current locale or null if the locale currently used is the default one.- Specified by:
getLocale
in interfaceLocalizable
-
setLocale
public void setLocale(java.util.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 interfaceLocalizable
- Parameters:
locale
- The locale to set.
-
formatMessage
public java.lang.String formatMessage(java.lang.String key, java.lang.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 interfaceLocalizable
- Parameters:
key
- The key used to retreive the message from the resource bundle.args
- The objects that compose the message.- Throws:
java.util.MissingResourceException
- if the key is not in the bundle.
-
-