001: /*
002: * %W% %E% %U%
003: *
004: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
005: * SUN PROPRIETARY/CONFIDENTAIL. Use is subject to license terms.
006: */
007:
008: package javax.script;
009:
010: import java.io.Reader;
011: import java.util.Map;
012: import java.util.Iterator;
013:
014: /**
015: * Provides a standard implementation for several of the variants of the <code>eval</code>
016: * method.
017: * <br><br>
018: * <code><b>eval(Reader)</b></code><p><code><b>eval(String)</b></code><p>
019: * <code><b>eval(String, Bindings)</b></code><p><code><b>eval(Reader, Bindings)</b></code>
020: * <br><br> are implemented using the abstract methods
021: * <br><br>
022: * <code><b>eval(Reader,ScriptContext)</b></code> or
023: * <code><b>eval(String, ScriptContext)</b></code>
024: * <br><br>
025: * with a <code>SimpleScriptContext</code>.
026: * <br><br>
027: * A <code>SimpleScriptContext</code> is used as the default <code>ScriptContext</code>
028: * of the <code>AbstractScriptEngine</code>..
029: *
030: * @author Mike Grogan
031: * @version 1.0
032: * @since 1.6
033: */
034: public abstract class AbstractScriptEngine implements ScriptEngine {
035:
036: /**
037: * The default <code>ScriptContext</code> of this <code>AbstractScriptEngine</code>.
038: */
039:
040: protected ScriptContext context;
041:
042: /**
043: * Creates a new instance of AbstractScriptEngine using a <code>SimpleScriptContext</code>
044: * as its default <code>ScriptContext</code>.
045: */
046: public AbstractScriptEngine() {
047:
048: context = new SimpleScriptContext();
049:
050: }
051:
052: /**
053: * Creates a new instance using the specified <code>Bindings</code> as the
054: * <code>ENGINE_SCOPE</code> <code>Bindings</code> in the protected <code>ScriptContext</code> field.
055: *
056: * @param n The specified <code>Bindings</code>.
057: */
058: public AbstractScriptEngine(Bindings n) {
059:
060: this ();
061: context.setBindings(n, ScriptContext.ENGINE_SCOPE);
062: }
063:
064: /**
065: * Sets the value of the protected <code>ScriptContext</code> field to the specified
066: * <code>ScriptContext</code>.
067: *
068: * @param ctxt The specified <code>ScriptContext</code>.
069: */
070: public void setContext(ScriptContext ctxt) {
071: context = ctxt;
072: }
073:
074: /**
075: * Returns the value of the protected <code>ScriptContext</code>> field.
076: *
077: * @return The value of the protected <code>ScriptContext</code> field.
078: */
079: public ScriptContext getContext() {
080: return context;
081: }
082:
083: /**
084: * Returns the <code>Bindings</code> with the specified scope value in
085: * the protected <code>ScriptContext</code> field.
086: *
087: * @param scope The specified scope
088: *
089: * @return The corresponding <code>Bindings</code>.
090: *
091: **@throws <code>IllegalArgumentException</code> if the value of scope is
092: * invalid for the type the protected <code>ScriptContext</code> field.
093: */
094:
095: public Bindings getBindings(int scope) {
096:
097: if (scope == ScriptContext.GLOBAL_SCOPE) {
098: return context.getBindings(ScriptContext.GLOBAL_SCOPE);
099: } else if (scope == ScriptContext.ENGINE_SCOPE) {
100: return context.getBindings(ScriptContext.ENGINE_SCOPE);
101: } else {
102: throw new IllegalArgumentException("Invalid scope value.");
103: }
104: }
105:
106: /**
107: * Sets the <code>Bindings</code> with the corresponding scope value in the
108: * <code>context</code> field.
109: *
110: * @param bindings The specified <code>Bindings</code>.
111: * @param scope The specified scope.
112: *
113: * @throws <code>IllegalArgumentException</code> if the value of scope is
114: * invalid for the type the <code>context</code> field.
115: */
116: public void setBindings(Bindings bindings, int scope) {
117:
118: if (scope == ScriptContext.GLOBAL_SCOPE) {
119: context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
120: ;
121: } else if (scope == ScriptContext.ENGINE_SCOPE) {
122: context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
123: ;
124: } else {
125: throw new IllegalArgumentException("Invalid scope value.");
126: }
127: }
128:
129: /**
130: * Sets the specified value with the specified key in the <code>ENGINE_SCOPE</code>
131: * <code>Bindings</code> of the protected <code>ScriptContext</code> field.
132: *
133: * @param key The specified key.
134: * @param value The specified value.
135: */
136: public void put(String key, Object value) {
137:
138: Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);
139: if (nn != null) {
140: nn.put(key, value);
141: }
142:
143: }
144:
145: /**
146: * Gets the value for the specified key in the <code>ENGINE_SCOPE</code> of the
147: * protected <code>ScriptContext</code> field.
148: *
149: * @return The value for the specified key.
150: */
151: public Object get(String key) {
152:
153: Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);
154: if (nn != null) {
155: return nn.get(key);
156: }
157:
158: return null;
159: }
160:
161: /**
162: * <code>eval(Reader, Bindings)</code> calls the abstract
163: * <code>eval(Reader, ScriptContext)</code> method, passing it a <code>ScriptContext</code>
164: * whose Reader, Writers and Bindings for scopes other that <code>ENGINE_SCOPE</code>
165: * are identical to those members of the protected <code>ScriptContext</code> field. The specified
166: * <code>Bindings</code> is used instead of the <code>ENGINE_SCOPE</code>
167: *
168: * <code>Bindings</code> of the <code>context</code> field.
169: *
170: * @param reader A <code>Reader</code> containing the source of the script.
171: * @param bindings A <code>Bindings</code> to use for the <code>ENGINE_SCOPE</code>
172: * while the script executes.
173: *
174: * @return The return value from <code>eval(Reader, ScriptContext)</code>
175: */
176: public Object eval(Reader reader, Bindings bindings)
177: throws ScriptException {
178:
179: ScriptContext ctxt = getScriptContext(bindings);
180:
181: return eval(reader, ctxt);
182: }
183:
184: /**
185: * Same as <code>eval(Reader, Bindings)</code> except that the abstract
186: * <code>eval(String, ScriptContext)</code> is used.
187: *
188: * @param script A <code>String</code> containing the source of the script.
189: *
190: * @param bindings A <code>Bindings</code> to use as the <code>ENGINE_SCOPE</code>
191: * while the script executes.
192: *
193: * @return The return value from <code>eval(String, ScriptContext)</code>
194: */
195: public Object eval(String script, Bindings bindings)
196: throws ScriptException {
197:
198: ScriptContext ctxt = getScriptContext(bindings);
199:
200: return eval(script, ctxt);
201: }
202:
203: /**
204: * <code>eval(Reader)</code> calls the abstract
205: * <code>eval(Reader, ScriptContext)</code> passing the value of the <code>context</code>
206: * field.
207: *
208: * @param reader A <code>Reader</code> containing the source of the script.
209: * @return The return value from <code>eval(Reader, ScriptContext)</code>
210: */
211: public Object eval(Reader reader) throws ScriptException {
212:
213: return eval(reader, context);
214: }
215:
216: /**
217: * Same as <code>eval(Reader)</code> except that the abstract
218: * <code>eval(String, ScriptContext)</code> is used.
219: *
220: * @param script A <code>String</code> containing the source of the script.
221: * @return The return value from <code>eval(String, ScriptContext)</code>
222: */
223: public Object eval(String script) throws ScriptException {
224:
225: return eval(script, context);
226: }
227:
228: /**
229: * Returns a <code>SimpleScriptContext</code>. The <code>SimpleScriptContext</code>:
230: *<br><br>
231: * <ul>
232: * <li>Uses the specified <code>Bindings</code> for its <code>ENGINE_SCOPE</code>
233: * </li>
234: * <li>Uses the <code>Bindings</code> returned by the abstract <code>getGlobalScope</code>
235: * method as its <code>GLOBAL_SCOPE</code>
236: * </li>
237: * <li>Uses the Reader and Writer in the default <code>ScriptContext</code> of this
238: * <code>ScriptEngine</code>
239: * </li>
240: * </ul>
241: * <br><br>
242: * A <code>SimpleScriptContext</code> returned by this method is used to implement eval methods
243: * using the abstract <code>eval(Reader,Bindings)</code> and <code>eval(String,Bindings)</code>
244: * versions.
245: *
246: * @param nn Bindings to use for the <code>ENGINE_SCOPE</code>
247: * @return The <code>SimpleScriptContext</code>
248: */
249: protected ScriptContext getScriptContext(Bindings nn) {
250:
251: SimpleScriptContext ctxt = new SimpleScriptContext();
252: Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE);
253:
254: if (gs != null) {
255: ctxt.setBindings(gs, ScriptContext.GLOBAL_SCOPE);
256: }
257:
258: if (nn != null) {
259: ctxt.setBindings(nn, ScriptContext.ENGINE_SCOPE);
260: } else {
261: throw new NullPointerException(
262: "Engine scope Bindings may not be null.");
263: }
264:
265: ctxt.setReader(context.getReader());
266: ctxt.setWriter(context.getWriter());
267: ctxt.setErrorWriter(context.getErrorWriter());
268:
269: return ctxt;
270:
271: }
272: }
|