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);
}
}
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\"}"));
}
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\\\"\"}"));
}
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);
}
}
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);
}
}
}
Aggregations