Search in sources :

Example 96 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.

the class SpelReproTests method SPR9486_addFloatWithDouble.

@Test
public void SPR9486_addFloatWithDouble() {
    Number expectedNumber = 10.21f + 10.2;
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    Expression expression = parser.parseExpression("10.21f + 10.2");
    Number result = expression.getValue(context, null, Number.class);
    assertEquals(expectedNumber, result);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Example 97 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser in project opennms by OpenNMS.

the class VarbindMapping method evaluate.

/**
     * Evaluates a SPEL expression based on a given northbound alarm
     *
     * @param expression the expression
     * @param alarm the northbound alarm
     * @return the string
     */
private String evaluate(String expression, NorthboundAlarm alarm) {
    if (expression == null) {
        return null;
    }
    try {
        StandardEvaluationContext context = new StandardEvaluationContext(alarm);
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(expression);
        return (String) exp.getValue(context, String.class);
    } catch (Exception e) {
        LOG.warn("Can't evaluate expression {} for alarm {} because: {}", getValue(), alarm.getUei(), e.getMessage());
    }
    return null;
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser)

Example 98 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser in project opennms by OpenNMS.

the class SyslogEventForwarder method getTranslatedMessage.

/**
     * Gets the translated message.
     *
     * @param event the event
     * @param node the node
     * @param msgFormat the message format
     * @return the translated message
     */
private String getTranslatedMessage(Event event, OnmsNode node, String msgFormat) {
    StandardEvaluationContext context = new StandardEvaluationContext(event);
    if (node != null) {
        context.setVariable("node", node);
    }
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression(msgFormat, new TemplateParserContext("${", "}"));
    try {
        final String msg = (String) exp.getValue(context, String.class);
        LOG.debug("getTranslatedMessage: {} ==> {}", msgFormat, msg);
        return msg;
    } catch (Exception e) {
        LOG.warn("getTranslatedMessage: can't evaluate expression {} for alarm {} because: {}", msgFormat, event.getUei(), e.getMessage());
    }
    return null;
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) TemplateParserContext(org.springframework.expression.common.TemplateParserContext) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) IOException(java.io.IOException) SyslogRuntimeException(org.graylog2.syslog4j.SyslogRuntimeException)

Example 99 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser in project opennms by OpenNMS.

the class ResponseHandlingUtils method matchesFilter.

public static boolean matchesFilter(String spelFilter, ListMultimap<String, String> valuesByName) {
    // Compile the expression
    final ExpressionParser parser = new SpelExpressionParser();
    final Expression exp = parser.parseExpression(spelFilter);
    // Build the context with the first values for all of the attributes
    final StandardEvaluationContext context = new StandardEvaluationContext();
    for (String name : valuesByName.keySet()) {
        final List<String> values = valuesByName.get(name);
        if (values.size() > 0) {
            context.setVariable(name, values.get(0));
        }
    }
    // Evaluate our expression
    try {
        return exp.getValue(context, Boolean.class);
    } catch (Exception e) {
        LOG.error("Failed to evaluate expression {}. Assuming match is negative. Msg: {}", exp.getExpressionString(), e.getMessage());
        throw Throwables.propagate(e);
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) NoSuchElementException(java.util.NoSuchElementException)

Example 100 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser in project opennms by OpenNMS.

the class ResponseHandlingUtils method getMatchingIndex.

public static int getMatchingIndex(String spelExpression, ListMultimap<String, String> values) throws NoSuchElementException {
    // Compile the expression
    final ExpressionParser parser = new SpelExpressionParser();
    final Expression exp = parser.parseExpression(spelExpression);
    // Recursively check all levels, defaulting to the first element
    return getMatchingIndex(exp, values, 0);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser)

Aggregations

SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)210 Test (org.junit.Test)190 Expression (org.springframework.expression.Expression)172 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)151 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)101 ExpressionParser (org.springframework.expression.ExpressionParser)87 EvaluationContext (org.springframework.expression.EvaluationContext)32 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)16 List (java.util.List)14 EvaluationException (org.springframework.expression.EvaluationException)13 Map (java.util.Map)11 LinkedHashMap (java.util.LinkedHashMap)8 BigInteger (java.math.BigInteger)6 AccessException (org.springframework.expression.AccessException)6 CompositeStringExpression (org.springframework.expression.common.CompositeStringExpression)6 ParseException (org.springframework.expression.ParseException)5 BigDecimal (java.math.BigDecimal)4 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)4 TypedValue (org.springframework.expression.TypedValue)4