KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > script > SimpleScriptContext


1 /*
2  * @(#)SimpleScriptContext.java 1.6 06/06/19 20:55:48
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
10 import java.util.*;
11 import java.io.*;
12
13 /**
14  * Simple implementation of ScriptContext.
15  *
16  * @author Mike Grogan
17  * @version 1.0
18  * @since 1.6
19  */

20 public class SimpleScriptContext implements ScriptContext {
21     
22     /**
23      * This is the writer to be used to output from scripts.
24      * By default, a <code>PrintWriter</code> based on <code>System.out</code>
25      * is used. Accessor methods getWriter, setWriter are used to manage
26      * this field.
27      * @see java.lang.System#out
28      * @see java.io.PrintWriter
29      */

30     protected Writer writer;
31     
32     /**
33      * This is the writer to be used to output errors from scripts.
34      * By default, a <code>PrintWriter</code> based on <code>System.err</code> is
35      * used. Accessor methods getErrorWriter, setErrorWriter are used to manage
36      * this field.
37      * @see java.lang.System#err
38      * @see java.io.PrintWriter
39      */

40     protected Writer errorWriter;
41     
42     /**
43      * This is the reader to be used for input from scripts.
44      * By default, a <code>InputStreamReader</code> based on <code>System.in</code>
45      * is used and default charset is used by this reader. Accessor methods
46      * getReader, setReader are used to manage this field.
47      * @see java.lang.System#in
48      * @see java.io.InputStreamReader
49      */

50     protected Reader reader;
51     
52     
53     /**
54      * This is the engine scope bindings.
55      * By default, a <code>SimpleBindings</code> is used. Accessor
56      * methods setBindings, getBindings are used to manage this field.
57      * @see SimpleBindings
58      */

59     protected Bindings engineScope;
60     
61     /**
62      * This is the global scope bindings.
63      * By default, a null value (which means no global scope) is used. Accessor
64      * methods setBindings, getBindings are used to manage this field.
65      */

66     protected Bindings globalScope;
67     
68     
69     public SimpleScriptContext() {
70         engineScope = new SimpleBindings();
71         globalScope = null;
72         reader = new InputStreamReader(System.in);
73         writer = new PrintWriter(System.out , true);
74         errorWriter = new PrintWriter(System.err, true);
75     }
76     
77     /**
78      * Sets a <code>Bindings</code> of attributes for the given scope. If the value
79      * of scope is <code>ENGINE_SCOPE</code> the given <code>Bindings</code> replaces the
80      * <code>engineScope</code> field. If the value
81      * of scope is <code>GLOBAL_SCOPE</code> the given <code>Bindings</code> replaces the
82      * <code>globalScope</code> field.
83      *
84      * @param bindings The <code>Bindings</code> of attributes to set.
85      * @param scope The value of the scope in which the attributes are set.
86      *
87      * @throws IllegalArgumentException if scope is invalid.
88      * @throws NullPointerException if the value of scope is <code>ENGINE_SCOPE</code> and
89      * the specified <code>Bindings</code> is null.
90      */

91     public void setBindings(Bindings bindings, int scope) {
92         
93         switch (scope) {
94             
95             case ENGINE_SCOPE:
96                 if (bindings == null) {
97                     throw new NullPointerException JavaDoc("Engine scope cannot be null.");
98                 }
99                 engineScope = bindings;
100                 break;
101             case GLOBAL_SCOPE:
102                 globalScope = bindings;
103                 break;
104             default:
105                 throw new IllegalArgumentException JavaDoc("Invalid scope value.");
106         }
107     }
108     
109     
110     /**
111      * Retrieves the value of the attribute with the given name in
112      * the scope occurring earliest in the search order. The order
113      * is determined by the numeric value of the scope parameter (lowest
114      * scope values first.)
115      *
116      * @param name The name of the the attribute to retrieve.
117      * @return The value of the attribute in the lowest scope for
118      * which an attribute with the given name is defined. Returns
119      * null if no attribute with the name exists in any scope.
120      * @throws NullPointerException if the name is null.
121      * @throws IllegalArgumentException if the name is empty.
122      */

123     public Object JavaDoc getAttribute(String JavaDoc name) {
124         if (engineScope.containsKey(name)) {
125             return getAttribute(name, ENGINE_SCOPE);
126         } else if (globalScope != null && globalScope.containsKey(name)) {
127             return getAttribute(name, GLOBAL_SCOPE);
128         }
129         
130         return null;
131     }
132     
133     /**
134      * Gets the value of an attribute in a given scope.
135      *
136      * @param name The name of the attribute to retrieve.
137      * @param scope The scope in which to retrieve the attribute.
138      * @return The value of the attribute. Returns <code>null</code> is the name
139      * does not exist in the given scope.
140      *
141      * @throws IllegalArgumentException
142      * if the name is empty or if the value of scope is invalid.
143      * @throws NullPointerException if the name is null.
144      */

145     public Object JavaDoc getAttribute(String JavaDoc name, int scope) {
146         
147         switch (scope) {
148             
149             case ENGINE_SCOPE:
150                 return engineScope.get(name);
151                 
152             case GLOBAL_SCOPE:
153                 if (globalScope != null) {
154                     return globalScope.get(name);
155                 }
156                 return null;
157                 
158             default:
159                 throw new IllegalArgumentException JavaDoc("Illegal scope value.");
160         }
161     }
162  
163     /**
164      * Remove an attribute in a given scope.
165      *
166      * @param name The name of the attribute to remove
167      * @param scope The scope in which to remove the attribute
168      *
169      * @return The removed value.
170      * @throws IllegalArgumentException
171      * if the name is empty or if the scope is invalid.
172      * @throws NullPointerException if the name is null.
173      */

174     public Object JavaDoc removeAttribute(String JavaDoc name, int scope) {
175         
176         switch (scope) {
177             
178             case ENGINE_SCOPE:
179                 if (getBindings(ENGINE_SCOPE) != null) {
180                     return getBindings(ENGINE_SCOPE).remove(name);
181                 }
182                 return null;
183                 
184             case GLOBAL_SCOPE:
185                 if (getBindings(GLOBAL_SCOPE) != null) {
186                     return getBindings(GLOBAL_SCOPE).remove(name);
187                 }
188                 return null;
189                 
190             default:
191                 throw new IllegalArgumentException JavaDoc("Illegal scope value.");
192         }
193     }
194    
195     /**
196      * Sets the value of an attribute in a given scope.
197      *
198      * @param name The name of the attribute to set
199      * @param value The value of the attribute
200      * @param scope The scope in which to set the attribute
201      *
202      * @throws IllegalArgumentException
203      * if the name is empty or if the scope is invalid.
204      * @throws NullPointerException if the name is null.
205      */

206     public void setAttribute(String JavaDoc name, Object JavaDoc value, int scope) {
207         
208         switch (scope) {
209             
210             case ENGINE_SCOPE:
211                 engineScope.put(name, value);
212                 return;
213                 
214             case GLOBAL_SCOPE:
215                 if (globalScope != null) {
216                     globalScope.put(name, value);
217                 }
218                 return;
219                 
220             default:
221                 throw new IllegalArgumentException JavaDoc("Illegal scope value.");
222         }
223     }
224     
225     /** {@inheritDoc} */
226     public Writer getWriter() {
227         return writer;
228     }
229
230     /** {@inheritDoc} */
231     public Reader getReader() {
232         return reader;
233     }
234
235     /** {@inheritDoc} */
236     public void setReader(Reader reader) {
237         this.reader = reader;
238     }
239
240     /** {@inheritDoc} */
241     public void setWriter(Writer writer) {
242         this.writer = writer;
243     }
244
245     /** {@inheritDoc} */
246     public Writer getErrorWriter() {
247         return errorWriter;
248     }
249     
250     /** {@inheritDoc} */
251     public void setErrorWriter(Writer writer) {
252         this.errorWriter = writer;
253     }
254     
255     /**
256      * Get the lowest scope in which an attribute is defined.
257      * @param name Name of the attribute
258      * .
259      * @return The lowest scope. Returns -1 if no attribute with the given
260      * name is defined in any scope.
261      * @throws NullPointerException if name is null.
262      * @throws IllegalArgumentException if name is empty.
263      */

264     public int getAttributesScope(String JavaDoc name) {
265         if (engineScope.containsKey(name)) {
266             return ENGINE_SCOPE;
267         } else if (globalScope != null && globalScope.containsKey(name)) {
268             return GLOBAL_SCOPE;
269         } else {
270             return -1;
271         }
272     }
273     
274     /**
275      * Returns the value of the <code>engineScope</code> field if specified scope is
276      * <code>ENGINE_SCOPE</code>. Returns the value of the <code>globalScope</code> field if the specified scope is
277      * <code>GLOBAL_SCOPE</code>.
278      *
279      * @param scope The specified scope
280      * @return The value of either the <code>engineScope</code> or <code>globalScope</code> field.
281      * @throws IllegalArgumentException if the value of scope is invalid.
282      */

283     public Bindings getBindings(int scope) {
284         if (scope == ENGINE_SCOPE) {
285             return engineScope;
286         } else if (scope == GLOBAL_SCOPE) {
287             return globalScope;
288         } else {
289             throw new IllegalArgumentException JavaDoc("Illegal scope value.");
290         }
291     }
292     
293     /** {@inheritDoc} */
294     public List<Integer JavaDoc> getScopes() {
295         return scopes;
296     }
297     
298     private static List<Integer JavaDoc> scopes;
299     static {
300         scopes = new ArrayList<Integer JavaDoc>(2);
301         scopes.add(ENGINE_SCOPE);
302         scopes.add(GLOBAL_SCOPE);
303         scopes = Collections.unmodifiableList(scopes);
304     }
305 }
306
Popular Tags