Search in sources :

Example 61 with Invocable

use of javax.script.Invocable in project omegat by omegat-org.

the class ScriptRunner method executeScript.

/**
 * Execute a script with a given engine and bindings.
 *
 * @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;
}
Also used : Invocable(javax.script.Invocable) Bindings(javax.script.Bindings)

Example 62 with Invocable

use of javax.script.Invocable in project CitizensAPI by CitizensDev.

the class ScriptCompiler method loadEngine.

private ScriptEngine loadEngine(String extension) {
    ScriptEngine engine = engines.get(extension);
    if (engine != null)
        return engine;
    ScriptEngine search = null;
    if (extension.equals("js") || extension.equals("javascript")) {
        search = engineManager.getEngineByName("graal.js");
        if (search == null) {
            search = engineManager.getEngineByName("nashorn");
        }
    }
    if (search == null) {
        search = engineManager.getEngineByExtension(extension);
    }
    if (search != null && (!(search instanceof Compilable) || !(search instanceof Invocable))) {
        search = null;
    } else if (search != null) {
        search = tryUpdateClassLoader(search);
    }
    engines.put(extension, search);
    ClassLoader cl = classLoader.get();
    if (cl != null) {
        updateSunClassLoader(cl);
    }
    return search;
}
Also used : Invocable(javax.script.Invocable) Compilable(javax.script.Compilable) ScriptEngine(javax.script.ScriptEngine)

Example 63 with Invocable

use of javax.script.Invocable in project jgnash by ccavanaugh.

the class ImportFilter method processPayee.

public String processPayee(final String payee) {
    try {
        final Invocable invocable = (Invocable) scriptEngine;
        final Object result = invocable.invokeFunction("processPayee", payee);
        return result.toString();
    } catch (final ScriptException | NoSuchMethodException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    return payee;
}
Also used : Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException)

Example 64 with Invocable

use of javax.script.Invocable in project jgnash by ccavanaugh.

the class ImportFilter method processMemo.

public String processMemo(final String memo) {
    try {
        final Invocable invocable = (Invocable) scriptEngine;
        final Object result = invocable.invokeFunction("processMemo", memo);
        return result.toString();
    } catch (final ScriptException | NoSuchMethodException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    return memo;
}
Also used : Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException)

Example 65 with Invocable

use of javax.script.Invocable in project es6draft by anba.

the class InvocableTest method setUp.

@Before
public void setUp() {
    manager = new ScriptEngineManager();
    engine = manager.getEngineByName("es6draft");
    assertThat(engine, notNullValue());
    assertThat(engine, instanceOf(Invocable.class));
    invocable = (Invocable) engine;
}
Also used : Invocable(javax.script.Invocable) ScriptEngineManager(javax.script.ScriptEngineManager) Before(org.junit.Before)

Aggregations

Invocable (javax.script.Invocable)74 ScriptException (javax.script.ScriptException)45 ScriptEngine (javax.script.ScriptEngine)34 ScriptEngineManager (javax.script.ScriptEngineManager)24 IOException (java.io.IOException)19 File (java.io.File)10 InputStreamReader (java.io.InputStreamReader)10 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)10 FileReader (java.io.FileReader)9 Reader (java.io.Reader)9 Compilable (javax.script.Compilable)8 CompiledScript (javax.script.CompiledScript)8 Bindings (javax.script.Bindings)7 Map (java.util.Map)5 BufferedReader (java.io.BufferedReader)4 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 Writer (java.io.Writer)3 HashMap (java.util.HashMap)3 ScriptContext (javax.script.ScriptContext)3