Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/script/AbstractScriptEngine.java
38829 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.script;26import java.io.Reader;27import java.util.Map;28import java.util.Iterator;2930/**31* Provides a standard implementation for several of the variants of the <code>eval</code>32* method.33* <br><br>34* <code><b>eval(Reader)</b></code><p><code><b>eval(String)</b></code><p>35* <code><b>eval(String, Bindings)</b></code><p><code><b>eval(Reader, Bindings)</b></code>36* <br><br> are implemented using the abstract methods37* <br><br>38* <code><b>eval(Reader,ScriptContext)</b></code> or39* <code><b>eval(String, ScriptContext)</b></code>40* <br><br>41* with a <code>SimpleScriptContext</code>.42* <br><br>43* A <code>SimpleScriptContext</code> is used as the default <code>ScriptContext</code>44* of the <code>AbstractScriptEngine</code>..45*46* @author Mike Grogan47* @since 1.648*/49public abstract class AbstractScriptEngine implements ScriptEngine {5051/**52* The default <code>ScriptContext</code> of this <code>AbstractScriptEngine</code>.53*/5455protected ScriptContext context;5657/**58* Creates a new instance of AbstractScriptEngine using a <code>SimpleScriptContext</code>59* as its default <code>ScriptContext</code>.60*/61public AbstractScriptEngine() {6263context = new SimpleScriptContext();6465}6667/**68* Creates a new instance using the specified <code>Bindings</code> as the69* <code>ENGINE_SCOPE</code> <code>Bindings</code> in the protected <code>context</code> field.70*71* @param n The specified <code>Bindings</code>.72* @throws NullPointerException if n is null.73*/74public AbstractScriptEngine(Bindings n) {7576this();77if (n == null) {78throw new NullPointerException("n is null");79}80context.setBindings(n, ScriptContext.ENGINE_SCOPE);81}8283/**84* Sets the value of the protected <code>context</code> field to the specified85* <code>ScriptContext</code>.86*87* @param ctxt The specified <code>ScriptContext</code>.88* @throws NullPointerException if ctxt is null.89*/90public void setContext(ScriptContext ctxt) {91if (ctxt == null) {92throw new NullPointerException("null context");93}94context = ctxt;95}9697/**98* Returns the value of the protected <code>context</code> field.99*100* @return The value of the protected <code>context</code> field.101*/102public ScriptContext getContext() {103return context;104}105106/**107* Returns the <code>Bindings</code> with the specified scope value in108* the protected <code>context</code> field.109*110* @param scope The specified scope111*112* @return The corresponding <code>Bindings</code>.113*114* @throws IllegalArgumentException if the value of scope is115* invalid for the type the protected <code>context</code> field.116*/117public Bindings getBindings(int scope) {118119if (scope == ScriptContext.GLOBAL_SCOPE) {120return context.getBindings(ScriptContext.GLOBAL_SCOPE);121} else if (scope == ScriptContext.ENGINE_SCOPE) {122return context.getBindings(ScriptContext.ENGINE_SCOPE);123} else {124throw new IllegalArgumentException("Invalid scope value.");125}126}127128/**129* Sets the <code>Bindings</code> with the corresponding scope value in the130* <code>context</code> field.131*132* @param bindings The specified <code>Bindings</code>.133* @param scope The specified scope.134*135* @throws IllegalArgumentException if the value of scope is136* invalid for the type the <code>context</code> field.137* @throws NullPointerException if the bindings is null and the scope is138* <code>ScriptContext.ENGINE_SCOPE</code>139*/140public void setBindings(Bindings bindings, int scope) {141142if (scope == ScriptContext.GLOBAL_SCOPE) {143context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);;144} else if (scope == ScriptContext.ENGINE_SCOPE) {145context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);;146} else {147throw new IllegalArgumentException("Invalid scope value.");148}149}150151/**152* Sets the specified value with the specified key in the <code>ENGINE_SCOPE</code>153* <code>Bindings</code> of the protected <code>context</code> field.154*155* @param key The specified key.156* @param value The specified value.157*158* @throws NullPointerException if key is null.159* @throws IllegalArgumentException if key is empty.160*/161public void put(String key, Object value) {162163Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);164if (nn != null) {165nn.put(key, value);166}167168}169170/**171* Gets the value for the specified key in the <code>ENGINE_SCOPE</code> of the172* protected <code>context</code> field.173*174* @return The value for the specified key.175*176* @throws NullPointerException if key is null.177* @throws IllegalArgumentException if key is empty.178*/179public Object get(String key) {180181Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);182if (nn != null) {183return nn.get(key);184}185186return null;187}188189190/**191* <code>eval(Reader, Bindings)</code> calls the abstract192* <code>eval(Reader, ScriptContext)</code> method, passing it a <code>ScriptContext</code>193* whose Reader, Writers and Bindings for scopes other that <code>ENGINE_SCOPE</code>194* are identical to those members of the protected <code>context</code> field. The specified195* <code>Bindings</code> is used instead of the <code>ENGINE_SCOPE</code>196*197* <code>Bindings</code> of the <code>context</code> field.198*199* @param reader A <code>Reader</code> containing the source of the script.200* @param bindings A <code>Bindings</code> to use for the <code>ENGINE_SCOPE</code>201* while the script executes.202*203* @return The return value from <code>eval(Reader, ScriptContext)</code>204* @throws ScriptException if an error occurs in script.205* @throws NullPointerException if any of the parameters is null.206*/207public Object eval(Reader reader, Bindings bindings ) throws ScriptException {208209ScriptContext ctxt = getScriptContext(bindings);210211return eval(reader, ctxt);212}213214215/**216* Same as <code>eval(Reader, Bindings)</code> except that the abstract217* <code>eval(String, ScriptContext)</code> is used.218*219* @param script A <code>String</code> containing the source of the script.220*221* @param bindings A <code>Bindings</code> to use as the <code>ENGINE_SCOPE</code>222* while the script executes.223*224* @return The return value from <code>eval(String, ScriptContext)</code>225* @throws ScriptException if an error occurs in script.226* @throws NullPointerException if any of the parameters is null.227*/228public Object eval(String script, Bindings bindings) throws ScriptException {229230ScriptContext ctxt = getScriptContext(bindings);231232return eval(script , ctxt);233}234235/**236* <code>eval(Reader)</code> calls the abstract237* <code>eval(Reader, ScriptContext)</code> passing the value of the <code>context</code>238* field.239*240* @param reader A <code>Reader</code> containing the source of the script.241* @return The return value from <code>eval(Reader, ScriptContext)</code>242* @throws ScriptException if an error occurs in script.243* @throws NullPointerException if any of the parameters is null.244*/245public Object eval(Reader reader) throws ScriptException {246247248return eval(reader, context);249}250251/**252* Same as <code>eval(Reader)</code> except that the abstract253* <code>eval(String, ScriptContext)</code> is used.254*255* @param script A <code>String</code> containing the source of the script.256* @return The return value from <code>eval(String, ScriptContext)</code>257* @throws ScriptException if an error occurs in script.258* @throws NullPointerException if any of the parameters is null.259*/260public Object eval(String script) throws ScriptException {261262263return eval(script, context);264}265266/**267* Returns a <code>SimpleScriptContext</code>. The <code>SimpleScriptContext</code>:268*<br><br>269* <ul>270* <li>Uses the specified <code>Bindings</code> for its <code>ENGINE_SCOPE</code>271* </li>272* <li>Uses the <code>Bindings</code> returned by the abstract <code>getGlobalScope</code>273* method as its <code>GLOBAL_SCOPE</code>274* </li>275* <li>Uses the Reader and Writer in the default <code>ScriptContext</code> of this276* <code>ScriptEngine</code>277* </li>278* </ul>279* <br><br>280* A <code>SimpleScriptContext</code> returned by this method is used to implement eval methods281* using the abstract <code>eval(Reader,Bindings)</code> and <code>eval(String,Bindings)</code>282* versions.283*284* @param nn Bindings to use for the <code>ENGINE_SCOPE</code>285* @return The <code>SimpleScriptContext</code>286*/287protected ScriptContext getScriptContext(Bindings nn) {288289SimpleScriptContext ctxt = new SimpleScriptContext();290Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE);291292if (gs != null) {293ctxt.setBindings(gs, ScriptContext.GLOBAL_SCOPE);294}295296if (nn != null) {297ctxt.setBindings(nn,298ScriptContext.ENGINE_SCOPE);299} else {300throw new NullPointerException("Engine scope Bindings may not be null.");301}302303ctxt.setReader(context.getReader());304ctxt.setWriter(context.getWriter());305ctxt.setErrorWriter(context.getErrorWriter());306307return ctxt;308309}310}311312313