I'd like to use EL in my application. But I can't find any howto. I usually end up needing some interface for which I don't have an implementation.
I have a map of objects, and I want a string expression like Hello, ${person.name}
to be evaluated to a string.
How can I achieve that, using any of Commons EL, javax.el, OGNL, or such? Must be a standalone library.
And I know Java: using EL outside J2EE, and have seen JSTL/JSP EL (Expression Language) in a non JSP (standalone) context. That is not what I'm looking for.
What I am looking for is an example of what dependency to add, and then how to initialize a parser which will have:
private static String evaluateEL( String expr, Map<String, String> properties );
and allow me to do:
String greet = evaluateEL("Hello ${person.name}",
new HashMap(){{
put("person", new Person("Ondra"));
}}
);
And I need it to use some rational value, e.g. ""
on null
instead of throwing NPE or so.
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.
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