@Override public void partial(TemplateContext tc, final String variable) { TemplateContext partialTC = new TemplateContext("{{", "}}", tc.file(), tc.line(), tc.startOfLine()); list.add(new PartialCode(partialTC, df, variable)); }
protected void tag(Writer writer, String tag) throws IOException { writer.write(tc.startChars()); writer.write(tag); writer.write(name); writer.write(tc.endChars()); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; FragmentKey that = (FragmentKey) o; return tc.equals(that.tc) && templateText.equals(that.templateText); }
protected Function<FragmentKey, Mustache> getFragmentCacheFunction() { return (fragmentKey) -> { StringReader reader = new StringReader(fragmentKey.templateText); TemplateContext tc = fragmentKey.tc; return mc.compile(reader, tc.file(), tc.startChars(), tc.endChars(), tc.startOfLine()); }; }
public static void logWarning(String warning, String name, List<Object> scopes, TemplateContext tc) { StringBuilder sb = new StringBuilder(warning) .append(name) .append(" (") .append(tc.file()) .append(":") .append(tc.line()) .append(") ") .append("in"); scopes.stream().filter(Objects::nonNull).forEach(scope -> { Class aClass = scope.getClass(); try { sb.append(" ").append(aClass.getSimpleName()); } catch (Exception e) { // Some generated classes don't have simple names try { sb.append(" ").append(aClass.getName()); } catch (Exception e1) { // Some generated classes don't have proper names at all } } }); logger.warning(sb.toString()); } }
@Override protected synchronized Wrapper getWrapper(String name, List<Object> scopes) { Wrapper wrapper = super.getWrapper(name, scopes); // This should only do anything when the template param is e.g. "{{hello}}", not "{{#hello}}hi{{/hello}}". // The latter case implies an expectation that the value will sometimes be unset. We can determine the // situation based on the code type: // - "{{hello}}" = ValueCode <-- check for this case // - "{{#hello}}{{/hello}}" = IterableCode // - "{{^hello}}{{/hello}}" = NotIterableCode // - etc... "{{>partial}}", "{{!comment}}" if (code instanceof ValueCode && wrapper instanceof MissingWrapper) { missingValues.add(new MissingValue(name, tc.line())); } return wrapper; } }
protected PartialCode(TemplateContext tc, DefaultMustacheFactory df, Mustache mustache, String type, String variable) { super(tc, df, mustache, variable, type); // Use the name of the parent to get the name of the partial String file = tc.file(); int dotindex = file.lastIndexOf("."); extension = dotindex == -1 ? "" : file.substring(dotindex); int slashindex = file.lastIndexOf("/"); dir = file.substring(0, slashindex + 1); recrusionLimit = df.getRecursionLimit(); }
/** * Ignore empty strings and append to the previous code if it was also a write. */ private StringBuilder write(MustacheVisitor mv, StringBuilder out, String file, int line, boolean startOfLine) { String text = out.toString(); mv.write(new TemplateContext(null, null, file, line, startOfLine), text); return new StringBuilder(); }
TemplateContext templeteContext = new TemplateContext(new ASContextType()) { return templeteContext.evaluate(template).getString(); } catch (BadLocationException e) { logger.error("Couldnt evaluate template",e);
@Override public int hashCode() { int result = tc.hashCode(); result = 31 * result + templateText.hashCode(); return result; } }
protected void filterText() { if (df != null && appended != null) { appended = df.filterText(appended, tc.startOfLine()); } }
protected Function<FragmentKey, Mustache> getFragmentCacheFunction() { return (fragmentKey) -> { StringReader reader = new StringReader(fragmentKey.templateText); TemplateContext tc = fragmentKey.tc; return mc.compile(reader, tc.file(), tc.startChars(), tc.endChars(), tc.startOfLine()); }; }
/** * 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); } } }
@Override public Mustache mustache(TemplateContext templateContext) { return new DefaultMustache(templateContext, df, list.toArray(EMPTY_CODES), templateContext.file()); }
TemplateContext tc = new TemplateContext(sm, em, file, currentLine.get(), startOfLine); throw new MustacheException("Empty mustache", tc); switch (ch) { case '#': mv.iterable(new TemplateContext(sm, em, file, line, startOfLine), variable, mustache); break; case '^': mv.notIterable(new TemplateContext(sm, em, file, line, startOfLine), variable, mustache); break; case '<': mv.extend(new TemplateContext(sm, em, file, line, startOfLine), variable, mustache); break; case '$': mv.name(new TemplateContext(sm, em, file, line, startOfLine), variable, mustache); break; case '?': mv.checkName(new TemplateContext(sm, em, file, line, startOfLine), variable, mustache); break; TemplateContext tc = new TemplateContext(sm, em, file, currentLine.get(), startOfLine); throw new MustacheException( "Mismatched start/end tags: " + tag + " != " + variable + " in " + file + ":" + currentLine, tc); return mv.mustache(new TemplateContext(sm, em, file, 0, startOfLine)); mv.partial(new TemplateContext(sm, em, file, currentLine.get(), startOfLine), variable); break;
@Override public int hashCode() { int result = tc.hashCode(); result = 31 * result + templateText.hashCode(); return result; } }
protected void filterText() { if (df != null && appended != null) { appended = df.filterText(appended, tc.startOfLine()); } }
@Override public void partial(TemplateContext tc, final String variable) { TemplateContext partialTC = new TemplateContext("{{", "}}", tc.file(), tc.line(), tc.startOfLine()); final Long divid = id.incrementAndGet(); list.add(new PartialCode(partialTC, df, variable) {
@Override public void identity(Writer writer) { try { if (name != null) { writer.write(tc.startChars()); if (!encoded) { writer.write("{"); } writer.write(type); writer.write(name); if (!encoded) { writer.write("}"); } writer.write(tc.endChars()); } appendText(writer); } catch (IOException e) { throw new MustacheException(e, tc); } }