Java Code Examples for com.github.mustachejava.MustacheFactory#compile()

The following examples show how to use com.github.mustachejava.MustacheFactory#compile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SmtpConfiguration.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Override
public String serializeAsXml() throws IOException {
    HashMap<String, Object> scopes = new HashMap<>();
    scopes.put("hasAuthorizedAddresses", authorizedAddresses.isPresent());
    authorizedAddresses.ifPresent(value -> scopes.put("authorizedAddresses", value));
    scopes.put("authRequired", authRequired);
    scopes.put("verifyIdentity", verifyIndentity);
    scopes.put("maxmessagesize", maxMessageSizeInKb);
    scopes.put("bracketEnforcement", bracketEnforcement);
    scopes.put("hooks", addittionalHooks);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Writer writer = new OutputStreamWriter(byteArrayOutputStream);
    MustacheFactory mf = new DefaultMustacheFactory();
    Mustache mustache = mf.compile(getPatternReader(), "example");
    mustache.execute(writer, scopes);
    writer.flush();
    return new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
}
 
Example 2
Source File: Mustache.java    From template-benchmark with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Setup
public void setup() {
    MustacheFactory mustacheFactory = new DefaultMustacheFactory() {

        @Override
        public void encode(String value, Writer writer) {
            // Disable HTML escaping
            try {
                writer.write(value);
            } catch (IOException e) {
                throw new MustacheException(e);
            }
        }
    };
    template = mustacheFactory.compile("templates/stocks.mustache.html");
}
 
Example 3
Source File: ModifiedDeathMessagesModule.java    From UHC with MIT License 6 votes vote down vote up
@Override
public void initialize() throws InvalidConfigurationException {
    if (!config.contains(FORMAT_KEY)) {
        config.set(FORMAT_KEY, "&c{{original}} at {{player.world}},{{player.blockCoords}}");
    }

    if (!config.contains(FORMAT_EXPLANATION_KEY)) {
        config.set(FORMAT_EXPLANATION_KEY, "<message> at <coords>");
    }

    final String format = config.getString(FORMAT_KEY);
    formatExplanation = config.getString(FORMAT_EXPLANATION_KEY);

    final MustacheFactory mf = new DefaultMustacheFactory();
    try {
        template = mf.compile(
                new StringReader(ChatColor.translateAlternateColorCodes('&', format)),
                "death-message"
        );
    } catch (Exception ex) {
        throw new InvalidConfigurationException("Error parsing death message template", ex);
    }

    super.initialize();
}
 
Example 4
Source File: InstanceTemplatingTest.java    From roboconf-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void testImportTemplate() throws Exception {

	Map<String, String> vars = new HashMap<>();
	vars.put("name1", "val1");
	vars.put("name2", "val2");
	vars.put("name3", "val3");
	Import impt = new Import( "/", "component1", vars );

	MustacheFactory mf = new DefaultMustacheFactory();
	File templateFile = TestUtils.findTestFile( "/importTemplate.mustache" );
	Mustache mustache = mf.compile( templateFile.getAbsolutePath());
	StringWriter writer = new StringWriter();
	mustache.execute(writer, new ImportBean(impt)).flush();

	String writtenString = writer.toString();
	for( Map.Entry<String,String> entry : vars.entrySet()) {
		Assert.assertTrue("Var was not displayed correctly", writtenString.contains( entry.getKey() + " : " + entry.getValue()));
	}
}
 
Example 5
Source File: MicraServlet.java    From micra with MIT License 5 votes vote down vote up
protected String Mustache (String template, Map scopes)
{
    StringWriter writer = new StringWriter();
    MustacheFactory mf = new DefaultMustacheFactory();
    Mustache mustache = mf.compile(new StringReader(template), template);
    mustache.execute(writer, scopes);
    writer.flush();

    return writer.toString();
}
 
Example 6
Source File: MustacheProvider.java    From web-budget with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructor
 *
 * @param template the template file name inside src/java/resources/mail
 */
public MustacheProvider(String template) {

    this.data = new HashMap<>();

    final MustacheFactory factory = new DefaultMustacheFactory();
    this.mustache = factory.compile("/mail/" + template);
}
 
Example 7
Source File: QuotaThresholdNotice.java    From james-project with Apache License 2.0 5 votes vote down vote up
private String renderTemplate(FileSystem fileSystem, String template) throws IOException {
    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
         Writer writer = new OutputStreamWriter(byteArrayOutputStream)) {

        MustacheFactory mf = new DefaultMustacheFactory();
        Mustache mustache = mf.compile(getPatternReader(fileSystem, template), "example");
        mustache.execute(writer, computeScopes());
        writer.flush();
        return new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
    }
}
 
Example 8
Source File: MustacheProvider.java    From web-budget with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructor
 *
 * @param template the template file name inside src/java/resources/mail
 */
public MustacheProvider(String template) {

    this.data = new HashMap<>();

    final MustacheFactory factory = new DefaultMustacheFactory();
    this.mustache = factory.compile("/mail/" + template);
}
 
Example 9
Source File: MustacheProvider.java    From library with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor
 *
 * @param template the template file name inside src/java/resources/mail
 */
public MustacheProvider(String template) {

    this.data = new HashMap<>();

    final MustacheFactory factory = new DefaultMustacheFactory();
    this.mustache = factory.compile("/mail/" + template);
}
 
