1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
/* * @(#)SimpleScriptContext.java 1.6 06/06/19 20:55:48 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTAIL. Use is subject to license terms. */ package javax.script; import java.util.*; import java.io.*; /** * Simple implementation of ScriptContext. * * @author Mike Grogan * @version 1.0 * @since 1.6 */ public class SimpleScriptContext implements ScriptContext { /** * This is the writer to be used to output from scripts. * By default, a <code>PrintWriter</code> based on <code>System.out</code> * is used. Accessor methods getWriter, setWriter are used to manage * this field. * @see java.lang.System#out * @see java.io.PrintWriter */ protected Writer writer; /** * This is the writer to be used to output errors from scripts. * By default, a <code>PrintWriter</code> based on <code>System.err</code> is * used. Accessor methods getErrorWriter, setErrorWriter are used to manage * this field. * @see java.lang.System#err * @see java.io.PrintWriter */ protected Writer errorWriter; /** * This is the reader to be used for input from scripts. * By default, a <code>InputStreamReader</code> based on <code>System.in</code> * is used and default charset is used by this reader. Accessor methods * getReader, setReader are used to manage this field. * @see java.lang.System#in * @see java.io.InputStreamReader */ protected Reader reader; /** * This is the engine scope bindings. * By default, a <code>SimpleBindings</code> is used. Accessor * methods setBindings, getBindings are used to manage this field. * @see SimpleBindings */ protected Bindings engineScope; /** * This is the global scope bindings. * By default, a null value (which means no global scope) is used. Accessor * methods setBindings, getBindings are used to manage this field. */ protected Bindings globalScope; public SimpleScriptContext() { engineScope = new SimpleBindings(); globalScope = null; reader = new InputStreamReader(System.in); writer = new PrintWriter(System.out , true); errorWriter = new PrintWriter(System.err, true); } /** * Sets a <code>Bindings</code> of attributes for the given scope. If the value * of scope is <code>ENGINE_SCOPE</code> the given <code>Bindings</code> replaces the * <code>engineScope</code> field. If the value * of scope is <code>GLOBAL_SCOPE</code> the given <code>Bindings</code> replaces the * <code>globalScope</code> field. * * @param bindings The <code>Bindings</code> of attributes to set. * @param scope The value of the scope in which the attributes are set. * * @throws IllegalArgumentException if scope is invalid. * @throws NullPointerException if the value of scope is <code>ENGINE_SCOPE</code> and * the specified <code>Bindings</code> is null. */ public void setBindings(Bindings bindings, int scope) { switch (scope) { case ENGINE_SCOPE: if (bindings == null) { throw new NullPointerException("Engine scope cannot be null."); } engineScope = bindings; break; case GLOBAL_SCOPE: globalScope = bindings; break; default: throw new IllegalArgumentException("Invalid scope value."); } } /** * Retrieves the value of the attribute with the given name in * the scope occurring earliest in the search order. The order * is determined by the numeric value of the scope parameter (lowest * scope values first.) * * @param name The name of the the attribute to retrieve. * @return The value of the attribute in the lowest scope for * which an attribute with the given name is defined. Returns * null if no attribute with the name exists in any scope. * @throws NullPointerException if the name is null. * @throws IllegalArgumentException if the name is empty. */ public Object getAttribute(String name) { if (engineScope.containsKey(name)) { return getAttribute(name, ENGINE_SCOPE); } else if (globalScope != null && globalScope.containsKey(name)) { return getAttribute(name, GLOBAL_SCOPE); } return null; } /** * Gets the value of an attribute in a given scope. * * @param name The name of the attribute to retrieve. * @param scope The scope in which to retrieve the attribute. * @return The value of the attribute. Returns <code>null</code> is the name * does not exist in the given scope. * * @throws IllegalArgumentException * if the name is empty or if the value of scope is invalid. * @throws NullPointerException if the name is null. */ public Object getAttribute(String name, int scope) { switch (scope) { case ENGINE_SCOPE: return engineScope.get(name); case GLOBAL_SCOPE: if (globalScope != null) { return globalScope.get(name); } return null; default: throw new IllegalArgumentException("Illegal scope value."); } } /** * Remove an attribute in a given scope. * * @param name The name of the attribute to remove * @param scope The scope in which to remove the attribute * * @return The removed value. * @throws IllegalArgumentException * if the name is empty or if the scope is invalid. * @throws NullPointerException if the name is null. */ public Object removeAttribute(String name, int scope) { switch (scope) { case ENGINE_SCOPE: if (getBindings(ENGINE_SCOPE) != null) { return getBindings(ENGINE_SCOPE).remove(name); } return null; case GLOBAL_SCOPE: if (getBindings(GLOBAL_SCOPE) != null) { return getBindings(GLOBAL_SCOPE).remove(name); } return null; default: throw new IllegalArgumentException("Illegal scope value."); } } /** * Sets the value of an attribute in a given scope. * * @param name The name of the attribute to set * @param value The value of the attribute * @param scope The scope in which to set the attribute * * @throws IllegalArgumentException * if the name is empty or if the scope is invalid. * @throws NullPointerException if the name is null. */ public void setAttribute(String name, Object value, int scope) { switch (scope) { case ENGINE_SCOPE: engineScope.put(name, value); return; case GLOBAL_SCOPE: if (globalScope != null) { globalScope.put(name, value); } return; default: throw new IllegalArgumentException("Illegal scope value."); } } /** {@inheritDoc} */ public Writer getWriter() { return writer; } /** {@inheritDoc} */ public Reader getReader() { return reader; } /** {@inheritDoc} */ public void setReader(Reader reader) { this.reader = reader; } /** {@inheritDoc} */ public void setWriter(Writer writer) { this.writer = writer; } /** {@inheritDoc} */ public Writer getErrorWriter() { return errorWriter; } /** {@inheritDoc} */ public void setErrorWriter(Writer writer) { this.errorWriter = writer; } /** * Get the lowest scope in which an attribute is defined. * @param name Name of the attribute * . * @return The lowest scope. Returns -1 if no attribute with the given * name is defined in any scope. * @throws NullPointerException if name is null. * @throws IllegalArgumentException if name is empty. */ public int getAttributesScope(String name) { if (engineScope.containsKey(name)) { return ENGINE_SCOPE; } else if (globalScope != null && globalScope.containsKey(name)) { return GLOBAL_SCOPE; } else { return -1; } } /** * Returns the value of the <code>engineScope</code> field if specified scope is * <code>ENGINE_SCOPE</code>. Returns the value of the <code>globalScope</code> field if the specified scope is * <code>GLOBAL_SCOPE</code>. * * @param scope The specified scope * @return The value of either the <code>engineScope</code> or <code>globalScope</code> field. * @throws IllegalArgumentException if the value of scope is invalid. */ public Bindings getBindings(int scope) { if (scope == ENGINE_SCOPE) { return engineScope; } else if (scope == GLOBAL_SCOPE) { return globalScope; } else { throw new IllegalArgumentException("Illegal scope value."); } } /** {@inheritDoc} */ public List<Integer> getScopes() { return scopes; } private static List<Integer> scopes; static { scopes = new ArrayList<Integer>(2); scopes.add(ENGINE_SCOPE); scopes.add(GLOBAL_SCOPE); scopes = Collections.unmodifiableList(scopes); } }