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

2

15

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.

Halle answered 10/6, 2013 at 14:58 Comment(3)
While not exactly EL, FreeMarker has a similar syntax and feature set.Allhallows
Also Velocity has a similar syntax as EL.Gourmand
OGNL might be it, but their website doesn't work ATM.Ambidexterity
H
23

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
Halle answered 11/6, 2013 at 18:43 Comment(0)
H
5

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>
Halle answered 10/6, 2013 at 15:34 Comment(3)
Use <artifactId>juel-impl</artifactId>, otherwise you'll have two conflicting EL-APIs.Towandatoward
Based on the comment I have changed artifactId from juel to juel-impl, but without testing it.Ambidexterity
I have updated the versions. Looks like the JUEL impl has changed groupId to de.odysseus.juel.Ambidexterity

© 2022 - 2024 — McMap. All rights reserved.