APIdia Launch Partner
Top Description Fields Methods
javax.script

public Interface ScriptContext

Known Direct Implementers
javax.script.SimpleScriptContext
Imports
java.util.List, java.io.Writer, .Reader

The interface whose implementing classes are used to connect Script Engines with objects, such as scoped Bindings, in hosting applications. Each scope is a set of named attributes whose values can be set and retrieved using the ScriptContext methods. ScriptContexts also expose Readers and Writers that can be used by the ScriptEngines for input and output.
Author
Mike Grogan
Since
1.6

Field Summary

Modifier and TypeField and Description
public static final int
ENGINE_SCOPE

EngineScope attributes are visible during the lifetime of a single ScriptEngine and a set of attributes is maintained for each engine.

public static final int
GLOBAL_SCOPE

GlobalScope attributes are visible to all engines created by same ScriptEngineFactory.

Method Summary

Modifier and TypeMethod and Description
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
)

Gets the value of an attribute in a given scope.

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
)

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

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
)

Get the lowest scope in which an attribute is defined.

public Bindings

Returns:

The associated Bindings. Returns null if it has not been set.
getBindings
(int
The scope
scope
)

Gets the Bindings associated with the given scope in this ScriptContext.

public Writer

Returns:

The Writer
getErrorWriter
()

Returns the Writer used to display error output.

public Reader

Returns:

The Reader.
getReader
()

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

public List<Integer>

Returns:

list of scope values
getScopes
()

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

public Writer

Returns:

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

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
)

Sets the value of an attribute in a given scope.

public void
setBindings(Bindings
The Bindings to associate with the given scope
bindings
,
int
The scope
scope
)

Associates a Bindings instance with a particular scope in this ScriptContext.

public void
setErrorWriter(Writer
The Writer.
writer
)

Sets the Writer used to display error output.

public void
setReader(Reader
The new Reader.
reader
)

Sets the Reader for scripts to read input .

public void
setWriter(Writer
The new Writer.
writer
)

Sets the Writer for scripts to use when displaying output.

Field Detail

ENGINE_SCOPEback to summary
public static final int ENGINE_SCOPE

EngineScope attributes are visible during the lifetime of a single ScriptEngine and a set of attributes is maintained for each engine.

GLOBAL_SCOPEback to summary
public static final int GLOBAL_SCOPE

GlobalScope attributes are visible to all engines created by same ScriptEngineFactory.

Method Detail

getAttributeback to summary
public Object getAttribute(String name, int scope)

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.
getAttributeback to summary
public Object getAttribute(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.)

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.
getAttributesScopeback to summary
public int getAttributesScope(String name)

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)

Gets the Bindings associated with the given scope in this ScriptContext.

Parameters
scope:int

The scope

Returns:Bindings

The associated Bindings. Returns null if it has not been set.

Exceptions
IllegalArgumentException:
If no Bindings is defined for the specified scope value in ScriptContext of this type.
getErrorWriterback to summary
public Writer getErrorWriter()

Returns the Writer used to display error output.

Returns:Writer

The Writer

getReaderback to summary
public Reader getReader()

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

Returns:Reader

The Reader.

getScopesback to summary
public List<Integer> getScopes()

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

Returns:List<Integer>

list of scope values

getWriterback to summary
public Writer getWriter()

Returns the Writer for scripts to use when displaying output.

Returns:Writer

The Writer.

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

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)

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)

Associates a Bindings instance with a particular scope in this ScriptContext. Calls to the getAttribute and setAttribute methods must map to the get and put methods of the Bindings for the specified scope.

Parameters
bindings:Bindings

The Bindings to associate with the given scope

scope:int

The scope

Exceptions
IllegalArgumentException:
If no Bindings is defined for the specified scope value in ScriptContexts of this type.
NullPointerException:
if value of scope is ENGINE_SCOPE and the specified Bindings is null.
setErrorWriterback to summary
public void setErrorWriter(Writer writer)

Sets the Writer used to display error output.

Parameters
writer:Writer

The Writer.

setReaderback to summary
public void setReader(Reader reader)

Sets the Reader for scripts to read input .

Parameters
reader:Reader

The new Reader.

setWriterback to summary
public void setWriter(Writer writer)

Sets the Writer for scripts to use when displaying output.

Parameters
writer:Writer

The new Writer.