Example usage for javax.script SimpleBindings SimpleBindings

List of usage examples for javax.script SimpleBindings SimpleBindings

Introduction

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

Prototype

public SimpleBindings() 

Source Link

Document

Default constructor uses a HashMap .

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) 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) {
    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:com.tussle.script.StackedBindings.java

public StackedBindings() {
    bindingStack = new ArrayDeque<>();
    bindingStack.push(new SimpleBindings());
    bindingMap = new HashMap<>();
}

From source file:org.paxml.test.SelfTest.java

public void testSyntax() throws Exception {
    ScriptEngine runtime = new ScriptEngineManager().getEngineByName("javascript");
    Bindings bindings = runtime.getBindings(ScriptContext.ENGINE_SCOPE);
    bindings.put("util", "xx");
    runtime.setBindings(new SimpleBindings() {

    }, ScriptContext.ENGINE_SCOPE);

}

From source file:org.craftercms.engine.scripting.impl.Jsr233CompiledScript.java

@Override
public Object execute(Map<String, Object> variables) throws ScriptException {
    Bindings bindings = new SimpleBindings();
    if (MapUtils.isNotEmpty(globalVariables)) {
        bindings.putAll(globalVariables);
    }/*from w  w w.  ja v a  2  s  .c o  m*/
    if (MapUtils.isNotEmpty(variables)) {
        bindings.putAll(variables);
    }

    try {
        return script.eval(bindings);
    } catch (Exception e) {
        throw new ScriptException(e.getMessage(), e);
    }
}

From source file:utybo.branchingstorytree.swing.impl.XSFClient.java

@Override
public Object invokeScript(String resourceName, String function, XSFBridge bst, BranchingStory story, int line)
        throws BSTException {
    ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
    SimpleBindings binds = new SimpleBindings();
    binds.putAll(story.getRegistry().getAllInt());
    binds.putAll(story.getRegistry().getAllString());
    binds.put("bst", bst);
    scriptEngine.setBindings(binds, ScriptContext.ENGINE_SCOPE);
    try {/*from   w w  w .ja  v a 2  s  .  c  om*/
        scriptEngine.eval(scripts.get(resourceName));
        return scriptEngine.eval(function + "()");
    } catch (ScriptException e) {
        throw new BSTException(line, "Script exception : " + e.getMessage(), story);
    }
}

From source file:Engine.Lua.PlayerLua.java

public PlayerLua(String luaFile) {
    ScriptEngineManager sem = new ScriptEngineManager();
    ScriptEngine scriptEngine = sem.getEngineByName("luaj");
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    InputStream is = classloader.getResourceAsStream("Scripts/" + luaFile);
    InputStreamReader isr = new InputStreamReader(is);
    CompiledScript script;//from   w w w  .j  a va  2  s  .c om
    try {
        script = ((Compilable) scriptEngine).compile(isr);
        isr.close();
        is.close();
        sb = new SimpleBindings();
        script.eval(sb); // Put the Lua functions into the sb environment
    } catch (ScriptException ex) {
        Logger.getLogger(PlayerLua.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(PlayerLua.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.nuxeo.usermapper.extension.NashornUserMapper.java

@Override
public Object wrapNuxeoPrincipal(NuxeoPrincipal principal, Object userObject,
        Map<String, Serializable> params) {
    if (StringUtils.isEmpty(wrapperSource)) {
        return null;
    }//from   w  w w  . j  a  va  2s.  c  o m
    Bindings bindings = new SimpleBindings();
    bindings.put("nuxeoPrincipal", principal);
    bindings.put("userObject", userObject);
    bindings.put("params", params);
    try {
        engine.eval(wrapperSource, bindings);
    } catch (ScriptException e) {
        log.error("Error while executing JavaScript mapper", e);
    }
    return bindings.get("userObject");
}

From source file:com.tussle.script.StackedBindings.java

public void clear() {
    //Complete remove the stack
    bindingStack.clear();
    bindingStack.push(new SimpleBindings());
    bindingMap.clear();
}