Search in sources :

Example 11 with JexlExpression

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

Example 12 with JexlExpression

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;
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) JexlBuilder(org.apache.commons.jexl3.JexlBuilder)

Example 13 with JexlExpression

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]);
}
Also used : Engine(org.apache.commons.jexl3.internal.Engine) Test(org.junit.Test)

Example 14 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 15 with JexlExpression

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);
}
Also used : Debugger(org.apache.commons.jexl3.internal.Debugger) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

JexlExpression (org.apache.commons.jexl3.JexlExpression)42 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