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);
}
}
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;
}
}
}
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);
}
}
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);
}
}
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));
}
Aggregations