Example usage for javax.script SimpleScriptContext SimpleScriptContext

List of usage examples for javax.script SimpleScriptContext SimpleScriptContext

Introduction

In this page you can find the example usage for javax.script SimpleScriptContext SimpleScriptContext.

Prototype

public SimpleScriptContext() 

Source Link

Document

Create a SimpleScriptContext .

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptContext ctx = new SimpleScriptContext();
    ctx.setAttribute("year", 2015, ScriptContext.ENGINE_SCOPE);
    ctx.setAttribute("month", 9, ScriptContext.ENGINE_SCOPE);
    ctx.setAttribute("day", 21, ScriptContext.ENGINE_SCOPE);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptContext ctx = new SimpleScriptContext();
    Bindings globalBindings = new SimpleBindings();
    ctx.setBindings(globalBindings, ScriptContext.GLOBAL_SCOPE);
    ctx.setAttribute("year", 2015, ScriptContext.GLOBAL_SCOPE);
    ctx.setAttribute("name", "Java", ScriptContext.GLOBAL_SCOPE);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptContext ctx = new SimpleScriptContext();

    // Get the reader and writers from the script context
    Reader inputReader = ctx.getReader();
    Writer outputWriter = ctx.getWriter();
    Writer errWriter = ctx.getErrorWriter();

    // Write all script outputs to an out.txt file
    Writer fileWriter = new FileWriter("out.txt");
    ctx.setWriter(fileWriter);//from  ww  w . j  a v a2s . c  o  m
}

From source file:Main.java

public static void main(String[] args) {
    ScriptContext ctx = new SimpleScriptContext();
    List<Integer> scopes = ctx.getScopes();
    System.out.println("Supported Scopes: " + scopes);

    ctx.setAttribute("year", 2015, ENGINE_SCOPE);

    Bindings globalBindings = new SimpleBindings();
    ctx.setBindings(globalBindings, GLOBAL_SCOPE);

    ctx.setAttribute("year", 2014, GLOBAL_SCOPE);
    ctx.setAttribute("name", "Jack", GLOBAL_SCOPE);

    String nameValue = (String) ctx.getAttribute("name");
    System.out.println("nameValue = " + nameValue);

    int engineScopeYear = (Integer) ctx.getAttribute("year", ENGINE_SCOPE);
    int globalScopeYear = (Integer) ctx.getAttribute("year", GLOBAL_SCOPE);

    System.out.println("engineScopeYear = " + engineScopeYear);
    System.out.println("globalScopeYear = " + globalScopeYear);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    engine.eval("var msg = 'Hello globals'");
    engine.eval("print(this.msg);");
    ScriptContext ctx = new SimpleScriptContext();
    engine.eval("print(this.msg);", ctx);
}

From source file:Main.java

public static void main(String[] args) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    ScriptContext defaultCtx = engine.getContext();
    // Work with defaultCtx here

    // Create a new context
    ScriptContext ctx = new SimpleScriptContext();
    // Configure ctx here

    engine.setContext(ctx);//  w w  w.jav  a2s .  c om
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    engine.eval("var msg = 'Hello globals'");
    engine.eval("print(this.msg);");
    ScriptContext ctx = new SimpleScriptContext();
    ScriptContext defaultCtx = engine.getContext();
    Bindings engineBindings = defaultCtx.getBindings(ENGINE_SCOPE);
    ctx.setBindings(engineBindings, ENGINE_SCOPE);
    engine.eval("print(this.msg);", ctx);
}

From source file:Main.java

public static void main(String[] args) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    ScriptContext defaultCtx = engine.getContext();
    // Work with defaultCtx here

    // Create a new context
    ScriptContext ctx = new SimpleScriptContext();

    // Configure ctx here

    // Set ctx as the new default context for the engine
    engine.setContext(ctx);//from  w  ww  .ja v a  2  s  . com

    ctx.setBindings(manager.getBindings(), ScriptContext.GLOBAL_SCOPE);

    engine.setContext(ctx);
}

From source file:Main.java

public static void main(String[] args) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    manager.put("n1", 1);
    String script = "var sum = n1 + n2;print(msg + " + "' n1=' + n1 + ', n2=' + n2 + " + "', sum=' + sum);";

    engine.put("n2", 2);
    engine.put("msg", "a string");
    engine.eval(script);//ww w.  j  a v a 2s .  co m

    Bindings bindings = engine.createBindings();
    bindings.put("n2", 3);
    bindings.put("msg", "another string");
    engine.eval(script, bindings);

    ScriptContext ctx = new SimpleScriptContext();
    Bindings ctxGlobalBindings = engine.createBindings();
    ctx.setBindings(ctxGlobalBindings, GLOBAL_SCOPE);
    ctx.setAttribute("n1", 4, GLOBAL_SCOPE);
    ctx.setAttribute("n2", 5, ENGINE_SCOPE);
    ctx.setAttribute("msg", "ScriptContext:", ENGINE_SCOPE);
    engine.eval(script, ctx);

    engine.eval(script);
}

From source file:io.cloudslang.lang.runtime.bindings.ScriptEvaluator.java

public Serializable evalExpr(String expr, Map<String, ? extends Serializable> context) {
    ScriptContext scriptContext = new SimpleScriptContext();
    for (Map.Entry<String, ? extends Serializable> entry : context.entrySet()) {
        scriptContext.setAttribute(entry.getKey(), entry.getValue(), ScriptContext.ENGINE_SCOPE);
    }/*from   w  w w. ja v a2 s .  c  o m*/
    if (scriptContext.getAttribute(TRUE) == null)
        scriptContext.setAttribute(TRUE, Boolean.TRUE, ScriptContext.ENGINE_SCOPE);
    if (scriptContext.getAttribute(FALSE) == null)
        scriptContext.setAttribute(FALSE, Boolean.FALSE, ScriptContext.ENGINE_SCOPE);
    try {
        return (Serializable) engine.eval(expr, scriptContext);
    } catch (ScriptException e) {
        ScriptException scriptException = new ScriptException(e);
        throw new RuntimeException("Error in running script expression or variable reference, for expression: '"
                + expr + "',\n\tScript exception is: " + scriptException.getMessage(), scriptException);
    }
}