public class SimpleScriptContext extends Object implements ScriptContext
Simple implementation of ScriptContext.
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.
System.out
, PrintWriter
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.
System.err
, PrintWriter
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.
System.in
, InputStreamReader
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.
SimpleBindings
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.
public SimpleScriptContext()
Create a SimpleScriptContext
.
public void setBindings(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.
setBindings
in interface ScriptContext
bindings
- The Bindings
of attributes to set.scope
- The value of the scope in which the attributes are set.IllegalArgumentException
- if scope is invalid.NullPointerException
- if the value of scope is ENGINE_SCOPE
and the specified Bindings
is null.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.)
getAttribute
in interface ScriptContext
name
- The name of the the attribute to retrieve.NullPointerException
- if the name is null.IllegalArgumentException
- if the name is empty.public Object getAttribute(String name, int scope)
Gets the value of an attribute in a given scope.
getAttribute
in interface ScriptContext
name
- The name of the attribute to retrieve.scope
- The scope in which to retrieve the attribute.null
is the name does not exist in the given scope.IllegalArgumentException
- if the name is empty or if the value of scope is invalid.NullPointerException
- if the name is null.public Object removeAttribute(String name, int scope)
Remove an attribute in a given scope.
removeAttribute
in interface ScriptContext
name
- The name of the attribute to removescope
- The scope in which to remove the attributeIllegalArgumentException
- if the name is empty or if the scope is invalid.NullPointerException
- if the name is null.public void setAttribute(String name, Object value, int scope)
Sets the value of an attribute in a given scope.
setAttribute
in interface ScriptContext
name
- The name of the attribute to setvalue
- The value of the attributescope
- The scope in which to set the attributeIllegalArgumentException
- if the name is empty or if the scope is invalid.NullPointerException
- if the name is null.public Writer getWriter()
Returns the Writer
for scripts to use when displaying output.
getWriter
in interface ScriptContext
Writer
.public Reader getReader()
Returns a Reader
to be used by the script to read input.
getReader
in interface ScriptContext
Reader
.public void setReader(Reader reader)
Sets the Reader
for scripts to read input .
setReader
in interface ScriptContext
reader
- The new Reader
.public void setWriter(Writer writer)
Sets the Writer
for scripts to use when displaying output.
setWriter
in interface ScriptContext
writer
- The new Writer
.public Writer getErrorWriter()
Returns the Writer
used to display error output.
getErrorWriter
in interface ScriptContext
Writer
public void setErrorWriter(Writer writer)
Sets the Writer
used to display error output.
setErrorWriter
in interface ScriptContext
writer
- The Writer
.public int getAttributesScope(String name)
Get the lowest scope in which an attribute is defined.
getAttributesScope
in interface ScriptContext
name
- Name of the attribute .NullPointerException
- if name is null.IllegalArgumentException
- if name is empty.public Bindings getBindings(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
.
getBindings
in interface ScriptContext
scope
- The specified scopeengineScope
or globalScope
field.IllegalArgumentException
- if the value of scope is invalid.public List<Integer> getScopes()
Returns immutable List
of all the valid values for scope in the ScriptContext.
getScopes
in interface ScriptContext
© 1993–2017, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.