Search in sources :

Example 26 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project dbeaver by serge-rider.

the class DBVUtils method executeExpression.

public static Object executeExpression(DBVEntityAttribute attribute, DBDAttributeBinding[] allAttributes, Object[] row) {
    String exprString = attribute.getExpression();
    if (CommonUtils.isEmpty(exprString)) {
        return null;
    }
    JexlExpression expression = attribute.getParsedExpression();
    if (expression == null) {
        return null;
    }
    return evaluateDataExpression(allAttributes, row, expression, attribute.getName());
}
Also used : JexlExpression(org.apache.commons.jexl3.JexlExpression)

Example 27 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project dbeaver by serge-rider.

the class DBVUtils method evaluateDataExpression.

public static Object evaluateDataExpression(DBDAttributeBinding[] allAttributes, Object[] row, JexlExpression expression, String attributeName) {
    Map<String, Object> nsList = getExpressionNamespaces();
    JexlContext context = new JexlContext() {

        @Override
        public Object get(String s) {
            Object ns = nsList.get(s);
            if (ns != null) {
                return ns;
            }
            if (s.equals(attributeName)) {
                return null;
            }
            for (DBDAttributeBinding attr : allAttributes) {
                if (s.equals(attr.getLabel())) {
                    return DBUtils.getAttributeValue(attr, allAttributes, row);
                }
            }
            return null;
        }

        @Override
        public void set(String s, Object o) {
        }

        @Override
        public boolean has(String s) {
            return get(s) != null;
        }
    };
    try {
        return expression.evaluate(context);
    } catch (Exception e) {
        return GeneralUtils.getExpressionParseMessage(e);
    }
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBException(org.jkiss.dbeaver.DBException)

Example 28 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project dbeaver by dbeaver.

the class DBVUtils method evaluateDataExpression.

public static Object evaluateDataExpression(DBDAttributeBinding[] allAttributes, Object[] row, JexlExpression expression, String attributeName) {
    Map<String, Object> nsList = getExpressionNamespaces();
    JexlContext context = new JexlContext() {

        @Override
        public Object get(String s) {
            Object ns = nsList.get(s);
            if (ns != null) {
                return ns;
            }
            if (s.equals(attributeName)) {
                return null;
            }
            for (DBDAttributeBinding attr : allAttributes) {
                if (s.equals(attr.getLabel())) {
                    return DBUtils.getAttributeValue(attr, allAttributes, row);
                }
            }
            return null;
        }

        @Override
        public void set(String s, Object o) {
        }

        @Override
        public boolean has(String s) {
            return get(s) != null;
        }
    };
    try {
        return expression.evaluate(context);
    } catch (Exception e) {
        return GeneralUtils.getExpressionParseMessage(e);
    }
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBException(org.jkiss.dbeaver.DBException)

Example 29 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project dbeaver by dbeaver.

the class DBVUtils method executeExpression.

public static Object executeExpression(DBVEntityAttribute attribute, DBDAttributeBinding[] allAttributes, Object[] row) {
    String exprString = attribute.getExpression();
    if (CommonUtils.isEmpty(exprString)) {
        return null;
    }
    JexlExpression expression = attribute.getParsedExpression();
    if (expression == null) {
        return null;
    }
    return evaluateDataExpression(allAttributes, row, expression, attribute.getName());
}
Also used : JexlExpression(org.apache.commons.jexl3.JexlExpression)

Example 30 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project nifi by apache.

the class ExtractCCDAAttributes method processElement.

/**
 * Process elements children based on the parser mapping.
 * Any String values are added to attributes
 * For List, the processList method is called to iterate and process
 * For an Object this method is called recursively
 * While adding to the attributes the key is prefixed by parent
 * @param parent       parent key for this element, used as a prefix for attribute key
 * @param element      element to be processed
 * @param attributes   map of attributes to populate
 * @return             map of processed data, value can contain String or Map of Strings
 */
protected Map<String, Object> processElement(String parent, Object element, Map<String, String> attributes) {
    final StopWatch stopWatch = new StopWatch(true);
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    String name = element.getClass().getName();
    // get JEXL mappings for this element
    Map<String, String> jexlMap = processMap.get(name);
    if (jexlMap == null) {
        getLogger().warn("Missing mapping for element " + name);
        return null;
    }
    for (Entry<String, String> entry : jexlMap.entrySet()) {
        // evaluate JEXL for each child element
        jexlCtx.set("element", element);
        JexlExpression jexlExpr = jexl.createExpression(entry.getValue());
        Object value = jexlExpr.evaluate(jexlCtx);
        String key = entry.getKey();
        String prefix = parent != null ? parent + "." + key : key;
        addElement(map, prefix, key, value, attributes);
    }
    stopWatch.stop();
    getLogger().debug("Processed {} in {}", new Object[] { name, stopWatch.getDuration(TimeUnit.MILLISECONDS) });
    return map;
}
Also used : JexlExpression(org.apache.commons.jexl3.JexlExpression) StopWatch(org.apache.nifi.util.StopWatch) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

JexlExpression (org.apache.commons.jexl3.JexlExpression)43 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)18 MapContext (org.apache.commons.jexl3.MapContext)18 JexlEngine (org.apache.commons.jexl3.JexlEngine)17 JexlContext (org.apache.commons.jexl3.JexlContext)13 Test (org.junit.jupiter.api.Test)10 Test (org.junit.Test)9 JexlException (org.apache.commons.jexl3.JexlException)8 HashMap (java.util.HashMap)4 DBException (org.jkiss.dbeaver.DBException)4 IntrospectionException (java.beans.IntrospectionException)2 IOException (java.io.IOException)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Engine (org.apache.commons.jexl3.internal.Engine)2 IOUtilities.readAsString (org.finos.waltz.common.IOUtilities.readAsString)2 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 ResultSetRow (org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow)2 ThreadSharedProperties (com.revolsys.collection.map.ThreadSharedProperties)1 DataExtractionException (io.github.linuxforhealth.core.exception.DataExtractionException)1