KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > script > AbstractScriptEngine


1 /*
2  * @(#)AbstractScriptEngine.java 1.5 06/06/19 20:54:01
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTAIL. Use is subject to license terms.
6  */

7
8 package javax.script;
9 import java.io.Reader JavaDoc;
10 import java.util.Map JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13 /**
14  * Provides a standard implementation for several of the variants of the <code>eval</code>
15  * method.
16  * <br><br>
17  * <code><b>eval(Reader)</b></code><p><code><b>eval(String)</b></code><p>
18  * <code><b>eval(String, Bindings)</b></code><p><code><b>eval(Reader, Bindings)</b></code>
19  * <br><br> are implemented using the abstract methods
20  * <br><br>
21  * <code><b>eval(Reader,ScriptContext)</b></code> or
22  * <code><b>eval(String, ScriptContext)</b></code>
23  * <br><br>
24  * with a <code>SimpleScriptContext</code>.
25  * <br><br>
26  * A <code>SimpleScriptContext</code> is used as the default <code>ScriptContext</code>
27  * of the <code>AbstractScriptEngine</code>..
28  *
29  * @author Mike Grogan
30  * @version 1.0
31  * @since 1.6
32  */

33 public abstract class AbstractScriptEngine implements ScriptEngine {
34     
35     /**
36      * The default <code>ScriptContext</code> of this <code>AbstractScriptEngine</code>.
37      */

38     
39     protected ScriptContext context;
40     
41     /**
42      * Creates a new instance of AbstractScriptEngine using a <code>SimpleScriptContext</code>
43      * as its default <code>ScriptContext</code>.
44      */

45     public AbstractScriptEngine() {
46         
47         context = new SimpleScriptContext();
48         
49     }
50     
51     /**
52      * Creates a new instance using the specified <code>Bindings</code> as the
53      * <code>ENGINE_SCOPE</code> <code>Bindings</code> in the protected <code>context</code> field.
54      *
55      * @param n The specified <code>Bindings</code>.
56      * @throws NullPointerException if n is null.
57      */

58     public AbstractScriptEngine(Bindings n) {
59         
60         this();
61         if (n == null) {
62             throw new NullPointerException JavaDoc("n is null");
63         }
64         context.setBindings(n, ScriptContext.ENGINE_SCOPE);
65     }
66     
67     /**
68      * Sets the value of the protected <code>context</code> field to the specified
69      * <code>ScriptContext</code>.
70      *
71      * @param ctxt The specified <code>ScriptContext</code>.
72      * @throws NullPointerException if ctxt is null.
73      */

74     public void setContext(ScriptContext ctxt) {
75         if (ctxt == null) {
76             throw new NullPointerException JavaDoc("null context");
77         }
78         context = ctxt;
79     }
80     
81     /**
82      * Returns the value of the protected <code>context</code> field.
83      *
84      * @return The value of the protected <code>context</code> field.
85      */

86     public ScriptContext getContext() {
87         return context;
88     }
89     
90     /**
91      * Returns the <code>Bindings</code> with the specified scope value in
92      * the protected <code>context</code> field.
93      *
94      * @param scope The specified scope
95      *
96      * @return The corresponding <code>Bindings</code>.
97      *
98      * @throws IllegalArgumentException if the value of scope is
99      * invalid for the type the protected <code>context</code> field.
100      */

101     public Bindings getBindings(int scope) {
102         
103         if (scope == ScriptContext.GLOBAL_SCOPE) {
104             return context.getBindings(ScriptContext.GLOBAL_SCOPE);
105         } else if (scope == ScriptContext.ENGINE_SCOPE) {
106             return context.getBindings(ScriptContext.ENGINE_SCOPE);
107         } else {
108             throw new IllegalArgumentException JavaDoc("Invalid scope value.");
109         }
110     }
111     
112     /**
113      * Sets the <code>Bindings</code> with the corresponding scope value in the
114      * <code>context</code> field.
115      *
116      * @param bindings The specified <code>Bindings</code>.
117      * @param scope The specified scope.
118      *
119      * @throws IllegalArgumentException if the value of scope is
120      * invalid for the type the <code>context</code> field.
121      * @throws NullPointerException if the bindings is null and the scope is
122      * <code>ScriptContext.ENGINE_SCOPE</code>
123      */

124     public void setBindings(Bindings bindings, int scope) {
125         
126         if (scope == ScriptContext.GLOBAL_SCOPE) {
127             context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);;
128         } else if (scope == ScriptContext.ENGINE_SCOPE) {
129             context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);;
130         } else {
131             throw new IllegalArgumentException JavaDoc("Invalid scope value.");
132         }
133     }
134     
135     /**
136      * Sets the specified value with the specified key in the <code>ENGINE_SCOPE</code>
137      * <code>Bindings</code> of the protected <code>context</code> field.
138      *
139      * @param key The specified key.
140      * @param value The specified value.
141      *
142      * @throws NullPointerException if key is null.
143      * @throws IllegalArgumentException if key is empty.
144      */

145     public void put(String JavaDoc key, Object JavaDoc value) {
146         
147         Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);
148         if (nn != null) {
149             nn.put(key, value);
150         }
151         
152     }
153     
154     /**
155      * Gets the value for the specified key in the <code>ENGINE_SCOPE</code> of the
156      * protected <code>context</code> field.
157      *
158      * @return The value for the specified key.
159      *
160      * @throws NullPointerException if key is null.
161      * @throws IllegalArgumentException if key is empty.
162      */

