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

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

Introduction

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

Prototype

public void addPropertyAccessor(PropertyAccessor accessor) 

Source Link

Usage

From source file:com.fitbur.core.el.spring.SpringExpressionService.java

@Override
public <T> T eval(String name, Reader template, Map<String, Object> context, Class<T> resultType) {
    Expression expression = compile(name, template);
    StandardEvaluationContext ctx = contextProvider.get();
    ctx.addPropertyAccessor(mapAccessor);
    ctx.setRootObject(context);// ww  w.j  a va  2s .c  om
    return expression.getValue(ctx, resultType);
}

From source file:cz.jirutka.spring.exhandler.interpolators.SpelMessageInterpolator.java

/**
 * Creates a new instance with {@link StandardEvaluationContext} including
 * {@link org.springframework.expression.spel.support.ReflectivePropertyAccessor ReflectivePropertyAccessor}
 * and {@link MapAccessor}.//from  w  w w . j a va 2  s.  c o  m
 */
public SpelMessageInterpolator() {
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.addPropertyAccessor(new MapAccessor());
    this.evalContext = ctx;
}

From source file:net.asfun.jangod.interpret.FloorBindings.java

public Object get(String key, int level) {
    checkKey(key);// w  w w.j  a  v  a2 s .  c o m
    Map<String, Object> bindings = getBindings(level);
    try {
        StandardEvaluationContext context = new StandardEvaluationContext();
        context.addPropertyAccessor(new ReflectivePropertyAccessor());
        context.addPropertyAccessor(new MapAccessor());
        context.setRootObject(bindings);
        return parser.parseExpression(key).getValue(context);
    } catch (Exception e) {
        return bindings.get(key);
    }
}

From source file:org.jasig.portlet.widget.service.SpringELProcessor.java

/**
 * @{inheritDoc}//from w w  w  .jav  a  2  s  .c om
 */
@Override
public String process(String value, PortletRequest request) {
    Map<String, Object> context = getContext(request);

    StandardEvaluationContext sec = new StandardEvaluationContext(context);
    sec.addPropertyAccessor(new MapAccessor());
    sec.addPropertyAccessor(new ReflectivePropertyAccessor());
    sec.addPropertyAccessor(new DefaultPropertyAccessor(PARSER_CONTEXT.getExpressionPrefix(),
            PARSER_CONTEXT.getExpressionSuffix()));
    sec.setBeanResolver(beanResolver);
    SpelExpressionParser parser = new SpelExpressionParser();

    String processed = parser.parseExpression(value, PARSER_CONTEXT).getValue(sec, String.class);

    return processed;
}

From source file:org.jasig.portlet.spring.SpringELProcessor.java

@Override
public String process(String value, PortletRequest request) {
    Map<String, Object> context = getContext(request);

    StandardEvaluationContext sec = new StandardEvaluationContext(context);
    sec.addPropertyAccessor(new MapAccessor());
    sec.addPropertyAccessor(new ReflectivePropertyAccessor());
    sec.addPropertyAccessor(new DefaultPropertyAccessor(PARSER_CONTEXT.getExpressionPrefix(),
            PARSER_CONTEXT.getExpressionSuffix()));
    if (beanResolver != null) {
        sec.setBeanResolver(beanResolver);
    }//from   ww  w. j av  a2s  . c o m
    SpelExpressionParser parser = new SpelExpressionParser();

    try {
        String processed = parser.parseExpression(value, PARSER_CONTEXT).getValue(sec, String.class);
        return processed;
    } catch (SpelEvaluationException e) {
        throw new EvaluationException("Failed to process string '" + value
                + "'. See nested error message and check your SpEL tokens in your string", e);
    }
}

From source file:net.asfun.jangod.base.Context.java

public Object getAttribute(String expression) {
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.addPropertyAccessor(new ReflectivePropertyAccessor());
    context.addPropertyAccessor(new BeanFactoryAccessor());
    context.addPropertyAccessor(new MapAccessor());
    Expression parseExpression = null;
    try {/*from ww w  .jav  a  2s.co m*/
        parseExpression = parser.parseExpression(expression);
    } catch (ParseException e) {
        // ignore parsing problem, might be jangod token
        return null;
    }
    try {
        return parseExpression.getValue(context, sessionBindings);
    } catch (EvaluationException e) {
        // ignore. try global application global bindings
    }
    try {
        return parseExpression.getValue(context, application.globalBindings);
    } catch (EvaluationException e) {
        return null;
    }
}

