These days I always cuddle method braces.

This commit is contained in:
Michael Bayne
2013-03-27 10:37:41 -07:00
parent fe2664627f
commit 3a5da915e9
3 changed files with 22 additions and 43 deletions
@@ -133,7 +133,7 @@ public class Mustache
} }
/** Returns a compiler configured to use the supplied delims as default delimiters. /** Returns a compiler configured to use the supplied delims as default delimiters.
* @param delims a string of the form {@code {AB CD} or {@code {A D} where A and B are * @param delims a string of the form {@code AB CD} or {@code A D} where A and B are
* opening delims and C and D are closing delims. */ * opening delims and C and D are closing delims. */
public Compiler withDelims (String delims) { public Compiler withDelims (String delims) {
return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue, return new Compiler(this.escapeHTML, this.standardsMode, this.nullValue,
@@ -209,8 +209,7 @@ public class Mustache
/** /**
* Returns a compiler that escapes HTML by default and does not use standards mode. * Returns a compiler that escapes HTML by default and does not use standards mode.
*/ */
public static Compiler compiler () public static Compiler compiler () {
{
return new Compiler(true, false, null, true, false, FAILING_LOADER, new DefaultCollector(), return new Compiler(true, false, null, true, false, FAILING_LOADER, new DefaultCollector(),
new Delims()); new Delims());
} }
@@ -218,32 +217,28 @@ public class Mustache
/** /**
* Compiles the supplied template into a repeatedly executable intermediate form. * Compiles the supplied template into a repeatedly executable intermediate form.
*/ */
protected static Template compile (Reader source, Compiler compiler) protected static Template compile (Reader source, Compiler compiler) {
{
Accumulator accum = new Parser(compiler).parse(source); Accumulator accum = new Parser(compiler).parse(source);
return new Template(accum.finish(), compiler); return new Template(accum.finish(), compiler);
} }
private Mustache () {} // no instantiateski private Mustache () {} // no instantiateski
protected static void restoreStartTag (StringBuilder text, Delims starts) protected static void restoreStartTag (StringBuilder text, Delims starts) {
{
text.insert(0, starts.start1); text.insert(0, starts.start1);
if (starts.start2 != NO_CHAR) { if (starts.start2 != NO_CHAR) {
text.insert(1, starts.start2); text.insert(1, starts.start2);
} }
} }
protected static String escapeHTML (String text) protected static String escapeHTML (String text) {
{
for (String[] escape : ATTR_ESCAPES) { for (String[] escape : ATTR_ESCAPES) {
text = text.replace(escape[0], escape[1]); text = text.replace(escape[0], escape[1]);
} }
return text; return text;
} }
protected static boolean allowsWhitespace (char typeChar) protected static boolean allowsWhitespace (char typeChar) {
{
return (typeChar == '=') || // change delimiters return (typeChar == '=') || // change delimiters
(typeChar == '!'); // comment (typeChar == '!'); // comment
} }
@@ -8,13 +8,11 @@ package com.samskivert.mustache;
*/ */
public class MustacheParseException extends MustacheException public class MustacheParseException extends MustacheException
{ {
public MustacheParseException (String message) public MustacheParseException (String message) {
{
super(message); super(message);
} }
public MustacheParseException (String message, int lineNo) public MustacheParseException (String message, int lineNo) {
{
super(message + " @ line " + lineNo); super(message + " @ line " + lineNo);
} }
} }
@@ -56,8 +56,7 @@ public class Template
* Executes this template with the given context, returning the results as a string. * Executes this template with the given context, returning the results as a string.
* @throws MustacheException if an error occurs while executing or writing the template. * @throws MustacheException if an error occurs while executing or writing the template.
*/ */
public String execute (Object context) throws MustacheException public String execute (Object context) throws MustacheException {
{
StringWriter out = new StringWriter(); StringWriter out = new StringWriter();
execute(context, out); execute(context, out);
return out.toString(); return out.toString();
@@ -67,8 +66,7 @@ public class Template
* Executes this template with the given context, writing the results to the supplied writer. * Executes this template with the given context, writing the results to the supplied writer.
* @throws MustacheException if an error occurs while executing or writing the template. * @throws MustacheException if an error occurs while executing or writing the template.
*/ */
public void execute (Object context, Writer out) throws MustacheException public void execute (Object context, Writer out) throws MustacheException {
{
executeSegs(new Context(context, null, 0, false, false), out); executeSegs(new Context(context, null, 0, false, false), out);
} }
@@ -79,28 +77,24 @@ public class Template
* a block. * a block.
* @throws MustacheException if an error occurs while executing or writing the template. * @throws MustacheException if an error occurs while executing or writing the template.
*/ */
public void execute (Object context, Object parentContext, Writer out) throws MustacheException public void execute (Object context, Object parentContext, Writer out) throws MustacheException {
{
Context pctx = new Context(parentContext, null, 0, false, false); Context pctx = new Context(parentContext, null, 0, false, false);
executeSegs(new Context(context, pctx, 0, false, false), out); executeSegs(new Context(context, pctx, 0, false, false), out);
} }
protected Template (Segment[] segs, Mustache.Compiler compiler) protected Template (Segment[] segs, Mustache.Compiler compiler) {
{
_segs = segs; _segs = segs;
_compiler = compiler; _compiler = compiler;
_fcache = compiler.collector.createFetcherCache(); _fcache = compiler.collector.createFetcherCache();
} }
protected void executeSegs (Context ctx, Writer out) throws MustacheException protected void executeSegs (Context ctx, Writer out) throws MustacheException {
{
for (Segment seg : _segs) { for (Segment seg : _segs) {
seg.execute(this, ctx, out); seg.execute(this, ctx, out);
} }
} }
protected Fragment createFragment (final Segment[] segs, final Context ctx) protected Fragment createFragment (final Segment[] segs, final Context ctx) {
{
return new Fragment() { return new Fragment() {
@Override public void execute (Writer out) { @Override public void execute (Writer out) {
for (Segment seg : segs) { for (Segment seg : segs) {
@@ -121,8 +115,7 @@ public class Template
* *
* @return the value associated with the supplied name or null if no value could be resolved. * @return the value associated with the supplied name or null if no value could be resolved.
*/ */
protected Object getValue (Context ctx, String name, int line, boolean missingIsNull) protected Object getValue (Context ctx, String name, int line, boolean missingIsNull) {
{
if (!_compiler.standardsMode) { if (!_compiler.standardsMode) {
// if we're dealing with a compound key, resolve each component and use the result to // if we're dealing with a compound key, resolve each component and use the result to
// resolve the subsequent component and so forth // resolve the subsequent component and so forth
@@ -179,8 +172,7 @@ public class Template
* will be the means by which we enact configured behavior for sections that reference null or * will be the means by which we enact configured behavior for sections that reference null or
* missing variables. Right now, all such variables result in a length 0 section. * missing variables. Right now, all such variables result in a length 0 section.
*/ */
protected Object getSectionValue (Context ctx, String name, int line) protected Object getSectionValue (Context ctx, String name, int line) {
{
// TODO: configurable behavior on missing values // TODO: configurable behavior on missing values
Object value = getValue(ctx, name, line, _compiler.missingIsNull); Object value = getValue(ctx, name, line, _compiler.missingIsNull);
// TODO: configurable behavior on null values // TODO: configurable behavior on null values
@@ -191,8 +183,7 @@ public class Template
* Returns the value for the specified variable, or the configured default value if the * Returns the value for the specified variable, or the configured default value if the
* variable resolves to null. See {@link #getValue}. * variable resolves to null. See {@link #getValue}.
*/ */
protected Object getValueOrDefault (Context ctx, String name, int line) protected Object getValueOrDefault (Context ctx, String name, int line) {
{
Object value = getValue(ctx, name, line, _compiler.missingIsNull); Object value = getValue(ctx, name, line, _compiler.missingIsNull);
// getValue will raise MustacheException if a variable cannot be resolved and missingIsNull // getValue will raise MustacheException if a variable cannot be resolved and missingIsNull
// is not configured; so we're safe to assume that any null that makes it up to this point // is not configured; so we're safe to assume that any null that makes it up to this point
@@ -200,8 +191,7 @@ public class Template
return (value == null) ? _compiler.computeNullValue(name) : value; return (value == null) ? _compiler.computeNullValue(name) : value;
} }
protected Object getValueIn (Object data, String name, int line) protected Object getValueIn (Object data, String name, int line) {
{
if (data == null) { if (data == null) {
throw new NullPointerException( throw new NullPointerException(
"Null context for variable '" + name + "' on line " + line); "Null context for variable '" + name + "' on line " + line);
@@ -236,8 +226,7 @@ public class Template
} }
} }
protected Object checkForMissing (String name, int line, boolean missingIsNull, Object value) protected Object checkForMissing (String name, int line, boolean missingIsNull, Object value) {
{
if (value == NO_FETCHER_FOUND) { if (value == NO_FETCHER_FOUND) {
if (missingIsNull) return null; if (missingIsNull) return null;
throw new MustacheException.Context( throw new MustacheException.Context(
@@ -251,8 +240,7 @@ public class Template
protected final Mustache.Compiler _compiler; protected final Mustache.Compiler _compiler;
protected final Map<Key, Mustache.VariableFetcher> _fcache; protected final Map<Key, Mustache.VariableFetcher> _fcache;
protected static class Context protected static class Context {
{
public final Object data; public final Object data;
public final Context parent; public final Context parent;
public final int index; public final int index;
@@ -273,8 +261,7 @@ public class Template
} }
/** A template is broken into segments. */ /** A template is broken into segments. */
protected static abstract class Segment protected static abstract class Segment {
{
abstract void execute (Template tmpl, Context ctx, Writer out); abstract void execute (Template tmpl, Context ctx, Writer out);
protected static void write (Writer out, String data) { protected static void write (Writer out, String data) {
@@ -287,8 +274,7 @@ public class Template
} }
/** Used to cache variable fetchers for a given context class, name combination. */ /** Used to cache variable fetchers for a given context class, name combination. */
protected static class Key protected static class Key {
{
public final Class<?> cclass; public final Class<?> cclass;
public final String name; public final String name;