Ondra Žižka Ondra Žižka - 5 months ago 13
Java Question

Java: How to evaluate an EL expression - standalone (outside any web framework) without implementing interfaces?

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.

Answer

There's quite a bunch of EL engines, of which most implement Java Expression Language API.

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