public static void main(String[] args) throws MustacheException, IOException { Writer writer = new OutputStreamWriter(System.out); MustacheFactory mf = new DefaultMustacheFactory(); Mustache mustache = mf.compile(new StringReader("{{hello}}, {{world}}!"), "helloworld"); mustache.execute(writer, new HelloWorld()); writer.flush(); } }
public Mustache compile(String file) { Reader reader = mf.getReader(file); if (reader == null) { throw new MustacheNotFoundException(file); } return compile(reader, file); }
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(options); Writer yamlWriter = new StringWriter(); yamlWriter.write("---\n"); yaml.dump(frontmatterData, yamlWriter); yamlWriter.write("---\n"); templateData.put("frontmatter", yamlWriter.toString()); MustacheFactory mf = new DefaultMustacheFactory(); Mustache mustache = mf.compile("com/google/errorprone/resources/bugpatterns_external.mustache"); mustache.execute(w, templateData); } else { MustacheFactory mf = new DefaultMustacheFactory(); Mustache mustache = mf.compile("com/google/errorprone/resources/bugpatterns_internal.mustache"); mustache.execute(w, templateData);
public void testComplex() throws MustacheException, IOException { StringWriter json = new StringWriter(); MappingJsonFactory jf = new MappingJsonFactory(); final JsonGenerator jg = jf.createJsonGenerator(json); jg.writeStartObject(); final JsonCapturer captured = new JsonCapturer(jg); MustacheFactory c = new DefaultMustacheFactory(root) { @Override public MustacheVisitor createMustacheVisitor() { return new CapturingMustacheVisitor(this, captured); } }; Mustache m = c.compile("complex.html"); StringWriter sw = new StringWriter(); m.execute(sw, new ComplexObject()); jg.writeEndObject(); jg.flush(); assertEquals(getContents(root, "complex.txt"), sw.toString()); JsonNode jsonNode = jf.createJsonParser(json.toString()).readValueAsTree(); Object o = JsonInterpreterTest.toObject(jsonNode); sw = new StringWriter(); m = createMustacheFactory().compile("complex.html"); m.execute(sw, o); assertEquals(getContents(root, "complex.txt"), sw.toString()); }
static String createSingleFileCommentContent( final ChangedFile changedFile, final Violation violation, final String commentTemplate) { final MustacheFactory mf = new DefaultMustacheFactory(); Reader templateReader = null; final Optional<String> commentTemplateOpt = ofNullable(commentTemplate); if (commentTemplateOpt.isPresent() && !commentTemplateOpt.get().isEmpty()) { templateReader = new StringReader(commentTemplateOpt.get()); } else { templateReader = mf.getReader(DEFAULT_VIOLATION_TEMPLATE_MUSTACH); } final Mustache mustache = mf.compile(templateReader, "Violation Template"); final Writer writer = new StringWriter(); final Map<String, Object> context = new HashMap<>(); context.put("violation", new ViolationData(violation)); context.put("changedFile", changedFile); final List<Object> scopes = new ArrayList<>(); scopes.add(context); mustache.execute(writer, scopes); return writer.toString() + "\n\n*" + FINGERPRINT + "* *<" + identifier(violation) + ">*"; }
@Before public void setUp() { MustacheFactory factory = new DefaultMustacheFactory(); template = factory.compile(new StringReader(TEMPLATE), "template"); scopesTemplate = factory.compile(new StringReader(SCOPES_TEMPLATE), "template"); }
private Mustache compile(String template) { Reader reader = new StringReader(template); return factory.compile(reader, "template"); }
public static String toString(URL resource, Map<String, ?> scopes) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Writer writer = new OutputStreamWriter(outputStream); String content = Resources.toString(resource, Charsets.UTF_8); MustacheFactory factory = new DefaultMustacheFactory(); factory.compile(new StringReader(content), resource.toString()) .execute(writer, scopes); writer.close(); return outputStream.toString(); }
@Test public void testCollectPoints() throws Exception { MustacheFactory dmf = new DefaultMustacheFactory(); Mustache compile = dmf.compile(new StringReader("{{#page}}This is a {{test}}{{/page}}"), UUID.randomUUID().toString()); Node node = compile.invert("This is a good day"); assertNotNull(node); }
@Override public void render(View view, Locale locale, OutputStream output) throws IOException { try { final MustacheFactory mustacheFactory = useCache ? factories.get(view.getClass()) : createNewMustacheFactory(view.getClass()); final Mustache template = mustacheFactory.compile(view.getTemplateName()); final Charset charset = view.getCharset().orElse(StandardCharsets.UTF_8); try (OutputStreamWriter writer = new OutputStreamWriter(output, charset)) { template.execute(writer, view); } } catch (Throwable e) { throw new ViewRenderException("Mustache template error: " + view.getTemplateName(), e); } }
public void testClosingReader() { final AtomicBoolean closed = new AtomicBoolean(); StringReader reader = new StringReader("{{test") { @Override public void close() { closed.set(true); } }; MustacheFactory mf = new DefaultMustacheFactory(); try { mf.compile(reader, "test"); fail("Should have thrown an exception"); } catch (MustacheException me) { // The reader should be closed now assertEquals(true, closed.get()); } }
boolean sawCR = false; int startLine = currentLine.get(); MustacheVisitor mv = mf.createMustacheVisitor(); final String command = mf.translate(sb.toString()); if (command.length() == 0) { TemplateContext tc = new TemplateContext(sm, em, file, currentLine.get(), startOfLine);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(options); Writer yamlWriter = new StringWriter(); yamlWriter.write("---\n"); yaml.dump(frontmatterData, yamlWriter); yamlWriter.write("---\n"); templateData.put("frontmatter", yamlWriter.toString()); MustacheFactory mf = new DefaultMustacheFactory(); Mustache mustache = mf.compile("com/google/errorprone/resources/bugpattern.mustache"); mustache.execute(writer, templateData.build()); writer.write("\n----------\n\n");
@Before public void setUp() { MustacheFactory factory = new DefaultMustacheFactory(); Reader reader = new StringReader(TEMPLATE); mustache = factory.compile(reader, "template"); }
public void testInvalidDelimiters() { try { MustacheFactory mf = createMustacheFactory(); Mustache m = mf.compile(new StringReader("{{=toolong}}"), "testInvalidDelimiters"); fail("Not invalid"); } catch (MustacheException e) { // Success } }
@Test public void testNoNode() throws Exception { MustacheFactory dmf = new DefaultMustacheFactory(); Mustache compile = dmf.compile(new StringReader("Using cluster file [^\\n]+\nHello World"), UUID.randomUUID().toString()); Node node = compile.invert("Using cluster file `/etc/foundationdb/fdb.cluster'.\nHello World"); assertNotNull(node); } }
boolean sawCR = false; int startLine = currentLine.get(); MustacheVisitor mv = mf.createMustacheVisitor(); final String command = mf.translate(sb.toString()); if (command.length() == 0) { TemplateContext tc = new TemplateContext(sm, em, file, currentLine.get(), startOfLine);
@Test public void should_load_teamplates_with_absolute_references_using_filepath() throws Exception { File file = new File("compiler/src/test/resources/templates_filepath"); File root = new File(file, TEMPLATE_FILE).exists() ? file : new File("src/test/resources/templates_filepath"); MustacheFactory factory = new DefaultMustacheFactory(root); Mustache maven = factory.compile(TEMPLATE_FILE); StringWriter sw = new StringWriter(); maven.execute(sw, new Object() { List<String> messages = Arrays.asList("w00pw00p", "mustache rocks"); }).close(); assertEquals("w00pw00p mustache rocks ", sw.toString()); }
@Test public void testTree() { MustacheFactory mf = new DefaultMustacheFactory(); Mustache test = mf.compile(new StringReader("Hello {{firstName}} {{lastName}}\n" + "Your Education Qualification is\n" + "\n" + "{{#qualification}}\n" + " - {{ college}}\n" + " - {{ years }}\n" + "{{/qualification }}"), "test"); walk(test, 0); }
public void testMismatch() { try { MustacheFactory mf = createMustacheFactory(); Mustache m = mf.compile(new StringReader("{{#value}}"), "testMismatch"); fail("Not mismatched"); } catch (MustacheException e) { // Success try { MustacheFactory mf = createMustacheFactory(); Mustache m = mf.compile(new StringReader("{{#value}}{{/values}}"), "testMismatch"); fail("Not mismatched"); } catch (MustacheException e2) { // Success } } }