KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > script > ScriptContext


1 /*
2  * @(#)ScriptContext.java 1.4 05/11/17 14:24:13
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.util.List JavaDoc;
10 import java.io.Writer JavaDoc;
11 import java.io.Reader JavaDoc;
12
13 /**
14  * The interface whose implementing classes are used to connect Script Engines
15  * with objects, such as scoped Bindings, in hosting applications. Each scope is a set
16  * of named attributes whose values can be set and retrieved using the
17  * <code>ScriptContext</code> methods. ScriptContexts also expose Readers and Writers
18  * that can be used by the ScriptEngines for input and output.
19  *
20  * @author Mike Grogan
21  * @version 1.0
22  * @since 1.6
23  */

24 public interface ScriptContext {
25     
26     
27     /**
28      * EngineScope attributes are visible during the lifetime of a single
29      * <code>ScriptEngine</code> and a set of attributes is maintained for each
30      * engine.
31      */

32     public static final int ENGINE_SCOPE = 100;
33     
34     /**
35      * GlobalScope attributes are visible to all engines created by same ScriptEngineFactory.
36      */

37     public static final int GLOBAL_SCOPE = 200;
38     
39     
40     /**
41      * Associates a <code>Bindings</code> instance with a particular scope in this
42      * <code>ScriptContext</code>. Calls to the <code>getAttribute</code> and
43      * <code>setAttribute</code> methods must map to the <code>get</code> and
44      * <code>put</code> methods of the <code>Bindings</code> for the specified scope.
45      *
46      * @param bindings The <code>Bindings</code> to associate with the given scope
47      * @param scope The scope
48      *
49      * @throws IllegalArgumentException If no <code>Bindings</code> is defined for the
50      * specified scope value in ScriptContexts of this type.
51      * @throws NullPointerException if value of scope is <code>ENGINE_SCOPE</code> and
52      * the specified <code>Bindings</code> is null.
53      *
54      */

55     public void setBindings(Bindings bindings, int scope);
56     
57     /**
58      * Gets the <code>Bindings</code> associated with the given scope in this
59      * <code>ScriptContext</code>.
60      *
61      * @return The associated <code>Bindings</code>. Returns <code>null</code> if it has not
62      * been set.
63      *
64      * @throws IllegalArgumentException If no <code>Bindings</code> is defined for the
65      * specified scope value in <code>ScriptContext</code> of this type.
66      */

67     public Bindings getBindings(int scope);
68     
69     /**
70      * Sets the value of an attribute in a given scope.
71      *
72      * @param name The name of the attribute to set
73      * @param value The value of the attribute
74      * @param scope The scope in which to set the attribute
75      *
76      * @throws IllegalArgumentException
77      * if the name is empty or if the scope is invalid.
78      * @throws NullPointerException if the name is null.
79      */

80     public void setAttribute(String JavaDoc name, Object JavaDoc value, int scope);
81     
82     /**
83      * Gets the value of an attribute in a given scope.
84      *
85      * @param name The name of the attribute to retrieve.
86      * @param scope The scope in which to retrieve the attribute.
87      * @return The value of the attribute. Returns <code>null</code> is the name
88      * does not exist in the given scope.
89      *
90      * @throws IllegalArgumentException
91      * if the name is empty or if the value of scope is invalid.
92      * @throws NullPointerException if the name is null.
93      */

94     public Object JavaDoc getAttribute(String JavaDoc name, int scope);
95     
96     /**
97      * Remove an attribute in a given scope.
98      *
99      * @param name The name of the attribute to remove
100      * @param scope The scope in which to remove the attribute
101      *
102      * @return The removed value.
103      * @throws IllegalArgumentException
104      * if the name is empty or if the scope is invalid.
105      * @throws NullPointerException if the name is null.
106      */

107     public Object JavaDoc removeAttribute(String JavaDoc name, int scope);
108     
109     /**
110      * Retrieves the value of the attribute with the given name in
111      * the scope occurring earliest in the search order. The order
112      * is determined by the numeric value of the scope parameter (lowest
113      * scope values first.)
114      *
115      * @param name The name of the the attribute to retrieve.
116      * @return The value of the attribute in the lowest scope for
117      * which an attribute with the given name is defined. Returns
118      * null if no attribute with the name exists in any scope.
119      * @throws NullPointerException if the name is null.
120      * @throws IllegalArgumentException if the name is empty.
121      */

122     public Object JavaDoc getAttribute(String JavaDoc name);
123     
124     
125     /**
126      * Get the lowest scope in which an attribute is defined.
127      * @param name Name of the attribute
128      * .
129      * @return The lowest scope. Returns -1 if no attribute with the given
130      * name is defined in any scope.
131      * @throws NullPointerException if name is null.
132      * @throws IllegalArgumentException if name is empty.
133      */

134     public int getAttributesScope(String JavaDoc name);
135     
136     /**
137      * Returns the <code>Writer</code> for scripts to use when displaying output.
138      *
139      * @return The <code>Writer</code>.
140      */

141     public Writer JavaDoc getWriter();
142     
143     
144     /**
145      * Returns the <code>Writer</code> used to display error output.
146      *
147      * @return The <code>Writer</code>
148      */

149     public Writer JavaDoc getErrorWriter();
150     
151     /**
152      * Sets the <code>Writer</code> for scripts to use when displaying output.
153      *
154      * @param writer The new <code>Writer</code>.
155      */

156     public void setWriter(Writer JavaDoc writer);
157     
158     
159     /**
160      * Sets the <code>Writer</code> used to display error output.
161      *
162      * @param writer The <code>Writer</code>.
163      */

164     public void setErrorWriter(Writer JavaDoc writer);
165     
166     /**
167      * Returns a <code>Reader</code> to be used by the script to read
168      * input.
169      *
170      * @return The <code>Reader</code>.
171      */

172     public Reader JavaDoc getReader();
173     
174     
175     /**
176      * Sets the <code>Reader</code> for scripts to read input
177      * .
178      * @param reader The new <code>Reader</code>.
179      */

180     public void setReader(Reader JavaDoc reader);
181     
182     /**
183      * Returns immutable <code>List</code> of all the valid values for
184      * scope in the ScriptContext.
185      *
186      * @return list of scope values
187      */

188     public List JavaDoc<Integer JavaDoc> getScopes();
189 }
190
Popular Tags