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, ParserContext context) throws ParseException;

Source Link

Document

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

Usage

From source file:org.kuali.rice.krad.uif.service.impl.ExpressionEvaluatorServiceImpl.java

/**
 * @see org.kuali.rice.krad.uif.service.ExpressionEvaluatorService#evaluateExpressionTemplate(java.lang.Object,
 *      java.util.Map, java.lang.String)
 *//*w  w  w  .j a v a2 s . co  m*/
public String evaluateExpressionTemplate(Object contextObject, Map<String, Object> evaluationParameters,
        String expressionTemplate) {
    StandardEvaluationContext context = new StandardEvaluationContext(contextObject);
    context.setVariables(evaluationParameters);
    addCustomFunctions(context);

    ExpressionParser parser = new SpelExpressionParser();

    String result = null;
    try {
        Expression expression = null;
        if (StringUtils.contains(expressionTemplate, UifConstants.EL_PLACEHOLDER_PREFIX)) {
            expression = parser.parseExpression(expressionTemplate, new TemplateParserContext(
                    UifConstants.EL_PLACEHOLDER_PREFIX, UifConstants.EL_PLACEHOLDER_SUFFIX));
        } else {
            expression = parser.parseExpression(expressionTemplate);
        }

        result = expression.getValue(context, String.class);
    } catch (Exception e) {
        LOG.error("Exception evaluating expression: " + expressionTemplate);
        throw new RuntimeException("Exception evaluating expression: " + expressionTemplate, e);
    }

    return result;
}

From source file:org.metaeffekt.dcc.commons.mapping.AbstractPropertyExpression.java

@SuppressWarnings("unchecked")
protected <T> T evaluateExpression(String originalExpression, String value, Class<T> type) {
    final ExpressionParser parser = new SpelExpressionParser();

    try {/*from w  w  w  .j  av a2 s.c  om*/
        final StandardEvaluationContext context = new StandardEvaluationContext();
        context.setRootObject(propertiesHolder);
        context.addPropertyAccessor(new EnvironmentAccessor());
        context.setVariable(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, STANDARD_ENVIRONMENT);
        context.setVariable(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, STANDARD_ENVIRONMENT);

        final Expression expression = parser.parseExpression(value, PARSER_CONTEXT);

        return expression.getValue(context, type);
    } catch (ParseException | EvaluationException e) {
        LOG.debug(String.format("Failed to evaluate expression %s, and with replaced properties %s",
                originalExpression, value), e);
    }

    if (type == String.class) {
        return (T) value;
    }

    return null;
}

From source file:org.openehealth.ipf.commons.ihe.core.payload.SpringExpressionResolver.java

public SpringExpressionResolver(final String filePathPattern) {
    Validate.notEmpty(filePathPattern, "log file path/name pattern");
    final ExpressionParser parser = new SpelExpressionParser();
    final TemplateParserContext parserContext = new TemplateParserContext("[", "]");
    parser.parseExpression(filePathPattern, parserContext);
    expression = parser.parseExpression(filePathPattern, parserContext);
}