APIdia Launch Partner
Top Description Fields Constructors Methods
javax.script

public Class SimpleScriptContext

extends Object
implements ScriptContext
Class Inheritance
All Implemented Interfaces
javax.script.ScriptContext
Imports
java.nio.charset.Charset, java.util.*, java.io.*

Simple implementation of ScriptContext.
Author
Mike Grogan
Since
1.6

Field Summary

Modifier and TypeField and Description
protected Bindings
engineScope

This is the engine scope bindings.

protected Writer
errorWriter

This is the writer to be used to output errors from scripts.

protected Bindings
globalScope

This is the global scope bindings.

protected Reader
reader

This is the reader to be used for input from scripts.

private static final List<Integer>
protected Writer
writer

This is the writer to be used to output from scripts.

Constructor Summary

AccessConstructor and Description
public
SimpleScriptContext()

Create a SimpleScriptContext.

pack-priv
SimpleScriptContext(Reader
the reader
reader
,
Writer
the writer
writer
,
Writer
the error writer
errorWriter
)

Package-private constructor to avoid needless creation of reader and writers.

Method Summary

Modifier and TypeMethod and Description
private void
public Object

Returns:

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.
getAttribute
(String
The name of the attribute to retrieve.
name
)

Implements javax.script.ScriptContext.getAttribute.

Retrieves the value of the attribute with the given name in the scope occurring earliest in the search order.

public Object

Returns:

The value of the attribute. Returns null is the name does not exist in the given scope.
getAttribute
(String
The name of the attribute to retrieve.
name
,
int
The scope in which to retrieve the attribute.
scope
)

Implements javax.script.ScriptContext.getAttribute.

Gets the value of an attribute in a given scope.

public int

Returns:

The lowest scope. Returns -1 if no attribute with the given name is defined in any scope.
getAttributesScope
(String
Name of the attribute .
name
)

Implements javax.script.ScriptContext.getAttributesScope.

Get the lowest scope in which an attribute is defined.

public Bindings

Returns:

The value of either the engineScope or globalScope field.
getBindings
(int
The specified scope
scope
)

Implements javax.script.ScriptContext.getBindings.

Returns the value of the engineScope field if specified scope is ENGINE_SCOPE.

public Writer
getErrorWriter()

Implements javax.script.ScriptContext.getErrorWriter.

Returns the Writer used to display error output.

public Reader
getReader()

Implements javax.script.ScriptContext.getReader.

Returns a Reader to be used by the script to read input.

public List<Integer>
getScopes()

Implements javax.script.ScriptContext.getScopes.

Returns immutable List of all the valid values for scope in the ScriptContext.

public Writer
getWriter()

Implements javax.script.ScriptContext.getWriter.

Returns the Writer for scripts to use when displaying output.

public Object

Returns:

The removed value.
removeAttribute
(String
The name of the attribute to remove
name
,
int
The scope in which to remove the attribute
scope
)

Implements javax.script.ScriptContext.removeAttribute.

Remove an attribute in a given scope.

public void
setAttribute(String
The name of the attribute to set
name
,
Object
The value of the attribute
value
,
int
The scope in which to set the attribute
scope
)

Implements javax.script.ScriptContext.setAttribute.

Sets the value of an attribute in a given scope.

public void
setBindings(Bindings
The Bindings of attributes to set.
bindings
,
int
The value of the scope in which the attributes are set.
scope
)

Implements javax.script.ScriptContext.setBindings.

Sets a Bindings of attributes for the given scope.

public void
setErrorWriter(Writer writer)

Implements javax.script.ScriptContext.setErrorWriter.

Sets the Writer used to display error output.

public void
setReader(Reader reader)

Implements javax.script.ScriptContext.setReader.

Sets the Reader for scripts to read input .

public void
setWriter(Writer writer)

Implements javax.script.ScriptContext.setWriter.

Sets the Writer for scripts to use when displaying output.

private static InputStreamReader
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

engineScopeback to summary
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.

See Also
SimpleBindings
errorWriterback to summary
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.

See Also
java.lang.System#err, java.io.PrintWriter
globalScopeback to summary
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.

readerback to summary
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.

See Also
java.lang.System#in, java.io.InputStreamReader
scopesback to summary
private static final List<Integer> scopes
writerback to 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.

See Also
java.lang.System#out, java.io.PrintWriter

Constructor Detail

SimpleScriptContextback to summary
public SimpleScriptContext()

Create a SimpleScriptContext.

SimpleScriptContextback to summary
pack-priv SimpleScriptContext(Reader reader, Writer writer, Writer errorWriter)

Package-private constructor to avoid needless creation of reader and writers. It is the caller's responsibility to initialize the engine scope.

