Brace fiddling.

This commit is contained in:
Michael Bayne
2017-12-17 14:46:42 -08:00
parent af623f1335
commit 326f04cb1e
2 changed files with 28 additions and 26 deletions
@@ -26,11 +26,11 @@ import java.util.Map;
* tmpl.execute(context); // returns "Hello world!" * tmpl.execute(context); // returns "Hello world!"
* }</pre> * }</pre>
*/ */
public class Mustache public class Mustache {
{
/** Compiles templates into executable form. See {@link Mustache}. */ /** Compiles templates into executable form. See {@link Mustache}. */
public static class Compiler public static class Compiler {
{
/** Whether or not standards mode is enabled. */ /** Whether or not standards mode is enabled. */
public final boolean standardsMode; public final boolean standardsMode;
@@ -226,15 +226,15 @@ public class Mustache
} }
/** Handles converting objects to strings when rendering templates. */ /** Handles converting objects to strings when rendering templates. */
public interface Formatter public interface Formatter {
{
/** Converts {@code value} to a string for inclusion in a template. */ /** Converts {@code value} to a string for inclusion in a template. */
String format (Object value); String format (Object value);
} }
/** Handles lambdas. */ /** Handles lambdas. */
public interface Lambda public interface Lambda {
{
/** Executes this lambda on the supplied template fragment. The lambda should write its /** Executes this lambda on the supplied template fragment. The lambda should write its
* results to {@code out}. * results to {@code out}.
* *
@@ -245,8 +245,8 @@ public class Mustache
} }
/** Handles lambdas that are also invoked for inverse sections.. */ /** Handles lambdas that are also invoked for inverse sections.. */
public interface InvertibleLambda extends Lambda public interface InvertibleLambda extends Lambda {
{
/** Executes this lambda on the supplied template fragment, when the lambda is used in an /** Executes this lambda on the supplied template fragment, when the lambda is used in an
* inverse section. The lambda should write its results to {@code out}. * inverse section. The lambda should write its results to {@code out}.
* *
@@ -257,22 +257,22 @@ public class Mustache
} }
/** Reads variables from context objects. */ /** Reads variables from context objects. */
public interface VariableFetcher public interface VariableFetcher {
{
/** Reads the so-named variable from the supplied context object. */ /** Reads the so-named variable from the supplied context object. */
Object get (Object ctx, String name) throws Exception; Object get (Object ctx, String name) throws Exception;
} }
/** Handles escaping characters in substituted text. */ /** Handles escaping characters in substituted text. */
public interface Escaper public interface Escaper {
{
/** Returns {@code raw} with the appropriate characters replaced with escape sequences. */ /** Returns {@code raw} with the appropriate characters replaced with escape sequences. */
String escape (String raw); String escape (String raw);
} }
/** Handles loading partial templates. */ /** Handles loading partial templates. */
public interface TemplateLoader public interface TemplateLoader {
{
/** Returns a reader for the template with the supplied name. /** Returns a reader for the template with the supplied name.
* Reader will be closed by callee. * Reader will be closed by callee.
* @throws Exception if the template could not be loaded for any reason. */ * @throws Exception if the template could not be loaded for any reason. */
@@ -280,8 +280,8 @@ public class Mustache
} }
/** Handles interpreting objects as collections. */ /** Handles interpreting objects as collections. */
public interface Collector public interface Collector {
{
/** Returns an iterator that can iterate over the supplied value, or null if the value is /** Returns an iterator that can iterate over the supplied value, or null if the value is
* not a collection. */ * not a collection. */
Iterator<?> toIterator (final Object value); Iterator<?> toIterator (final Object value);
@@ -674,8 +674,7 @@ public class Mustache
} }
} }
protected static void requireSameName (String name1, String name2, int line) protected static void requireSameName (String name1, String name2, int line) {
{
if (!name1.equals(name2)) { if (!name1.equals(name2)) {
throw new MustacheParseException("Section close tag with mismatched open tag '" + throw new MustacheParseException("Section close tag with mismatched open tag '" +
name2 + "' != '" + name1 + "'", line); name2 + "' != '" + name1 + "'", line);
@@ -798,7 +797,7 @@ public class Mustache
_formatter = formatter; _formatter = formatter;
_escaper = escaper; _escaper = escaper;
} }
@Override public void execute (Template tmpl, Template.Context ctx, Writer out) { @Override public void execute (Template tmpl, Template.Context ctx, Writer out) {
Object value = tmpl.getValueOrDefault(ctx, _name, _line); Object value = tmpl.getValueOrDefault(ctx, _name, _line);
if (value == null) { if (value == null) {
throw new MustacheException.Context("No key, method or field with name '" + _name + throw new MustacheException.Context("No key, method or field with name '" + _name +
@@ -840,7 +839,7 @@ public class Mustache
super(name, line); super(name, line);
_segs = trim(segs, false); _segs = trim(segs, false);
} }
protected void executeSegs (Template tmpl, Template.Context ctx, Writer out) { protected void executeSegs (Template tmpl, Template.Context ctx, Writer out) {
for (Template.Segment seg : _segs) { for (Template.Segment seg : _segs) {
seg.execute(tmpl, ctx, out); seg.execute(tmpl, ctx, out);
} }
@@ -855,7 +854,7 @@ public class Mustache
super(name, segs, line); super(name, segs, line);
_comp = compiler; _comp = compiler;
} }
@Override public void execute (Template tmpl, Template.Context ctx, Writer out) { @Override public void execute (Template tmpl, Template.Context ctx, Writer out) {
Object value = tmpl.getSectionValue(ctx, _name, _line); // won't return null Object value = tmpl.getSectionValue(ctx, _name, _line); // won't return null
Iterator<?> iter = _comp.collector.toIterator(value); Iterator<?> iter = _comp.collector.toIterator(value);
if (iter != null) { if (iter != null) {
@@ -898,7 +897,7 @@ public class Mustache
super(name, segs, line); super(name, segs, line);
_comp = compiler; _comp = compiler;
} }
@Override public void execute (Template tmpl, Template.Context ctx, Writer out) { @Override public void execute (Template tmpl, Template.Context ctx, Writer out) {
Object value = tmpl.getSectionValue(ctx, _name, _line); // won't return null Object value = tmpl.getSectionValue(ctx, _name, _line); // won't return null
Iterator<?> iter = _comp.collector.toIterator(value); Iterator<?> iter = _comp.collector.toIterator(value);
if (iter != null) { if (iter != null) {
@@ -16,6 +16,7 @@ import java.util.Map;
* output. The context can be any tree of objects. Variables are resolved against the context. * output. The context can be any tree of objects. Variables are resolved against the context.
* Given a name {@code foo}, the following mechanisms are supported for resolving its value * Given a name {@code foo}, the following mechanisms are supported for resolving its value
* (and are sought in this order): * (and are sought in this order):
*
* <ul> * <ul>
* <li>If the variable has the special name {@code this} the context object itself will be * <li>If the variable has the special name {@code this} the context object itself will be
* returned. This is useful when iterating over lists. * returned. This is useful when iterating over lists.
@@ -25,6 +26,7 @@ import java.util.Map;
* <li>A method named {@code getFoo} in the supplied object (with non-void return value). * <li>A method named {@code getFoo} in the supplied object (with non-void return value).
* <li>A field named {@code foo} in the supplied object. * <li>A field named {@code foo} in the supplied object.
* </ul> * </ul>
*
* <p> The field type, method return type, or map value type should correspond to the desired * <p> The field type, method return type, or map value type should correspond to the desired
* behavior if the resolved name corresponds to a section. {@link Boolean} is used for showing or * behavior if the resolved name corresponds to a section. {@link Boolean} is used for showing or
* hiding sections without binding a sub-context. Arrays, {@link Iterator} and {@link Iterable} * hiding sections without binding a sub-context. Arrays, {@link Iterator} and {@link Iterable}
@@ -33,13 +35,14 @@ import java.util.Map;
* to add if desire exists. See the <a href="http://mustache.github.com/mustache.5.html">Mustache * to add if desire exists. See the <a href="http://mustache.github.com/mustache.5.html">Mustache
* documentation</a> for more details on section behavior. </p> * documentation</a> for more details on section behavior. </p>
*/ */
public class Template public class Template {
{
/** /**
* Encapsulates a fragment of a template that is passed to a lambda. The fragment is bound to * Encapsulates a fragment of a template that is passed to a lambda. The fragment is bound to
* the variable context that was in effect at the time the lambda was called. * the variable context that was in effect at the time the lambda was called.
*/ */
public abstract class Fragment { public abstract class Fragment {
/** Executes this fragment; writes its result to {@code out}. */ /** Executes this fragment; writes its result to {@code out}. */
public abstract void execute (Writer out); public abstract void execute (Writer out);