FileDocCategorySizeDatePackage
AbstractScriptEngine.javaAPI DocJava SE 6 API10293Tue Jun 10 00:26:26 BST 2008javax.script

AbstractScriptEngine

public abstract class AbstractScriptEngine extends Object implements ScriptEngine
Provides a standard implementation for several of the variants of the eval method.

eval(Reader)

eval(String)

eval(String, Bindings)

eval(Reader, Bindings)

are implemented using the abstract methods

eval(Reader,ScriptContext) or eval(String, ScriptContext)

with a SimpleScriptContext.

A SimpleScriptContext is used as the default ScriptContext of the AbstractScriptEngine..

author
Mike Grogan
version
1.0
since
1.6

Fields Summary
protected ScriptContext
context
The default ScriptContext of this AbstractScriptEngine.
Constructors Summary
public AbstractScriptEngine()
Creates a new instance of AbstractScriptEngine using a SimpleScriptContext as its default ScriptContext.

        
        context = new SimpleScriptContext();
        
    
public AbstractScriptEngine(Bindings n)
Creates a new instance using the specified Bindings as the ENGINE_SCOPE Bindings in the protected context field.

param
n The specified Bindings.
throws
NullPointerException if n is null.

        
        this();
        if (n == null) { 
            throw new NullPointerException("n is null");
        }
        context.setBindings(n, ScriptContext.ENGINE_SCOPE);
    
Methods Summary
public java.lang.Objecteval(java.lang.String script, javax.script.Bindings bindings)
Same as eval(Reader, Bindings) except that the abstract eval(String, ScriptContext) is used.

param
script A String containing the source of the script.
param
bindings A Bindings to use as the ENGINE_SCOPE while the script executes.
return
The return value from eval(String, ScriptContext)
throws
ScriptException if an error occurs in script.
throws
NullPointerException if any of the parameters is null.

        
        ScriptContext ctxt = getScriptContext(bindings);
        
        return eval(script , ctxt);
    
public java.lang.Objecteval(java.io.Reader reader)
eval(Reader) calls the abstract eval(Reader, ScriptContext) passing the value of the context field.

param
reader A Reader containing the source of the script.
return
The return value from eval(Reader, ScriptContext)
throws
ScriptException if an error occurs in script.
throws
NullPointerException if any of the parameters is null.

        
        
        return eval(reader, context);
    
public java.lang.Objecteval(java.lang.String script)
Same as eval(Reader) except that the abstract eval(String, ScriptContext) is used.

param
script A String containing the source of the script.
return
The return value from eval(String, ScriptContext)
throws
ScriptException if an error occurrs in script.
throws
NullPointerException if any of the parameters is null.

        
        
        return eval(script, context);
    
public java.lang.Objecteval(java.io.Reader reader, javax.script.Bindings bindings)
eval(Reader, Bindings) calls the abstract eval(Reader, ScriptContext) method, passing it a ScriptContext whose Reader, Writers and Bindings for scopes other that ENGINE_SCOPE are identical to those members of the protected context field. The specified Bindings is used instead of the ENGINE_SCOPE Bindings of the context field.

param
reader A Reader containing the source of the script.
param
bindings A Bindings to use for the ENGINE_SCOPE while the script executes.
return
The return value from eval(Reader, ScriptContext)
throws
ScriptException if an error occurs in script.
throws
NullPointerException if any of the parameters is null.

        
        ScriptContext ctxt = getScriptContext(bindings);
        
        return eval(reader, ctxt);
    
public java.lang.Objectget(java.lang.String key)
Gets the value for the specified key in the ENGINE_SCOPE of the protected context field.

return
The value for the specified key.
throws
NullPointerException if key is null.
throws
IllegalArgumentException if key is empty.

        
        Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);
        if (nn != null) {
            return nn.get(key);
        }
        
        return null;
    
public javax.script.BindingsgetBindings(int scope)
Returns the Bindings with the specified scope value in the protected context field.

param
scope The specified scope
return
The corresponding Bindings.
throws
IllegalArgumentException if the value of scope is invalid for the type the protected context field.

        
        if (scope == ScriptContext.GLOBAL_SCOPE) {
            return context.getBindings(ScriptContext.GLOBAL_SCOPE);
        } else if (scope == ScriptContext.ENGINE_SCOPE) {
            return context.getBindings(ScriptContext.ENGINE_SCOPE);
        } else {
            throw new IllegalArgumentException("Invalid scope value.");
        }
    
public javax.script.ScriptContextgetContext()
Returns the value of the protected context field.

return
The value of the protected context field.

        return context;
    
protected javax.script.ScriptContextgetScriptContext(javax.script.Bindings nn)
Returns a SimpleScriptContext. The SimpleScriptContext:

  • Uses the specified Bindings for its ENGINE_SCOPE
  • Uses the Bindings returned by the abstract getGlobalScope method as its GLOBAL_SCOPE
  • Uses the Reader and Writer in the default ScriptContext of this ScriptEngine


A SimpleScriptContext returned by this method is used to implement eval methods using the abstract eval(Reader,Bindings) and eval(String,Bindings) versions.

param
nn Bindings to use for the ENGINE_SCOPE
return
The SimpleScriptContext

        
        SimpleScriptContext ctxt = new SimpleScriptContext();
        Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE);
        
        if (gs != null) {
            ctxt.setBindings(gs, ScriptContext.GLOBAL_SCOPE);
        }
        
        if (nn != null) {
            ctxt.setBindings(nn,
                    ScriptContext.ENGINE_SCOPE);
        } else {
            throw new NullPointerException("Engine scope Bindings may not be null.");
        }
        
        ctxt.setReader(context.getReader());
        ctxt.setWriter(context.getWriter());
        ctxt.setErrorWriter(context.getErrorWriter());
        
        return ctxt;
        
    
public voidput(java.lang.String key, java.lang.Object value)
Sets the specified value with the specified key in the ENGINE_SCOPE Bindings of the protected context field.

param
key The specified key.
param
value The specified value.
throws
NullPointerException if key is null.
throws
IllegalArgumentException if key is empty.

        
        Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);
        if (nn != null) {
            nn.put(key, value);
        }
        
    
public voidsetBindings(javax.script.Bindings bindings, int scope)
Sets the Bindings with the corresponding scope value in the context field.

param
bindings The specified Bindings.
param
scope The specified scope.
throws
IllegalArgumentException if the value of scope is invalid for the type the context field.
throws
NullPointerException if the bindings is null and the scope is ScriptContext.ENGINE_SCOPE

        
        if (scope == ScriptContext.GLOBAL_SCOPE) {
            context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);;
        } else if (scope == ScriptContext.ENGINE_SCOPE) {
            context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);;
        } else {
            throw new IllegalArgumentException("Invalid scope value.");
        }
    
public voidsetContext(javax.script.ScriptContext ctxt)
Sets the value of the protected context field to the specified ScriptContext.

param
ctxt The specified ScriptContext.
throws
NullPointerException if ctxt is null.

        if (ctxt == null) {
            throw new NullPointerException("null context");
        }
        context = ctxt;