use of org.apache.commons.jexl3.JexlExpression in project com.revolsys.open by revolsys.
the class Menu method addParameter.
public void addParameter(final String name, final Object value) {
if (value != null) {
this.parameters.put(name, value);
JexlExpression expression = null;
try {
expression = JexlUtil.newExpression(value.toString());
} catch (final Exception e) {
LOG.error("Invalid Jexl Expression '" + value + "': " + e.getMessage(), e);
}
if (expression != null) {
this.dynamicParameters.put(name, expression);
this.staticParameters.remove(name);
} else {
this.dynamicParameters.remove(name);
this.staticParameters.put(name, value);
}
} else {
removeParameter(name);
}
}
use of org.apache.commons.jexl3.JexlExpression in project com.revolsys.open by revolsys.
the class JexlUtil method newExpression.
/**
* <p>
* Convert expressions into valid JexlExpressions, if the string does not
* contain any expressions that match the expressionPattern then null will be
* returned and the caller can use the raw string.
* </p>
* <p>
* The expressionPattern can be used to define an alternate pattern than the
* {@link #DEFAULT_EXPRESSION_PATTERN} that defines expressions in the form
* ${el}. The pattern is defined as a Java Regular expression. The contents of
* the expression part of the pattern must be enclosed in () to define the
* group. The characters outside the first group will be removed from the
* string and the expression portion will be added to the expression.
* </p>
*
* @param expression The string containing expressions.
* @param expressionPattern The regular expression pattern used to identify
* expressions in the string. The first group in the expression will
* be used as the expression.
* @return The expression object for the string expression.
* @throws Exception If there was an error creating the expression.
*/
public static JexlExpression newExpression(final String expression, final String expressionPattern) throws Exception {
final String newExpression = expression.replaceAll("\n", "");
// Wrap the entires expression in '' and replace the expressions in the
// form "${expr)" to ' + expr + '
final Pattern compiledPattern = Pattern.compile(expressionPattern);
final Matcher matcher = compiledPattern.matcher(newExpression);
int lastEnd = 0;
if (matcher.find()) {
final StringBuilder jexlExpression = new StringBuilder();
do {
final int startIndex = matcher.start();
if (startIndex != lastEnd) {
final String text = newExpression.substring(lastEnd, startIndex);
addText(jexlExpression, text);
jexlExpression.append(" + ");
}
final String matchedExpression = matcher.group(1);
jexlExpression.append(matchedExpression).append(" + ");
lastEnd = matcher.end();
} while (matcher.find());
addText(jexlExpression, newExpression.substring(lastEnd));
// Remove any empty strings from the expression to improve
// performance
String expr = jexlExpression.toString();
expr = expr.replaceAll(" \\+ '' \\+ ", " + ");
expr = expr.replaceAll("^'' \\+ ", "");
expr = expr.replaceAll("\\+ ''$", "");
return new JexlBuilder().create().createExpression(expr);
} else {
return null;
}
}
use of org.apache.commons.jexl3.JexlExpression in project commons-jexl by apache.
the class Issues100Test method test105.
@Test
public void test105() throws Exception {
final JexlContext context = new MapContext();
final JexlExpression selectExp = new Engine().createExpression("[a.propA]");
context.set("a", new A105("a1", "p1"));
Object[] r = (Object[]) selectExp.evaluate(context);
Assert.assertEquals("p1", r[0]);
// selectExp = new Engine().createExpression("[a.propA]");
context.set("a", new A105("a2", "p2"));
r = (Object[]) selectExp.evaluate(context);
Assert.assertEquals("p2", r[0]);
}
use of org.apache.commons.jexl3.JexlExpression in project commons-jexl by apache.
the class ArrayTest method example.
/**
* An example for array access.
*/
static void example(final Output out) throws Exception {
/*
* First step is to retrieve an instance of a JexlEngine;
* it might be already existing and shared or created anew.
*/
final JexlEngine jexl = new JexlBuilder().create();
/*
* Second make a jexlContext and put stuff in it
*/
final JexlContext jc = new MapContext();
final List<Object> l = new ArrayList<Object>();
l.add("Hello from location 0");
final Integer two = 2;
l.add(two);
jc.set("array", l);
JexlExpression e = jexl.createExpression("array[1]");
Object o = e.evaluate(jc);
out.print("Object @ location 1 = ", o, two);
e = jexl.createExpression("array[0].length()");
o = e.evaluate(jc);
out.print("The length of the string at location 0 is : ", o, 21);
}
use of org.apache.commons.jexl3.JexlExpression in project commons-jexl by apache.
the class PropertyAccessTest method testStringIdentifier.
@Test
public void testStringIdentifier() throws Exception {
final Map<String, String> foo = new HashMap<String, String>();
final JexlContext jc = new MapContext();
jc.set("foo", foo);
foo.put("q u u x", "456");
JexlExpression e = JEXL.createExpression("foo.\"q u u x\"");
Object result = e.evaluate(jc);
Assert.assertEquals("456", result);
e = JEXL.createExpression("foo.'q u u x'");
result = e.evaluate(jc);
Assert.assertEquals("456", result);
JexlScript s = JEXL.createScript("foo.\"q u u x\"");
result = s.execute(jc);
Assert.assertEquals("456", result);
s = JEXL.createScript("foo.'q u u x'");
result = s.execute(jc);
Assert.assertEquals("456", result);
final Debugger dbg = new Debugger();
dbg.debug(e);
final String dbgdata = dbg.toString();
Assert.assertEquals("foo.'q u u x'", dbgdata);
}
Aggregations