Example usage for org.springframework.expression.spel.standard SpelExpressionParser SpelExpressionParser

List of usage examples for org.springframework.expression.spel.standard SpelExpressionParser SpelExpressionParser

Introduction

In this page you can find the example usage for org.springframework.expression.spel.standard SpelExpressionParser SpelExpressionParser.

Prototype

public SpelExpressionParser() 

Source Link

Document

Create a parser with default settings.

Usage

From source file:prospring3.spel.SpelMain.java

public static void main(String[] args) {
    int sum = 0;//from  w  w w  .  j a v  a  2  s . co  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:gov.nih.nci.calims2.taglib.form.SingleSelectHelper.java

/**
 * Interprets the properties String and returns a map of property name to expressions.
 * /*  w  w  w  . ja v a  2s  .co  m*/
 * @param propertyString The string to interpret
 * @return The properties expression map
 */
static Map<String, Expression> getPropertyMap(String propertyString) {
    ExpressionParser expressionParser = new SpelExpressionParser();
    Map<String, Expression> map = new TreeMap<String, Expression>();
    if (propertyString != null) {
        for (String propertySpec : propertyString.split(",")) {
            String[] propertyElements = propertySpec.split(":");
            if (propertyElements == null || propertyElements.length != 2) {
                throw new IllegalArgumentException("Invalid property specification: " + propertySpec);
            }
            String name = StringUtils.stripToNull(propertyElements[0]);
            if (name == null) {
                throw new IllegalArgumentException("Invalid property name");
            }
            String value = StringUtils.stripToNull(propertyElements[1]);
            if (value == null) {
                throw new IllegalArgumentException("Invalid property value");
            }
            map.put(name, expressionParser.parseExpression(value));
        }

    }
    return map;
}

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);//w w  w  .j  av a  2 s.  co  m
    //        ctx.setVariable("root", rootObject);
    return parser.parseExpression(expression).getValue(ctx);
}

From source file:bugs.TestEL.java

/**
 * Test the EL.//from   ww  w.  ja  va2s  . c  o  m
 */
@SuppressWarnings({ "boxing", "deprecation" })
@Test
public void testName() {
    // Arrange
    final SpelExpressionParser parser = new SpelExpressionParser();
    final StandardEvaluationContext context = new StandardEvaluationContext();

    final String spelExpression1 = "testDate.date";
    final String spelExpression2 = "class";
    final String spelExpression3 = "class.name";
    final String spelExpression4 = "name";

    final Date now = new Date();
    final String name = "Testname";

    final TestObject testObject = new TestObject();
    testObject.setTestDate(now);
    testObject.setName(name);

    final SpelExpression exp1 = (SpelExpression) parser.parseExpression(spelExpression1);
    final SpelExpression exp2 = (SpelExpression) parser.parseExpression(spelExpression2);
    final SpelExpression exp3 = (SpelExpression) parser.parseExpression(spelExpression3);
    final SpelExpression exp4 = (SpelExpression) parser.parseExpression(spelExpression4);
    // Act

    final Object value1 = exp1.getValue(context, testObject);

    final Object value2 = exp2.getValue(context, testObject);

    final Object value3 = exp3.getValue(context, testObject);

    Object value4 = null;
    try {
        value4 = exp4.getValue(context, testObject);
    } catch (SpelEvaluationException e) {
        Assert.assertEquals(
                "EL1021E:(pos 0): A problem occurred whilst " + "attempting to access the property 'name': "
                        + "'Unable to access property 'name' through getter'",
                e.getMessage());
        // CSOFF: RegexpSinglelineJava
        System.err.println("Spring-Bug SPR-9017 happens!");
        // CSON: RegexpSinglelineJava
        value4 = name;
    }

    // Assert
    Assert.assertEquals(value1, now.getDate());
    Assert.assertEquals(value2, TestObject.class);
    Assert.assertEquals(value3, TestObject.class.getName());
    Assert.assertEquals(value4, name);

}

From source file:com.github.sebhoss.contract.verifier.SpELBasedContractContextFactory.java

@Override
public ContractContext createContext(final Object instance, final Object[] arguments,
        final String[] parameterNames) {
    final ExpressionParser parser = new SpelExpressionParser();
    final EvaluationContext context = new StandardEvaluationContext(instance);

    for (int index = 0; index < arguments.length; index++) {
        context.setVariable(parameterNames[index], arguments[index]);
    }/*www  . ja  v  a 2s .  c o  m*/

    return new SpELContractContext(parser, context);
}

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.cloudfoundry.identity.app.web.MapWrapper.java

public MapWrapper(Object target) throws Exception {
    this.target = target;
    context = new StandardEvaluationContext();
    context.addPropertyAccessor(new MapAccessor());
    parser = new SpelExpressionParser();
}

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");
    }//w ww  .j a v  a 2 s  .  com
    expressionString.append(')');

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

    return expression;
}

From source file:com.graphaware.common.policy.spel.SpelInclusionPolicy.java

protected SpelInclusionPolicy(String expression) {
    this.expression = expression;
    this.exp = new SpelExpressionParser().parseExpression(expression);
}

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);
}