Class SimpleScriptContext
java.lang.Object
javax.script.SimpleScriptContext
- All Implemented Interfaces:
ScriptContext
Simple implementation of ScriptContext.
- Since:
- 1.6
-
Field Summary
Modifier and TypeFieldDescriptionprotected Bindings
This is the engine scope bindings.protected Writer
This is the writer to be used to output errors from scripts.protected Bindings
This is the global scope bindings.protected Reader
This is the reader to be used for input from scripts.protected Writer
This is the writer to be used to output from scripts.Fields declared in interface javax.script.ScriptContext
ENGINE_SCOPE, GLOBAL_SCOPE
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiongetAttribute
(String name) Retrieves the value of the attribute with the given name in the scope occurring earliest in the search order.getAttribute
(String name, int scope) Gets the value of an attribute in a given scope.int
getAttributesScope
(String name) Get the lowest scope in which an attribute is defined.getBindings
(int scope) Returns the value of theengineScope
field if specified scope isENGINE_SCOPE
.Returns theWriter
used to display error output.Returns aReader
to be used by the script to read input.Returns immutableList
of all the valid values for scope in the ScriptContext.Returns theWriter
for scripts to use when displaying output.removeAttribute
(String name, int scope) Remove an attribute in a given scope.void
setAttribute
(String name, Object value, int scope) Sets the value of an attribute in a given scope.void
setBindings
(Bindings bindings, int scope) Sets aBindings
of attributes for the given scope.void
setErrorWriter
(Writer writer) Sets theWriter
used to display error output.void
Sets theReader
for scripts to read input .void
Sets theWriter
for scripts to use when displaying output.
-
Field Details
-
writer
This is the writer to be used to output from scripts. By default, aPrintWriter
based onSystem.out
is used. Accessor methods getWriter, setWriter are used to manage this field.- See Also:
-
errorWriter
This is the writer to be used to output errors from scripts. By default, aPrintWriter
based onSystem.err
is used. Accessor methods getErrorWriter, setErrorWriter are used to manage this field.- See Also:
-
reader
This is the reader to be used for input from scripts. By default, aInputStreamReader
based onSystem.in
is used and default charset is used by this reader. Accessor methods getReader, setReader are used to manage this field.- See Also:
-
engineScope
This is the engine scope bindings. By default, aSimpleBindings
is used. Accessor methods setBindings, getBindings are used to manage this field.- See Also:
-
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.
-
-
Constructor Details
-
SimpleScriptContext
public SimpleScriptContext()Create aSimpleScriptContext
.
-
-
Method Details
-
setBindings
Sets aBindings
of attributes for the given scope. If the value of scope isENGINE_SCOPE
the givenBindings
replaces theengineScope
field. If the value of scope isGLOBAL_SCOPE
the givenBindings
replaces theglobalScope
field.- Specified by:
setBindings
in interfaceScriptContext
- Parameters:
bindings
- TheBindings
of attributes to set.scope
- The value of the scope in which the attributes are set.- Throws:
IllegalArgumentException
- if scope is invalid.NullPointerException
- if the value of scope isENGINE_SCOPE
and the specifiedBindings
is null.
-
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.)- Specified by:
getAttribute
in interfaceScriptContext
- Parameters:
name
- The name of the attribute to retrieve.- 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.
- Throws:
NullPointerException
- if the name is null.IllegalArgumentException
- if the name is empty.
-
getAttribute
Gets the value of an attribute in a given scope.- Specified by:
getAttribute
in interfaceScriptContext
- Parameters:
name
- The name of the attribute to retrieve.scope
- The scope in which to retrieve the attribute.- Returns:
- 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.NullPointerException
- if the name is null.
-
removeAttribute
Remove an attribute in a given scope.- Specified by:
removeAttribute
in interfaceScriptContext
- Parameters:
name
- The name of the attribute to removescope
- The scope in which to remove the attribute- Returns:
- The removed value.
- Throws:
IllegalArgumentException
- if the name is empty or if the scope is invalid.NullPointerException
- if the name is null.
-
setAttribute
Sets the value of an attribute in a given scope. If the scope isGLOBAL_SCOPE
and no Bindings is set forGLOBAL_SCOPE
, then setAttribute call is a no-op.- Specified by:
setAttribute
in interfaceScriptContext
- Parameters:
name
- The name of the attribute to setvalue
- The value of the attributescope
- The scope in which to set the attribute- Throws:
IllegalArgumentException
- if the name is empty or if the scope is invalid.NullPointerException
- if the name is null.
-
getWriter
Returns theWriter
for scripts to use when displaying output.- Specified by:
getWriter
in interfaceScriptContext
- Returns:
- The
Writer
.
-
getReader
Returns aReader
to be used by the script to read input.- Specified by:
getReader
in interfaceScriptContext
- Returns:
- The
Reader
.
-
setReader
Sets theReader
for scripts to read input .- Specified by:
setReader
in interfaceScriptContext
- Parameters:
reader
- The newReader
.
-
setWriter
Sets theWriter
for scripts to use when displaying output.- Specified by:
setWriter
in interfaceScriptContext
- Parameters:
writer
- The newWriter
.
-
getErrorWriter
Returns theWriter
used to display error output.- Specified by:
getErrorWriter
in interfaceScriptContext
- Returns:
- The
Writer
-
setErrorWriter
Sets theWriter
used to display error output.- Specified by:
setErrorWriter
in interfaceScriptContext
- Parameters:
writer
- TheWriter
.
-
getAttributesScope
Get the lowest scope in which an attribute is defined.- Specified by:
getAttributesScope
in interfaceScriptContext
- Parameters:
name
- Name of the attribute .- Returns:
- The lowest scope. Returns -1 if no attribute with the given name is defined in any scope.
- Throws:
NullPointerException
- if name is null.IllegalArgumentException
- if name is empty.
-
getBindings
Returns the value of theengineScope
field if specified scope isENGINE_SCOPE
. Returns the value of theglobalScope
field if the specified scope isGLOBAL_SCOPE
.- Specified by:
getBindings
in interfaceScriptContext
- Parameters:
scope
- The specified scope- Returns:
- The value of either the
engineScope
orglobalScope
field. - Throws:
IllegalArgumentException
- if the value of scope is invalid.
-
getScopes
Returns immutableList
of all the valid values for scope in the ScriptContext.- Specified by:
getScopes
in interfaceScriptContext
- Returns:
- list of scope values
-