Search in sources :

Example 6 with Mustache

use of com.github.mustachejava.Mustache in project dropwizard by dropwizard.

the class MustacheViewRenderer method render.

@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);
    }
}
Also used : ViewRenderException(io.dropwizard.views.ViewRenderException) Mustache(com.github.mustachejava.Mustache) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) MustacheFactory(com.github.mustachejava.MustacheFactory)

Example 7 with Mustache

use of com.github.mustachejava.Mustache in project elasticsearch by elastic.

the class CustomMustacheFactoryTests method testUrlEncoder.

public void testUrlEncoder() {
    final ScriptEngineService engine = new MustacheScriptEngineService();
    final Map<String, String> params = singletonMap(Script.CONTENT_TYPE_OPTION, X_WWW_FORM_URLENCODED_MIME_TYPE);
    Mustache script = (Mustache) engine.compile(null, "{\"field\": \"{{value}}\"}", params);
    CompiledScript compiled = new CompiledScript(INLINE, null, MustacheScriptEngineService.NAME, script);
    ExecutableScript executable = engine.executable(compiled, singletonMap("value", "tilde~ AND date:[2016 FROM*]"));
    BytesReference result = (BytesReference) executable.run();
    assertThat(result.utf8ToString(), equalTo("{\"field\": \"tilde%7E+AND+date%3A%5B2016+FROM*%5D\"}"));
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) BytesReference(org.elasticsearch.common.bytes.BytesReference) ExecutableScript(org.elasticsearch.script.ExecutableScript) Mustache(com.github.mustachejava.Mustache) ScriptEngineService(org.elasticsearch.script.ScriptEngineService)

Example 8 with Mustache

use of com.github.mustachejava.Mustache in project elasticsearch by elastic.

the class CustomMustacheFactoryTests method testJsonEscapeEncoder.

public void testJsonEscapeEncoder() {
    final ScriptEngineService engine = new MustacheScriptEngineService();
    final Map<String, String> params = randomBoolean() ? singletonMap(Script.CONTENT_TYPE_OPTION, JSON_MIME_TYPE) : emptyMap();
    Mustache script = (Mustache) engine.compile(null, "{\"field\": \"{{value}}\"}", params);
    CompiledScript compiled = new CompiledScript(INLINE, null, MustacheScriptEngineService.NAME, script);
    ExecutableScript executable = engine.executable(compiled, singletonMap("value", "a \"value\""));
    BytesReference result = (BytesReference) executable.run();
    assertThat(result.utf8ToString(), equalTo("{\"field\": \"a \\\"value\\\"\"}"));
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) BytesReference(org.elasticsearch.common.bytes.BytesReference) ExecutableScript(org.elasticsearch.script.ExecutableScript) Mustache(com.github.mustachejava.Mustache) ScriptEngineService(org.elasticsearch.script.ScriptEngineService)

Example 9 with Mustache

use of com.github.mustachejava.Mustache in project error-prone by google.

the class BugPatternIndexWriter method dump.

