Example usage for javax.script Invocable invokeFunction

List of usage examples for javax.script Invocable invokeFunction

Introduction

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

Prototype

public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException;

Source Link

Document

Used to call top-level procedures and functions defined in scripts.

Usage

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

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

    try {//from ww  w  .  j av  a2s .  c  o  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:temp.groovy.java

@Test
public void foo() {
    JexlEngine eng = new JexlEngine();
    eng.setLenient(false);//from  w  w  w. j  av a 2 s. c o  m
    eng.setSilent(false);
    Expression expr = eng.createExpression("a == '9' and a =~ ['7','9']");
    System.out.println(expr.dump());
    JexlContext ctx = new MapContext();
    ctx.set("a", "9");
    Boolean result = (Boolean) expr.evaluate(ctx);
    try {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("jython");

        engine.put("first", 9);
        result = (Boolean) engine.eval("first not in [7,9]");
        result = (Boolean) engine.eval("first in ['7','9']");
        assertEquals(true, result);
        //This next example illustrates calling an invokable function:

        String fact = "def factorial(n) { n == 1 ? 1 : n * factorial(n - 1) }";
        engine.eval(fact);
        Invocable inv = (Invocable) engine;
        Object[] params = { 5 };
        Object oresult = inv.invokeFunction("factorial", params);
        assertEquals(120, oresult);
    } catch (ScriptException | NoSuchMethodException ex) {
        Logger.getLogger(groovy.class.getName()).log(Level.SEVERE, null, ex);
    }
}

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

@SuppressWarnings({ "null", "unchecked" })
@Override/*from  w w w. j  a  v a  2 s.c  om*/
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. j  av a  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/* w  w w. j ava2 s. co 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// ww  w. java 2 s.c  o m
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;
}

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

@SuppressWarnings({ "null", "unchecked" })
@Override//from w w w  .j  a  va  2  s. c  om
public Object call(Object o, Object o2, Object o3, Object o4, Object o5) 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, o5 };

    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.JSUDF7.java

@SuppressWarnings({ "null", "unchecked" })
@Override//from  w  w  w  .j a v a  2 s.c o  m
public Object call(Object o, Object o2, Object o3, Object o4, Object o5, Object o6, Object o7)
        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, o5, o6, o7 };

    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.JSUDF8.java

@SuppressWarnings({ "null", "unchecked" })
@Override//ww w. j a v  a2s  . c om
public Object call(Object o, Object o2, Object o3, Object o4, Object o5, Object o6, Object o7, Object o8)
        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, o5, o6, o7, o8 };

    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.JSUDF9.java

@SuppressWarnings({ "null", "unchecked" })
@Override/*from  w  ww  . ja v a2s  . c  o  m*/
public Object call(Object o, Object o2, Object o3, Object o4, Object o5, Object o6, Object o7, Object o8,
        Object o9) 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, o5, o6, o7, o8, o9 };

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