Java: How to evaluate an EL expression - standalone (outside any web framework) without implementing interfaces?
There's quite a bunch of EL engines, of which most implement Java Expression Language API.
Commons EL (http://jakarta.apache.org/commons/el/) Implementation of the JSP EL API that's existed forever. This library can be found in many JSP containers (Tomcat for example) or used as a foundation for within many vendor's J2EE servers.
OGNL (http://commons.apache.org/proper/commons-ognl/) One of the most expressive ELs available today and widely used with WebWork (Struts 2) and Tapestry.
MVEL (https://github.com/mvel/mvel) A newcomer to EL which is part of the MVFlex/Valhalla project. Features look more in line with OGNL's offering with method invocation and some interesting regular expression support.
(Unified) Expression Language (https://jcp.org/aboutJava/communityprocess/final/jsr341/index.html and http://jcp.org/en/jsr/detail?id=245) Standard expression language first introduced in Java EE 5 (EL 2.1) and enhanced in Java EE 6 (EL 2.2) and Java EE 7 (EL 3.0). Reference implementation available from Glassfish project - Unified Expression Language.
JEXL (http://jakarta.apache.org/commons/jexl/) An implementation based on Velocity's parser. Because of this, it acts more like a limited templating solution with things like method invocation.
Source
For now I ended up with this code using BeanUtils - ugly but works.
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
public static class SimpleEvaluator implements IExprLangEvaluator {
private static final org.slf4j.Logger log = LoggerFactory.getLogger( SimpleEvaluator.class );
@Override
public String evaluateEL( String template, Map<String, String> properties ) {
StringTokenizer st = new StringTokenizer( template );
String text = st.nextToken("${");
StringBuilder sb = new StringBuilder();
// Parse the template: "Hello ${person.name} ${person.surname}, ${person.age}!"
do{
try {
sb.append(text);
if( ! st.hasMoreTokens() )
break;
// "${foo.bar[a]"
String expr = st.nextToken("}");
// "foo.bar[a].baz"
expr = expr.substring(2);
// "foo"
String var = StringUtils.substringBefore( expr, ".");
Object subject = properties.get( var );
// "bar[a].baz"
String propPath = StringUtils.substringAfter( expr, ".");
sb.append( resolveProperty( subject, propPath ) );
text = st.nextToken("${");
text = text.substring(1);
} catch( NoSuchElementException ex ){
// Unclosed ${
log.warn("Unclosed ${ expression, missing } : " + template);
}
} while( true );
return sb.toString();
}
// BeanUtils
private String resolveProperty( Object subject, String propPath ) {
if( subject == null ) return "";
if( propPath == null || propPath.isEmpty() ) return subject.toString();
try {
return "" + PropertyUtils.getProperty( subject, propPath );
} catch( IllegalAccessException | InvocationTargetException | NoSuchMethodException ex ) {
log.warn("Failed resolving '" + propPath + "' on " + subject + ":\n " + ex.getMessage(), ex);
return "";
}
}
}// class SimpleEvaluator
I found one at http://juel.sourceforge.net/guide/start.html . Still not exactly 1-liner, but close.
ExpressionFactory factory = new de.odysseus.el.ExpressionFactoryImpl();
de.odysseus.el.util.SimpleContext context = new de.odysseus.el.util.SimpleContext();
context.setVariable("foo", factory.createValueExpression("bar", String.class));
ValueExpression e = factory.createValueExpression(context, "Hello ${foo}!", String.class);
System.out.println(e.getValue(context)); // --> Hello, bar!
Maven deps:
<!-- Expression language -->
<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>
<type>jar</type>
</dependency>