FileDocCategorySizeDatePackage
SimpleScriptContext.javaAPI DocJava SE 6 API9996Tue Jun 10 00:26:26 BST 2008javax.script

SimpleScriptContext

public class SimpleScriptContext extends Object implements ScriptContext
Simple implementation of ScriptContext.
author
Mike Grogan
version
1.0
since
1.6

Fields Summary
protected Writer
writer
This is the writer to be used to output from scripts. By default, a PrintWriter based on System.out is used. Accessor methods getWriter, setWriter are used to manage this field.
protected Writer
errorWriter
This is the writer to be used to output errors from scripts. By default, a PrintWriter based on System.err is used. Accessor methods getErrorWriter, setErrorWriter are used to manage this field.
protected Reader
reader
This is the reader to be used for input from scripts. By default, a InputStreamReader based on System.in is used and default charset is used by this reader. Accessor methods getReader, setReader are used to manage this field.
protected Bindings
engineScope
This is the engine scope bindings. By default, a SimpleBindings is used. Accessor methods setBindings, getBindings are used to manage this field.
protected Bindings
globalScope
This is the global scope bindings. By default, a null value (which means no global scope) is used. Accessor methods setBindings, getBindings are used to manage this field.
private static List
scopes
Constructors Summary
public SimpleScriptContext()

       
        engineScope = new SimpleBindings();
        globalScope = null;
        reader = new InputStreamReader(System.in);
        writer = new PrintWriter(System.out , true);
        errorWriter = new PrintWriter(System.err, true);        
    
Methods Summary
public java.lang.ObjectgetAttribute(java.lang.String name)
Retrieves the value of the attribute with the given name in the scope occurring earliest in the search order. The order is determined by the numeric value of the scope parameter (lowest scope values first.)

param
name The name of the the attribute to retrieve.
return
The value of the attribute in the lowest scope for which an attribute with the given name is defined. Returns null if no attribute with the name exists in any scope.
throws
NullPointerException if the name is null.
throws
IllegalArgumentException if the name is empty.

        if (engineScope.containsKey(name)) {
            return getAttribute(name, ENGINE_SCOPE);
        } else if (globalScope != null && globalScope.containsKey(name)) {
            return getAttribute(name, GLOBAL_SCOPE);
        }
        
        return null;
    
public java.lang.ObjectgetAttribute(java.lang.String name, int scope)
Gets the value of an attribute in a given scope.

param
name The name of the attribute to retrieve.
param
scope The scope in which to retrieve the attribute.
return
The value of the attribute. Returns null is the name does not exist in the given scope.
throws
IllegalArgumentException if the name is empty or if the value of scope is invalid.
throws
NullPointerException if the name is null.

        
        switch (scope) {
            
            case ENGINE_SCOPE:
                return engineScope.get(name);
                
            case GLOBAL_SCOPE:
                if (globalScope != null) {
                    return globalScope.get(name);
                }
                return null;
                
            default:
                throw new IllegalArgumentException("Illegal scope value.");
        }
    
public intgetAttributesScope(java.lang.String name)
Get the lowest scope in which an attribute is defined.

param
name Name of the attribute .
return
The lowest scope. Returns -1 if no attribute with the given name is defined in any scope.
throws
NullPointerException if name is null.
throws
IllegalArgumentException if name is empty.

        if (engineScope.containsKey(name)) {
            return ENGINE_SCOPE;
        } else if (globalScope != null && globalScope.containsKey(name)) {
            return GLOBAL_SCOPE;
        } else {
            return -1;
        }
    
public javax.script.BindingsgetBindings(int scope)
Returns the value of the engineScope field if specified scope is ENGINE_SCOPE. Returns the value of the globalScope field if the specified scope is GLOBAL_SCOPE.

param
scope The specified scope
return
The value of either the engineScope or globalScope field.
throws
IllegalArgumentException if the value of scope is invalid.

        if (scope == ENGINE_SCOPE) {
            return engineScope;
        } else if (scope == GLOBAL_SCOPE) {
            return globalScope;
        } else {
            throw new IllegalArgumentException("Illegal scope value.");
        }        
    
public java.io.WritergetErrorWriter()
{@inheritDoc}

        return errorWriter;
    
public java.io.ReadergetReader()
{@inheritDoc}

        return reader;
    
public java.util.ListgetScopes()
{@inheritDoc}

        return scopes;
    
public java.io.WritergetWriter()
{@inheritDoc}

        return writer;
    
public java.lang.ObjectremoveAttribute(java.lang.String name, int scope)
Remove an attribute in a given scope.

param
name The name of the attribute to remove
param
scope The scope in which to remove the attribute
return
The removed value.
throws
IllegalArgumentException if the name is empty or if the scope is invalid.
throws
NullPointerException if the name is null.

        
        switch (scope) {
            
            case ENGINE_SCOPE:
                if (getBindings(ENGINE_SCOPE) != null) {
                    return getBindings(ENGINE_SCOPE).remove(name);
                }
                return null;
                
            case GLOBAL_SCOPE:
                if (getBindings(GLOBAL_SCOPE) != null) {
                    return getBindings(GLOBAL_SCOPE).remove(name);
                }
                return null;
                
            default:
                throw new IllegalArgumentException("Illegal scope value.");
        }
    
public voidsetAttribute(java.lang.String name, java.lang.Object value, int scope)
Sets the value of an attribute in a given scope.

param
name The name of the attribute to set
param
value The value of the attribute
param
scope The scope in which to set the attribute
throws
IllegalArgumentException if the name is empty or if the scope is invalid.
throws
NullPointerException if the name is null.

        
        
        switch (scope) {
            
            case ENGINE_SCOPE:
                engineScope.put(name, value);
                return;
                
            case GLOBAL_SCOPE:
                if (globalScope != null) {
                    globalScope.put(name, value);
                }
                return;
                
            default:
                throw new IllegalArgumentException("Illegal scope value.");
        }
    
public voidsetBindings(javax.script.Bindings bindings, int scope)
Sets a Bindings of attributes for the given scope. If the value of scope is ENGINE_SCOPE the given Bindings replaces the engineScope field. If the value of scope is GLOBAL_SCOPE the given Bindings replaces the globalScope field.

param
bindings The Bindings of attributes to set.
param
scope The value of the scope in which the attributes are set.
throws
IllegalArgumentException if scope is invalid.
throws
NullPointerException if the value of scope is ENGINE_SCOPE and the specified Bindings is null.

        
        switch (scope) {
            
            case ENGINE_SCOPE:
                if (bindings == null) {
                    throw new NullPointerException("Engine scope cannot be null.");
                }
                engineScope = bindings;
                break;
            case GLOBAL_SCOPE:
                globalScope = bindings;
                break;
            default:
                throw new IllegalArgumentException("Invalid scope value.");
        }
    
public voidsetErrorWriter(java.io.Writer writer)
{@inheritDoc}

        this.errorWriter = writer;
    
public voidsetReader(java.io.Reader reader)
{@inheritDoc}

        this.reader = reader;
    
public voidsetWriter(java.io.Writer writer)
{@inheritDoc}

        this.writer = writer;