* <code>eval(String, ScriptContext)</code> is used.
*
* @param script A <code>String</code> containing the source of the script.
*
* @param bindings A <code>Bindings</code> to use as the <code>ENGINE_SCOPE</code>
* while the script executes.
*
* @return The return value from <code>eval(String, ScriptContext)</code>
* @throws ScriptException if an error occurs in script.
* @throws NullPointerException if any of the parameters is null.
*/
public Object eval(String script, Bindings bindings) throws ScriptException {
ScriptContext ctxt = getScriptContext(bindings);
return eval(script , ctxt);
}
/**
* <code>eval(Reader)</code> calls the abstract
* <code>eval(Reader, ScriptContext)</code> passing the value of the <code>context</code>
* field.
*
* @param reader A <code>Reader</code> containing the source of the script.
* @return The return value from <code>eval(Reader, ScriptContext)</code>
* @throws ScriptException if an error occurs in script.
* @throws NullPointerException if any of the parameters is null.
*/
public Object eval(Reader reader) throws ScriptException {
return eval(reader, context);
}
/**
* Same as <code>eval(Reader)</code> except that the abstract
* <code>eval(String, ScriptContext)</code> is used.
*
* @param script A <code>String</code> containing the source of the script.
* @return The return value from <code>eval(String, ScriptContext)</code>
* @throws ScriptException if an error occurrs in script.
* @throws NullPointerException if any of the parameters is null.
*/
public Object eval(String script) throws ScriptException {
return eval(script, context);
}
/**
* Returns a <code>SimpleScriptContext</code>. The <code>SimpleScriptContext</code>:
*<br><br>
* <ul>
* <li>Uses the specified <code>Bindings</code> for its <code>ENGINE_SCOPE</code>
* </li>
* <li>Uses the <code>Bindings</code> returned by the abstract <code>getGlobalScope</code>
* method as its <code>GLOBAL_SCOPE</code>
* </li>
* <li>Uses the Reader and Writer in the default <code>ScriptContext</code> of this
* <code>ScriptEngine</code>
* </li>
* </ul>
* <br><br>
* A <code>SimpleScriptContext</code> returned by this method is used to implement eval methods
* using the abstract <code>eval(Reader,Bindings)</code> and <code>eval(String,Bindings)</code>
* versions.
*
* @param nn Bindings to use for the <code>ENGINE_SCOPE</code>
* @return The <code>SimpleScriptContext</code>
*/
protected ScriptContext getScriptContext(Bindings nn) {
SimpleScriptContext ctxt = new SimpleScriptContext();
Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE);
if (gs != null) {
ctxt.setBindings(gs, ScriptContext.GLOBAL_SCOPE);
}
if (nn != null) {
ctxt.setBindings(nn,
ScriptContext.ENGINE_SCOPE);
} else {
throw new NullPointerException("Engine scope Bindings may not be null.");
}
ctxt.setReader(context.getReader());
ctxt.setWriter(context.getWriter());
ctxt.setErrorWriter(context.getErrorWriter());
return ctxt;
}
}
=3=
THE END |