Search in sources :

Example 11 with JexlEngine

use of org.apache.commons.jexl3.JexlEngine in project dhis2-core by dhis2.

the class ExpressionUtils method evaluate.

/**
     * @param expression the expression.
     * @param vars the variables, can be null.
     * @param strict indicates whether to use strict or lenient engine mode.
     * @return the result of the evaluation.
     */
private static Object evaluate(String expression, Map<String, Object> vars, boolean strict) {
    expression = expression.replaceAll(IGNORED_KEYWORDS_REGEX, StringUtils.EMPTY);
    JexlEngine engine = strict ? JEXL_STRICT : JEXL;
    Expression exp = engine.createExpression(expression);
    JexlContext context = vars != null ? new MapContext(vars) : new MapContext();
    return exp.evaluate(context);
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext)

Example 12 with JexlEngine

use of org.apache.commons.jexl3.JexlEngine in project nutch by apache.

the class JexlUtil method parseExpression.

/**
 * Parses the given experssion to a Jexl expression. This supports
 * date parsing.
 *
 * @param expr the Jexl expression
 * @return parsed Jexl expression or null in case of parse error
 */
public static Expression parseExpression(String expr) {
    if (expr == null)
        return null;
    try {
        // Translate any date object into a long, dates must be specified as 20-03-2016T00:00:00Z
        Matcher matcher = datePattern.matcher(expr);
        if (matcher.find()) {
            String date = matcher.group();
            // Parse the thing and get epoch!
            Date parsedDate = DateUtils.parseDateStrictly(date, new String[] { "yyyy-MM-dd'T'HH:mm:ss'Z'" });
            long time = parsedDate.getTime();
            // Replace in the original expression
            expr = expr.replace(date, Long.toString(time));
        }
        JexlEngine jexl = new JexlEngine();
        jexl.setSilent(true);
        jexl.setStrict(true);
        return jexl.createExpression(expr);
    } catch (Exception e) {
        LOG.error(e.getMessage());
    }
    return null;
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) Matcher(java.util.regex.Matcher) Date(java.util.Date)

Aggregations

JexlEngine (org.apache.commons.jexl2.JexlEngine)10 MapContext (org.apache.commons.jexl2.MapContext)5 Expression (org.apache.commons.jexl2.Expression)3 HashMap (java.util.HashMap)2 JexlContext (org.apache.commons.jexl2.JexlContext)2 JexlEngine (org.apache.commons.jexl3.JexlEngine)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 ExpressionImpl (org.apache.commons.jexl2.ExpressionImpl)1 JexlException (org.apache.commons.jexl2.JexlException)1 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)1 DBException (org.jkiss.dbeaver.DBException)1 RrdException (org.jrobin.core.RrdException)1 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)1 MissingParameterException (org.opennms.web.servlet.MissingParameterException)1