Example usage for javax.script ScriptEngine eval

List of usage examples for javax.script ScriptEngine eval

Introduction

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

Prototype

public Object eval(Reader reader) throws ScriptException;

Source Link

Document

Same as eval(String) except that the source of the script is provided as a Reader

Usage

From source file:org.eclairjs.nashorn.Utils.java

public static Object toJsArray(Object javaArray) {
    Object jsArray = javaArray;// ww w  . ja v a2s . c  om
    ScriptEngine engine = NashornEngineSingleton.getEngine();
    try {
        String func = "function(javaArray){return Java.from(javaArray)}";
        Object fn = engine.eval(func);
        Object params[] = { jsArray };
        jsArray = ((ScriptObjectMirror) fn).call(null, params);

    } catch (ScriptException e) {
        e.printStackTrace();
    }
    return jsArray;
}

From source file:org.myperl.MathService.java

@GET
@Path("/")
public String get(@QueryParam("exper") String expression) throws ScriptException {
    ScriptEngine engine = manager.getEngineByName("nashorn");
    return engine.eval(expression).toString();
}

From source file:org.tamilunicodeconverter.converter.BaminiConverter.java

public String getUnicodeTextUsingJavaScript(String text) {
    String unicodeText = "";

    try {/*from w  ww. jav a2 s.co m*/
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        engine.eval(getScriptContent());
        Invocable invokable = (Invocable) engine;
        unicodeText = (String) invokable.invokeFunction("convert", new String[] { text });
    } catch (Exception ex) {
        throw new UnhandledException("Error occured while converting bamini text to unicode", ex);
    }
    return unicodeText;
}

From source file:org.geoserver.script.wfs.WfsTxHookTest.java

public void testHookError() throws Exception {
    File script = copyOverFile("tx-error");

    TransactionRequest tx = new TransactionRequest.WFS11(null);
    TransactionResponse res = new TransactionResponse.WFS11(null);

    Map context = new HashMap();

    ScriptEngine eng = scriptMgr.createNewEngine(script);
    eng.eval(new FileReader(script));

    WfsTxHook hook = getScriptManager().lookupWfsTxHook(script);
    try {//from   w  w  w  . j av a  2  s.co  m
        hook.handleBefore(eng, tx, context);
        fail("exected WFS exception");
    } catch (WFSException e) {
        assertEquals("before exception", e.getMessage());
    }
}

From source file:io.lightlink.translator.JSBeautifyPostProcessor.java

@Override
public String process(String script) throws IOException {
    String beautifier = IOUtils.toString(Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("io/lightlink/translator/beautify.js"));

    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    try {/*from w  w w  . j av a  2  s . c  o m*/
        engine.eval(beautifier);
        engine.getBindings(ScriptContext.ENGINE_SCOPE).put("$originalScript", script);
        return engine.eval("js_beautify($originalScript, {})") + "";
    } catch (ScriptException e) {
        LOG.error(e.toString(), e);
        return script;
    }

}

From source file:org.siphon.common.js.JSON.java

public JSON(ScriptEngine jsEngine) {
    try {/*  ww  w  .j  a va2 s  .c  o  m*/
        this.json = (ScriptObjectMirror) jsEngine.eval("JSON");
    } catch (ScriptException e) {
    }
}

From source file:org.eclairjs.nashorn.sql.JSUDF1.java

@SuppressWarnings({ "null", "unchecked" })
@Override/*from   www. j  a  v  a 2s.  co  m*/
public Object call(Object o) throws Exception {
    ScriptEngine e = NashornEngineSingleton.getEngine();
    if (this.fn == null) {
        this.fn = e.eval(func);
    }
    Invocable invocable = (Invocable) e;

    Object params[] = { this.fn, o };

    if (this.args != null && this.args.length > 0) {
        params = ArrayUtils.addAll(params, this.args);
    }

    Object ret = invocable.invokeFunction("Utils_invoke", params);
    ret = this.castValueToReturnType(ret);
    return ret;
}

From source file:org.eclairjs.nashorn.sql.JSUDF2.java

@SuppressWarnings({ "null", "unchecked" })
@Override/*from  w w w.ja  va 2  s  . c o  m*/
public Object call(Object o, Object o2) throws Exception {
    ScriptEngine e = NashornEngineSingleton.getEngine();
    if (this.fn == null) {
        this.fn = e.eval(func);
    }
    Invocable invocable = (Invocable) e;

    Object params[] = { this.fn, o, o2 };

    if (this.args != null && this.args.length > 0) {
        params = ArrayUtils.addAll(params, this.args);
    }

    Object ret = invocable.invokeFunction("Utils_invoke", params);
    ret = this.castValueToReturnType(ret);
    return ret;
}

From source file:org.eclairjs.nashorn.sql.JSUDF3.java

@SuppressWarnings({ "null", "unchecked" })
@Override/*from  w  w w .ja  v  a2s.c o m*/
public Object call(Object o, Object o2, Object o3) throws Exception {
    ScriptEngine e = NashornEngineSingleton.getEngine();
    if (this.fn == null) {
        this.fn = e.eval(func);
    }
    Invocable invocable = (Invocable) e;

    Object params[] = { this.fn, o, o2, o3 };

    if (this.args != null && this.args.length > 0) {
        params = ArrayUtils.addAll(params, this.args);
    }

    Object ret = invocable.invokeFunction("Utils_invoke", params);
    ret = this.castValueToReturnType(ret);
    return ret;
}

From source file:org.eclairjs.nashorn.sql.JSUDF4.java

@SuppressWarnings({ "null", "unchecked" })
@Override//w w w .j av a  2s  . com
public Object call(Object o, Object o2, Object o3, Object o4) throws Exception {
    ScriptEngine e = NashornEngineSingleton.getEngine();
    if (this.fn == null) {
        this.fn = e.eval(func);
    }
    Invocable invocable = (Invocable) e;

    Object params[] = { this.fn, o, o2, o3, o4 };

    if (this.args != null && this.args.length > 0) {
        params = ArrayUtils.addAll(params, this.args);
    }

    Object ret = invocable.invokeFunction("Utils_invoke", params);
    ret = this.castValueToReturnType(ret);
    return ret;
}