Java by API/javax.script/ScriptContext — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:35, 31 мая 2010

ScriptContext: ENGINE_SCOPE

  
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Main {
  public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    manager.put("global", "global bindings");
    dumpBindings(manager.getBindings());
    ScriptEngine engine = manager.getEngineByExtension("js");
    engine.put("engine", "engine bindings");
    dumpBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE));
    dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE));
    Bindings bindings = engine.createBindings();
    bindings.put("engine", "overridden engine bindings");
    bindings.put("bindings", bindings);
    engine.eval("app.dumpBindings (bindings);", bindings);
    ScriptEngine engine2 = manager.getEngineByExtension("js");
    engine2.put("engine2", "engine2 bindings");
    dumpBindings(engine2.getBindings(ScriptContext.GLOBAL_SCOPE));
    dumpBindings(engine2.getBindings(ScriptContext.ENGINE_SCOPE));
    dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE));
  }
  public static void dumpBindings(Bindings bindings) {
    if (bindings == null)
      System.out.println("  No bindings");
    else
      for (String key : bindings.keySet())
        System.out.println("  " + key + ": " + bindings.get(key));
    System.out.println();
  }
}





ScriptContext: GLOBAL_SCOPE

  
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Main {
  public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    manager.put("global", "global bindings");
    dumpBindings(manager.getBindings());
    ScriptEngine engine = manager.getEngineByExtension("js");
    engine.put("engine", "engine bindings");
    dumpBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE));
    dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE));
    Bindings bindings = engine.createBindings();
    bindings.put("engine", "overridden engine bindings");
    bindings.put("bindings", bindings);
    engine.eval("app.dumpBindings (bindings);", bindings);
    ScriptEngine engine2 = manager.getEngineByExtension("js");
    engine2.put("engine2", "engine2 bindings");
    dumpBindings(engine2.getBindings(ScriptContext.GLOBAL_SCOPE));
    dumpBindings(engine2.getBindings(ScriptContext.ENGINE_SCOPE));
    dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE));
  }
  public static void dumpBindings(Bindings bindings) {
    if (bindings == null)
      System.out.println("  No bindings");
    else
      for (String key : bindings.keySet())
        System.out.println("  " + key + ": " + bindings.get(key));
    System.out.println();
  }
}