@Test public void testTypeInfos() { Engine engine = Engine.builder().addDefaultSectionHelpers() .build(); Template template = engine.parse("{@org.acme.Foo foo}" + "{@java.util.List<org.acme.Label> labels}" + "{foo.name}" + "{#for item in foo.items}" + "{item.name}{bar.name}" + "{/for}" + "{#each labels}" + "{it.name}" + "{/each}" + "{inject:bean.name}" + "{#each inject:bean.labels}" + "{it.value}" + "{/each}" + "{#set baz=foo.bar}" + "{baz.name}" + "{/set}" + "{#for foo in foos}" + "{foo.baz}" + "{/for}" + "{foo.call(labels,bar)}"); Set<Expression> expressions = template.getExpressions(); assertExpr(expressions, "foo.name", 2, "|org.acme.Foo|.name"); assertExpr(expressions, "foo.items", 2, "|org.acme.Foo|.items"); assertExpr(expressions, "item.name", 2, "|org.acme.Foo|.items<for-element>.name"); assertExpr(expressions, "bar.name", 2, null); assertExpr(expressions, "labels", 1, "|java.util.List<org.acme.Label>|"); assertExpr(expressions, "it.name", 2, "|java.util.List<org.acme.Label>|<for-element>.name"); assertExpr(expressions, "inject:bean.name", 2, "bean.name"); assertExpr(expressions, "inject:bean.labels", 2, "bean.labels"); assertExpr(expressions, "it.value", 2, "bean.labels<for-element>.value"); assertExpr(expressions, "foo.bar", 2, "|org.acme.Foo|.bar"); assertExpr(expressions, "baz.name", 2, "|org.acme.Foo|.bar.name"); assertExpr(expressions, "foo.baz", 2, null); assertExpr(expressions, "foo.call(labels,bar)", 2, "|org.acme.Foo|.call(labels,bar)"); }
Template parse(Reader reader, Optional<Variant> variant, String id, String generatedId) { long start = System.currentTimeMillis(); this.id = id; this.generatedId = generatedId; this.variant = variant; try { int val; while ((val = reader.read()) != -1) { processCharacter((char) val); lineCharacter++; } if (buffer.length() > 0) { if (state == State.TEXT || state == State.LINE_SEPARATOR) { flushText(); } else { throw parserError( "unexpected non-text buffer at the end of the template - probably an unterminated tag: " + buffer); } } SectionNode.Builder root = sectionStack.peek(); if (root == null) { throw parserError("no root section found"); } if (!root.helperName.equals(ROOT_HELPER_NAME)) { throw parserError("unterminated section [" + root.helperName + "] detected"); } SectionBlock.Builder part = sectionBlockStack.peek(); if (part == null) { throw parserError("no root section part found"); } root.addBlock(part.build()); TemplateImpl template = new TemplateImpl(engine, root.build(), generatedId, variant); Set<TemplateNode> nodesToRemove; if (engine.removeStandaloneLines) { nodesToRemove = new HashSet<>(); List<List<TemplateNode>> lines = readLines(template.root); for (List<TemplateNode> line : lines) { if (isStandalone(line)) { for (TemplateNode node : line) { if (node instanceof SectionNode) { continue; } nodesToRemove.add(node); } } } } else { nodesToRemove = Collections.emptySet(); } template.root.optimizeNodes(nodesToRemove); LOGGER.tracef("Parsing finished in %s ms", System.currentTimeMillis() - start); return template; } catch (IOException e) { throw new TemplateException(e); } }
Parser implements Function<String, Expression>, ParserHelper { Template parse(Reader reader, Optional<Variant> variant, String id, String generatedId) { long start = System.currentTimeMillis(); this.id = id; this.generatedId = generatedId; this.variant = variant; try { int val; while ((val = reader.read()) != -1) { processCharacter((char) val); lineCharacter++; } if (buffer.length() > 0) { if (state == State.TEXT || state == State.LINE_SEPARATOR) { flushText(); } else { throw parserError( "unexpected non-text buffer at the end of the template - probably an unterminated tag: " + buffer); } } SectionNode.Builder root = sectionStack.peek(); if (root == null) { throw parserError("no root section found"); } if (!root.helperName.equals(ROOT_HELPER_NAME)) { throw parserError("unterminated section [" + root.helperName + "] detected"); } SectionBlock.Builder part = sectionBlockStack.peek(); if (part == null) { throw parserError("no root section part found"); } root.addBlock(part.build()); TemplateImpl template = new TemplateImpl(engine, root.build(), generatedId, variant); Set<TemplateNode> nodesToRemove; if (engine.removeStandaloneLines) { nodesToRemove = new HashSet<>(); List<List<TemplateNode>> lines = readLines(template.root); for (List<TemplateNode> line : lines) { if (isStandalone(line)) { for (TemplateNode node : line) { if (node instanceof SectionNode) { continue; } nodesToRemove.add(node); } } } } else { nodesToRemove = Collections.emptySet(); } template.root.optimizeNodes(nodesToRemove); LOGGER.tracef("Parsing finished in %s ms", System.currentTimeMillis() - start); return template; } catch (IOException e) { throw new TemplateException(e); } } }
Parser implements Function<String, Expression>, ParserHelper { Template parse(Reader reader, Optional<Variant> variant, String id, String generatedId) { long start = System.currentTimeMillis(); this.id = id; this.generatedId = generatedId; this.variant = variant; try { int val; while ((val = reader.read()) != -1) { processCharacter((char) val); lineCharacter++; } if (buffer.length() > 0) { if (state == State.TEXT || state == State.LINE_SEPARATOR) { flushText(); } else { throw parserError( "unexpected non-text buffer at the end of the template - probably an unterminated tag: " + buffer); } } SectionNode.Builder root = sectionStack.peek(); if (root == null) { throw parserError("no root section found"); } if (!root.helperName.equals(ROOT_HELPER_NAME)) { throw parserError("unterminated section [" + root.helperName + "] detected"); } SectionBlock.Builder part = sectionBlockStack.peek(); if (part == null) { throw parserError("no root section part found"); } root.addBlock(part.build()); TemplateImpl template = new TemplateImpl(engine, root.build(), generatedId, variant); Set<TemplateNode> nodesToRemove; if (engine.removeStandaloneLines) { nodesToRemove = new HashSet<>(); List<List<TemplateNode>> lines = readLines(template.root); for (List<TemplateNode> line : lines) { if (isStandalone(line)) { for (TemplateNode node : line) { if (node instanceof SectionNode) { continue; } nodesToRemove.add(node); } } } } else { nodesToRemove = Collections.emptySet(); } template.root.optimizeNodes(nodesToRemove); LOGGER.tracef("Parsing finished in %s ms", System.currentTimeMillis() - start); return template; } catch (IOException e) { throw new TemplateException(e); } } Parser(EngineImpl engine); }
Parser implements Function<String, Expression>, ParserHelper { Template parse(Reader reader, Optional<Variant> variant, String id, String generatedId) { long start = System.currentTimeMillis(); this.id = id; this.generatedId = generatedId; this.variant = variant; try { int val; while ((val = reader.read()) != -1) { processCharacter((char) val); lineCharacter++; } if (buffer.length() > 0) { if (state == State.TEXT || state == State.LINE_SEPARATOR) { flushText(); } else { throw parserError( "unexpected non-text buffer at the end of the template - probably an unterminated tag: " + buffer); } } SectionNode.Builder root = sectionStack.peek(); if (root == null) { throw parserError("no root section found"); } if (!root.helperName.equals(ROOT_HELPER_NAME)) { throw parserError("unterminated section [" + root.helperName + "] detected"); } SectionBlock.Builder part = sectionBlockStack.peek(); if (part == null) { throw parserError("no root section part found"); } root.addBlock(part.build()); TemplateImpl template = new TemplateImpl(engine, root.build(), generatedId, variant); Set<TemplateNode> nodesToRemove; if (engine.removeStandaloneLines) { nodesToRemove = new HashSet<>(); List<List<TemplateNode>> lines = readLines(template.root); for (List<TemplateNode> line : lines) { if (isStandalone(line)) { for (TemplateNode node : line) { if (node instanceof SectionNode) { continue; } nodesToRemove.add(node); } } } } else { nodesToRemove = Collections.emptySet(); } template.root.optimizeNodes(nodesToRemove); LOGGER.tracef("Parsing finished in %s ms", System.currentTimeMillis() - start); return template; } catch (IOException e) { throw new TemplateException(e); } } Parser(EngineImpl engine); @Override ExpressionImpl apply(String value); @Override void addParameter(String name, String type); }
Parser implements Function<String, Expression>, ParserHelper { Template parse(Reader reader, Optional<Variant> variant, String id, String generatedId) { long start = System.currentTimeMillis(); this.id = id; this.generatedId = generatedId; this.variant = variant; try { int val; while ((val = reader.read()) != -1) { processCharacter((char) val); lineCharacter++; } if (buffer.length() > 0) { if (state == State.TEXT || state == State.LINE_SEPARATOR) { flushText(); } else { throw parserError( "unexpected non-text buffer at the end of the template - probably an unterminated tag: " + buffer); } } SectionNode.Builder root = sectionStack.peek(); if (root == null) { throw parserError("no root section found"); } if (!root.helperName.equals(ROOT_HELPER_NAME)) { throw parserError("unterminated section [" + root.helperName + "] detected"); } SectionBlock.Builder part = sectionBlockStack.peek(); if (part == null) { throw parserError("no root section part found"); } root.addBlock(part.build()); TemplateImpl template = new TemplateImpl(engine, root.build(), generatedId, variant); Set<TemplateNode> nodesToRemove; if (engine.removeStandaloneLines) { nodesToRemove = new HashSet<>(); List<List<TemplateNode>> lines = readLines(template.root); for (List<TemplateNode> line : lines) { if (isStandalone(line)) { for (TemplateNode node : line) { if (node instanceof SectionNode) { continue; } nodesToRemove.add(node); } } } } else { nodesToRemove = Collections.emptySet(); } template.root.optimizeNodes(nodesToRemove); LOGGER.tracef("Parsing finished in %s ms", System.currentTimeMillis() - start); return template; } catch (IOException e) { throw new TemplateException(e); } } Parser(EngineImpl engine); @Override ExpressionImpl apply(String value); @Override void addParameter(String name, String type);  }