J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005-2006 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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 | public SimpleScriptContext() { |
| 87 | engineScope = new SimpleBindings(); |
| 88 | globalScope = null; |
| 89 | reader = new InputStreamReader(System.in); |
| 90 | writer = new PrintWriter(System.out , true); |
| 91 | errorWriter = new PrintWriter(System.err, true); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Sets a <code>Bindings</code> of attributes for the given scope. If the value |
| 96 | * of scope is <code>ENGINE_SCOPE</code> the given <code>Bindings</code> replaces the |
| 97 | * <code>engineScope</code> field. If the value |
| 98 | * of scope is <code>GLOBAL_SCOPE</code> the given <code>Bindings</code> replaces the |
| 99 | * <code>globalScope</code> field. |
| 100 | * |
| 101 | * @param bindings The <code>Bindings</code> of attributes to set. |
| 102 | * @param scope The value of the scope in which the attributes are set. |
| 103 | * |
| 104 | * @throws IllegalArgumentException if scope is invalid. |
| 105 | * @throws NullPointerException if the value of scope is <code>ENGINE_SCOPE</code> and |
| 106 | * the specified <code>Bindings</code> is null. |
| 107 | */ |
| 108 | public void setBindings(Bindings bindings, int scope) { |
| 109 | |
| 110 | switch (scope) { |
| 111 | |
| 112 | case ENGINE_SCOPE: |
| 113 | if (bindings == null) { |
| 114 | throw new NullPointerException("Engine scope cannot be null."); |
| 115 | } |
| 116 | engineScope = bindings; |
| 117 | break; |
| 118 | case GLOBAL_SCOPE: |
| 119 | globalScope = bindings; |
| 120 | break; |
| 121 | default: |
| 122 | throw new IllegalArgumentException("Invalid scope value."); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /** |
| 128 | * Retrieves the value of the attribute with the given name in |
| 129 | * the scope occurring earliest in the search order. The order |
| 130 | * is determined by the numeric value of the scope parameter (lowest |
| 131 | * scope values first.) |
| 132 | * |
| 133 | * @param name The name of the the attribute to retrieve. |
| 134 | * @return The value of the attribute in the lowest scope for |
| 135 | * which an attribute with the given name is defined. Returns |
| 136 | * null if no attribute with the name exists in any scope. |
| 137 | * @throws NullPointerException if the name is null. |
| 138 | * @throws IllegalArgumentException if the name is empty. |
| 139 | */ |
| 140 | public Object getAttribute(String name) { |
| 141 | if (engineScope.containsKey(name)) { |
| 142 | return getAttribute(name, ENGINE_SCOPE); |
| 143 | } else if (globalScope != null && globalScope.containsKey(name)) { |
| 144 | return getAttribute(name, GLOBAL_SCOPE); |
| 145 | } |
| 146 | |
| 147 | return null; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Gets the value of an attribute in a given scope. |
| 152 | * |
| 153 | * @param name The name of the attribute to retrieve. |
| 154 | * @param scope The scope in which to retrieve the attribute. |
| 155 | * @return The value of the attribute. Returns <code>null</code> is the name |
| 156 | * does not exist in the given scope. |
| 157 | * |
| 158 | * @throws IllegalArgumentException |
| 159 | * if the name is empty or if the value of scope is invalid. |
| 160 | * @throws NullPointerException if the name is null. |
| 161 | */ |
| 162 | public Object getAttribute(String name, int scope) { |
| 163 | |
| 164 | switch (scope) { |
| 165 | |
| 166 | case ENGINE_SCOPE: |
| 167 | return engineScope.get(name); |
| 168 | |
| 169 | case GLOBAL_SCOPE: |
| 170 | if (globalScope != null) { |
| 171 | return globalScope.get(name); |
| 172 | } |
| 173 | return null; |
| 174 | |
| 175 | default: |
| 176 | throw new IllegalArgumentException("Illegal scope value."); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Remove an attribute in a given scope. |
| 182 | * |
| 183 | * @param name The name of the attribute to remove |
| 184 | * @param scope The scope in which to remove the attribute |
| 185 | * |
| 186 | * @return The removed value. |
| 187 | * @throws IllegalArgumentException |
| 188 | * if the name is empty or if the scope is invalid. |
| 189 | * @throws NullPointerException if the name is null. |
| 190 | */ |
| 191 | public Object removeAttribute(String name, int scope) { |
| 192 | |
| 193 | switch (scope) { |
| 194 | |
| 195 | case ENGINE_SCOPE: |
| 196 | if (getBindings(ENGINE_SCOPE) != null) { |
| 197 | return getBindings(ENGINE_SCOPE).remove(name); |
| 198 | } |
| 199 | return null; |
| 200 | |
| 201 | case GLOBAL_SCOPE: |
| 202 | if (getBindings(GLOBAL_SCOPE) != null) { |
| 203 | return getBindings(GLOBAL_SCOPE).remove(name); |
| 204 | } |
| 205 | return null; |
| 206 | |
| 207 | default: |
| 208 | throw new IllegalArgumentException("Illegal scope value."); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Sets the value of an attribute in a given scope. |
| 214 | * |
| 215 | * @param name The name of the attribute to set |
| 216 | * @param value The value of the attribute |
| 217 | * @param scope The scope in which to set the attribute |
| 218 | * |
| 219 | * @throws IllegalArgumentException |
| 220 | * if the name is empty or if the scope is invalid. |
| 221 | * @throws NullPointerException if the name is null. |
| 222 | */ |
| 223 | public void setAttribute(String name, Object value, int scope) { |
| 224 | |
| 225 | switch (scope) { |
| 226 | |
| 227 | case ENGINE_SCOPE: |
| 228 | engineScope.put(name, value); |
| 229 | return; |
| 230 | |
| 231 | case GLOBAL_SCOPE: |
| 232 | if (globalScope != null) { |
| 233 | globalScope.put(name, value); |
| 234 | } |
| 235 | return; |
| 236 | |
| 237 | default: |
| 238 | throw new IllegalArgumentException("Illegal scope value."); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** {@inheritDoc} */ |
| 243 | public Writer getWriter() { |
| 244 | return writer; |
| 245 | } |
| 246 | |
| 247 | /** {@inheritDoc} */ |
| 248 | public Reader getReader() { |
| 249 | return reader; |
| 250 | } |
| 251 | |
| 252 | /** {@inheritDoc} */ |
| 253 | public void setReader(Reader reader) { |
| 254 | this.reader = reader; |
| 255 | } |
| 256 | |
| 257 | /** {@inheritDoc} */ |
| 258 | public void setWriter(Writer writer) { |
| 259 | this.writer = writer; |
| 260 | } |
| 261 | |
| 262 | /** {@inheritDoc} */ |
| 263 | public Writer getErrorWriter() { |
| 264 | return errorWriter; |
| 265 | } |
| 266 | |
| 267 | /** {@inheritDoc} */ |
| 268 | public void setErrorWriter(Writer writer) { |
| 269 | this.errorWriter = writer; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Get the lowest scope in which an attribute is defined. |
| 274 | * @param name Name of the attribute |
| 275 | * . |
| 276 | * @return The lowest scope. Returns -1 if no attribute with the given |
| 277 | * name is defined in any scope. |
| 278 | * @throws NullPointerException if name is null. |
| 279 | * @throws IllegalArgumentException if name is empty. |
| 280 | */ |
| 281 | public int getAttributesScope(String name) { |
| 282 | if (engineScope.containsKey(name)) { |
| 283 | return ENGINE_SCOPE; |
| 284 | } else if (globalScope != null && globalScope.containsKey(name)) { |
| 285 | return GLOBAL_SCOPE; |
| 286 | } else { |
| 287 | return -1; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Returns the value of the <code>engineScope</code> field if specified scope is |
| 293 | * <code>ENGINE_SCOPE</code>. Returns the value of the <code>globalScope</code> field if the specified scope is |
| 294 | * <code>GLOBAL_SCOPE</code>. |
| 295 | * |
| 296 | * @param scope The specified scope |
| 297 | * @return The value of either the <code>engineScope</code> or <code>globalScope</code> field. |
| 298 | * @throws IllegalArgumentException if the value of scope is invalid. |
| 299 | */ |
| 300 | public Bindings getBindings(int scope) { |
| 301 | if (scope == ENGINE_SCOPE) { |
| 302 | return engineScope; |
| 303 | } else if (scope == GLOBAL_SCOPE) { |
| 304 | return globalScope; |
| 305 | } else { |
| 306 | throw new IllegalArgumentException("Illegal scope value."); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** {@inheritDoc} */ |
| 311 | public List<Integer> getScopes() { |
| 312 | return scopes; |
| 313 | } |
| 314 | |
| 315 | private static List<Integer> scopes; |
| 316 | static { |
| 317 | scopes = new ArrayList<Integer>(2); |
| 318 | scopes.add(ENGINE_SCOPE); |
| 319 | scopes.add(GLOBAL_SCOPE); |
| 320 | scopes = Collections.unmodifiableList(scopes); |
| 321 | } |
| 322 | } |