Example usage for javax.script Bindings put

List of usage examples for javax.script Bindings put

Introduction

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

Prototype

public Object put(String name, Object value);

Source Link

Document

Set a named value.

Usage

From source file:Main.java

public static void main(String[] args) {
    Bindings params = new SimpleBindings();
    params.put("stringKey", "Hello");
    params.put("valueKey", 2015);

    Object msg = params.get("stringKey");
    Object year = params.get("valueKey");
    System.out.println("stringKey" + msg);
    System.out.println("valueKey = " + year);

    params.remove("valueKey");
    year = params.get("valueKey");

    boolean containsYear = params.containsKey("valueKey");
    System.out.println("valueKey = " + year);
    System.out.println("params contains year = " + containsYear);
}

From source file:Main.java

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

    Bindings params = engine.createBindings();

    params.put("stringKey", "Hello");
    params.put("valueKey", 2015);

    Object msg = params.get("stringKey");
    Object year = params.get("valueKey");
    System.out.println("stringKey" + msg);
    System.out.println("valueKey = " + year);

    params.remove("valueKey");
    year = params.get("valueKey");

    boolean containsYear = params.containsKey("valueKey");
    System.out.println("valueKey = " + year);
    System.out.println("params contains year = " + containsYear);
}

From source file:Main.java

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

    if (!(engine instanceof Compilable)) {
        System.out.println("Script compilation not supported.");
        return;//  w  w  w  . j a v a  2s .c om
    }
    Compilable comp = (Compilable) engine;

    CompiledScript cScript = comp.compile("print(n1 + n2)");

    Bindings scriptParams = engine.createBindings();
    scriptParams.put("n1", 2);
    scriptParams.put("n2", 3);
    cScript.eval(scriptParams);

    scriptParams.put("n1", 9);
    scriptParams.put("n2", 7);
    cScript.eval(scriptParams);
}

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);//from   w  w  w. ja  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:Main.java

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));
}

From source file:GetToKnowBindingsAndScopes.java

public static void main(String[] args) {
    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));

    try {//from  www.  j a  v  a2  s .  co m
        Bindings bindings = engine.createBindings();
        bindings.put("engine", "overridden engine bindings");
        bindings.put("app", new GetToKnowBindingsAndScopes());
        bindings.put("bindings", bindings);
        engine.eval("app.dumpBindings (bindings);", bindings);
    } catch (ScriptException se) {
        System.err.println(se.getMessage());
    }

    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));
}

From source file:Main.java

public static void main(String[] args) throws ScriptException {
    ScriptEngine jse = new ScriptEngineManager().getEngineByName("javascript");
    Bindings bindings = jse.getBindings(ScriptContext.ENGINE_SCOPE);
    Main test = new Main();
    bindings.put("test", test);
    String preFix = "demo";
    jse.eval("test." + preFix + "_test()");
    preFix = "actual";
    jse.eval("test." + preFix + "_test()");
}

From source file:com.xafero.vee.cmd.MainApp.java

private static void inject(Bindings env, File file) {
    env.put("window", new Window());
    env.put("console", new Console(file.getName()));
    env.put("fs", new FileSystem());
    env.put("media", new Media());
    env.put("time", new Time());
    env.put("localStorage", new LocalStorage());
    env.put("sessionStorage", new SessionStorage());
}

From source file:org.opennms.opennms.pris.plugins.script.util.ScriptManager.java

public static Object execute(final InstanceConfiguration config, final Map<String, Object> bindings)
        throws IOException, ScriptException {

    Requisition requisition = null;// w  ww  .  j a  va  2  s.  co m
    // Get the path to the script
    final List<Path> scripts = config.getPaths("file");

    // Get the script engine by language defined in config or by extension if it
    // is not defined in the config
    final ScriptEngineManager SCRIPT_ENGINE_MANAGER = new ScriptEngineManager(
            ScriptManager.class.getClassLoader());

    for (Path script : scripts) {

        final ScriptEngine scriptEngine = config.containsKey("lang")
                ? SCRIPT_ENGINE_MANAGER.getEngineByName(config.getString("lang"))
                : SCRIPT_ENGINE_MANAGER.getEngineByExtension(FilenameUtils.getExtension(script.toString()));

        if (scriptEngine == null) {
            throw new RuntimeException("Script engine implementation not found");
        }

        // Create some bindings for values available in the script
        final Bindings scriptBindings = scriptEngine.createBindings();
        scriptBindings.put("script", script);
        scriptBindings.put("logger", LoggerFactory.getLogger(script.toString()));
        scriptBindings.put("config", config);
        scriptBindings.put("instance", config.getInstanceIdentifier());
        scriptBindings.put("interfaceUtils", new InterfaceUtils(config));
        scriptBindings.putAll(bindings);

        // Overwrite initial requisition with the requisition from the previous script, if there was any.
        if (requisition != null) {
            scriptBindings.put("requisition", requisition);
        }

        // Evaluate the script and return the requisition created in the script
        try (final Reader scriptReader = Files.newBufferedReader(script, StandardCharsets.UTF_8)) {
            LOGGER.debug("Start Script {}", script);
            requisition = (Requisition) scriptEngine.eval(scriptReader, scriptBindings);
            LOGGER.debug("Done  Script {}", script);
        }
    }
    return requisition;
}

From source file:org.omegat.gui.scripting.ScriptRunner.java

/**
 * Execute a script with a given engine and bindings.
 * /*from  w w w .j ava 2 s.c o  m*/
 * @param script
 *            The script in string form
 * @param engine
 *            The engine
 * @param additionalBindings
 *            A map of bindings that will be included along with other
 *            bindings
 * @return The evaluation result
 * @throws ScriptException
 */
public static Object executeScript(String script, ScriptEngine engine, Map<String, Object> additionalBindings)
        throws ScriptException {
    // logResult(StaticUtils.format(OStrings.getString("SCW_SELECTED_LANGUAGE"),
    // engine.getFactory().getEngineName()));
    Bindings bindings = engine.createBindings();
    bindings.put(VAR_PROJECT, Core.getProject());
    bindings.put(VAR_EDITOR, Core.getEditor());
    bindings.put(VAR_GLOSSARY, Core.getGlossary());
    bindings.put(VAR_MAINWINDOW, Core.getMainWindow());
    bindings.put(VAR_CORE, Core.class);

    if (additionalBindings != null) {
        bindings.putAll(additionalBindings);
    }
    engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
    Object result = engine.eval(script);
    if (engine instanceof Invocable) {
        invokeGuiScript((Invocable) engine);
    }
    return result;
}