1 7 8 package javax.script; 9 10 import java.util.*; 11 import java.io.*; 12 13 20 public class SimpleScriptContext implements ScriptContext { 21 22 30 protected Writer writer; 31 32 40 protected Writer errorWriter; 41 42 50 protected Reader reader; 51 52 53 59 protected Bindings engineScope; 60 61 66 protected Bindings globalScope; 67 68 69 public SimpleScriptContext() { 70 engineScope = new SimpleBindings(); 71 globalScope = null; 72 reader = new InputStreamReader(System.in); 73 writer = new PrintWriter(System.out , true); 74 errorWriter = new PrintWriter(System.err, true); 75 } 76 77 91 public void setBindings(Bindings bindings, int scope) { 92 93 switch (scope) { 94 95 case ENGINE_SCOPE: 96 if (bindings == null) { 97 throw new NullPointerException ("Engine scope cannot be null."); 98 } 99 engineScope = bindings; 100 break; 101 case GLOBAL_SCOPE: 102 globalScope = bindings; 103 break; 104 default: 105 throw new IllegalArgumentException ("Invalid scope value."); 106 } 107 } 108 109 110 123 public Object getAttribute(String name) { 124 if (engineScope.containsKey(name)) { 125 return getAttribute(name, ENGINE_SCOPE); 126 } else if (globalScope != null && globalScope.containsKey(name)) { 127 return getAttribute(name, GLOBAL_SCOPE); 128 } 129 130 return null; 131 } 132 133 145 public Object getAttribute(String name, int scope) { 146 147 switch (scope) { 148 149 case ENGINE_SCOPE: 150 return engineScope.get(name); 151 152 case GLOBAL_SCOPE: 153 if (globalScope != null) { 154 return globalScope.get(name); 155 } 156 return null; 157 158 default: 159 throw new IllegalArgumentException ("Illegal scope value."); 160 } 161 } 162 163 174 public Object removeAttribute(String name, int scope) { 175 176 switch (scope) { 177 178 case ENGINE_SCOPE: 179 if (getBindings(ENGINE_SCOPE) != null) { 180 return getBindings(ENGINE_SCOPE).remove(name); 181 } 182 return null; 183 184 case GLOBAL_SCOPE: 185 if (getBindings(GLOBAL_SCOPE) != null) { 186 return getBindings(GLOBAL_SCOPE).remove(name); 187 } 188 return null; 189 190 default: 191 throw new IllegalArgumentException ("Illegal scope value."); 192 } 193 } 194 195 206 public void setAttribute(String name, Object value, int scope) { 207 208 switch (scope) { 209 210 case ENGINE_SCOPE: 211 engineScope.put(name, value); 212 return; 213 214 case GLOBAL_SCOPE: 215 if (globalScope != null) { 216 globalScope.put(name, value); 217 } 218 return; 219 220 default: 221 throw new IllegalArgumentException ("Illegal scope value."); 222 } 223 } 224 225 226 public Writer getWriter() { 227 return writer; 228 } 229 230 231 public Reader getReader() { 232 return reader; 233 } 234 235 236 public void setReader(Reader reader) { 237 this.reader = reader; 238 } 239 240 241 public void setWriter(Writer writer) { 242 this.writer = writer; 243 } 244 245 246 public Writer getErrorWriter() { 247 return errorWriter; 248 } 249 250 251 public void setErrorWriter(Writer writer) { 252 this.errorWriter = writer; 253 } 254 255 264 public int getAttributesScope(String name) { 265 if (engineScope.containsKey(name)) { 266 return ENGINE_SCOPE; 267 } else if (globalScope != null && globalScope.containsKey(name)) { 268 return GLOBAL_SCOPE; 269 } else { 270 return -1; 271 } 272 } 273 274 283 public Bindings getBindings(int scope) { 284 if (scope == ENGINE_SCOPE) { 285 return engineScope; 286 } else if (scope == GLOBAL_SCOPE) { 287 return globalScope; 288 } else { 289 throw new IllegalArgumentException ("Illegal scope value."); 290 } 291 } 292 293 294 public List<Integer > getScopes() { 295 return scopes; 296 } 297 298 private static List<Integer > scopes; 299 static { 300 scopes = new ArrayList<Integer >(2); 301 scopes.add(ENGINE_SCOPE); 302 scopes.add(GLOBAL_SCOPE); 303 scopes = Collections.unmodifiableList(scopes); 304 } 305 } 306 | Popular Tags |