All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jxls.expression.JexlExpressionEvaluator Maven / Gradle / Ivy

package org.jxls.expression;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.jexl3.JexlBuilder;
import org.apache.commons.jexl3.JexlContext;
import org.apache.commons.jexl3.JexlEngine;
import org.apache.commons.jexl3.JexlExpression;
import org.apache.commons.jexl3.MapContext;

/**
 * JEXL based implementation of {@link ExpressionEvaluator} interface
 * @author Leonid Vysochyn
 */
public class JexlExpressionEvaluator implements ExpressionEvaluator {
    private final boolean silent;
    private final boolean strict;
    private JexlExpression jexlExpression;
    private JexlContext jexlContext;
    private static ThreadLocal> jexlThreadLocal = new ThreadLocal>() {
        @Override
        protected Map initialValue() {
            return new HashMap();
        }
    };
    private static ThreadLocal> expressionMapThreadLocal = new ThreadLocal>() {
        @Override
        protected Map initialValue() {
            return new HashMap<>();
        }
    };

    public JexlExpressionEvaluator() {
        this(true, false);
    }

    public JexlExpressionEvaluator(final boolean silent, final boolean strict) {
        this.silent = silent;
        this.strict = strict;
    }

    public JexlExpressionEvaluator(String expression) {
        this();
        jexlExpression = getJexlEngine().createExpression(expression);
    }

    public JexlExpressionEvaluator(Map context) {
        this();
        jexlContext = new MapContext(context);
    }

    public JexlExpressionEvaluator(JexlContext jexlContext) {
        this();
        this.jexlContext = jexlContext;
    }

    @Override
    public Object evaluate(String expression, Map context) {
        JexlContext jexlContext = new MapContext(context);
        try {
            Map expressionMap = expressionMapThreadLocal.get();
            JexlExpression jexlExpression = expressionMap.get(expression);
            if (jexlExpression == null) {
                jexlExpression = getJexlEngine().createExpression(expression);
                expressionMap.put(expression, jexlExpression);
            }
            return jexlExpression.evaluate(jexlContext);
        } catch (Exception e) {
            throw new EvaluationException("An error occurred when evaluating expression " + expression, e);
        }
    }

    @Override
    public Object evaluate(Map context) {
        jexlContext = new MapContext(context);
        try {
            return jexlExpression.evaluate(jexlContext);
        } catch (Exception e) {
            throw new EvaluationException("An error occurred when evaluating expression " + jexlExpression.getSourceText(), e);
        }
    }

    public JexlExpression getJexlExpression() {
        return jexlExpression;
    }

    public void setJexlEngine(JexlEngine jexlEngine) {
        String key = key();
        Map map = jexlThreadLocal.get();
        map.put(key, jexlEngine);
    }

    public JexlEngine getJexlEngine() {
        String key = key();
        Map map = jexlThreadLocal.get();
        JexlEngine ret = map.get(key);
        if (ret == null) {
            ret = new JexlBuilder().silent(silent).strict(strict).create();
            map.put(key, ret);
        }
        return ret;
    }

    private String key() {
        return silent + "-" + strict;
    }

    @Override
    public String getExpression() {
        return jexlExpression == null ? null : jexlExpression.getSourceText();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy