use of org.apache.commons.jexl3.JexlExpression in project commons-jexl by apache.
the class SandboxTest method testGetNullKeyAllowed0.
@Test
public void testGetNullKeyAllowed0() throws Exception {
JexlEngine jexl = new JexlBuilder().sandbox(new JexlSandbox(true)).create();
JexlExpression expression = jexl.createExpression("{null : 'foo'}[null]");
Object o = expression.evaluate(null);
Assert.assertEquals("foo", o);
}
use of org.apache.commons.jexl3.JexlExpression in project commons-jexl by apache.
the class SandboxTest method testSetNullKeyAllowed0.
@Test
public void testSetNullKeyAllowed0() throws Exception {
Arithmetic350 a350 = new Arithmetic350(true);
JexlEngine jexl = new JexlBuilder().arithmetic(a350).sandbox(new JexlSandbox(true)).create();
JexlContext jc = new MapContext();
JexlExpression expression = jexl.createExpression("{null : 'foo'}[null] = 'bar'");
expression.evaluate(jc);
Map<?, ?> map = a350.getLastMap();
Assert.assertEquals("bar", map.get(null));
}
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 newts by OpenNMS.
the class ResultDescriptor method expression.
public ResultDescriptor expression(String label, String expression) {
final JexlExpression expr = JEXL_ENGINE.createExpression(expression);
final String[] labels = getLabels().toArray(new String[0]);
CalculationFunction evaluate = new CalculationFunction() {
private static final long serialVersionUID = -3328049421398096252L;
@Override
public double apply(double... ds) {
JexlContext jc = new MapContext();
for (int i = 0; i < labels.length; i++) {
jc.set(labels[i], ds[i]);
}
return ((Number) expr.evaluate(jc)).doubleValue();
}
};
return calculate(label, evaluate, labels);
}
use of org.apache.commons.jexl3.JexlExpression in project dbeaver by serge-rider.
the class EditVirtualAttributePage method generatePreviewValue.
private void generatePreviewValue() {
if (viewer == null) {
return;
}
String expression = expressionText.getText();
ResultSetRow currentRow = viewer.getCurrentRow();
if (currentRow == null) {
previewText.setText("Select a row in data viewer to see expression results");
return;
}
try {
JexlExpression parsedExpression = DBVUtils.parseExpression(expression);
Object result = DBVUtils.evaluateDataExpression(viewer.getModel().getAttributes(), currentRow.values, parsedExpression, nameText.getText());
previewText.setText(CommonUtils.toString(result));
} catch (Exception e) {
previewText.setText(GeneralUtils.getExpressionParseMessage(e));
}
}
Aggregations