Last active
June 23, 2016 11:28
-
-
Save dominikschlosser/58f6e414ddfb10306cf5aacb514b7f0c to your computer and use it in GitHub Desktop.
Java Expression Language Usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExpressionContext { | |
private Map<String, Object> context = new HashMap<>(); | |
public Object get(String key) { | |
return context.get(key); | |
} | |
public void add(String key, Object obj) { | |
context.put(key, obj); | |
} | |
public SimpleContext toELContext(ExpressionFactory factory) { | |
SimpleContext result = new SimpleContext(); | |
for (Map.Entry<String, Object> entry : context.entrySet()) { | |
result.setVariable(entry.getKey(), factory.createValueExpression(entry.getValue(), entry.getValue().getClass())); | |
} | |
return result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExpressionResolver { | |
public String resolve(String expression, ExpressionContext context) { | |
return resolve(expression, context, String.class); | |
} | |
@SuppressWarnings("unchecked") | |
public <T> T resolve(String expression, ExpressionContext context, Class<T> clazz) { | |
ExpressionFactory factory = new ExpressionFactoryImpl(); | |
SimpleContext elContext = context.toELContext(factory); | |
ValueExpression val = factory.createValueExpression(elContext, expression, clazz); | |
return (T) val.getValue(elContext); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExpressionResolverTest { | |
@Test | |
public void noExpressionInString() { | |
String input = "input1"; | |
ExpressionResolver resolver = new ExpressionResolver(); | |
ExpressionContext context = new ExpressionContext(); | |
context.add("input1", "bl"); | |
String resolved = resolver.resolve(input, context); | |
Assert.assertEquals("input1", resolved); | |
} | |
@Test | |
public void wholeStringIsExpression() { | |
String input = "${input1.concat('a')}"; | |
ExpressionResolver resolver = new ExpressionResolver(); | |
ExpressionContext context = new ExpressionContext(); | |
context.add("input1", "bl"); | |
String resolved = resolver.resolve(input, context); | |
Assert.assertEquals("bla", resolved); | |
} | |
@Test | |
public void expressionIsEmbedded() { | |
String input = "a${input1.concat('a')}c"; | |
ExpressionResolver resolver = new ExpressionResolver(); | |
ExpressionContext context = new ExpressionContext(); | |
context.add("input1", "bl"); | |
String resolved = resolver.resolve(input, context); | |
Assert.assertEquals("ablac", resolved); | |
} | |
@Test(expected = RuntimeException.class) | |
public void variableNotInContext() { | |
String input = "${input2.concat('a')}"; | |
ExpressionResolver resolver = new ExpressionResolver(); | |
ExpressionContext context = new ExpressionContext(); | |
context.add("input1", "bl"); | |
resolver.resolve(input, context); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>de.odysseus.juel</groupId> | |
<artifactId>juel-api</artifactId> | |
<version>2.2.7</version> | |
</dependency> | |
<dependency> | |
<groupId>de.odysseus.juel</groupId> | |
<artifactId>juel-impl</artifactId> | |
<version>2.2.7</version> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment