Search in sources :

Example 36 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project com.revolsys.open by revolsys.

the class MenuItem method addParameter.

public void addParameter(final String name, final Object value) throws Exception {
    if (value instanceof String) {
        final String stringValue = (String) value;
        final JexlExpression expression = JexlUtil.newExpression(stringValue);
        if (expression != null) {
            this.parameters.put(name, expression);
        } else {
            this.staticParameters.put(name, value);
        }
    } else {
        this.staticParameters.put(name, value);
    }
}
Also used : JexlExpression(org.apache.commons.jexl3.JexlExpression)

Example 37 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project com.revolsys.open by revolsys.

the class Menu method getLink.

public String getLink(final JexlContext context) {
    String baseUri = this.uri;
    if (this.uriExpression != null) {
        if (context != null) {
            baseUri = (String) JexlUtil.evaluateExpression(context, this.uriExpression);
        } else {
            baseUri = null;
        }
    }
    if (baseUri == null) {
        if (this.anchor != null) {
            return "#" + this.anchor;
        } else {
            return null;
        }
    } else {
        baseUri = HttpServletUtils.getAbsoluteUrl(PathAliasController.getPath(baseUri));
        Map<String, Object> params;
        if (context != null) {
            params = new HashMap<>(this.staticParameters);
            for (final Entry<String, JexlExpression> param : this.dynamicParameters.entrySet()) {
                final String key = param.getKey();
                final JexlExpression expression = param.getValue();
                final Object value = JexlUtil.evaluateExpression(context, expression);
                params.put(key, value);
            }
        } else {
            params = this.staticParameters;
        }
        final String link = UrlUtil.getUrl(baseUri, params);
        if (this.anchor == null) {
            return link;
        } else {
            return link + "#" + this.anchor;
        }
    }
}
Also used : JexlExpression(org.apache.commons.jexl3.JexlExpression)

Example 38 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project qaf by qmetry.

the class StringUtil method eval.

/**
 * <ul>
 * <li>In addition to context provided in method call, any property and static
 * methods/variables with fully qualified class name are supported as variable
 * in expression.
 * <li>Variable names are case-sensitive, e.g. var1 and Var1 are different
 * variables. However those provided in context are case insensitive.
 * <li>If variable names are not following jexl standards for variable names, it
 * can be referenced with <code>_ctx</code>. For example:
 * <br />commons-logging // invalid variable name (hyphenated) can be used in expression as <code>_ctx['commons-logging']</code>
 * <li>static methods and variables are allowed in expressions. For example,
 * <br />eval("java.lang.Math.min(23,a)", context)
 * </ul>
 * Refer <a href="https://commons.apache.org/proper/commons-jexl/reference/syntax.html">documentation</a> for expression syntax.
 *
 * @param expression
 * @param context
 * @return
 * @throws ScriptException
 */
@SuppressWarnings("unchecked")
public static <T> T eval(String expression, Map<? extends String, ? extends Object> context) throws ScriptException {
    try {
        JexlEngine jexlEngine = new JexlBuilder().create();
        JexlExpression expr = jexlEngine.createExpression(expression);
        JexlContext jc = new QAFJexlContext(context);
        return (T) expr.evaluate(jc);
    } catch (Exception e) {
        throw new ScriptException(e);
    }
}
Also used : ScriptException(javax.script.ScriptException) JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlBuilder(org.apache.commons.jexl3.JexlBuilder) JexlContext(org.apache.commons.jexl3.JexlContext) JexlExpression(org.apache.commons.jexl3.JexlExpression) ParseException(java.text.ParseException) ScriptException(javax.script.ScriptException)

Example 39 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project commons-jexl by apache.

the class SandboxTest method testGetNullKeyAllowed1.

@Test
public void testGetNullKeyAllowed1() throws Exception {
    JexlSandbox sandbox = new JexlSandbox(true, true);
    JexlSandbox.Permissions p = sandbox.permissions("java.util.Map", false, true, true);
    p.read().add("quux");
    JexlEngine jexl = new JexlBuilder().sandbox(sandbox).create();
    // cant read quux
    // quotes are important!
    String q = "'quux'";
    JexlExpression expression = jexl.createExpression("{" + q + " : 'foo'}[" + q + "]");
    try {
        Object o = expression.evaluate(null);
        Assert.fail("should have blocked " + q);
    } catch (JexlException.Property xp) {
        Assert.assertTrue(xp.getMessage().contains("undefined"));
    }
    // can read foo, null
    for (String k : Arrays.asList("'foo'", "null")) {
        expression = jexl.createExpression("{" + k + " : 'foo'}[" + k + "]");
        Object o = expression.evaluate(null);
        Assert.assertEquals("foo", o);
    }
}
Also used : JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlException(org.apache.commons.jexl3.JexlException) JexlBuilder(org.apache.commons.jexl3.JexlBuilder) JexlExpression(org.apache.commons.jexl3.JexlExpression) Test(org.junit.Test)

Example 40 with JexlExpression

use of org.apache.commons.jexl3.JexlExpression in project commons-jexl by apache.

the class SandboxTest method testSetNullKeyAllowed1.

@Test
public void testSetNullKeyAllowed1() throws Exception {
    Arithmetic350 a350 = new Arithmetic350(true);
    JexlSandbox sandbox = new JexlSandbox(true, true);
    JexlSandbox.Permissions p = sandbox.permissions("java.util.Map", true, false, true);
    p.write().add("quux");
    JexlEngine jexl = new JexlBuilder().arithmetic(a350).sandbox(sandbox).create();
    // can not write quux
    // quotes are important!
    String q = "'quux'";
    JexlExpression expression = jexl.createExpression("{" + q + " : 'foo'}[" + q + "] = '42'");
    try {
        Object o = expression.evaluate(null);
        Assert.fail("should have blocked " + q);
    } catch (JexlException.Property xp) {
        Assert.assertTrue(xp.getMessage().contains("undefined"));
    }
    // can write bar, null
    expression = jexl.createExpression("{'bar' : 'foo'}['bar'] = '42'");
    expression.evaluate(null);
    Map<?, ?> map = a350.getLastMap();
    Assert.assertEquals("42", map.get("bar"));
    map.clear();
    expression = jexl.createExpression("{null : 'foo'}[null] = '42'");
    expression.evaluate(null);
    map = a350.getLastMap();
    Assert.assertEquals("42", map.get(null));
}
Also used : JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlException(org.apache.commons.jexl3.JexlException) JexlBuilder(org.apache.commons.jexl3.JexlBuilder) JexlExpression(org.apache.commons.jexl3.JexlExpression) Test(org.junit.Test)

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