jdk/src/java.scripting/share/classes/javax/script/SimpleScriptContext.java
author jwilhelm
Thu, 30 Oct 2014 01:01:37 +0100
changeset 27445 a8354c76ae20
parent 25859 3317bb8137f4
child 28059 e576535359cc
permissions -rw-r--r--
Merge
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param name The name of the the attribute to retrieve.
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) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (engineScope.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return getAttribute(name, ENGINE_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        } else if (globalScope != null && globalScope.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return getAttribute(name, GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return null;
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
     * Gets the value of an attribute in a given scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param name The name of the attribute to retrieve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param scope The scope in which to retrieve the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @return The value of the attribute. Returns <code>null</code> is the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * does not exist in the given scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *         if the name is empty or if the value of scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @throws NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public Object getAttribute(String name, int scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        switch (scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            case ENGINE_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                return engineScope.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            case GLOBAL_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                if (globalScope != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    return globalScope.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                throw new IllegalArgumentException("Illegal scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
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
     * Remove an attribute in a given scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param name The name of the attribute to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param scope The scope in which to remove the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @return The removed value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *         if the name is empty or if the scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @throws NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public Object removeAttribute(String name, int scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        switch (scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            case ENGINE_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                if (getBindings(ENGINE_SCOPE) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    return getBindings(ENGINE_SCOPE).remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            case GLOBAL_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                if (getBindings(GLOBAL_SCOPE) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    return getBindings(GLOBAL_SCOPE).remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                throw new IllegalArgumentException("Illegal scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
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
     * Sets the value of an attribute in a given scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param name The name of the attribute to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param value The value of the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param scope The scope in which to set the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *         if the name is empty or if the scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @throws NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public void setAttribute(String name, Object value, int scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        switch (scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            case ENGINE_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                engineScope.put(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            case GLOBAL_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                if (globalScope != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    globalScope.put(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                throw new IllegalArgumentException("Illegal scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public Writer getWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public Reader getReader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public void setReader(Reader reader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        this.reader = reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public void setWriter(Writer writer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        this.writer = writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public Writer getErrorWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return errorWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public void setErrorWriter(Writer writer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        this.errorWriter = writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Get the lowest scope in which an attribute is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @param name Name of the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @return The lowest scope.  Returns -1 if no attribute with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * name is defined in any scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @throws NullPointerException if name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @throws IllegalArgumentException if name is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public int getAttributesScope(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (engineScope.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return ENGINE_SCOPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } else if (globalScope != null && globalScope.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return GLOBAL_SCOPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Returns the value of the <code>engineScope</code> field if specified scope is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <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
   296
     * <code>GLOBAL_SCOPE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param scope The specified scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @return The value of either the  <code>engineScope</code> or <code>globalScope</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @throws IllegalArgumentException if the value of scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public Bindings getBindings(int scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (scope == ENGINE_SCOPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return engineScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        } else if (scope == GLOBAL_SCOPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            return globalScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            throw new IllegalArgumentException("Illegal scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public List<Integer> getScopes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return scopes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    private static List<Integer> scopes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        scopes = new ArrayList<Integer>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        scopes.add(ENGINE_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        scopes.add(GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        scopes = Collections.unmodifiableList(scopes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
}