TextContentParser { public TextBlock parse(String text) { TextBlock textBlock = new TextBlock(); final Matcher matcher = INTERPOLATION_PATTERN.matcher(text); int start = 0; while (matcher.find()) { String key = matcher.group(1).trim(); textBlock.add(new TextContentStatic(text.substring(start, matcher.start()))); List<TextContentFilter> applicableFilters = Lists.newArrayList(); final String[] parts = key.split("\\|"); key = parts[0]; for (int i = 1; i < parts.length; i++) { String filterKey = parts[i].trim(); TextContentFilter filter = this.filters.get(filterKey); if (filter == null) { throw new TemplateException("There is no TextContentFilter with the id: '" + filterKey + "'"); } applicableFilters.add(filter); } textBlock.add(new TextContentReplacement(key, applicableFilters)); start = matcher.end(); } textBlock.add(new TextContentStatic(text.substring(start, text.length()))); return textBlock; }  TextBlock parse(String text);  }
@Test public void testParse() throws Exception { final TextContentParser parser = createTestTextContentParser(); final TextBlock contents = parser.parse("Hi {{name}}\n\n\tThis is a great test\n\nFrom {{from}}"); Map<String, Object> model = Maps.newHashMap(); model.put("name", "Name"); model.put("from", "From"); assertCorrectRender(contents, model, "Hi Name\n\n\tThis is a great test\n\nFrom From"); model.remove("from"); assertCorrectRender(contents, model, "Hi Name\n\n\tThis is a great test\n\nFrom from"); model.remove("name"); assertCorrectRender(contents, model, "Hi name\n\n\tThis is a great test\n\nFrom from"); model.put("name", "Name"); model.put("from", "From"); assertCorrectRender(parser.parse("{{name}}{{from}}"), model, "NameFrom"); }
@Test public void testParseAmp() throws Exception { final TextContentParser parser = createTestTextContentParser(); TextBlock contents = parser.parse("{{name}}"); Map<String, Object> model = Maps.newHashMap(); model.put("name", "&Name"); assertCorrectRender(contents, model, "&Name"); final Component annotation = FilterEscapeXmlAttrs.class.getAnnotation(Component.class); contents = parser.parse("{{name | " + annotation.value() + "}}"); model.put("name", "&Name"); assertCorrectRender(contents, model, "&amp;Name"); }