From source file:com.creactiviti.piper.core.task.SpelTaskEvaluator.java

private StandardEvaluationContext createEvaluationContext(Context aContext) {
    StandardEvaluationContext context = new StandardEvaluationContext(aContext);
    context.addPropertyAccessor(new MapPropertyAccessor());
    context.addMethodResolver(methodResolver());
    return context;
}

From source file:org.craftercms.core.util.template.impl.spel.SpELStringTemplateCompiler.java

@PostConstruct
public void init() {
    if (evalContext == null) {
        evalContext = new StandardEvaluationContext();
    }/*from w  w w  .  j a v a 2s . com*/

    if (evalContext instanceof StandardEvaluationContext) {
        StandardEvaluationContext standardEvalContext = (StandardEvaluationContext) evalContext;
        // PropertyAccessor used when the model is a BeanFactory.
        standardEvalContext.addPropertyAccessor(new BeanFactoryAccessor());
        if (beanFactory != null) {
            if (standardEvalContext.getBeanResolver() == null) {
                standardEvalContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
            }
            if (standardEvalContext.getTypeLocator() == null) {
                standardEvalContext.setTypeLocator(new StandardTypeLocator(beanFactory.getBeanClassLoader()));
            }
            if (standardEvalContext.getTypeConverter() == null) {
                ConversionService conversionService = beanFactory.getConversionService();
                if (conversionService != null) {
                    standardEvalContext.setTypeConverter(new StandardTypeConverter(conversionService));
                }
            }
        }
    }
}

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  w w.  j  a  v a2  s. c  o m*/
        aContext.addPropertyAccessor(new MapAccessor());
    }
    aContext.setVariable(sName, sValue);
    return aContext;
}

From source file:com.arondor.common.reflection.parser.spring.BeanPropertyParser.java

public ElementConfiguration parseProperty(Object value) {
    LOGGER.debug("value : " + value);
    if (value instanceof TypedStringValue) {
        TypedStringValue stringValue = (TypedStringValue) value;
        if (stringValue.getTargetTypeName() != null) {
            return getEnumObjectConfiguration(stringValue);
        } else {//  w  ww.  j  a  va  2  s. c  o m
            PrimitiveConfiguration primitiveConfiguration = objectConfigurationFactory
                    .createPrimitiveConfiguration();
            if (useSPEL(stringValue)) {
                ExpressionParser parser = new SpelExpressionParser();
                String expAsStringWithToken = stringValue.getValue().trim();
                String expAsString = expAsStringWithToken.substring(2, expAsStringWithToken.length() - 1)
                        .trim();
                LOGGER.trace("This property is a SPEL expression: " + expAsString);

                // String regex = "systemProperties\\['([^\\s]+)'\\]";

                Expression exp = parser.parseExpression(expAsString);
                StandardEvaluationContext sec = null;
                if (sec == null) {
                    sec = new StandardEvaluationContext();
                    sec.addPropertyAccessor(new EnvironmentAccessor());
                    sec.addPropertyAccessor(new BeanExpressionContextAccessor());
                    sec.addPropertyAccessor(new BeanFactoryAccessor());
                    sec.addPropertyAccessor(new MapAccessor());
                }
                primitiveConfiguration.setValue(String.valueOf(exp.getValue()));
            } else {
                LOGGER.trace("This property is NOT a SPEL expression: " + stringValue.getValue());
                primitiveConfiguration.setValue(stringValue.getValue());
            }
            return primitiveConfiguration;
        }
    } else if (value instanceof RuntimeBeanReference) {
        RuntimeBeanReference beanReference = (RuntimeBeanReference) value;
        ReferenceConfiguration referenceConfiguration = objectConfigurationFactory
                .createReferenceConfiguration();
        referenceConfiguration.setReferenceName(beanReference.getBeanName());
        return referenceConfiguration;
    } else if (value instanceof ManagedList<?>) {
        return parseValueList((ManagedList<?>) value);
    } else if (value instanceof ManagedMap<?, ?>) {
        return parseValueMap((ManagedMap<?, ?>) value);
    } else if (value instanceof BeanDefinitionHolder) {
        BeanDefinitionHolder beanDefinitionHolder = (BeanDefinitionHolder) value;
        return parseBeanDefinition(beanDefinitionHolder.getBeanDefinition());
    } else {
        throw new UnsupportedOperationException("The type of property value is not suppported : " + value
                + " (class : " + value.getClass().getName() + ")");
    }
}