com.github.mustachejava.MustacheException Java Examples
The following examples show how to use
com.github.mustachejava.MustacheException.
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: Mustache.java From template-benchmark with BSD 3-Clause "New" or "Revised" License | 6 votes |
@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 #2
Source File: CustomMustacheFactory.java From elasticsearch-learning-to-rank with Apache License 2.0 | 6 votes |
/** * At compile time, this function extracts the name of the variable: * {{#toJson}}variable_name{{/toJson}} */ protected static String extractVariableName(String fn, Mustache mustache, TemplateContext tc) { Code[] codes = mustache.getCodes(); if (codes == null || codes.length != 1) { throw new MustacheException("Mustache function [" + fn + "] must contain one and only one identifier"); } try (StringWriter capture = new StringWriter()) { // Variable name is in plain text and has type WriteCode if (codes[0] instanceof WriteCode) { codes[0].execute(capture, Collections.emptyList()); return capture.toString(); } else { codes[0].identity(capture); return capture.toString(); } } catch (IOException e) { throw new MustacheException("Exception while parsing mustache function [" + fn + "] at line " + tc.line(), e); } }
Example #3
Source File: TemplateUtilsTest.java From dcos-commons with Apache License 2.0 | 6 votes |
@Test public void testApplyMissingExceptionValueThrows() throws IOException { Map<String, String> env = new TreeMap<>(); env.put("foo", "bar"); env.put("bar", "baz"); env.put("baz", "foo"); try { TemplateUtils.renderMustacheThrowIfMissing( "testTemplate", "hello this is {{a_missing_parameter}},\nand {{another_missing_parameter}}. thanks for reading bye", env); Assert.fail("expected exception"); } catch (MustacheException e) { Assert.assertEquals(String.format( "Missing 2 values when rendering testTemplate:%n" + "- Missing values: [a_missing_parameter@L1, another_missing_parameter@L2]%n" + "- Provided values: %s", env), e.getMessage()); } }
Example #4
Source File: TemplateUtils.java From dcos-commons with Apache License 2.0 | 6 votes |
/** * Throws a descriptive exception if {@code missingValues} is non-empty. Exposed as a utility function to allow * custom filtering of missing values before the validation occurs. */ public static void validateMissingValues( String templateName, Map<String, String> values, Collection<MissingValue> missingValues) throws MustacheException { if (!missingValues.isEmpty()) { Map<String, String> orderedValues = new TreeMap<>(); orderedValues.putAll(values); throw new MustacheException(String.format( "Missing %d value%s when rendering %s:%n- Missing values: %s%n- Provided values: %s", missingValues.size(), missingValues.size() == 1 ? "" : "s", templateName, missingValues, orderedValues)); } }
Example #5
Source File: CustomMustacheFactory.java From elasticsearch-learning-to-rank with Apache License 2.0 | 6 votes |
@Override public Writer run(Writer writer, List<Object> scopes) { if (getCodes() != null) { for (Code code : getCodes()) { try (StringWriter capture = new StringWriter()) { code.execute(capture, scopes); String s = capture.toString(); if (s != null) { encoder.encode(s, writer); } } catch (IOException e) { throw new MustacheException("Exception while parsing mustache function at line " + tc.line(), e); } } } return writer; }
Example #6
Source File: MustacheHelper.java From helidon-build-tools with Apache License 2.0 | 6 votes |
@Override public MustacheVisitor createMustacheVisitor() { return new DefaultMustacheVisitor(this) { @Override public void value(TemplateContext tc, String variable, boolean encoded) { list.add(new ValueCode(tc, df, variable, encoded) { @Override public Writer execute(Writer writer, List<Object> scopes) { try { final Object object = get(scopes); if (object instanceof NotEncoded) { writer.write(oh.stringify(object)); return appendText(run(writer, scopes)); } else { return super.execute(writer, scopes); } } catch (Exception e) { throw new MustacheException("Failed to get value for " + name, e, tc); } } }); } }; }
Example #7
Source File: UnescapedWithJsonStringMustacheFactory.java From sawmill with Apache License 2.0 | 5 votes |
@Override public void encode(String value, Writer writer) { try { writer.write(value); } catch (IOException e) { throw new MustacheException("Failed to encode value: " + value); } }
Example #8
Source File: FilterResourcesProcessor.java From takari-lifecycle with Eclipse Public License 1.0 | 5 votes |
@Override public void encode(String value, Writer writer) { // // TODO: encoding rules // try { writer.write(value); } catch (IOException e) { throw new MustacheException(e); } }
Example #9
Source File: MustacheTemplater.java From dropwizard-debpkg-maven-plugin with Apache License 2.0 | 5 votes |
@Override public void execute(final Reader input, final Writer output, final String name, final Object parameters) { try { final Mustache template = MUSTACHE.compile(input, name); template.execute(output, parameters); } catch (MustacheException e) { final Throwable cause = Throwables.getRootCause(e); Throwables.propagateIfInstanceOf(cause, MissingParameterException.class); throw e; } }
Example #10
Source File: TestMustache.java From tools with Apache License 2.0 | 5 votes |
@Test public void testMustache() throws MustacheException, IOException { File root = new File("TestFiles"); DefaultMustacheFactory builder = new DefaultMustacheFactory(root); Map<String, Object> context = Maps.newHashMap(); context.put(TEST_FIELD1, TEST_RESULT1); Mustache m = builder.compile("testSimpleTemplate.txt"); StringWriter writer = new StringWriter(); m.execute(writer, context); assertEquals(TEST_RESULT1, writer.toString()); }
Example #11
Source File: Template.java From hesperides with GNU General Public License v3.0 | 5 votes |
private List<AbstractProperty> extractPropertiesFromStringContent(String fileName, String fieldName, String string) { try { return AbstractProperty.extractPropertiesFromStringContent(string); } catch (IllegalArgumentException | MustacheException illegalArgException) { throw new InvalidTemplateException(templateContainerKey.toString(), fileName, fieldName, illegalArgException); } }
Example #12
Source File: TemplateUtils.java From dcos-commons with Apache License 2.0 | 5 votes |
/** * Renders a given Mustache template using the provided value map, throwing an exception if any template parameters * weren't found in the map. * * @param templateContent String representation of template * @param values Map of values to be inserted into the template * @return Rendered Mustache template String * @throws MustacheException if parameters in the {@code templateContent} weren't provided in the {@code values} */ public static String renderMustacheThrowIfMissing( String templateName, String templateContent, Map<String, String> values) throws MustacheException { List<MissingValue> missingValues = new ArrayList<>(); String rendered = renderMustache(templateName, templateContent, values, missingValues); validateMissingValues(templateName, values, missingValues); return rendered; }
Example #13
Source File: UnescapedMustacheFactory.java From sawmill with Apache License 2.0 | 5 votes |
@Override public void encode(String value, Writer writer) { try { writer.write(value); } catch (IOException e) { throw new MustacheException("Failed to encode value: " + value); } }
Example #14
Source File: CustomMustacheFactory.java From elasticsearch-learning-to-rank with Apache License 2.0 | 5 votes |
private static String extractDelimiter(String variable) { Matcher matcher = PATTERN.matcher(variable); if (matcher.find()) { return matcher.group(1); } throw new MustacheException("Failed to extract delimiter for join function"); }
Example #15
Source File: CustomMustacheFactory.java From elasticsearch-learning-to-rank with Apache License 2.0 | 5 votes |
@Override @SuppressWarnings("unchecked") protected Function<String, String> createFunction(Object resolved) { return s -> { if (resolved == null) { return null; } try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { if (resolved == null) { builder.nullValue(); } else if (resolved instanceof Iterable) { builder.startArray(); for (Object o : (Iterable) resolved) { builder.value(o); } builder.endArray(); } else if (resolved instanceof Map) { builder.map((Map<String, ?>) resolved); } else { // Do not handle as JSON return oh.stringify(resolved); } return Strings.toString(builder); } catch (IOException e) { throw new MustacheException("Failed to convert object to JSON", e); } }; }
Example #16
Source File: CustomMustacheFactory.java From elasticsearch-learning-to-rank with Apache License 2.0 | 5 votes |
@Override public void encode(String value, Writer writer) { try { encoder.encode(value, writer); } catch (IOException e) { throw new MustacheException("Unable to encode value", e); } }
Example #17
Source File: MustacheUtils.java From elasticsearch-learning-to-rank with Apache License 2.0 | 5 votes |
public static Mustache compile(String name, String template) { // Don't use compile(String name) to avoid caching in the factory try { return FACTORY.compile(new StringReader(template), name); } catch (MustacheException me) { throw new IllegalArgumentException(me.getMessage(), me); } }
Example #18
Source File: MustacheFactoryUnescapeHtml.java From apollo with Apache License 2.0 | 5 votes |
@Override public void encode(String value, Writer writer) { int position = 0; int length = value.length(); try { writer.append(value, position, length); } catch (IOException e) { throw new MustacheException("Failed to encode value: " + value); } }
Example #19
Source File: NoneEscapingMustacheFactory.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public void encode(String value, Writer writer) { try { writer.write(value); } catch (IOException e) { throw new MustacheException("Failed to encode value: " + value); } }
Example #20
Source File: JsonEscapingMustacheFactory.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public void encode(String value, Writer writer) { try { writer.write(JsonStringEncoder.getInstance().quoteAsString(value)); } catch (IOException e) { throw new MustacheException("Failed to encode value: " + value); } }
Example #21
Source File: CustomMustacheFactory.java From elasticsearch-learning-to-rank with Apache License 2.0 | 4 votes |
ToJsonCode(TemplateContext tc, DefaultMustacheFactory df, Mustache mustache, String variable) { super(tc, df, mustache, CODE); if (!CODE.equalsIgnoreCase(variable)) { throw new MustacheException("Mismatch function code [" + CODE + "] cannot be applied to [" + variable + "]"); } }
Example #22
Source File: RdfToHtml.java From tools with Apache License 2.0 | 4 votes |
public static void rdfToHtml(SpdxDocument doc, File docHtmlFile, File licenseHtmlFile, File snippetHtmlFile, File docFilesHtmlFile) throws MustacheException, IOException, InvalidSPDXAnalysisException { rdfToHtml(doc, templateRootPath.toFile(), docHtmlFile, licenseHtmlFile, snippetHtmlFile, docFilesHtmlFile); }
Example #23
Source File: TemplatesTest.java From passopolis-server with GNU General Public License v3.0 | 4 votes |
@Test(expected=MustacheException.class) public void compileMissing() { Templates.compile("missing.mustache"); }
Example #24
Source File: TemplatesTest.java From passopolis-server with GNU General Public License v3.0 | 4 votes |
@Test(expected=MustacheException.class) public void compileMissingPartial() { Templates.compile("missing_partial.mustache"); }
Example #25
Source File: MustacheTemplateTest.java From dropwizard-debpkg-maven-plugin with Apache License 2.0 | 4 votes |
@Test(expected = MustacheException.class) public void testInvalidTemplate() throws IOException { final String template = getTemplate("invalid.mustache"); templater.execute(template, "test", Collections.emptySet()); }