/*
* @(#)SimpleScriptContext.java 1.6 06/06/19 20:55:48
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTAIL. Use is subject to license terms.
*/
package javax.script;
import java.util.*;
import java.io.*;
/**
* Simple implementation of ScriptContext.
*
* @author Mike Grogan
* @version 1.0
* @since 1.6
*/
public class SimpleScriptContext implements ScriptContext {
/**
* This is the writer to be used to output from scripts.
* By default, a <code>PrintWriter</code> based on <code>System.out</code>
* is used. Accessor methods getWriter, setWriter are used to manage
* this field.
* @see java.lang.System#out
* @see java.io.PrintWriter
*/
protected Writer writer;
/**
* This is the writer to be used to output errors from scripts.
* By default, a <code>PrintWriter</code> based on <code>System.err</code> is
* used. Accessor methods getErrorWriter, setErrorWriter are used to manage
* this field.
* @see java.lang.System#err
* @see java.io.PrintWriter
*/
protected Writer errorWriter;
/**
* This is the reader to be used for input from scripts.
* By default, a <code>InputStreamReader</code> based on <code>System.in</code>
* is used and default charset is used by this reader. Accessor methods
* getReader, setReader are used to manage this field.
* @see java.lang.System#in
* @see java.io.InputStreamReader
*/
protected Reader reader;
/**
* This is the engine scope bindings.
* By default, a <code>SimpleBindings</code> is used. Accessor
* methods setBindings, getBindings are used to manage this field.
* @see SimpleBindings
*/
protected Bindings engineScope;
/**
* 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.
*/
protected Bindings globalScope;
public SimpleScriptContext() {
engineScope = new SimpleBindings();
globalScope = null;
reader = new InputStreamReader(System.in);
writer = new PrintWriter(System.out , true);
errorWriter = new PrintWriter(System.err, true);
}
/**
* Sets a <code>Bindings</code> of attributes for the given scope. If the value
* of scope is <code>ENGINE_SCOPE</code> the given <code>Bindings</code> replaces the
* <code>engineScope</code> field. If the value
* of scope is <code>GLOBAL_SCOPE</code> the given <code>Bindings</code> replaces the
* <code>globalScope</code> field.
*
* @param bindings The <code>Bindings</code> of attributes to set.
* @param scope The value of the scope in which the attributes are set.
*
* @throws IllegalArgumentException if scope is invalid.
* @throws NullPointerException if the value of scope is <code>ENGINE_SCOPE</code> and
* the specified <code>Bindings</code> is null.
*/
public void setBindings(Bindings bindings, int scope) {
switch (scope) {
case ENGINE_SCOPE:
if (bindings == null) {
throw new NullPointerException("Engine scope cannot be null.");
}
engineScope = bindings;
break;
=1= |