Package org.apache.commons.jexl

Examples of org.apache.commons.jexl.Expression


     * @param expressionText The JEXL expression text
     * @return A compiled JEXL expression representing the expression text
     * @throws Exception Thrown if there was an error compiling the expression text
     */
    protected Expression getJexlExpression(String expressionText) throws Exception {
        Expression retVal = jexlExpressionCache.get(expressionText);
        if (retVal == null) {
            //Don't need synchronization here - if we end up calling createExpression in 2 separate threads, that's fine
            jexlExpressionCache.put(expressionText, retVal = ExpressionFactory.createExpression(expressionText));
        }
        return retVal;
View Full Code Here


public class JexlTest extends TestCase {

    public void test_NEG1() throws Exception {
        JexlContext jc = JexlHelper.createContext();
        jc.getVars().put("A", Boolean.TRUE);
        Expression e = ExpressionFactory.createExpression("!A");
        Object actual = e.evaluate(jc);
        assertEquals(Boolean.FALSE, actual);
    }
View Full Code Here

    }

    public void test_NEG2() throws Exception {
        JexlContext jc = JexlHelper.createContext();
        jc.getVars().put("A", Boolean.TRUE);
        Expression e = ExpressionFactory.createExpression("!(A)");
        Object actual = e.evaluate(jc);
        assertEquals(Boolean.FALSE, actual);
    }
View Full Code Here

        jc.getVars().put("A", Boolean.TRUE);
        jc.getVars().put("B", Boolean.FALSE);
        jc.getVars().put("C", Boolean.FALSE);

        Expression e = ExpressionFactory.createExpression("(A || B) && !C");
        Object actual = e.evaluate(jc);
        assertEquals(Boolean.TRUE, actual);
    }
View Full Code Here

    @Override
    public Object executeExpr(final String expression) throws Exception {

        // 从缓存中获取解析的表达式
        Expression expr = cache.get(expression);

        if (expr == null) {
            //
            StringBuilder builder = new StringBuilder(expression.length() * 2);

            // 将[name]替换为['name']
            Matcher mapMatcher = MAP_PATTERN.matcher(expression);
            int index = 0;
            while (mapMatcher.find()) {
                builder.append(expression.substring(index, mapMatcher.start()));
                String t = mapMatcher.group(1);
                if (!NumberUtils.isDigits(t)) {
                    builder.append("['");
                    builder.append(mapMatcher.group(1));
                    builder.append("']");
                } else {
                    builder.append(mapMatcher.group(0));
                }
                index = mapMatcher.end();
            }

            String expression2;
            if (builder.length() == 0) {
                expression2 = expression;
            } else {
                builder.append(expression.substring(index));
                expression2 = builder.toString();
                builder.setLength(0);
            }

            index = 0;

            // 匹配正则表达式, 并替换内容
            Matcher matcher = PREFIX_PATTERN.matcher(expression2);
            while (matcher.find()) {

                builder.append(expression2.substring(index, matcher.start()));

                String prefix = matcher.group(1);
                String name = matcher.group(2);
                if (":".equals(prefix)) {
                    boolean isDigits = NumberUtils.isDigits(name);
                    if (isDigits) {
                        // 按顺序访问变量
                        name = ':' + name;
                    }

                    if (!mapVars.containsKey(name)) {
                        throw new IllegalArgumentException("Variable \'" + name
                                + "\' not defined in DAO method");
                    }

                    // 按名称访问变量
                    builder.append(VAR_PREFIX);
                    builder.append("['");
                    builder.append(name);
                    builder.append("']");

                } else if ("$".equals(prefix)) {

                    if (!mapConsts.containsKey(name)) {
                        throw new IllegalArgumentException("Constant \'" + name
                                + "\' not defined in DAO class");
                    }

                    // 拼出常量访问语句
                    builder.append(CONST_PREFIX);
                    builder.append("[\'");
                    builder.append(name);
                    builder.append("\']");
                }

                index = matcher.end(2);
            }

            builder.append(expression2.substring(index));

            // 编译表达式
            expr = ExpressionFactory.createExpression(builder.toString());
            cache.putIfAbsent(expression2, expr);
        }

        // 进行表达式求值
        return expr.evaluate(context);
    }
View Full Code Here

    @Override
    protected String resolvePlaceholder(String placeholder, Properties props) {
        String sPropertyValue = super.resolvePlaceholder(placeholder, props);
        if (sPropertyValue == null) {
            try {
                Expression expression = ExpressionFactory.createExpression(placeholder);
                JexlContext jexlContext = JexlHelper.createContext();
                jexlContext.setVars(props);
                sPropertyValue = expression.evaluate(jexlContext).toString();
            } catch (Throwable t) {
            }
        }
        return sPropertyValue;
    }
View Full Code Here

        return sPropertyValue;
    }

    private String parseSystemProperty(String placeholder) {
        try {
            Expression expression = ExpressionFactory.createExpression(placeholder);
            JexlContext jexlContext = JexlHelper.createContext();
            jexlContext.setVars(System.getProperties());
            return expression.evaluate(jexlContext).toString();
        } catch (Throwable t) {
            return null;
        }
    }
View Full Code Here

        }
    }

    private String parseEnvProperty(String placeholder) {
        try {
            Expression expression = ExpressionFactory.createExpression(placeholder);
            JexlContext jexlContext = JexlHelper.createContext();
            jexlContext.setVars(System.getenv());
            return expression.evaluate(jexlContext).toString();
        } catch (Throwable t) {
            return null;
        }
    }
View Full Code Here

public class JexlTest extends TestCase {

    public void test_NEG1() throws Exception {
        JexlContext jc = JexlHelper.createContext();
        jc.getVars().put("A", Boolean.TRUE);
        Expression e = ExpressionFactory.createExpression("!A");
        Object actual = e.evaluate(jc);
        assertEquals(Boolean.FALSE, actual);
    }
View Full Code Here

    }

    public void test_NEG2() throws Exception {
        JexlContext jc = JexlHelper.createContext();
        jc.getVars().put("A", Boolean.TRUE);
        Expression e = ExpressionFactory.createExpression("!(A)");
        Object actual = e.evaluate(jc);
        assertEquals(Boolean.FALSE, actual);
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.jexl.Expression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.