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