Do our save/restore slightly differently.

This commit is contained in:
Michael Bayne
2013-05-03 10:12:00 -07:00
parent 751f671fd6
commit 426faf92c5
2 changed files with 18 additions and 30 deletions
@@ -218,15 +218,13 @@ 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) {
String originalDelims = compiler.delims == null ? null : compiler.delims.toString(); Delims orig = compiler.delims.save();
//compiler.delims.start1
try { try {
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);
} finally { } finally {
if (originalDelims != null) { // restore the original delimiters
compiler.delims.updateDelims(originalDelims); compiler.delims.restore(orig);
}
} }
} }
@@ -421,35 +419,13 @@ public class Mustache
} }
protected static class Delims { protected static class Delims {
public char start1 = '{'; public char start1 = '{', end1 = '}';
public char start2 = '{'; public char start2 = '{', end2 = '}';
public char end1 = '}';
public char end2 = '}';
public boolean isStaches () { public boolean isStaches () {
return start1 == '{' && start2 == '{' && end1 == '}' && end2 == '}'; return start1 == '{' && start2 == '{' && end1 == '}' && end2 == '}';
} }
/**
* Returns a String representation that is compatible with
* {@link #updateDelims(java.lang.String)}.
* @return
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(start1);
if (start2 != NO_CHAR) {
builder.append(start2);
}
builder.append(" ");
builder.append(end1);
if (end2 != NO_CHAR) {
builder.append(end2);
}
return builder.toString();
}
public Delims updateDelims (String dtext) { public Delims updateDelims (String dtext) {
String[] delims = dtext.split(" "); String[] delims = dtext.split(" ");
if (delims.length != 2) throw new MustacheException(errmsg(dtext)); if (delims.length != 2) throw new MustacheException(errmsg(dtext));
@@ -482,6 +458,18 @@ public class Mustache
return this; return this;
} }
Delims save () {
return new Delims().restore(this);
}
Delims restore (Delims delims) {
this.start1 = delims.start1;
this.start2 = delims.start2;
this.end1 = delims.end1;
this.end2 = delims.end2;
return this;
}
private static String errmsg (String dtext) { private static String errmsg (String dtext) {
return "Invalid delimiter configuration '" + dtext + "'. Must be of the " + return "Invalid delimiter configuration '" + dtext + "'. Must be of the " +
"form {{=1 2=}} or {{=12 34=}} where 1, 2, 3 and 4 are delimiter chars."; "form {{=1 2=}} or {{=12 34=}} where 1, 2, 3 and 4 are delimiter chars.";