jdk/src/share/classes/javax/script/SimpleScriptContext.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 18567 35b36d452864
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, 2006, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public SimpleScriptContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        engineScope = new SimpleBindings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        globalScope = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        reader = new InputStreamReader(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        writer = new PrintWriter(System.out , true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        errorWriter = new PrintWriter(System.err, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Sets a <code>Bindings</code> of attributes for the given scope.  If the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * of scope is <code>ENGINE_SCOPE</code> the given <code>Bindings</code> replaces the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <code>engineScope</code> field.  If the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * of scope is <code>GLOBAL_SCOPE</code> the given <code>Bindings</code> replaces the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <code>globalScope</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param bindings The <code>Bindings</code> of attributes to set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param scope The value of the scope in which the attributes are set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @throws IllegalArgumentException if scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @throws NullPointerException if the value of scope is <code>ENGINE_SCOPE</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * the specified <code>Bindings</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public void setBindings(Bindings bindings, int scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        switch (scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            case ENGINE_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                if (bindings == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    throw new NullPointerException("Engine scope cannot be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                engineScope = bindings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            case GLOBAL_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                globalScope = bindings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                throw new IllegalArgumentException("Invalid scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
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
     * Retrieves the value of the attribute with the given name in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * the scope occurring earliest in the search order.  The order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * is determined by the numeric value of the scope parameter (lowest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * scope values first.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param name The name of the the attribute to retrieve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @return The value of the attribute in the lowest scope for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * which an attribute with the given name is defined.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * null if no attribute with the name exists in any scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @throws NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @throws IllegalArgumentException if the name is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public Object getAttribute(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (engineScope.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return getAttribute(name, ENGINE_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } else if (globalScope != null && globalScope.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return getAttribute(name, GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Gets the value of an attribute in a given scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param name The name of the attribute to retrieve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param scope The scope in which to retrieve the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @return The value of the attribute. Returns <code>null</code> is the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * does not exist in the given scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *         if the name is empty or if the value of scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @throws NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public Object getAttribute(String name, int scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        switch (scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            case ENGINE_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                return engineScope.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            case GLOBAL_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                if (globalScope != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    return globalScope.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                throw new IllegalArgumentException("Illegal scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Remove an attribute in a given scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param name The name of the attribute to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param scope The scope in which to remove the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @return The removed value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *         if the name is empty or if the scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @throws NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public Object removeAttribute(String name, int scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        switch (scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            case ENGINE_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                if (getBindings(ENGINE_SCOPE) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    return getBindings(ENGINE_SCOPE).remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            case GLOBAL_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                if (getBindings(GLOBAL_SCOPE) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    return getBindings(GLOBAL_SCOPE).remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                throw new IllegalArgumentException("Illegal scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Sets the value of an attribute in a given scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param name The name of the attribute to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param value The value of the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param scope The scope in which to set the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *         if the name is empty or if the scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @throws NullPointerException if the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public void setAttribute(String name, Object value, int scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        switch (scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            case ENGINE_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                engineScope.put(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            case GLOBAL_SCOPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                if (globalScope != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    globalScope.put(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                throw new IllegalArgumentException("Illegal scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public Writer getWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public Reader getReader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public void setReader(Reader reader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        this.reader = reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public void setWriter(Writer writer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        this.writer = writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public Writer getErrorWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return errorWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public void setErrorWriter(Writer writer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        this.errorWriter = writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Get the lowest scope in which an attribute is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param name Name of the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @return The lowest scope.  Returns -1 if no attribute with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * name is defined in any scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @throws NullPointerException if name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @throws IllegalArgumentException if name is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public int getAttributesScope(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (engineScope.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return ENGINE_SCOPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        } else if (globalScope != null && globalScope.containsKey(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return GLOBAL_SCOPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Returns the value of the <code>engineScope</code> field if specified scope is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * <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
   294
     * <code>GLOBAL_SCOPE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @param scope The specified scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @return The value of either the  <code>engineScope</code> or <code>globalScope</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @throws IllegalArgumentException if the value of scope is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public Bindings getBindings(int scope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (scope == ENGINE_SCOPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return engineScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        } else if (scope == GLOBAL_SCOPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return globalScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            throw new IllegalArgumentException("Illegal scope value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /** {@inheritDoc} */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public List<Integer> getScopes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return scopes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private static List<Integer> scopes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        scopes = new ArrayList<Integer>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        scopes.add(ENGINE_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        scopes.add(GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        scopes = Collections.unmodifiableList(scopes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
}