void dump(Collection<BugPatternInstance> patterns, Writer w, Target target, Set<String> enabledChecks) throws IOException {
    // (Default, Severity) -> [Pattern...]
    SortedSetMultimap<IndexEntry, MiniDescription> sorted = TreeMultimap.create(Comparator.comparing(IndexEntry::onByDefault).reversed().thenComparing(IndexEntry::severity), Comparator.comparing(MiniDescription::name));
    for (BugPatternInstance pattern : patterns) {
        sorted.put(IndexEntry.create(enabledChecks.contains(pattern.name), pattern.severity), MiniDescription.create(pattern));
    }
    Map<String, Object> templateData = new HashMap<>();
    List<Map<String, Object>> bugpatternData = Multimaps.asMap(sorted).entrySet().stream().map(e -> ImmutableMap.of("category", e.getKey().asCategoryHeader(), "checks", e.getValue())).collect(Collectors.toCollection(ArrayList::new));
    templateData.put("bugpatterns", bugpatternData);
    if (target == Target.EXTERNAL) {
        Map<String, String> frontmatterData = ImmutableMap.<String, String>builder().put("title", "Bug Patterns").put("layout", "bugpatterns").build();
        DumperOptions options = new DumperOptions();
        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);
    }
}
Also used : DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) SortedSetMultimap(com.google.common.collect.SortedSetMultimap) ImmutableMap(com.google.common.collect.ImmutableMap) StringWriter(java.io.StringWriter) Collection(java.util.Collection) Mustache(com.github.mustachejava.Mustache) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Multimaps(com.google.common.collect.Multimaps) ArrayList(java.util.ArrayList) Yaml(org.yaml.snakeyaml.Yaml) DumperOptions(org.yaml.snakeyaml.DumperOptions) List(java.util.List) TreeMultimap(com.google.common.collect.TreeMultimap) Map(java.util.Map) MustacheFactory(com.github.mustachejava.MustacheFactory) AutoValue(com.google.auto.value.AutoValue) Writer(java.io.Writer) Comparator(java.util.Comparator) SeverityLevel(com.google.errorprone.BugPattern.SeverityLevel) Target(com.google.errorprone.DocGenTool.Target) HashMap(java.util.HashMap) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) Mustache(com.github.mustachejava.Mustache) Yaml(org.yaml.snakeyaml.Yaml) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) MustacheFactory(com.github.mustachejava.MustacheFactory) StringWriter(java.io.StringWriter) DumperOptions(org.yaml.snakeyaml.DumperOptions) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 10 with Mustache

use of com.github.mustachejava.Mustache in project camel by apache.

the class MustacheEndpoint method createMustache.

/**
     * Read and compile a Mustache template
     *
     * @param resourceReader Reader used to get template
     * @param resourceUri    Template Id
     * @return Template
     */
private Mustache createMustache(Reader resourceReader, String resourceUri) throws IOException {
    ClassLoader oldcl = Thread.currentThread().getContextClassLoader();
    try {
        ClassLoader apcl = getCamelContext().getApplicationContextClassLoader();
        if (apcl != null) {
            Thread.currentThread().setContextClassLoader(apcl);
        }
        Mustache newMustache;
        if (startDelimiter != null && endDelimiter != null && mustacheFactory instanceof DefaultMustacheFactory) {
            DefaultMustacheFactory defaultMustacheFactory = (DefaultMustacheFactory) mustacheFactory;
            newMustache = defaultMustacheFactory.compile(resourceReader, resourceUri, startDelimiter, endDelimiter);
        } else {
            newMustache = mustacheFactory.compile(resourceReader, resourceUri);
        }
        return newMustache;
    } finally {
        resourceReader.close();
        if (oldcl != null) {
            Thread.currentThread().setContextClassLoader(oldcl);
        }
    }
}
Also used : DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) Mustache(com.github.mustachejava.Mustache)

Aggregations

Mustache (com.github.mustachejava.Mustache)11 DefaultMustacheFactory (com.github.mustachejava.DefaultMustacheFactory)5 MustacheFactory (com.github.mustachejava.MustacheFactory)4 CompiledScript (org.elasticsearch.script.CompiledScript)4 ExecutableScript (org.elasticsearch.script.ExecutableScript)4 StringWriter (java.io.StringWriter)3 BytesReference (org.elasticsearch.common.bytes.BytesReference)3 ScriptEngineService (org.elasticsearch.script.ScriptEngineService)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 IOException (java.io.IOException)2 Writer (java.io.Writer)2 ArrayList (java.util.ArrayList)2 DumperOptions (org.yaml.snakeyaml.DumperOptions)2 Yaml (org.yaml.snakeyaml.Yaml)2 AutoValue (com.google.auto.value.AutoValue)1 Multimaps (com.google.common.collect.Multimaps)1 SortedSetMultimap (com.google.common.collect.SortedSetMultimap)1 TreeMultimap (com.google.common.collect.TreeMultimap)1 SeverityLevel (com.google.errorprone.BugPattern.SeverityLevel)1 Target (com.google.errorprone.DocGenTool.Target)1