001: /*
002: * %W% %E% %U%
003: *
004: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
005: * SUN PROPRIETARY/CONFIDENTAIL. Use is subject to license terms.
006: */
007:
008: package javax.script;
009:
010: import java.util.*;
011: import java.io.*;
012:
013: /**
014: * Simple implementation of ScriptContext.
015: *
016: * @author Mike Grogan
017: * @version 1.0
018: * @since 1.6
019: */
020: public class SimpleScriptContext implements ScriptContext {
021:
022: /**
023: * By default, a <code>PrintWriter</code> based on <code>System.out</code>
024: * is used.
025: */
026: protected Writer writer;
027:
028: /**
029: * By default, a <code>PrintWriter</code> based on <code>System.err</code> is
030: * used.
031: */
032: protected Writer errorWriter;
033:
034: /**
035: * By default, a <code>InputStreamReader</code> based on <code>System.in</code>
036: * is used.
037: */
038: protected Reader reader;
039:
040: /**
041: * By default, a <code>SimpleBindings</code> is used.
042: */
043: protected Bindings engineScope;
044:
045: /**
046: * By default, a <code>SimpleBindings</code> is used.
047: */
048: protected Bindings globalScope;
049:
050: public SimpleScriptContext() {
051:
052: engineScope = new SimpleBindings();
053: globalScope = null;
054: reader = new InputStreamReader(System.in);
055: writer = new PrintWriter(System.out, true);
056: errorWriter = new PrintWriter(System.err, true);
057:
058: }
059:
060: /**
061: * Sets a <code>Bindings</code> of attributes for the given scope. If the value
062: * of scope is <code>ENGINE_SCOPE</code> the given <code>Bindings</code> replaces the
063: * <code>engineScope</code> field. If the value
064: * of scope is <code>GLOBAL_SCOPE</code> the given <code>Bindings</code> replaces the
065: * <code>globalScope</code> field.
066: *
067: * @param bindings The <code>Bindings</code> of attributes to set.
068: * @param scope The value of the scope in which the attributes are set.
069: *
070: * @throws <code>IllegalArgumentException</code> if scope is invalid.
071: * @throws <code>NullPointerException</code> is the value of scope is <code>ENGINE_SCOPE</code> and
072: * the specified <code>Bindings</code> is null.
073: */
074: public void setBindings(Bindings bindings, int scope) {
075:
076: switch (scope) {
077:
078: case ENGINE_SCOPE:
079: if (bindings == null) {
080: throw new NullPointerException(
081: "Engine scope cannot be null.");
082: }
083: engineScope = bindings;
084: break;
085: case GLOBAL_SCOPE:
086: globalScope = bindings;
087: break;
088: default:
089: throw new IllegalArgumentException("Invalid scope value.");
090: }
091: }
092:
093: public Object getAttribute(String name) {
094:
095: Object ret;
096: if (null != (ret = getAttribute(name, ENGINE_SCOPE))) {
097: return ret;
098: } else if (null != (ret = getAttribute(name, GLOBAL_SCOPE))) {
099: return ret;
100: }
101:
102: return null;
103: }
104:
105: public Object getAttribute(String name, int scope) {
106:
107: switch (scope) {
108:
109: case ENGINE_SCOPE:
110: return engineScope.get(name);
111:
112: case GLOBAL_SCOPE:
113: if (globalScope != null) {
114: return globalScope.get(name);
115: }
116: return null;
117:
118: default:
119: throw new IllegalArgumentException("Illegal scope value.");
120: }
121: }
122:
123: public Object removeAttribute(String name, int scope) {
124:
125: switch (scope) {
126:
127: case ENGINE_SCOPE:
128: if (getBindings(ENGINE_SCOPE) != null) {
129: return getBindings(ENGINE_SCOPE).remove(name);
130: }
131: return null;
132:
133: case GLOBAL_SCOPE:
134: if (getBindings(GLOBAL_SCOPE) != null) {
135: return getBindings(GLOBAL_SCOPE).remove(name);
136: }
137: return null;
138:
139: default:
140: throw new IllegalArgumentException("Illegal scope value.");
141: }
142: }
143:
144: public void setAttribute(String name, Object value, int scope) {
145:
146: switch (scope) {
147:
148: case ENGINE_SCOPE:
149: engineScope.put(name, value);
150: return;
151:
152: case GLOBAL_SCOPE:
153: if (globalScope != null) {
154: globalScope.put(name, value);
155: }
156: return;
157:
158: default:
159: throw new IllegalArgumentException("Illegal scope value.");
160: }
161: }
162:
163: public Writer getWriter() {
164: return writer;
165: }
166:
167: public Reader getReader() {
168: return reader;
169: }
170:
171: public void setReader(Reader reader) {
172: this .reader = reader;
173: }
174:
175: public void setWriter(Writer writer) {
176: this .writer = writer;
177: }
178:
179: public Writer getErrorWriter() {
180: return errorWriter;
181: }
182:
183: public void setErrorWriter(Writer writer) {
184: this .errorWriter = writer;
185: }
186:
187: public int getAttributesScope(String name) {
188:
189: if (getAttribute(name, ENGINE_SCOPE) != null) {
190: return ENGINE_SCOPE;
191: } else if (globalScope != null
192: && getAttribute(name, GLOBAL_SCOPE) != null) {
193: return GLOBAL_SCOPE;
194: } else {
195: return 0;
196: }
197: }
198:
199: /**
200: * Returns the value of the <code>engineScope</code> field if specified scope is
201: * <code>ENGINE_SCOPE</code>. Returns the value of the <code>globalScope</code> field if the specified scope is
202: * <code>GLOBAL_SCOPE</code>.
203: *
204: * @param scope The specified scope
205: * @return The value of either the <code>engineScope</code> or <code>globalScope</code> field.
206: * @throws <code>IllegalArgumentException</code> if the value of scope is invalid.
207: */
208: public Bindings getBindings(int scope) {
209: if (scope == ENGINE_SCOPE) {
210: return engineScope;
211: } else if (scope == GLOBAL_SCOPE) {
212: return globalScope;
213: } else {
214: throw new IllegalArgumentException("Illegal scope value.");
215: }
216:
217: }
218:
219: public List<Integer> getScopes() {
220: return scopes;
221: }
222:
223: private static List<Integer> scopes;
224: static {
225: scopes = new ArrayList<Integer>(2);
226: scopes.add(ENGINE_SCOPE);
227: scopes.add(GLOBAL_SCOPE);
228: scopes = Collections.unmodifiableList(scopes);
229: }
230: }
|