Example usage for org.springframework.expression.spel.support StandardEvaluationContext StandardEvaluationContext

List of usage examples for org.springframework.expression.spel.support StandardEvaluationContext StandardEvaluationContext

Introduction

In this page you can find the example usage for org.springframework.expression.spel.support StandardEvaluationContext StandardEvaluationContext.

Prototype

public StandardEvaluationContext() 

Source Link

Document

Create a StandardEvaluationContext with a null root object.

Usage

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 ww  w  . jav  a 2  s  .  com*/
    //        ctx.setVariable("root", rootObject);
    return parser.parseExpression(expression).getValue(ctx);
}

From source file:bugs.TestEL.java

/**
 * Test the EL.// w w w  .  ja  v a 2 s.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: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:com.fitbur.core.el.spring.internal.StandardEvaluationContextProvider.java

@Rank(Integer.MIN_VALUE)
@PerLookup
@Override
public StandardEvaluationContext provide() {
    return new StandardEvaluationContext();
}

From source file:org.snaker.engine.spring.SpelExpression.java

public <T> T eval(Class<T> T, String expr, Map<String, Object> args) {
    EvaluationContext context = new StandardEvaluationContext();
    for (Entry<String, Object> entry : args.entrySet()) {
        context.setVariable(entry.getKey(), entry.getValue());
    }//w w w.  ja  v a  2 s  . co m
    return parser.parseExpression(expr).getValue(context, T);
}

From source file:fr.acxio.tools.agia.expression.StandardEvaluationContextFactory.java

@Override
public StandardEvaluationContext createContext(String sName, Object sValue,
        StandardEvaluationContext sContext) {
    StandardEvaluationContext aContext = sContext;
    if (sContext == null) {
        aContext = new StandardEvaluationContext();
        if (commonObjects != null) {
            aContext.setVariables(commonObjects);
        }//from w  ww.ja  v  a  2s .c o  m
        aContext.addPropertyAccessor(new MapAccessor());
    }
    aContext.setVariable(sName, sValue);
    return aContext;
}

From source file:org.jmingo.el.springexp.SpringELEngine.java

/**
 * {@inheritDoc}/*from  w w  w. j  ava2 s. c om*/
 */
@Override
public synchronized boolean evaluate(String expression, Map<String, Object> parameters) {
    Validate.notBlank(expression, "expression cannot be empty");
    Expression exp = parser.parseExpression(expression);
    EvaluationContext context = new StandardEvaluationContext();
    if (MapUtils.isNotEmpty(parameters)) {
        for (Map.Entry<String, Object> parameter : parameters.entrySet()) {
            context.setVariable(parameter.getKey(), parameter.getValue());
        }
    }
    return exp.getValue(context, Boolean.class);
}

From source file:org.springframework.batch.admin.web.JsonWrapper.java

@SuppressWarnings("unchecked")
public JsonWrapper(String content) throws Exception {
    this.content = content;
    try {/*from   w  w  w.jav  a 2s  .c o m*/
        target = new MappingJsonFactory().createJsonParser(content.replace("\\", "/")).readValueAs(Map.class);
    } catch (JsonParseException e) {
        throw new JsonMappingException("Cannot create wrapper for:\n" + content, e);
    }
    context = new StandardEvaluationContext();
    context.addPropertyAccessor(new MapAccessor());
    parser = new SpelExpressionParser();
}

From source file:com.couchbase.spring.core.mapping.BasicCouchbasePersistentEntity.java

public BasicCouchbasePersistentEntity(TypeInformation<T> typeInformation) {
    super(typeInformation);

    this.context = new StandardEvaluationContext();
}

From source file:de.hybris.platform.addonsupport.config.SpelBeanFactory.java

@Override
public void onApplicationEvent(final ContextRefreshedEvent event) {
    final ExpressionParser parser = new SpelExpressionParser();
    final StandardEvaluationContext context = new StandardEvaluationContext();
    context.setBeanResolver(new BeanFactoryResolver(applicationContext));

    final String[] beanNames = applicationContext.getBeanNamesForType(AbstractConverter.class);
    for (final String beanName : beanNames) {
        final BeanDefinition def = beanFactory.getBeanDefinition(beanName);
        final PropertyValue pv = def.getPropertyValues().getPropertyValue("targetClass");
        if (pv != null) {

            if (pv.getValue() instanceof TypedStringValue) {

                String expr = StringUtils.strip(((TypedStringValue) pv.getValue()).getValue());
                if (expr.startsWith("#{")) {
                    expr = StringUtils.replaceOnce(expr, "#{", "@");
                    expr = StringUtils.stripEnd(expr, "}");
                    final Object result = parser.parseExpression(expr).getValue(context);
                    if (result != null) {

                        try {
                            applicationContext.getBean(beanName, AbstractConverter.class)
                                    .setTargetClass(ClassUtils.getClass(result.toString()));
                        } catch (final ClassNotFoundException e) {
                            LOG.error(beanName + " target class instantiation failed", e);
                        }//w  w  w.j  av a 2s .c  o  m
                    }
                }
            }
        }
    }
}