- java.lang.Object
-
- javax.script.SimpleScriptContext
-
- All Implemented Interfaces:
-
ScriptContext
public class SimpleScriptContext extends Object implements ScriptContext
简单实现ScriptContext。- 从以下版本开始:
- 1.6
-
-
Field Summary
Fields Modifier and Type Field 描述 protected BindingsengineScope这是引擎范围绑定。protected WritererrorWriter这是用于从脚本输出错误的作者。protected BindingsglobalScope这是全局范围绑定。protected Readerreader这是用于从脚本输入的读者。protected Writerwriter这是用于从脚本输出的作者。-
Fields inherited from interface javax.script.ScriptContext
ENGINE_SCOPE, GLOBAL_SCOPE
-
-
构造方法摘要
构造方法 Constructor 描述 SimpleScriptContext()创建一个SimpleScriptContext。
-
方法摘要
所有方法 接口方法 具体的方法 Modifier and Type 方法 描述 ObjectgetAttribute(String name)在搜索顺序中最早出现的范围内检索具有给定名称的属性值。ObjectgetAttribute(String name, int scope)获取给定范围内的属性值。intgetAttributesScope(String name)获取定义属性的最低范围。BindingsgetBindings(int scope)如果指定范围为ENGINE_SCOPE则返回engineScope字段的值。WritergetErrorWriter()返回用于显示错误输出的Writer。ReadergetReader()返回一个Reader,由脚本用来读取输入。List<Integer>getScopes()返回不可变的List的ScriptContext中范围的所有有效值。WritergetWriter()返回显示输出时要使用的脚本的Writer。ObjectremoveAttribute(String name, int scope)删除给定范围内的属性。voidsetAttribute(String name, Object value, int scope)设置给定范围内的属性值。voidsetBindings(Bindings bindings, int scope)为给定的范围设置一个Bindings的属性。voidsetErrorWriter(Writer writer)设置Writer用于显示错误输出。voidsetReader(Reader reader)设置Reader脚本以读取输入。voidsetWriter(Writer writer)设置显示输出时要使用的脚本的Writer。
-
-
-
字段详细信息
-
writer
protected Writer writer
这是用于从脚本输出的作者。 默认情况下,PrintWriter基于System.out使用。 Accessor方法getWriter,setWriter用于管理此字段。- 另请参见:
-
System.out,PrintWriter
-
errorWriter
protected Writer errorWriter
这是用于从脚本输出错误的作者。 默认情况下,PrintWriter基于System.err使用。 访问者方法getErrorWriter,setErrorWriter用于管理此字段。- 另请参见:
-
System.err,PrintWriter
-
reader
protected Reader reader
这是用于从脚本输入的读者。 默认情况下,InputStreamReader基于System.in被使用,默认字符集是使用该阅读器。 访问者方法getReader,setReader用于管理此字段。- 另请参见:
-
System.in,InputStreamReader
-
engineScope
protected Bindings engineScope
这是引擎范围绑定。 默认情况下,使用SimpleBindings。 访问方法setBindings,getBindings用于管理此字段。- 另请参见:
-
SimpleBindings
-
globalScope
protected Bindings globalScope
这是全局范围绑定。 默认情况下,使用空值(这意味着没有全局范围)。 访问方法setBindings,getBindings用于管理此字段。
-
-
方法详细信息
-
setBindings
public void setBindings(Bindings bindings, int scope)
为给定的范围设置一个Bindings的属性。 如果范围值为ENGINE_SCOPE则给定Bindings替换engineScope字段。 如果范围值为GLOBAL_SCOPE则给出Bindings替换globalScope字段。- Specified by:
-
setBindings在接口ScriptContext - 参数
-
bindings- 要设置的属性的Bindings。 -
scope- 属性设置范围的值。 - 异常
-
IllegalArgumentException- 如果范围无效。 -
NullPointerException- 如果范围值为ENGINE_SCOPE,指定的Bindings为空。
-
getAttribute
public Object getAttribute(String name)
在搜索顺序中最早出现的范围内检索具有给定名称的属性值。 顺序由作用域参数的数值确定(最低范围值首先)- Specified by:
-
getAttribute在接口ScriptContext - 参数
-
name- 要检索的属性的名称。 - 结果
- 定义具有给定名称的属性的最低范围内的属性值。 如果在任何范围内都不存在名称的属性,则返回null。
- 异常
-
NullPointerException- 如果名称为空。 -
IllegalArgumentException- 如果名称为空。
-
getAttribute
public Object getAttribute(String name, int scope)
获取给定范围内的属性值。- Specified by:
-
getAttribute在接口ScriptContext - 参数
-
name- 要检索的属性的名称。 -
scope- 检索属性的范围。 - 结果
-
属性的值。
返回
null是给定范围内不存在的名称。 - 异常
-
IllegalArgumentException- 如果名称为空或范围的值无效。 -
NullPointerException- 如果名称为空。
-
removeAttribute
public Object removeAttribute(String name, int scope)
删除给定范围内的属性。- Specified by:
-
removeAttribute在接口ScriptContext - 参数
-
name- 要删除的属性的名称 -
scope- 删除属性的范围 - 结果
- 删除的值。
- 异常
-
IllegalArgumentException- 如果名称为空或范围无效。 -
NullPointerException- 如果名称为空。
-
setAttribute
public void setAttribute(String name, Object value, int scope)
设置给定范围内的属性值。 如果范围为GLOBAL_SCOPE并且没有为GLOBAL_SCOPE设置绑定,则setAttribute调用是无操作的。- Specified by:
-
setAttribute在接口ScriptContext - 参数
-
name- 要设置的属性的名称 -
value- 属性的值 -
scope- 设置属性的范围 - 异常
-
IllegalArgumentException- 如果名称为空或范围无效。 -
NullPointerException- 如果名称为空。
-
getWriter
public Writer getWriter()
返回显示输出时要使用的脚本的Writer。- Specified by:
-
getWriter在接口ScriptContext - 结果
-
Writer。
-
getReader
public Reader getReader()
返回一个Reader,由脚本用来读取输入。- Specified by:
-
getReader在接口ScriptContext - 结果
-
Reader。
-
setReader
public void setReader(Reader reader)
设置Reader的脚本以读取输入。- Specified by:
-
setReader在接口ScriptContext - 参数
-
reader- 新的Reader。
-
setWriter
public void setWriter(Writer writer)
设置Writer用于显示输出时使用的脚本。- Specified by:
-
setWriter在接口ScriptContext - 参数
-
writer- 新的Writer。
-
getErrorWriter
public Writer getErrorWriter()
返回用于显示错误输出的Writer。- Specified by:
-
getErrorWriter在接口ScriptContext - 结果
-
Writer
-
setErrorWriter
public void setErrorWriter(Writer writer)
设置Writer用于显示错误输出。- Specified by:
-
setErrorWriter在接口ScriptContext - 参数
-
writer-Writer。
-
getAttributesScope
public int getAttributesScope(String name)
获取定义属性的最低范围。- Specified by:
-
getAttributesScope在接口ScriptContext - 参数
-
name- 属性名称。 - 结果
- 最低的范围。 如果在任何范围内未定义具有给定名称的属性,则返回-1。
- 异常
-
NullPointerException- 如果name为null。 -
IllegalArgumentException- 如果名称为空。
-
getBindings
public Bindings getBindings(int scope)
如果指定范围是ENGINE_SCOPE则返回engineScope字段的值。 如果指定的范围是GLOBAL_SCOPE则返回globalScope字段的值。- Specified by:
-
getBindings在接口ScriptContext - 参数
-
scope- 指定的范围 - 结果
-
值为
engineScope或globalScope字段。 - 异常
-
IllegalArgumentException- 如果范围的值无效。
-
getScopes
public List<Integer> getScopes()
返回不可变的List的ScriptContext中范围的所有有效值。- Specified by:
-
getScopes在接口ScriptContext - 结果
- 范围值列表
-
-