Search in sources :

Example 1 with ExpressionContext

use of org.apache.commons.jxpath.ExpressionContext in project vorto by eclipse.

the class JavascriptEvalFunction method invoke.

@Override
@SuppressWarnings({ "rawtypes" })
public Object invoke(ExpressionContext context, Object[] parameters) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    try {
        engine.eval(functionBody);
    } catch (ScriptException e) {
        throw new JXPathException("Problem evaluating " + functionName, e);
    }
    Invocable inv = (Invocable) engine;
    try {
        Object[] args;
        int pi = 0;
        Class[] types = toTypes(parameters);
        if (types.length >= 1 && ExpressionContext.class.isAssignableFrom(types[0])) {
            pi = 1;
        }
        args = new Object[parameters.length + pi];
        if (pi == 1) {
            args[0] = context;
        }
        for (int i = 0; i < parameters.length; i++) {
            args[i + pi] = TypeUtils.convert(parameters[i], types[i + pi]);
        }
        return inv.invokeFunction(functionName, unwrap(args));
    } catch (Throwable ex) {
        throw new JXPathInvalidAccessException("Cannot invoke " + functionName, ex);
    }
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) JXPathInvalidAccessException(org.apache.commons.jxpath.JXPathInvalidAccessException) ScriptEngine(javax.script.ScriptEngine) ScriptException(javax.script.ScriptException) Invocable(javax.script.Invocable) ExpressionContext(org.apache.commons.jxpath.ExpressionContext) JXPathException(org.apache.commons.jxpath.JXPathException)

Aggregations

Invocable (javax.script.Invocable)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptEngineManager (javax.script.ScriptEngineManager)1 ScriptException (javax.script.ScriptException)1 ExpressionContext (org.apache.commons.jxpath.ExpressionContext)1 JXPathException (org.apache.commons.jxpath.JXPathException)1 JXPathInvalidAccessException (org.apache.commons.jxpath.JXPathInvalidAccessException)1