Search in sources :

Example 21 with JexlExpression

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);
}
Also used : JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlBuilder(org.apache.commons.jexl3.JexlBuilder) JexlExpression(org.apache.commons.jexl3.JexlExpression) Test(org.junit.Test)

Example 22 with JexlExpression

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));
}
Also used : JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlBuilder(org.apache.commons.jexl3.JexlBuilder) JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext) JexlExpression(org.apache.commons.jexl3.JexlExpression) Test(org.junit.Test)

Example 23 with JexlExpression

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);
}
Also used : JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlBuilder(org.apache.commons.jexl3.JexlBuilder) JexlContext(org.apache.commons.jexl3.JexlContext) ArrayList(java.util.ArrayList) MapContext(org.apache.commons.jexl3.MapContext) JexlExpression(org.apache.commons.jexl3.JexlExpression)

Example 24 with JexlExpression

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);
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) JexlExpression(org.apache.commons.jexl3.JexlExpression) MapContext(org.apache.commons.jexl3.MapContext)

Example 25 with JexlExpression

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));
    }
}
Also used : ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) JexlExpression(org.apache.commons.jexl3.JexlExpression) DBException(org.jkiss.dbeaver.DBException)

Aggregations

JexlExpression (org.apache.commons.jexl3.JexlExpression)43 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)18 MapContext (org.apache.commons.jexl3.MapContext)18 JexlEngine (org.apache.commons.jexl3.JexlEngine)17 JexlContext (org.apache.commons.jexl3.JexlContext)13 Test (org.junit.jupiter.api.Test)10 Test (org.junit.Test)9 JexlException (org.apache.commons.jexl3.JexlException)8 HashMap (java.util.HashMap)4 DBException (org.jkiss.dbeaver.DBException)4 IntrospectionException (java.beans.IntrospectionException)2 IOException (java.io.IOException)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Engine (org.apache.commons.jexl3.internal.Engine)2 IOUtilities.readAsString (org.finos.waltz.common.IOUtilities.readAsString)2 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 ResultSetRow (org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow)2 ThreadSharedProperties (com.revolsys.collection.map.ThreadSharedProperties)1 DataExtractionException (io.github.linuxforhealth.core.exception.DataExtractionException)1