163     public Object JavaDoc get(String JavaDoc key) {
164         
165         Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);
166         if (nn != null) {
167             return nn.get(key);
168         }
169         
170         return null;
171     }
172     
173     
174     /**
175      * <code>eval(Reader, Bindings)</code> calls the abstract
176      * <code>eval(Reader, ScriptContext)</code> method, passing it a <code>ScriptContext</code>
177      * whose Reader, Writers and Bindings for scopes other that <code>ENGINE_SCOPE</code>
178      * are identical to those members of the protected <code>context</code> field. The specified
179      * <code>Bindings</code> is used instead of the <code>ENGINE_SCOPE</code>
180      *
181      * <code>Bindings</code> of the <code>context</code> field.
182      *
183      * @param reader A <code>Reader</code> containing the source of the script.
184      * @param bindings A <code>Bindings</code> to use for the <code>ENGINE_SCOPE</code>
185      * while the script executes.
186      *
187      * @return The return value from <code>eval(Reader, ScriptContext)</code>
188      * @throws ScriptException if an error occurs in script.
189      * @throws NullPointerException if any of the parameters is null.
190      */

191     public Object JavaDoc eval(Reader JavaDoc reader, Bindings bindings ) throws ScriptException {
192         
193         ScriptContext ctxt = getScriptContext(bindings);
194         
195         return eval(reader, ctxt);
196     }
197     
198     
199     /**
200      * Same as <code>eval(Reader, Bindings)</code> except that the abstract
201      * <code>eval(String, ScriptContext)</code> is used.
202      *
203      * @param script A <code>String</code> containing the source of the script.
204      *
205      * @param bindings A <code>Bindings</code> to use as the <code>ENGINE_SCOPE</code>
206      * while the script executes.
207      *
208      * @return The return value from <code>eval(String, ScriptContext)</code>
209      * @throws ScriptException if an error occurs in script.
210      * @throws NullPointerException if any of the parameters is null.
211      */

212     public Object JavaDoc eval(String JavaDoc script, Bindings bindings) throws ScriptException {
213         
214         ScriptContext ctxt = getScriptContext(bindings);
215         
216         return eval(script , ctxt);
217     }
218     
219     /**
220      * <code>eval(Reader)</code> calls the abstract
221      * <code>eval(Reader, ScriptContext)</code> passing the value of the <code>context</code>
222      * field.
223      *
224      * @param reader A <code>Reader</code> containing the source of the script.
225      * @return The return value from <code>eval(Reader, ScriptContext)</code>
226      * @throws ScriptException if an error occurs in script.
227      * @throws NullPointerException if any of the parameters is null.
228      */

229     public Object JavaDoc eval(Reader JavaDoc reader) throws ScriptException {
230         
231         
232         return eval(reader, context);
233     }
234     
235     /**
236      * Same as <code>eval(Reader)</code> except that the abstract
237      * <code>eval(String, ScriptContext)</code> is used.
238      *
239      * @param script A <code>String</code> containing the source of the script.
240      * @return The return value from <code>eval(String, ScriptContext)</code>
241      * @throws ScriptException if an error occurrs in script.
242      * @throws NullPointerException if any of the parameters is null.
243      */

244     public Object JavaDoc eval(String JavaDoc script) throws ScriptException {
245         
246         
247         return eval(script, context);
248     }
249     
250     /**
251      * Returns a <code>SimpleScriptContext</code>. The <code>SimpleScriptContext</code>:
252      *<br><br>
253      * <ul>
254      * <li>Uses the specified <code>Bindings</code> for its <code>ENGINE_SCOPE</code>
255      * </li>
256      * <li>Uses the <code>Bindings</code> returned by the abstract <code>getGlobalScope</code>
257      * method as its <code>GLOBAL_SCOPE</code>
258      * </li>
259      * <li>Uses the Reader and Writer in the default <code>ScriptContext</code> of this
260      * <code>ScriptEngine</code>
261      * </li>
262      * </ul>
263      * <br><br>
264      * A <code>SimpleScriptContext</code> returned by this method is used to implement eval methods
265      * using the abstract <code>eval(Reader,Bindings)</code> and <code>eval(String,Bindings)</code>
266      * versions.
267      *
268      * @param nn Bindings to use for the <code>ENGINE_SCOPE</code>
269      * @return The <code>SimpleScriptContext</code>
270      */

271     protected ScriptContext getScriptContext(Bindings nn) {
272         
273         SimpleScriptContext ctxt = new SimpleScriptContext();
274         Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE);
275         
276         if (gs != null) {
277             ctxt.setBindings(gs, ScriptContext.GLOBAL_SCOPE);
278         }
279         
280         if (nn != null) {
281             ctxt.setBindings(nn,
282                     ScriptContext.ENGINE_SCOPE);
283         } else {
284             throw new NullPointerException JavaDoc("Engine scope Bindings may not be null.");
285         }
286         
287         ctxt.setReader(context.getReader());
288         ctxt.setWriter(context.getWriter());
289         ctxt.setErrorWriter(context.getErrorWriter());
290         
291         return ctxt;
292         
293     }
294 }
295
Popular Tags