Search in sources :

Example 1 with TemplateParserContext

use of org.springframework.expression.common.TemplateParserContext in project camel by apache.

the class SpelExpression method parseExpression.

private Expression parseExpression() {
    // Support template parsing with #{ } delimiters
    ParserContext parserContext = new TemplateParserContext();
    Expression expression = expressionParser.parseExpression(expressionString, parserContext);
    return expression;
}
Also used : Expression(org.springframework.expression.Expression) TemplateParserContext(org.springframework.expression.common.TemplateParserContext) ParserContext(org.springframework.expression.ParserContext) TemplateParserContext(org.springframework.expression.common.TemplateParserContext)

Example 2 with TemplateParserContext

use of org.springframework.expression.common.TemplateParserContext 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 3 with TemplateParserContext

use of org.springframework.expression.common.TemplateParserContext in project spring-framework by spring-projects.

the class TemplateExpressionParsingTests method testTemplateParserContext.

@Test
public void testTemplateParserContext() {
    TemplateParserContext tpc = new TemplateParserContext("abc", "def");
    assertEquals("abc", tpc.getExpressionPrefix());
    assertEquals("def", tpc.getExpressionSuffix());
    assertTrue(tpc.isTemplate());
    tpc = new TemplateParserContext();
    assertEquals("#{", tpc.getExpressionPrefix());
    assertEquals("}", tpc.getExpressionSuffix());
    assertTrue(tpc.isTemplate());
    ParserContext pc = ParserContext.TEMPLATE_EXPRESSION;
    assertEquals("#{", pc.getExpressionPrefix());
    assertEquals("}", pc.getExpressionSuffix());
    assertTrue(pc.isTemplate());
}
Also used : TemplateParserContext(org.springframework.expression.common.TemplateParserContext) ParserContext(org.springframework.expression.ParserContext) TemplateParserContext(org.springframework.expression.common.TemplateParserContext) Test(org.junit.Test)

Aggregations

TemplateParserContext (org.springframework.expression.common.TemplateParserContext)3 Expression (org.springframework.expression.Expression)2 ParserContext (org.springframework.expression.ParserContext)2 IOException (java.io.IOException)1 SyslogRuntimeException (org.graylog2.syslog4j.SyslogRuntimeException)1 Test (org.junit.Test)1 ExpressionParser (org.springframework.expression.ExpressionParser)1 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)1 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)1