ExpressionParser { public static Expression[] parseExpressions(String template, EvaluationContext context, String expressionPrefix, String expressionSuffix) throws ParseException { List<Expression> expressions = new ArrayList<>(); int startIdx = 0; while (startIdx < template.length()) { int prefixIndex = template.indexOf(expressionPrefix, startIdx); if (prefixIndex >= startIdx) { if (prefixIndex > startIdx) { log.debug("Found a literal expression starting at " + startIdx); expressions.add(new LiteralExpression(template.substring(startIdx, prefixIndex))); } int afterPrefixIndex = prefixIndex + expressionPrefix.length(); int suffixIndex = skipToCorrectEndSuffix(expressionSuffix, template, afterPrefixIndex); if (suffixIndex == -1) { log.info("No ending suffix '" + expressionSuffix + "' for expression starting at character " + prefixIndex + ": " + template.substring(prefixIndex)); throw new ParseException(template, prefixIndex, "No ending suffix '" + expressionSuffix + "' for expression starting at character " + prefixIndex + ": " + template.substring(prefixIndex)); } if (suffixIndex == afterPrefixIndex) { log.info("No expression defined within delimiter '" + expressionPrefix + expressionSuffix + "' at character " + prefixIndex); throw new ParseException(template, prefixIndex, "No expression defined within delimiter '" + expressionPrefix + expressionSuffix + "' at character " + prefixIndex); } String expr = template.substring(prefixIndex + expressionPrefix.length(), suffixIndex); expr = expr.trim(); if (expr.isEmpty()) { log.info("No expression defined within delimiter '" + expressionPrefix + expressionSuffix + "' at character " + prefixIndex); throw new ParseException(template, prefixIndex, "No expression defined within delimiter '" + expressionPrefix + expressionSuffix + "' at character " + prefixIndex); } expressions.add(doParseExpression(expr, context)); startIdx = suffixIndex + expressionSuffix.length(); log.debug("Expression accumulated. Pursuing with index " + startIdx + " on " + template.length()); } else { expressions.add(new LiteralExpression(template.substring(startIdx, template.length()))); break; } } return expressions.toArray(new Expression[0]); }  static Expression[] parseExpressions(String template, EvaluationContext context,
                                               String expressionPrefix, String expressionSuffix);  }
@Test public void testParseExpressions() { String template = "Hello {{ request.body/name}} it's {{now() }}"; EvaluationContext context = new EvaluationContext(); context.registerFunction("now", NowELFunction.class); context.setVariable("request", new EvaluableRequest("{'name': 'Laurent'}", null)); Expression[] expressions = ExpressionParser.parseExpressions(template, context, "{{", "}}"); assertEquals(4, expressions.length); assertTrue(expressions[0] instanceof LiteralExpression); assertTrue(expressions[1] instanceof VariableReferenceExpression); assertTrue(expressions[2] instanceof LiteralExpression); assertTrue(expressions[3] instanceof FunctionExpression); assertEquals("Hello ", ((LiteralExpression)expressions[0]).getValue(context)); assertEquals(" it's ", ((LiteralExpression)expressions[2]).getValue(context)); }