Package org.appng.el
Class ExpressionEvaluator
- java.lang.Object
-
- org.appng.el.ExpressionEvaluator
-
public final class ExpressionEvaluator extends Object
Used for evaluating expressions that conform the Expression Language 2.2 syntax.
A valid expression must start with ${ and end with }. A list of the allowed operators an be found here. Example:Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("intValue", 5); parameters.put("stringValue", "foobar"); ExpressionEvaluator ee = new ExpressionEvaluator(parameters); org.junit.Assert.assertEquals("foobar", ee.evaluate("${stringValue}", String.class)); org.junit.Assert.assertEquals(Integer.valueOf(5), ee.evaluate("${intValue}", Integer.class)); org.junit.Assert.assertTrue(ee.evaluate("${intValue eq (3+2)}")); org.junit.Assert.assertTrue(ee.evaluate("${(1 eq (2/2)) && (2<3)}")); org.junit.Assert.assertTrue(ee.evaluate("${stringValue.length() == 6}"));
- Author:
- Matthias Müller
-
-
Constructor Summary
Constructors Constructor Description ExpressionEvaluator(Map<String,?> variables)
Creates a newExpressionEvaluator
using the given variables.ExpressionEvaluator(javax.el.VariableMapper variableMapper)
Creates a newExpressionEvaluator
using the givenVariableMapper
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addFunction(String name, Method method)
Adds a function to thisExpressionEvaluator
.void
addFunction(String prefix, String name, Method method)
Adds a prefixed function to thisExpressionEvaluator
.boolean
evaluate(String expression)
Evaluates the given expression to aboolean
.<T> T
evaluate(String expression, Class<T> targetType)
Evaluates the given expression to an object of the given targetTypeBoolean
getBoolean(String expression)
Evaluates the given expression to aBoolean
, if possible.String
getString(String expression)
Evaluates the given expression to aString
, if possible.boolean
isExpression(String value)
Checks whether the givenString
is a valid expression (starts with ${ and ends with }).void
setVariable(String name, Object value)
Sets the variable with the given name to the given value.void
setVariables(Map<String,?> variables)
CallssetVariable(String, Object)
for each entry of the given map.
-
-
-
Constructor Detail
-
ExpressionEvaluator
public ExpressionEvaluator(Map<String,?> variables)
Creates a newExpressionEvaluator
using the given variables.- Parameters:
variables
- aMap
of variables to use
-
ExpressionEvaluator
public ExpressionEvaluator(javax.el.VariableMapper variableMapper)
Creates a newExpressionEvaluator
using the givenVariableMapper
.- Parameters:
variableMapper
- theVariableMapper
to use
-
-
Method Detail
-
evaluate
public final <T> T evaluate(String expression, Class<T> targetType)
Evaluates the given expression to an object of the given targetType- Parameters:
expression
- the expression to parsetargetType
- the expected target type- Returns:
- an object of the given type
- Throws:
NullPointerException
- if targetType isnull
javax.el.ELException
- if the expression has syntactical errors
-
evaluate
public final boolean evaluate(String expression)
Evaluates the given expression to aboolean
.- Parameters:
expression
- the expression to evaluate- Returns:
- evaluation result
-
addFunction
public final void addFunction(String prefix, String name, Method method)
Adds a prefixed function to thisExpressionEvaluator
. The givenMethod
must bestatic
.
Example:ExpressionEvaluator ee = ...; ee.addFunction("math","max", Math.class.getMethod("max", int.class, int.class)); org.junit.Assert.assertEquals(Integer.valueOf(47), ee.evaluate("${math:max(47,11)}", Integer.class));
- Parameters:
prefix
- the prefix for the methodname
- the name of the methodmethod
- theMethod
to add as a function
-
addFunction
public final void addFunction(String name, Method method)
Adds a function to thisExpressionEvaluator
. The givenMethod
must bestatic
.
static
.
Example:ExpressionEvaluator ee = ...; ee.addFunction("max", Math.class.getMethod("max", int.class, int.class)); org.junit.Assert.assertEquals(Integer.valueOf(47), ee.evaluate("${max(47,11)}", Integer.class));
- Parameters:
name
- the name of the methodmethod
- theMethod
to add as a function
-
setVariable
public final void setVariable(String name, Object value)
Sets the variable with the given name to the given value.- Parameters:
name
- the name of the variablevalue
- the value of the variable (must not benull
)
-
setVariables
public final void setVariables(Map<String,?> variables)
CallssetVariable(String, Object)
for each entry of the given map.- Parameters:
variables
- aMap
of variables
-
isExpression
public boolean isExpression(String value)
Checks whether the givenString
is a valid expression (starts with ${ and ends with }).- Parameters:
value
- the String to check- Returns:
true
if the given value is a valid expression,false
otherwise
-
getString
public String getString(String expression)
Evaluates the given expression to aString
, if possible.- Parameters:
expression
- the expression to evaluate- Returns:
- the
String
evaluated from the expression, in cases it's a valid expression - the given expression itself, otherwise
- the
- See Also:
isExpression(String)
-
getBoolean
public Boolean getBoolean(String expression)
Evaluates the given expression to aBoolean
, if possible.- Parameters:
expression
- the expression to evaluate- Returns:
- the
Boolean
evaluated from the expression, in cases it's a valid expression Boolean.valueOf(expression)
if the expression is notnull
but is not a valid expressionnull
if the expression isnull
- the
- See Also:
isExpression(String)
-
-