Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/script/SimpleScriptContext.java
38829 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.script;2627import java.util.*;28import java.io.*;2930/**31* Simple implementation of ScriptContext.32*33* @author Mike Grogan34* @since 1.635*/36public class SimpleScriptContext implements ScriptContext {3738/**39* This is the writer to be used to output from scripts.40* By default, a <code>PrintWriter</code> based on <code>System.out</code>41* is used. Accessor methods getWriter, setWriter are used to manage42* this field.43* @see java.lang.System#out44* @see java.io.PrintWriter45*/46protected Writer writer;4748/**49* This is the writer to be used to output errors from scripts.50* By default, a <code>PrintWriter</code> based on <code>System.err</code> is51* used. Accessor methods getErrorWriter, setErrorWriter are used to manage52* this field.53* @see java.lang.System#err54* @see java.io.PrintWriter55*/56protected Writer errorWriter;5758/**59* This is the reader to be used for input from scripts.60* By default, a <code>InputStreamReader</code> based on <code>System.in</code>61* is used and default charset is used by this reader. Accessor methods62* getReader, setReader are used to manage this field.63* @see java.lang.System#in64* @see java.io.InputStreamReader65*/66protected Reader reader;676869/**70* This is the engine scope bindings.71* By default, a <code>SimpleBindings</code> is used. Accessor72* methods setBindings, getBindings are used to manage this field.73* @see SimpleBindings74*/75protected Bindings engineScope;7677/**78* This is the global scope bindings.79* By default, a null value (which means no global scope) is used. Accessor80* methods setBindings, getBindings are used to manage this field.81*/82protected Bindings globalScope;8384/**85* Create a {@code SimpleScriptContext}.86*/87public SimpleScriptContext() {88engineScope = new SimpleBindings();89globalScope = null;90reader = new InputStreamReader(System.in);91writer = new PrintWriter(System.out , true);92errorWriter = new PrintWriter(System.err, true);93}9495/**96* Sets a <code>Bindings</code> of attributes for the given scope. If the value97* of scope is <code>ENGINE_SCOPE</code> the given <code>Bindings</code> replaces the98* <code>engineScope</code> field. If the value99* of scope is <code>GLOBAL_SCOPE</code> the given <code>Bindings</code> replaces the100* <code>globalScope</code> field.101*102* @param bindings The <code>Bindings</code> of attributes to set.103* @param scope The value of the scope in which the attributes are set.104*105* @throws IllegalArgumentException if scope is invalid.106* @throws NullPointerException if the value of scope is <code>ENGINE_SCOPE</code> and107* the specified <code>Bindings</code> is null.108*/109public void setBindings(Bindings bindings, int scope) {110111switch (scope) {112113case ENGINE_SCOPE:114if (bindings == null) {115throw new NullPointerException("Engine scope cannot be null.");116}117engineScope = bindings;118break;119case GLOBAL_SCOPE:120globalScope = bindings;121break;122default:123throw new IllegalArgumentException("Invalid scope value.");124}125}126127128/**129* Retrieves the value of the attribute with the given name in130* the scope occurring earliest in the search order. The order131* is determined by the numeric value of the scope parameter (lowest132* scope values first.)133*134* @param name The name of the the attribute to retrieve.135* @return The value of the attribute in the lowest scope for136* which an attribute with the given name is defined. Returns137* null if no attribute with the name exists in any scope.138* @throws NullPointerException if the name is null.139* @throws IllegalArgumentException if the name is empty.140*/141public Object getAttribute(String name) {142checkName(name);143if (engineScope.containsKey(name)) {144return getAttribute(name, ENGINE_SCOPE);145} else if (globalScope != null && globalScope.containsKey(name)) {146return getAttribute(name, GLOBAL_SCOPE);147}148149return null;150}151152/**153* Gets the value of an attribute in a given scope.154*155* @param name The name of the attribute to retrieve.156* @param scope The scope in which to retrieve the attribute.157* @return The value of the attribute. Returns <code>null</code> is the name158* does not exist in the given scope.159*160* @throws IllegalArgumentException161* if the name is empty or if the value of scope is invalid.162* @throws NullPointerException if the name is null.163*/164public Object getAttribute(String name, int scope) {165checkName(name);166switch (scope) {167168case ENGINE_SCOPE:169return engineScope.get(name);170171case GLOBAL_SCOPE:172if (globalScope != null) {173return globalScope.get(name);174}175return null;176177default:178throw new IllegalArgumentException("Illegal scope value.");179}180}181182/**183* Remove an attribute in a given scope.184*185* @param name The name of the attribute to remove186* @param scope The scope in which to remove the attribute187*188* @return The removed value.189* @throws IllegalArgumentException190* if the name is empty or if the scope is invalid.191* @throws NullPointerException if the name is null.192*/193public Object removeAttribute(String name, int scope) {194checkName(name);195switch (scope) {196197case ENGINE_SCOPE:198if (getBindings(ENGINE_SCOPE) != null) {199return getBindings(ENGINE_SCOPE).remove(name);200}201return null;202203case GLOBAL_SCOPE:204if (getBindings(GLOBAL_SCOPE) != null) {205return getBindings(GLOBAL_SCOPE).remove(name);206}207return null;208209default:210throw new IllegalArgumentException("Illegal scope value.");211}212}213214/**215* Sets the value of an attribute in a given scope.216*217* @param name The name of the attribute to set218* @param value The value of the attribute219* @param scope The scope in which to set the attribute220*221* @throws IllegalArgumentException222* if the name is empty or if the scope is invalid.223* @throws NullPointerException if the name is null.224*/225public void setAttribute(String name, Object value, int scope) {226checkName(name);227switch (scope) {228229case ENGINE_SCOPE:230engineScope.put(name, value);231return;232233case GLOBAL_SCOPE:234if (globalScope != null) {235globalScope.put(name, value);236}237return;238239default:240throw new IllegalArgumentException("Illegal scope value.");241}242}243244/** {@inheritDoc} */245public Writer getWriter() {246return writer;247}248249/** {@inheritDoc} */250public Reader getReader() {251return reader;252}253254/** {@inheritDoc} */255public void setReader(Reader reader) {256this.reader = reader;257}258259/** {@inheritDoc} */260public void setWriter(Writer writer) {261this.writer = writer;262}263264/** {@inheritDoc} */265public Writer getErrorWriter() {266return errorWriter;267}268269/** {@inheritDoc} */270public void setErrorWriter(Writer writer) {271this.errorWriter = writer;272}273274/**275* Get the lowest scope in which an attribute is defined.276* @param name Name of the attribute277* .278* @return The lowest scope. Returns -1 if no attribute with the given279* name is defined in any scope.280* @throws NullPointerException if name is null.281* @throws IllegalArgumentException if name is empty.282*/283public int getAttributesScope(String name) {284checkName(name);285if (engineScope.containsKey(name)) {286return ENGINE_SCOPE;287} else if (globalScope != null && globalScope.containsKey(name)) {288return GLOBAL_SCOPE;289} else {290return -1;291}292}293294/**295* Returns the value of the <code>engineScope</code> field if specified scope is296* <code>ENGINE_SCOPE</code>. Returns the value of the <code>globalScope</code> field if the specified scope is297* <code>GLOBAL_SCOPE</code>.298*299* @param scope The specified scope300* @return The value of either the <code>engineScope</code> or <code>globalScope</code> field.301* @throws IllegalArgumentException if the value of scope is invalid.302*/303public Bindings getBindings(int scope) {304if (scope == ENGINE_SCOPE) {305return engineScope;306} else if (scope == GLOBAL_SCOPE) {307return globalScope;308} else {309throw new IllegalArgumentException("Illegal scope value.");310}311}312313/** {@inheritDoc} */314public List<Integer> getScopes() {315return scopes;316}317318private void checkName(String name) {319Objects.requireNonNull(name);320if (name.isEmpty()) {321throw new IllegalArgumentException("name cannot be empty");322}323}324325private static List<Integer> scopes;326static {327scopes = new ArrayList<Integer>(2);328scopes.add(ENGINE_SCOPE);329scopes.add(GLOBAL_SCOPE);330scopes = Collections.unmodifiableList(scopes);331}332}333334335