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