1 /* 2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.script; 27 28 import java.util.*; 29 import java.io.*; 30 31 /** 32 * Simple implementation of ScriptContext. 33 * 34 * @author Mike Grogan 35 * @since 1.6 36 */ 37 public class SimpleScriptContext implements ScriptContext { 38 39 /** 40 * This is the writer to be used to output from scripts. 41 * By default, a <code>PrintWriter</code> based on <code>System.out</code> 42 * is used. Accessor methods getWriter, setWriter are used to manage 43 * this field. 44 * @see java.lang.System#out 45 * @see java.io.PrintWriter 46 */ 47 protected Writer writer; 48 49 /** 50 * This is the writer to be used to output errors from scripts. 51 * By default, a <code>PrintWriter</code> based on <code>System.err</code> is 52 * used. Accessor methods getErrorWriter, setErrorWriter are used to manage 53 * this field. 54 * @see java.lang.System#err 55 * @see java.io.PrintWriter 56 */ 57 protected Writer errorWriter; 58 59 /** 60 * This is the reader to be used for input from scripts. 61 * By default, a <code>InputStreamReader</code> based on <code>System.in</code> 62 * is used and default charset is used by this reader. Accessor methods 63 * getReader, setReader are used to manage this field. 64 * @see java.lang.System#in 65 * @see java.io.InputStreamReader 66 */ 67 protected Reader reader; 68 69 70 /** 71 * This is the engine scope bindings. 72 * By default, a <code>SimpleBindings</code> is used. Accessor 73 * methods setBindings, getBindings are used to manage this field. 74 * @see SimpleBindings 75 */ 76 protected Bindings engineScope; 77 78 /** 79 * This is the global scope bindings. 80 * By default, a null value (which means no global scope) is used. Accessor 81 * methods setBindings, getBindings are used to manage this field. 82 */ 83 protected Bindings globalScope; 84 85 /** 86 * Create a {@code SimpleScriptContext}. 87 */ 88 public SimpleScriptContext() { 89 engineScope = new SimpleBindings(); 90 globalScope = null; 91 reader = new InputStreamReader(System.in); 92 writer = new PrintWriter(System.out , true); 93 errorWriter = new PrintWriter(System.err, true); 94 } 95 96 /** 97 * Sets a <code>Bindings</code> of attributes for the given scope. If the value 98 * of scope is <code>ENGINE_SCOPE</code> the given <code>Bindings</code> replaces the 99 * <code>engineScope</code> field. If the value 100 * of scope is <code>GLOBAL_SCOPE</code> the given <code>Bindings</code> replaces the 101 * <code>globalScope</code> field. 102 * 103 * @param bindings The <code>Bindings</code> of attributes to set. 104 * @param scope The value of the scope in which the attributes are set. 105 * 106 * @throws IllegalArgumentException if scope is invalid. 107 * @throws NullPointerException if the value of scope is <code>ENGINE_SCOPE</code> and 108 * the specified <code>Bindings</code> is null. 109 */ 110 public void setBindings(Bindings bindings, int scope) { 111 112 switch (scope) { 113 114 case ENGINE_SCOPE: 115 if (bindings == null) { 116 throw new NullPointerException("Engine scope cannot be null."); 117 } 118 engineScope = bindings; 119 break; 120 case GLOBAL_SCOPE: 121 globalScope = bindings; 122 break; 123 default: 124 throw new IllegalArgumentException("Invalid scope value."); 125 } 126 } 127 128 129 /** 130 * Retrieves the value of the attribute with the given name in 131 * the scope occurring earliest in the search order. The order 132 * is determined by the numeric value of the scope parameter (lowest 133 * scope values first.) 134 * 135 * @param name The name of the attribute to retrieve. 136 * @return The value of the attribute in the lowest scope for 137 * which an attribute with the given name is defined. Returns 138 * null if no attribute with the name exists in any scope. 139 * @throws NullPointerException if the name is null. 140 * @throws IllegalArgumentException if the name is empty. 141 */ 142 public Object getAttribute(String name) { 143 if (engineScope.containsKey(name)) { 144 return getAttribute(name, ENGINE_SCOPE); 145 } else if (globalScope != null && globalScope.containsKey(name)) { 146 return getAttribute(name, GLOBAL_SCOPE); 147 } 148 149 return null; 150 } 151 152 /** 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 name 158 * does not exist in the given scope. 159 * 160 * @throws IllegalArgumentException 161 * if the name is empty or if the value of scope is invalid. 162 * @throws NullPointerException if the name is null. 163 */ 164 public Object getAttribute(String name, int scope) { 165 166 switch (scope) { 167 168 case ENGINE_SCOPE: 169 return engineScope.get(name); 170 171 case GLOBAL_SCOPE: 172 if (globalScope != null) { 173 return globalScope.get(name); 174 } 175 return null; 176 177 default: 178 throw new IllegalArgumentException("Illegal scope value."); 179 } 180 } 181 182 /** 183 * Remove an attribute in a given scope. 184 * 185 * @param name The name of the attribute to remove 186 * @param scope The scope in which to remove the attribute 187 * 188 * @return The removed value. 189 * @throws IllegalArgumentException 190 * if the name is empty or if the scope is invalid. 191 * @throws NullPointerException if the name is null. 192 */ 193 public Object removeAttribute(String name, int scope) { 194 195 switch (scope) { 196 197 case ENGINE_SCOPE: 198 if (getBindings(ENGINE_SCOPE) != null) { 199 return getBindings(ENGINE_SCOPE).remove(name); 200 } 201 return null; 202 203 case GLOBAL_SCOPE: 204 if (getBindings(GLOBAL_SCOPE) != null) { 205 return getBindings(GLOBAL_SCOPE).remove(name); 206 } 207 return null; 208 209 default: 210 throw new IllegalArgumentException("Illegal scope value."); 211 } 212 } 213 214 /** 215 * Sets the value of an attribute in a given scope. 216 * 217 * @param name The name of the attribute to set 218 * @param value The value of the attribute 219 * @param scope The scope in which to set the attribute 220 * 221 * @throws IllegalArgumentException 222 * if the name is empty or if the scope is invalid. 223 * @throws NullPointerException if the name is null. 224 */ 225 public void setAttribute(String name, Object value, int scope) { 226 227 switch (scope) { 228 229 case ENGINE_SCOPE: 230 engineScope.put(name, value); 231 return; 232 233 case GLOBAL_SCOPE: 234 if (globalScope != null) { 235 globalScope.put(name, value); 236 } 237 return; 238 239 default: 240 throw new IllegalArgumentException("Illegal scope value."); 241 } 242 } 243 244 /** {@inheritDoc} */ 245 public Writer getWriter() { 246 return writer; 247 } 248 249 /** {@inheritDoc} */ 250 public Reader getReader() { 251 return reader; 252 } 253 254 /** {@inheritDoc} */ 255 public void setReader(Reader reader) { 256 this.reader = reader; 257 } 258 259 /** {@inheritDoc} */ 260 public void setWriter(Writer writer) { 261 this.writer = writer; 262 } 263 264 /** {@inheritDoc} */ 265 public Writer getErrorWriter() { 266 return errorWriter; 267 } 268 269 /** {@inheritDoc} */ 270 public void setErrorWriter(Writer writer) { 271 this.errorWriter = writer; 272 } 273 274 /** 275 * Get the lowest scope in which an attribute is defined. 276 * @param name Name of the attribute 277 * . 278 * @return The lowest scope. Returns -1 if no attribute with the given 279 * name is defined in any scope. 280 * @throws NullPointerException if name is null. 281 * @throws IllegalArgumentException if name is empty. 282 */ 283 public int getAttributesScope(String name) { 284 if (engineScope.containsKey(name)) { 285 return ENGINE_SCOPE; 286 } else if (globalScope != null && globalScope.containsKey(name)) { 287 return GLOBAL_SCOPE; 288 } else { 289 return -1; 290 } 291 } 292 293 /** 294 * Returns the value of the <code>engineScope</code> field if specified scope is 295 * <code>ENGINE_SCOPE</code>. Returns the value of the <code>globalScope</code> field if the specified scope is 296 * <code>GLOBAL_SCOPE</code>. 297 * 298 * @param scope The specified scope 299 * @return The value of either the <code>engineScope</code> or <code>globalScope</code> field. 300 * @throws IllegalArgumentException if the value of scope is invalid. 301 */ 302 public Bindings getBindings(int scope) { 303 if (scope == ENGINE_SCOPE) { 304 return engineScope; 305 } else if (scope == GLOBAL_SCOPE) { 306 return globalScope; 307 } else { 308 throw new IllegalArgumentException("Illegal scope value."); 309 } 310 } 311 312 /** {@inheritDoc} */ 313 public List<Integer> getScopes() { 314 return scopes; 315 } 316 317 private static List<Integer> scopes; 318 static { 319 scopes = new ArrayList<Integer>(2); 320 scopes.add(ENGINE_SCOPE); 321 scopes.add(GLOBAL_SCOPE); 322 scopes = Collections.unmodifiableList(scopes); 323 } 324 } --- EOF ---