Search in sources :

Example 1 with JXPathInvalidAccessException

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

the class AbstractDataMapper method mapFunctionBlock.

private FunctionblockData mapFunctionBlock(ModelProperty fbProperty, JXPathContext context) {
    FunctionblockData fbData = new FunctionblockData(fbProperty.getName());
    FunctionblockModel fbModel = specification.getFunctionBlock(fbProperty.getName());
    for (ModelProperty statusProperty : fbModel.getStatusProperties()) {
        try {
            Object mapped = this.mapProperty(statusProperty, context);
            if (mapped != null) {
                fbData.withStatusProperty(statusProperty.getName(), mapped);
            }
        } catch (JXPathNotFoundException | JXPathInvalidAccessException ex) {
            if (statusProperty.isMandatory()) {
                return null;
            }
        }
    }
    for (ModelProperty configProperty : fbModel.getConfigurationProperties()) {
        try {
            Object mapped = this.mapProperty(configProperty, context);
            if (mapped != null) {
                fbData.withConfigurationProperty(configProperty.getName(), mapped);
            }
        } catch (JXPathNotFoundException | JXPathInvalidAccessException ex) {
            if (configProperty.isMandatory()) {
                return null;
            }
        }
    }
    return onlyReturnIfPopulated(fbData);
}
Also used : FunctionblockModel(org.eclipse.vorto.repository.api.content.FunctionblockModel) JXPathNotFoundException(org.apache.commons.jxpath.JXPathNotFoundException) ModelProperty(org.eclipse.vorto.repository.api.content.ModelProperty) FunctionblockData(org.eclipse.vorto.service.mapping.normalized.FunctionblockData) JXPathInvalidAccessException(org.apache.commons.jxpath.JXPathInvalidAccessException)

Example 2 with JXPathInvalidAccessException

use of org.apache.commons.jxpath.JXPathInvalidAccessException 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

JXPathInvalidAccessException (org.apache.commons.jxpath.JXPathInvalidAccessException)2 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 JXPathNotFoundException (org.apache.commons.jxpath.JXPathNotFoundException)1 FunctionblockModel (org.eclipse.vorto.repository.api.content.FunctionblockModel)1 ModelProperty (org.eclipse.vorto.repository.api.content.ModelProperty)1 FunctionblockData (org.eclipse.vorto.service.mapping.normalized.FunctionblockData)1