org.droolsassert.util.MvelProcessor Maven / Gradle / Ivy
The newest version!
package org.droolsassert.util;
import static org.apache.commons.lang3.StringUtils.defaultIfEmpty;
import static org.mvel2.MVEL.compileExpression;
import static org.mvel2.MVEL.executeExpression;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import org.mvel2.ParserContext;
public class MvelProcessor extends PatternProcessor {
private static final String PATTERN = "\\$\\$\\{(?(?!.*?\\$\\{).*?)\\}\\$|\\$\\{(?(?!.*?\\$\\{).*?)\\}";
protected final ParserContext parserContext;
protected volatile Map executionContext = executionContext();
public MvelProcessor() {
super(PATTERN);
try {
parserContext = parserContext();
} catch (Exception e) {
throw new RuntimeException("Cannot create parser context ", e);
}
}
public void importPackage(String packageName) {
parserContext.addPackageImport(packageName);
}
/**
* Define execution context variable
*
* @param name
* @param value
* @return
*/
public Object define(String name, Object value) {
return executionContext.put(name, value);
}
/**
* Reset execution context variables
*/
public void reset() {
executionContext = executionContext();
}
@Override
protected String resolve(Matcher matcher) {
String expression = defaultIfEmpty(matcher.group("long"), matcher.group("short"));
return String.valueOf(executionContext.containsKey(expression) ? executionContext.get(expression) : evaluate(expression));
}
@SuppressWarnings("unchecked")
public T evaluate(String expression) {
try {
return (T) executeExpression(compileExpression(expression, parserContext), executionContext);
} catch (Exception e) {
throw new RuntimeException("Cannot evaluate " + expression, e);
}
}
/**
* Execution context which could be reset with {@link #reset()}
*/
protected Map executionContext() {
return new HashMap<>();
}
/**
* Parser context used for evaluation.
*
* @throws Exception
*/
protected ParserContext parserContext() throws Exception {
return new ParserContext();
}
}