Example 10
Source File: SnakeYAMLMojo.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
private void generateHelperClass(Map<String, Type> types, Set<Import> imports) throws IOException {
    List<String> pathElementsToGeneratedClass = new ArrayList<>();
    String outputPackage = outputClass.substring(0, outputClass.lastIndexOf('.'));
    for (String segment : outputPackage.split("\\.")) {
        pathElementsToGeneratedClass.add(segment);
    }
    Path outputDir = Paths.get(outputDirectory.getAbsolutePath(), pathElementsToGeneratedClass.toArray(new String[0]));
    Files.createDirectories(outputDir);

    String simpleClassName = outputClass.substring(outputClass.lastIndexOf('.') + 1);
    Path outputPath = outputDir.resolve(simpleClassName + ".java");

    Writer writer = new OutputStreamWriter(new FileOutputStream(outputPath.toFile()));
    MustacheFactory mf = new DefaultMustacheFactory();
    Mustache m = mf.compile("typeClassTemplate.mustache");
    CodeGenModel model = new CodeGenModel(outputPackage, simpleClassName, types.values(), imports, interfacePrefix,
            implementationPrefix);
    m.execute(writer, model);
    writer.close();
}
 
Example 11
Source File: ContentRenderer.java    From sputnik with Apache License 2.0 5 votes vote down vote up
public String render(Review review) throws IOException {
    MustacheFactory mf = new DefaultMustacheFactory();
    Mustache mustache = mf.compile(TEMPLATE_MUSTACHE);
    Writer sink = new StringWriter();
    mustache.execute(sink, review).flush();
    return sink.toString();
}
 
Example 12
Source File: CamelKProjectBuilder.java    From syndesis with Apache License 2.0 5 votes vote down vote up
public CamelKProjectBuilder(String name, String syndesisVersion) {
    super(name, syndesisVersion);

    MustacheFactory mustacheFactory = new DefaultMustacheFactory();
    try (InputStream stream = CamelKProjectBuilder.class.getResource("template/pom.xml.mustache").openStream()) {
        this.pomMustache = mustacheFactory.compile(new InputStreamReader(stream, StandardCharsets.UTF_8), name);
    } catch (IOException e) {
        throw new IllegalStateException("Failed to read Camel-K pom template file", e);
    }
}
 
Example 13
Source File: MustacheTransformer.java    From tutorials with MIT License 5 votes vote down vote up
public String html() throws IOException {
    MustacheFactory mf = new DefaultMustacheFactory();
    Mustache mustache = mf.compile(templateFile);
    try (Writer output = new StringWriter()) {
        mustache.execute(output, staxTransformer.getMap());
        output.flush();
        return output.toString();
    }
}
 
Example 14
Source File: KubernetesInstanceFactory.java    From kubernetes-elastic-agents with Apache License 2.0 5 votes vote down vote up
public static String getTemplatizedPodSpec(String podSpec) {
    StringWriter writer = new StringWriter();
    MustacheFactory mf = new DefaultMustacheFactory();
    Mustache mustache = mf.compile(new StringReader(podSpec), "templatePod");
    mustache.execute(writer, KubernetesInstanceFactory.getJinJavaContext());
    return writer.toString();
}
 
Example 15
Source File: MustacheContentResolver.java    From swagger-brake with Apache License 2.0 5 votes vote down vote up
/**
 * Resolves a mustache template into a String based on the parameter map.
 * @param mustacheFile the path of the mustache template.
 * @param paramMap the parameter map for the mustache template.
 * @return the resolved content.
 */
public String resolve(String mustacheFile, Map<String, ?> paramMap) {
    try {
        MustacheFactory mf = new DefaultMustacheFactory();
        Mustache m = mf.compile(mustacheFile);
        StringWriter sw = new StringWriter();
        m.execute(sw, paramMap).flush();
        return sw.toString();
    } catch (IOException e) {
        throw new RuntimeException("Error while resolving mustache content", e);
    }
}
 
Example 16
Source File: GenerateMustache.java    From heimdall with Apache License 2.0 5 votes vote down vote up
/**
* Generates a Mustache file.
* 
* @param template		The template for generation
* @param parameters	The Map<String, Object> with the parameters for creation
* @return				The content of the file created
*/
  public static String generateTemplate(String template, Map<String, Object> parameters) {
       
       StringWriter writer = new StringWriter();
       MustacheFactory mf = new DefaultMustacheFactory();
       Mustache mustache = mf.compile(new StringReader(template), "example");
       mustache.execute(writer, parameters);
       
       return writer.toString();
  }
 
Example 17
Source File: FortuneResource.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public FortuneResource() {
    MustacheFactory mf = new DefaultMustacheFactory();
    template = mf.compile("fortunes.mustache");
    fortuneComparator = Comparator.comparing(fortune -> fortune.getMessage());
}
 
Example 18
Source File: MustacheRunner.java    From obridge with MIT License 4 votes vote down vote up
public static String build(String templateName, Object backingObject) {
    MustacheFactory mf = new DefaultMustacheFactory();
    Mustache mustache = mf.compile(templateName);
    Writer execute = mustache.execute(new StringWriter(), backingObject);
    return execute.toString();
}
 
Example 19
Source File: InstanceTemplateHelper.java    From roboconf-platform with Apache License 2.0 3 votes vote down vote up
/**
 * Reads the import values of the instances and injects them into the template file.
 * <p>
 * See test resources to see the associated way to write templates
 * </p>
 *
 * @param instance the instance whose imports must be injected
 * @param templateFile the template file
 * @param writer a writer
 * @throws IOException if something went wrong
 */
public static void injectInstanceImports(Instance instance, File templateFile, Writer writer)
throws IOException {

	MustacheFactory mf = new DefaultMustacheFactory( templateFile.getParentFile());
	Mustache mustache = mf.compile( templateFile.getName());
	mustache.execute(writer, new InstanceBean( instance )).flush();
}