Example usage for org.springframework.expression ExpressionParser parseExpression

List of usage examples for org.springframework.expression ExpressionParser parseExpression

Introduction

In this page you can find the example usage for org.springframework.expression ExpressionParser parseExpression.

Prototype

Expression parseExpression(String expressionString) throws ParseException;

Source Link

Document

Parse the expression string and return an Expression object you can use for repeated evaluation.

Usage

From source file:prospring3.spel.SpelMain.java

public static void main(String[] args) {
    int sum = 0;/* w  w w  .  j  a  v a  2s  . c  o  m*/
    for (int a = 0; a < 10; a++) {
        sum += a;
    }
    System.out.println("sum = " + sum);

    ExpressionParser parser = new SpelExpressionParser();
    Expression e2 = parser.parseExpression("'Hello World'.bytes.length");
    //        int v2 = e2.getValue(); // ALT+Enter and Idea suggest cast to int through Integer wrapper
    //        System.out.println("v2 = " + v2);

    Expression e = parser.parseExpression("'Hello World'");
    Object v = e.getValue();
    System.out.println("v = " + v);

    e = parser.parseExpression("'Hello World'.concat('!')");
    v = e.getValue();
    System.out.println("v = " + v);

}

From source file:org.wte4j.impl.format.FormatterRegistry.java

static Formatter createFormatter(Class<? extends Formatter> formatterClass, List<String> args) {
    try {//w  ww. j a va 2  s. co m
        String springExpression = createSpringExpression(formatterClass, args);
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(springExpression);
        return exp.getValue(Formatter.class);
    } catch (Exception e) {
        throw new FormatterInstantiationException(
                "Can not create new Instance of " + formatterClass.getName() + "with args " + args.toString(),
                e);
    }
}

From source file:app.commons.SpELUtils.java

public static Object parseExpression(String expression, Object rootObject) {
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    //        ctx.setBeanResolver();
    ctx.setRootObject(rootObject);//from   w w w .ja  v a 2  s. c o m
    //        ctx.setVariable("root", rootObject);
    return parser.parseExpression(expression).getValue(ctx);
}

From source file:de.itsvs.cwtrpc.security.DefaultRpcAuthenticationFailureHandler.java

public static Expression createExceptionExpression(Class<? extends Exception> exceptionClass,
        boolean includeMessage) {
    final StringBuilder expressionString;
    final ExpressionParser parser;
    final Expression expression;

    expressionString = new StringBuilder();
    expressionString.append("new ");
    expressionString.append(exceptionClass.getCanonicalName());
    expressionString.append('(');
    if (includeMessage) {
        expressionString.append("message");
    }/*from  ww w.j a  v a2  s. c o  m*/
    expressionString.append(')');

    parser = new SpelExpressionParser();
    expression = parser.parseExpression(expressionString.toString());

    return expression;
}

From source file:hu.petabyte.redflags.engine.util.MappingUtils.java

/**
 * Sets the value of a bean's property, using the given wrapper. The
 * property can be a deeper one, e.g. "a.b.c.d". If any of a, b or c
 * properties is null, the method will call an empty constructor for its
 * static type. If any error occurs, setDeepProperty will write it to the
 * LOG and returns false.//from   www. ja  v  a2  s .  com
 *
 * @param wrapper
 *            Wrapper of the root object.
 * @param property
 *            The property needs to be set.
 * @param value
 *            The new value of the property.
 * @return True if property setting was successful, false on error.
 */
public static synchronized boolean setDeepProperty(BeanWrapper wrapper, String property, Object value) {
    try {
        // this will help calling a constructor:
        ExpressionParser parser = new SpelExpressionParser();

        // go thru property path elements:
        int offset = 0;
        while ((offset = property.indexOf(".", offset + 1)) > -1) {
            String currentProperty = property.substring(0, offset);

            // if current property is null:
            if (null == wrapper.getPropertyValue(currentProperty)) {
                String className = wrapper.getPropertyType(currentProperty).getName();

                // build up a constructor call:
                Expression exp = parser.parseExpression(String.format("new %s()", className));

                // LIMITATIONS:
                // 1) uses static type
                // 2) needs defined empty constructor

                // run it:
                Object newObject = exp.getValue();

                // and set the property:
                wrapper.setPropertyValue(currentProperty, newObject);
            }
        }

        // finally, set the destination property:
        wrapper.setPropertyValue(property, value);
        return true;
    } catch (Exception ex) {
        LOG.error("Could not set property '{}'", property);
        LOG.debug("Exception: ", ex);
        return false;
    }
}

From source file:org.opentides.util.CrudUtil.java

/**
 * This method evaluates the given expression from the object.
 * This method now uses Spring Expression Language (SpEL).
 * //from  w w  w.  j a  va2  s . c o  m
 * @param obj
 * @param expression
 * @return
 */
public static Boolean evaluateExpression(Object obj, String expression) {
    if (StringUtil.isEmpty(expression))
        return false;
    try {
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(expression);
        return exp.getValue(obj, Boolean.class);
    } catch (Exception e) {
        _log.debug("Failed to evaluate expression [" + expression + "] for object [" + obj.getClass() + "].");
        _log.debug(e.getMessage());
        return false;
    }
}

From source file:com.tacitknowledge.flip.spring.SpElValueExpressionEvaluator.java

/** {@inheritDoc } */
public Object evaluate(Object context, String expression) {
    final ExpressionParser parser = new SpelExpressionParser();
    return parser.parseExpression(expression).getValue(context);
}

From source file:org.juiser.spring.jwt.ClaimsExpressionEvaluator.java

public ClaimsExpressionEvaluator(String spelExpression) {
    Assert.hasText(spelExpression, "spelExpression argument cannot be null or empty.");
    ExpressionParser parser = new SpelExpressionParser();
    this.expressionString = spelExpression;
    this.expression = parser.parseExpression(spelExpression);
}

From source file:com.googlecode.starflow.core.script.spel.SpelScriptEngine.java

private Expression parse(String script) {
    ExpressionParser expressionParser = getExpressionParser();
    return expressionParser.parseExpression(script);
}

From source file:cz.jirutka.validator.spring.SpELAssertValidator.java

public void initialize(SpELAssert constraint) {
    ExpressionParser parser = new SpelExpressionParser();

    expression = parser.parseExpression(constraint.value());
    if (hasText(constraint.applyIf())) {
        applyIfExpression = parser.parseExpression(constraint.applyIf());
    }// www  .j a  v  a2s. c om
    for (Class<?> clazz : constraint.helpers()) {
        functions = extractStaticMethods(clazz);
    }
}