src/java.scripting/share/classes/javax/script/SimpleScriptContext.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 41554 jdk/src/java.scripting/share/classes/javax/script/SimpleScriptContext.java@fc6a35a87a52
child 49858 56923ee4f07e
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18567
35b36d452864 8019320: Fix doclint issues in javax.script
darcy
parents: 5506
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Simple implementation of ScriptContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author Mike Grogan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class SimpleScriptContext  implements ScriptContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * This is the writer to be used to output from scripts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * By default, a <code>PrintWriter</code> based on <code>System.out</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * is used. Accessor methods getWriter, setWriter are used to manage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * @see java.lang.System#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * @see java.io.PrintWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    protected Writer writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * This is the writer to be used to output errors from scripts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * By default, a <code>PrintWriter</code> based on <code>System.err</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * used. Accessor methods getErrorWriter, setErrorWriter are used to manage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * @see java.lang.System#err
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @see java.io.PrintWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected Writer errorWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * This is the reader to be used for input from scripts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * By default, a <code>InputStreamReader</code> based on <code>System.in</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * is used and default charset is used by this reader. Accessor methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * getReader, setReader are used to manage this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @see java.lang.System#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @see java.io.InputStreamReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected Reader reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * This is the engine scope bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * By default, a <code>SimpleBindings</code> is used. Accessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * methods setBindings, getBindings are used to manage this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @see SimpleBindings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected Bindings engineScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * This is the global scope bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * By default, a null value (which means no global scope) is used. Accessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * methods setBindings, getBindings are used to manage this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    protected Bindings globalScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
18567
35b36d452864 8019320: Fix doclint issues in javax.script
darcy
parents: 5506
diff changeset
    85
    /**
35b36d452864 8019320: Fix doclint issues in javax.script
darcy
parents: 5506
diff changeset
    86
     * Create a {@code SimpleScriptContext}.
35b36d452864 8019320: Fix doclint issues in javax.script
darcy
parents: 5506
diff changeset
    87
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public SimpleScriptContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        engineScope = new SimpleBindings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        globalScope = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        reader = new InputStreamReader(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        writer = new PrintWriter(System.out , true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        errorWriter = new PrintWriter(System.err, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Sets a <code>Bindings</code> of attributes for the given scope.  If the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * of scope is <code>ENGINE_SCOPE</code> the given <code>Bindings</code> replaces the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <code>engineScope</code> field.  If the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * of scope is <code>GLOBAL_SCOPE</code> the given <code>Bindings</code> replaces the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * <code>globalScope</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param bindings The <code>Bindings</code> of attributes to set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param scope The value of the scope in which the attributes are set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @throws IllegalArgumentException if scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @throws NullPointerException if the value of scope is <code>ENGINE_SCOPE</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * the specified <code>Bindings</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public void setBindings(Bindings bindings, int scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        switch (scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            case ENGINE_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if (bindings == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    throw new NullPointerException("Engine scope cannot be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                engineScope = bindings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            case GLOBAL_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                globalScope = bindings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                throw new IllegalArgumentException("Invalid scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
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
     * Retrieves the value of the attribute with the given name in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * the scope occurring earliest in the search order.  The order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * is determined by the numeric value of the scope parameter (lowest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * scope values first.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   135
     * @param name The name of the attribute to retrieve.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return The value of the attribute in the lowest scope for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * which an attribute with the given name is defined.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * null if no attribute with the name exists in any scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @throws NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @throws IllegalArgumentException if the name is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public Object getAttribute(String name) {
30647
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   143
        checkName(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (engineScope.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return getAttribute(name, ENGINE_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } else if (globalScope != null && globalScope.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return getAttribute(name, GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Gets the value of an attribute in a given scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param name The name of the attribute to retrieve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param scope The scope in which to retrieve the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @return The value of the attribute. Returns <code>null</code> is the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * does not exist in the given scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *         if the name is empty or if the value of scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @throws NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public Object getAttribute(String name, int scope) {
30647
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   166
        checkName(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        switch (scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            case ENGINE_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                return engineScope.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            case GLOBAL_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                if (globalScope != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    return globalScope.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                throw new IllegalArgumentException("Illegal scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Remove an attribute in a given scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param name The name of the attribute to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param scope The scope in which to remove the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return The removed value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *         if the name is empty or if the scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @throws NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public Object removeAttribute(String name, int scope) {
30647
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   195
        checkName(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        switch (scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            case ENGINE_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                if (getBindings(ENGINE_SCOPE) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    return getBindings(ENGINE_SCOPE).remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            case GLOBAL_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                if (getBindings(GLOBAL_SCOPE) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    return getBindings(GLOBAL_SCOPE).remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                throw new IllegalArgumentException("Illegal scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
41554
fc6a35a87a52 8071678: javax.script.ScriptContext setAttribute method should clarify behavior when GLOBAL_SCOPE is used and global scope object is null
sundar
parents: 30647
diff changeset
   216
     * Sets the value of an attribute in a given scope. If the scope is <code>GLOBAL_SCOPE</code>
fc6a35a87a52 8071678: javax.script.ScriptContext setAttribute method should clarify behavior when GLOBAL_SCOPE is used and global scope object is null
sundar
parents: 30647
diff changeset
   217
     * and no Bindings is set for <code>GLOBAL_SCOPE</code>, then setAttribute call is a no-op.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param name The name of the attribute to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param value The value of the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param scope The scope in which to set the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *         if the name is empty or if the scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @throws NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public void setAttribute(String name, Object value, int scope) {
30647
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   228
        checkName(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        switch (scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            case ENGINE_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                engineScope.put(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            case GLOBAL_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                if (globalScope != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    globalScope.put(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                throw new IllegalArgumentException("Illegal scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public Writer getWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public Reader getReader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public void setReader(Reader reader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        this.reader = reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public void setWriter(Writer writer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        this.writer = writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public Writer getErrorWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return errorWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public void setErrorWriter(Writer writer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        this.errorWriter = writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Get the lowest scope in which an attribute is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param name Name of the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return The lowest scope.  Returns -1 if no attribute with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * name is defined in any scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @throws NullPointerException if name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @throws IllegalArgumentException if name is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public int getAttributesScope(String name) {
30647
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   286
        checkName(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (engineScope.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return ENGINE_SCOPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        } else if (globalScope != null && globalScope.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return GLOBAL_SCOPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Returns the value of the <code>engineScope</code> field if specified scope is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * <code>ENGINE_SCOPE</code>.  Returns the value of the <code>globalScope</code> field if the specified scope is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * <code>GLOBAL_SCOPE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param scope The specified scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @return The value of either the  <code>engineScope</code> or <code>globalScope</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @throws IllegalArgumentException if the value of scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public Bindings getBindings(int scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (scope == ENGINE_SCOPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return engineScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        } else if (scope == GLOBAL_SCOPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return globalScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            throw new IllegalArgumentException("Illegal scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public List<Integer> getScopes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return scopes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
30647
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   320
    private void checkName(String name) {
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   321
        Objects.requireNonNull(name);
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   322
        if (name.isEmpty()) {
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   323
            throw new IllegalArgumentException("name cannot be empty");
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   324
        }
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   325
    }
58e2d299805b 8072853: SimpleScriptContext used by NashornScriptEngine doesn't completely complies to the spec regarding exception throwing
sundar
parents: 28059
diff changeset
   326
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private static List<Integer> scopes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        scopes = new ArrayList<Integer>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        scopes.add(ENGINE_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        scopes.add(GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        scopes = Collections.unmodifiableList(scopes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
}