Example usage for javax.script SimpleBindings putAll

List of usage examples for javax.script SimpleBindings putAll

Introduction

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

Prototype

public void putAll(Map<? extends String, ? extends Object> toMerge) 

Source Link

Document

putAll is implemented using Map.putAll .

Usage

From source file:org.nuxeo.ecm.webengine.scripting.Scripting.java

protected Object _runScript(ScriptFile script, Map<String, Object> args) throws Exception {
    SimpleBindings bindings = new SimpleBindings();
    if (args != null) {
        bindings.putAll(args);
    }//from ww w.  j  av  a  2 s  . co m
    String ext = script.getExtension();
    // check for a script engine
    ScriptEngine engine = getEngineManager().getEngineByExtension(ext);
    if (engine != null) {
        ScriptContext ctx = new SimpleScriptContext();
        ctx.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        CompiledScript comp = getCompiledScript(engine, script.getFile()); // use cache for compiled scripts
        if (comp != null) {
            return comp.eval(ctx);
        } // compilation not supported - eval it on the fly
        try {
            Reader reader = new FileReader(script.getFile());
            try { // TODO use __result__ to pass return value for engine that doesn't returns like jython
                Object result = engine.eval(reader, ctx);
                if (result == null) {
                    result = bindings.get("__result__");
                }
                return result;
            } finally {
                reader.close();
            }
        } catch (IOException e) {
            throw new ScriptException(e);
        }
    }
    return null;
}

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 {/*  w  ww . j a  v  a  2s .co  m*/
        scriptEngine.eval(scripts.get(resourceName));
        return scriptEngine.eval(function + "()");
    } catch (ScriptException e) {
        throw new BSTException(line, "Script exception : " + e.getMessage(), story);
    }
}