Parameters
reader:Reader

the reader

writer:Writer

the writer

errorWriter:Writer

the error writer

Method Detail

checkNameback to summary
private void checkName(String name)
getAttributeback to summary
public Object getAttribute(String name)

Implements javax.script.ScriptContext.getAttribute.

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.)

Parameters
name:String

The name of the attribute to retrieve.

Returns:Object

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.

Exceptions
NullPointerException:
if the name is null.
IllegalArgumentException:
if the name is empty.
getAttributeback to summary
public Object getAttribute(String name, int scope)

Implements javax.script.ScriptContext.getAttribute.

Gets the value of an attribute in a given scope.

Parameters
name:String

The name of the attribute to retrieve.

scope:int

The scope in which to retrieve the attribute.

Returns:Object

The value of the attribute. Returns null is the name does not exist in the given scope.

Exceptions
IllegalArgumentException:
if the name is empty or if the value of scope is invalid.
NullPointerException:
if the name is null.
getAttributesScopeback to summary
public int getAttributesScope(String name)

Implements javax.script.ScriptContext.getAttributesScope.

Get the lowest scope in which an attribute is defined.

Parameters
name:String

Name of the attribute .

Returns:int

The lowest scope. Returns -1 if no attribute with the given name is defined in any scope.

Exceptions
NullPointerException:
if name is null.
IllegalArgumentException:
if name is empty.
getBindingsback to summary
public Bindings getBindings(int scope)

Implements javax.script.ScriptContext.getBindings.

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.

Parameters
scope:int

The specified scope

Returns:Bindings

The value of either the engineScope or globalScope field.

Exceptions
IllegalArgumentException:
if the value of scope is invalid.
getErrorWriterback to summary
public Writer getErrorWriter()

Implements javax.script.ScriptContext.getErrorWriter.

Doc from javax.script.ScriptContext.getErrorWriter.

Returns the Writer used to display error output.

getReaderback to summary
public Reader getReader()

Implements javax.script.ScriptContext.getReader.

Doc from javax.script.ScriptContext.getReader.

Returns a Reader to be used by the script to read input.

getScopesback to summary
public List<Integer> getScopes()

Implements javax.script.ScriptContext.getScopes.

Doc from javax.script.ScriptContext.getScopes.

Returns immutable List of all the valid values for scope in the ScriptContext.

getWriterback to summary
public Writer getWriter()

Implements javax.script.ScriptContext.getWriter.

Doc from javax.script.ScriptContext.getWriter.

Returns the Writer for scripts to use when displaying output.

removeAttributeback to summary
public Object removeAttribute(String name, int scope)

Implements javax.script.ScriptContext.removeAttribute.

Remove an attribute in a given scope.

Parameters
name:String

The name of the attribute to remove

scope:int

The scope in which to remove the attribute

Returns:Object

The removed value.

Exceptions
IllegalArgumentException:
if the name is empty or if the scope is invalid.
NullPointerException:
if the name is null.
setAttributeback to summary
public void setAttribute(String name, Object value, int scope)

Implements javax.script.ScriptContext.setAttribute.

Sets the value of an attribute in a given scope. If the scope is GLOBAL_SCOPE and no Bindings is set for GLOBAL_SCOPE, then setAttribute call is a no-op.

Parameters
name:String

The name of the attribute to set

value:Object

The value of the attribute

scope:int

The scope in which to set the attribute

Exceptions
IllegalArgumentException:
if the name is empty or if the scope is invalid.
NullPointerException:
if the name is null.
setBindingsback to summary
public void setBindings(Bindings bindings, int scope)

Implements javax.script.ScriptContext.setBindings.

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.

Parameters
bindings:Bindings

The Bindings of attributes to set.

scope:int

The value of the scope in which the attributes are set.

Exceptions
IllegalArgumentException:
if scope is invalid.
NullPointerException:
if the value of scope is ENGINE_SCOPE and the specified Bindings is null.
setErrorWriterback to summary
public void setErrorWriter(Writer writer)

Implements javax.script.ScriptContext.setErrorWriter.

Doc from javax.script.ScriptContext.setErrorWriter.

Sets the Writer used to display error output.

setReaderback to summary
public void setReader(Reader reader)

Implements javax.script.ScriptContext.setReader.

Doc from javax.script.ScriptContext.setReader.

Sets the Reader for scripts to read input .

setWriterback to summary
public void setWriter(Writer writer)

Implements javax.script.ScriptContext.setWriter.

Doc from javax.script.ScriptContext.setWriter.

Sets the Writer for scripts to use when displaying output.

stdinReaderback to summary
private static InputStreamReader stdinReader()