Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.scripting/share/classes/javax/script/AbstractScriptEngine.java
41128 views
1
/*
2
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.script;
27
import java.io.Reader;
28
29
/**
30
* Provides a standard implementation for several of the variants of the <code>eval</code>
31
* method.
32
* <br><br>
33
* <code><b>eval(Reader)</b></code><p><code><b>eval(String)</b></code><p>
34
* <code><b>eval(String, Bindings)</b></code><p><code><b>eval(Reader, Bindings)</b></code>
35
* <br><br> are implemented using the abstract methods
36
* <br><br>
37
* <code><b>eval(Reader,ScriptContext)</b></code> or
38
* <code><b>eval(String, ScriptContext)</b></code>
39
* <br><br>
40
* with a <code>SimpleScriptContext</code>.
41
* <br><br>
42
* A <code>SimpleScriptContext</code> is used as the default <code>ScriptContext</code>
43
* of the <code>AbstractScriptEngine</code>..
44
*
45
* @author Mike Grogan
46
* @since 1.6
47
*/
48
public abstract class AbstractScriptEngine implements ScriptEngine {
49
50
/**
51
* The default <code>ScriptContext</code> of this <code>AbstractScriptEngine</code>.
52
*/
53
54
protected ScriptContext context;
55
56
/**
57
* Creates a new instance of AbstractScriptEngine using a <code>SimpleScriptContext</code>
58
* as its default <code>ScriptContext</code>.
59
*/
60
public AbstractScriptEngine() {
61
62
context = new SimpleScriptContext();
63
64
}
65
66
/**
67
* Creates a new instance using the specified <code>Bindings</code> as the
68
* <code>ENGINE_SCOPE</code> <code>Bindings</code> in the protected <code>context</code> field.
69
*
70
* @param n The specified <code>Bindings</code>.
71
* @throws NullPointerException if n is null.
72
*/
73
public AbstractScriptEngine(Bindings n) {
74
75
this();
76
if (n == null) {
77
throw new NullPointerException("n is null");
78
}
79
context.setBindings(n, ScriptContext.ENGINE_SCOPE);
80
}
81
82
/**
83
* Sets the value of the protected <code>context</code> field to the specified
84
* <code>ScriptContext</code>.
85
*
86
* @param ctxt The specified <code>ScriptContext</code>.
87
* @throws NullPointerException if ctxt is null.
88
*/
89
public void setContext(ScriptContext ctxt) {
90
if (ctxt == null) {
91
throw new NullPointerException("null context");
92
}
93
context = ctxt;
94
}
95
96
/**
97
* Returns the value of the protected <code>context</code> field.
98
*
99
* @return The value of the protected <code>context</code> field.
100
*/
101
public ScriptContext getContext() {
102
return context;
103
}
104
105
/**
106
* Returns the <code>Bindings</code> with the specified scope value in
107
* the protected <code>context</code> field.
108
*
109
* @param scope The specified scope
110
*
111
* @return The corresponding <code>Bindings</code>.
112
*
113
* @throws IllegalArgumentException if the value of scope is
114
* invalid for the type the protected <code>context</code> field.
115
*/
116
public Bindings getBindings(int scope) {
117
118
if (scope == ScriptContext.GLOBAL_SCOPE) {
119
return context.getBindings(ScriptContext.GLOBAL_SCOPE);
120
} else if (scope == ScriptContext.ENGINE_SCOPE) {
121
return context.getBindings(ScriptContext.ENGINE_SCOPE);
122
} else {
123
throw new IllegalArgumentException("Invalid scope value.");
124
}
125
}
126
127
/**
128
* Sets the <code>Bindings</code> with the corresponding scope value in the
129
* <code>context</code> field.
130
*
131
* @param bindings The specified <code>Bindings</code>.
132
* @param scope The specified scope.
133
*
134
* @throws IllegalArgumentException if the value of scope is
135
* invalid for the type the <code>context</code> field.
136
* @throws NullPointerException if the bindings is null and the scope is
137
* <code>ScriptContext.ENGINE_SCOPE</code>
138
*/
139
public void setBindings(Bindings bindings, int scope) {
140
141
if (scope == ScriptContext.GLOBAL_SCOPE) {
142
context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
143
} else if (scope == ScriptContext.ENGINE_SCOPE) {
144
context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
145
} else {
146
throw new IllegalArgumentException("Invalid scope value.");
147
}
148
}
149
150
/**
151
* Sets the specified value with the specified key in the <code>ENGINE_SCOPE</code>
152
* <code>Bindings</code> of the protected <code>context</code> field.
153
*
154
* @param key The specified key.
155
* @param value The specified value.
156
*
157
* @throws NullPointerException if key is null.
158
* @throws IllegalArgumentException if key is empty.
159
*/
160
public void put(String key, Object value) {
161
162
Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);
163
if (nn != null) {
164
nn.put(key, value);
165
}
166
167
}
168
169
/**
170
* Gets the value for the specified key in the <code>ENGINE_SCOPE</code> of the
171
* protected <code>context</code> field.
172
*
173
* @return The value for the specified key.
174
*
175
* @throws NullPointerException if key is null.
176
* @throws IllegalArgumentException if key is empty.
177
*/
178
public Object get(String key) {
179
180
Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);
181
if (nn != null) {
182
return nn.get(key);
183
}
184
185
return null;
186
}
187
188
189
/**
190
* <code>eval(Reader, Bindings)</code> calls the abstract
191
* <code>eval(Reader, ScriptContext)</code> method, passing it a <code>ScriptContext</code>
192
* whose Reader, Writers and Bindings for scopes other that <code>ENGINE_SCOPE</code>
193
* are identical to those members of the protected <code>context</code> field. The specified
194
* <code>Bindings</code> is used instead of the <code>ENGINE_SCOPE</code>
195
*
196
* <code>Bindings</code> of the <code>context</code> field.
197
*
198
* @param reader A <code>Reader</code> containing the source of the script.
199
* @param bindings A <code>Bindings</code> to use for the <code>ENGINE_SCOPE</code>
200
* while the script executes.
201
*
202
* @return The return value from <code>eval(Reader, ScriptContext)</code>
203
* @throws ScriptException if an error occurs in script.
204
* @throws NullPointerException if any of the parameters is null.
205
*/
206
public Object eval(Reader reader, Bindings bindings ) throws ScriptException {
207
208
ScriptContext ctxt = getScriptContext(bindings);
209
210
return eval(reader, ctxt);
211
}
212
213
214
/**
215
* Same as <code>eval(Reader, Bindings)</code> except that the abstract
216
* <code>eval(String, ScriptContext)</code> is used.
217
*
218
* @param script A <code>String</code> containing the source of the script.
219
*
220
* @param bindings A <code>Bindings</code> to use as the <code>ENGINE_SCOPE</code>
221
* while the script executes.
222
*
223
* @return The return value from <code>eval(String, ScriptContext)</code>
224
* @throws ScriptException if an error occurs in script.
225
* @throws NullPointerException if any of the parameters is null.
226
*/
227
public Object eval(String script, Bindings bindings) throws ScriptException {
228
229
ScriptContext ctxt = getScriptContext(bindings);
230
231
return eval(script , ctxt);
232
}
233
234
/**
235
* <code>eval(Reader)</code> calls the abstract
236
* <code>eval(Reader, ScriptContext)</code> passing the value of the <code>context</code>
237
* field.
238
*
239
* @param reader A <code>Reader</code> containing the source of the script.
240
* @return The return value from <code>eval(Reader, ScriptContext)</code>
241
* @throws ScriptException if an error occurs in script.
242
* @throws NullPointerException if any of the parameters is null.
243
*/
244
public Object eval(Reader reader) throws ScriptException {
245
246
247
return eval(reader, context);
248
}
249
250
/**
251
* Same as <code>eval(Reader)</code> except that the abstract
252
* <code>eval(String, ScriptContext)</code> is used.
253
*
254
* @param script A <code>String</code> containing the source of the script.
255
* @return The return value from <code>eval(String, ScriptContext)</code>
256
* @throws ScriptException if an error occurs in script.
257
* @throws NullPointerException if any of the parameters is null.
258
*/
259
public Object eval(String script) throws ScriptException {
260
261
262
return eval(script, context);
263
}
264
265
/**
266
* Returns a <code>SimpleScriptContext</code>. The <code>SimpleScriptContext</code>:
267
*<br><br>
268
* <ul>
269
* <li>Uses the specified <code>Bindings</code> for its <code>ENGINE_SCOPE</code>
270
* </li>
271
* <li>Uses the <code>Bindings</code> returned by the abstract <code>getGlobalScope</code>
272
* method as its <code>GLOBAL_SCOPE</code>
273
* </li>
274
* <li>Uses the Reader and Writer in the default <code>ScriptContext</code> of this
275
* <code>ScriptEngine</code>
276
* </li>
277
* </ul>
278
* <br><br>
279
* A <code>SimpleScriptContext</code> returned by this method is used to implement eval methods
280
* using the abstract <code>eval(Reader,Bindings)</code> and <code>eval(String,Bindings)</code>
281
* versions.
282
*
283
* @param nn Bindings to use for the <code>ENGINE_SCOPE</code>
284
* @return The <code>SimpleScriptContext</code>
285
*/
286
protected ScriptContext getScriptContext(Bindings nn) {
287
288
SimpleScriptContext ctxt = new SimpleScriptContext(context.getReader(), context.getWriter(), context.getErrorWriter());
289
Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE);
290
291
if (gs != null) {
292
ctxt.setBindings(gs, ScriptContext.GLOBAL_SCOPE);
293
}
294
295
if (nn != null) {
296
ctxt.setBindings(nn,
297
ScriptContext.ENGINE_SCOPE);
298
} else {
299
throw new NullPointerException("Engine scope Bindings may not be null.");
300
}
301
302
return ctxt;
303
304
}
305
}
306
307