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