1 7 8 package javax.script; 9 import java.io.Reader ; 10 import java.util.Map ; 11 import java.util.Iterator ; 12 13 33 public abstract class AbstractScriptEngine implements ScriptEngine { 34 35 38 39 protected ScriptContext context; 40 41 45 public AbstractScriptEngine() { 46 47 context = new SimpleScriptContext(); 48 49 } 50 51 58 public AbstractScriptEngine(Bindings n) { 59 60 this(); 61 if (n == null) { 62 throw new NullPointerException ("n is null"); 63 } 64 context.setBindings(n, ScriptContext.ENGINE_SCOPE); 65 } 66 67 74 public void setContext(ScriptContext ctxt) { 75 if (ctxt == null) { 76 throw new NullPointerException ("null context"); 77 } 78 context = ctxt; 79 } 80 81 86 public ScriptContext getContext() { 87 return context; 88 } 89 90 101 public Bindings getBindings(int scope) { 102 103 if (scope == ScriptContext.GLOBAL_SCOPE) { 104 return context.getBindings(ScriptContext.GLOBAL_SCOPE); 105 } else if (scope == ScriptContext.ENGINE_SCOPE) { 106 return context.getBindings(ScriptContext.ENGINE_SCOPE); 107 } else { 108 throw new IllegalArgumentException ("Invalid scope value."); 109 } 110 } 111 112 124 public void setBindings(Bindings bindings, int scope) { 125 126 if (scope == ScriptContext.GLOBAL_SCOPE) { 127 context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);; 128 } else if (scope == ScriptContext.ENGINE_SCOPE) { 129 context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);; 130 } else { 131 throw new IllegalArgumentException ("Invalid scope value."); 132 } 133 } 134 135 145 public void put(String key, Object value) { 146 147 Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE); 148 if (nn != null) { 149 nn.put(key, value); 150 } 151 152 } 153 154 163 public Object get(String key) { 164 165 Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE); 166 if (nn != null) { 167 return nn.get(key); 168 } 169 170 return null; 171 } 172 173 174 191 public Object eval(Reader reader, Bindings bindings ) throws ScriptException { 192 193 ScriptContext ctxt = getScriptContext(bindings); 194 195 return eval(reader, ctxt); 196 } 197 198 199 212 public Object eval(String script, Bindings bindings) throws ScriptException { 213 214 ScriptContext ctxt = getScriptContext(bindings); 215 216 return eval(script , ctxt); 217 } 218 219 229 public Object eval(Reader reader) throws ScriptException { 230 231 232 return eval(reader, context); 233 } 234 235 244 public Object eval(String script) throws ScriptException { 245 246 247 return eval(script, context); 248 } 249 250 271 protected ScriptContext getScriptContext(Bindings nn) { 272 273 SimpleScriptContext ctxt = new SimpleScriptContext(); 274 Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE); 275 276 if (gs != null) { 277 ctxt.setBindings(gs, ScriptContext.GLOBAL_SCOPE); 278 } 279 280 if (nn != null) { 281 ctxt.setBindings(nn, 282 ScriptContext.ENGINE_SCOPE); 283 } else { 284 throw new NullPointerException ("Engine scope Bindings may not be null."); 285 } 286 287 ctxt.setReader(context.getReader()); 288 ctxt.setWriter(context.getWriter()); 289 ctxt.setErrorWriter(context.getErrorWriter()); 290 291 return ctxt; 292 293 } 294 } 295 | Popular Tags |