Skip to content

Commit 19785ab

Browse files
author
Sam Pullara
committed
thread the writer through the calls
1 parent 214d857 commit 19785ab

File tree

10 files changed

+77
-160
lines changed

10 files changed

+77
-160
lines changed

compiler/src/main/java/com/github/mustachejava/Code.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Code objects that are executed in order to evaluate the template
77
*/
88
public interface Code {
9-
void execute(Writer writer, Object... scopes);
9+
Writer execute(Writer writer, Object... scopes);
1010
void identity(Writer writer);
1111
void append(String text);
1212
}

compiler/src/main/java/com/github/mustachejava/impl/DefaultCode.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ public Object get(String name, Object... scopes) {
8080
* @param scopes The scopes to evaluate the embedded names against.
8181
*/
8282
@Override
83-
public void execute(Writer writer, Object... scopes) {
84-
runCodes(writer, scopes);
85-
appendText(writer);
83+
public Writer execute(Writer writer, Object... scopes) {
84+
writer = runCodes(writer, scopes);
85+
writer = appendText(writer);
86+
return writer;
8687
}
8788

8889
@Override
@@ -115,23 +116,25 @@ private void tag(Writer writer, String tag) throws IOException {
115116
writer.write(em);
116117
}
117118

118-
protected void appendText(Writer writer) {
119+
protected Writer appendText(Writer writer) {
119120
if (appended != null) {
120121
try {
121122
writer.write(appended);
122123
} catch (IOException e) {
123124
throw new MustacheException(e);
124125
}
125126
}
127+
return writer;
126128
}
127129

128-
protected void runCodes(Writer writer, Object... scopes) {
130+
protected Writer runCodes(Writer writer, Object... scopes) {
129131
if (codes != null) {
130132
int length = codes.length;
131133
for (int i = 0; i < length; i++) {
132-
codes[i].execute(writer, scopes);
134+
writer = codes[i].execute(writer, scopes);
133135
}
134136
}
137+
return writer;
135138
}
136139

137140
@Override

compiler/src/main/java/com/github/mustachejava/impl/DefaultCodeFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,17 @@ public Code partial(final String variable, String file, int line, String sm, Str
7777
private Mustache partial;
7878

7979
@Override
80-
public void execute(Writer writer, Object... scopes) {
80+
public Writer execute(Writer writer, Object... scopes) {
8181
if (partial == null) {
8282
partial = mc.compile(variable + extension);
8383
}
8484
Object scope = get(variable, scopes);
8585
Object[] newscopes = new Object[scopes.length + 1];
8686
System.arraycopy(scopes, 0, newscopes, 0, scopes.length);
8787
newscopes[scopes.length] = scope;
88-
partial.execute(writer, newscopes);
89-
appendText(writer);
88+
writer = partial.execute(writer, newscopes);
89+
writer = appendText(writer);
90+
return writer;
9091
}
9192
};
9293
}

compiler/src/main/java/com/github/mustachejava/impl/IterableCode.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public IterableCode(DefaultCodeFactory cf, List<Code> codes, String variable, St
3232
}
3333

3434
@Override
35-
public void execute(Writer writer, Object... scopes) {
35+
public Writer execute(Writer writer, Object... scopes) {
3636
Object resolve = get(variable, scopes);
3737
if (resolve != null) {
3838
if (resolve instanceof Function) {
@@ -47,7 +47,7 @@ public void execute(Writer writer, Object... scopes) {
4747
mustache = cf.mc.compile(new StringReader(templateText), file, sm, em);
4848
cf.templateCache.put(templateText, mustache);
4949
}
50-
mustache.execute(writer, scopes);
50+
writer = mustache.execute(writer, scopes);
5151
}
5252
} else {
5353
for (Iterator i = oh.iterate(resolve); i.hasNext(); ) {
@@ -58,10 +58,11 @@ public void execute(Writer writer, Object... scopes) {
5858
System.arraycopy(scopes, 0, iteratorScopes, 0, scopes.length);
5959
iteratorScopes[scopes.length] = next;
6060
}
61-
runCodes(writer, iteratorScopes);
61+
writer = runCodes(writer, iteratorScopes);
6262
}
6363
}
6464
}
65-
appendText(writer);
65+
writer = appendText(writer);
66+
return writer;
6667
}
6768
}

compiler/src/main/java/com/github/mustachejava/impl/NotIterableCode.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@ public NotIterableCode(DefaultCodeFactory cf, List<Code> codes, String variable,
2222
}
2323

2424
@Override
25-
public void execute(Writer writer, Object... scopes) {
25+
public Writer execute(Writer writer, Object... scopes) {
2626
Object resolve = get(variable, scopes);
2727
if (resolve != null) {
2828
Iterator i = oh.iterate(resolve);
2929
if (!i.hasNext()) {
30-
runCodes(writer, scopes);
30+
writer = runCodes(writer, scopes);
3131
}
3232
} else {
33-
runCodes(writer, scopes);
33+
writer = runCodes(writer, scopes);
3434
}
35-
appendText(writer);
35+
writer = appendText(writer);
36+
return writer;
3637
}
3738
}

compiler/src/main/java/com/github/mustachejava/impl/ValueCode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public ValueCode(DefaultCodeFactory cf, String variable, String sm, String em, b
3232
}
3333

3434
@Override
35-
public void execute(Writer writer, Object... scopes) {
35+
public Writer execute(Writer writer, Object... scopes) {
3636
Object object = get(variable, scopes);
3737
if (object != null) {
3838
try {
@@ -49,7 +49,7 @@ public void execute(Writer writer, Object... scopes) {
4949
cf.templateCache.put(templateText, mustache);
5050
}
5151
StringWriter sw = new StringWriter();
52-
mustache.execute(sw, scopes);
52+
writer = mustache.execute(sw, scopes);
5353
value = sw.toString();
5454
} else {
5555
value = "";
@@ -66,6 +66,6 @@ public void execute(Writer writer, Object... scopes) {
6666
throw new MustacheException("Failed to get value for " + variable + " at line " + line, e);
6767
}
6868
}
69-
super.execute(writer, scopes);
69+
return super.execute(writer, scopes);
7070
}
7171
}

compiler/src/main/java/com/github/mustachejava/impl/WriteCode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public void identity(Writer writer) {
2525
}
2626

2727
@Override
28-
public void execute(Writer writer, Object... scopes) {
28+
public Writer execute(Writer writer, Object... scopes) {
2929
try {
3030
writer.write(text);
3131
} catch (IOException e) {
3232
throw new MustacheException();
3333
}
34-
appendText(writer);
34+
return appendText(writer);
3535
}
3636
}

compiler/src/test/java/com/github/mustachejava/InterpreterTest.java

Lines changed: 43 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -304,62 +304,6 @@ int taxed_value() {
304304
assertEquals(getContents(root, "simple2.txt"), sw.toString());
305305
}
306306
307-
public void testEscaped() throws MustacheException, IOException {
308-
MustacheBuilder c = init();
309-
Mustache m = c.parseFile("escaped.html");
310-
StringWriter sw = new StringWriter();
311-
FutureWriter writer = new FutureWriter(sw);
312-
m.execute(writer, new Scope(new Object() {
313-
String title = "Bear > Shark";
314-
String entities = "&quot;";
315-
}));
316-
writer.flush();
317-
assertEquals(getContents(root, "escaped.txt"), sw.toString());
318-
}
319-
320-
public void testUnescaped() throws MustacheException, IOException {
321-
MustacheBuilder c = new MustacheBuilder(root);
322-
Mustache m = c.parseFile("unescaped.html");
323-
StringWriter sw = new StringWriter();
324-
FutureWriter writer = new FutureWriter(sw);
325-
m.execute(writer, new Scope(new Object() {
326-
String title() {
327-
return "Bear > Shark";
328-
}
329-
}));
330-
writer.flush();
331-
assertEquals(getContents(root, "unescaped.txt"), sw.toString());
332-
}
333-
334-
public void testInverted() throws MustacheException, IOException {
335-
MustacheBuilder c = init();
336-
Mustache m = c.parseFile("inverted_section.html");
337-
StringWriter sw = new StringWriter();
338-
FutureWriter writer = new FutureWriter(sw);
339-
m.execute(writer, new Scope(new Object() {
340-
String name() {
341-
return "Bear > Shark";
342-
}
343-
344-
ArrayList repo = new ArrayList();
345-
}));
346-
writer.flush();
347-
assertEquals(getContents(root, "inverted_section.txt"), sw.toString());
348-
}
349-
350-
public void testComments() throws MustacheException, IOException {
351-
MustacheBuilder c = init();
352-
Mustache m = c.parseFile("comments.html");
353-
StringWriter sw = new StringWriter();
354-
FutureWriter writer = new FutureWriter(sw);
355-
m.execute(writer, new Scope(new Object() {
356-
String title() {
357-
return "A Comedy of Errors";
358-
}
359-
}));
360-
writer.flush();
361-
assertEquals(getContents(root, "comments.txt"), sw.toString());
362-
}
363307
*/
364308
public void testPartial() throws MustacheException, IOException {
365309
MustacheCompiler c = init();
@@ -373,74 +317,47 @@ public void testPartial() throws MustacheException, IOException {
373317
m.execute(sw, scope);
374318
assertEquals(getContents(root, "template_partial.txt"), sw.toString());
375319
}
376-
/*
377-
public static class PartialChanged extends Mustache {
378-
static AtomicBoolean executed = new AtomicBoolean(false);
379-
protected Mustache compilePartial(String name) throws MustacheException {
380-
executed.set(true);
381-
return super.compilePartial(name);
382-
}
383-
}
384-
385-
public void testPartialOverride() throws MustacheException, IOException {
386-
MustacheBuilder c = init();
387-
c.setSuperclass(PartialChanged.class.getName());
388-
Mustache m = c.parseFile("template_partial.html");
320+
321+
public void testComplex() throws MustacheException, IOException {
322+
MustacheCompiler c = init();
323+
Mustache m = c.compile("complex.html");
389324
StringWriter sw = new StringWriter();
390-
FutureWriter writer = new FutureWriter(sw);
391-
Scope scope = new Scope();
392-
scope.put("title", "Welcome");
393-
scope.put("template_partial_2", new Object() {
394-
String again = "Goodbye";
395-
});
396-
m.execute(writer, scope);
397-
writer.flush();
398-
assertEquals(getContents(root, "template_partial.txt"), sw.toString());
399-
assertTrue(PartialChanged.executed.get());
325+
m.execute(sw, new ComplexObject());
326+
assertEquals(getContents(root, "complex.txt"), sw.toString());
400327
}
401328

402-
public void testComplex() throws MustacheException, IOException {
403-
Scope scope = new Scope(new Object() {
404-
String header = "Colors";
405-
List item = Arrays.asList(
406-
new Object() {
407-
String name = "red";
408-
boolean current = true;
409-
String url = "#Red";
410-
},
411-
new Object() {
412-
String name = "green";
413-
boolean current = false;
414-
String url = "#Green";
415-
},
416-
new Object() {
417-
String name = "blue";
418-
boolean current = false;
419-
String url = "#Blue";
420-
}
421-
);
329+
private static class ComplexObject {
330+
String header = "Colors";
331+
List<Color> item = Arrays.asList(
332+
new Color("red", true, "#Red"),
333+
new Color("green", false, "#Green"),
334+
new Color("blue", false, "#Blue")
335+
);
422336

423-
boolean link(Scope s) {
424-
return !((Boolean) s.get("current"));
425-
}
337+
boolean list() {
338+
return item.size() != 0;
339+
}
426340

427-
boolean list(Scope s) {
428-
return ((List) s.get("item")).size() != 0;
429-
}
341+
boolean empty() {
342+
return item.size() == 0;
343+
}
430344

431-
boolean empty(Scope s) {
432-
return ((List) s.get("item")).size() == 0;
345+
private static class Color {
346+
boolean link() {
347+
return !current;
433348
}
434-
});
435-
MustacheBuilder c = new MustacheBuilder(root);
436-
Mustache m = c.parseFile("complex.html");
437-
StringWriter sw = new StringWriter();
438-
FutureWriter writer = new FutureWriter(sw);
439-
m.execute(writer, scope);
440-
writer.flush();
441-
assertEquals(getContents(root, "complex.txt"), sw.toString());
349+
Color(String name, boolean current, String url) {
350+
this.name = name;
351+
this.current = current;
352+
this.url = url;
353+
}
354+
String name;
355+
boolean current;
356+
String url;
357+
}
442358
}
443359

360+
/*
444361
@SuppressWarnings("serial")
445362
public void testCurrentElementInArray() throws IOException, MustacheException {
446363
@@ -467,40 +384,34 @@ public void testCurrentElementInArray() throws IOException, MustacheException {
467384
assertEquals("\n\n", sw.toString());
468385
469386
}
470-
387+
*/
471388
public void testReadme() throws MustacheException, IOException {
472-
MustacheBuilder c = init();
473-
Mustache m = c.parseFile("items.html");
389+
MustacheCompiler c = init();
390+
Mustache m = c.compile("items.html");
474391
StringWriter sw = new StringWriter();
475-
FutureWriter writer = new FutureWriter(sw);
476392
long start = System.currentTimeMillis();
477-
m.execute(writer, new Scope(new Context()));
478-
writer.flush();
393+
m.execute(sw, new Context());
479394
long diff = System.currentTimeMillis() - start;
480395
assertEquals(getContents(root, "items.txt"), sw.toString());
481396
}
482397

483398
public void testReadme2() throws MustacheException, IOException {
484-
MustacheBuilder c = init();
485-
Mustache m = c.parseFile("items2.html");
399+
MustacheCompiler c = init();
400+
Mustache m = c.compile("items2.html");
486401
StringWriter sw = new StringWriter();
487-
FutureWriter writer = new FutureWriter(sw);
488402
long start = System.currentTimeMillis();
489-
m.execute(writer, new Scope(new Context()));
490-
writer.flush();
403+
m.execute(sw, new Context());
491404
long diff = System.currentTimeMillis() - start;
492405
assertEquals(getContents(root, "items.txt"), sw.toString());
493406
assertTrue("Should be a little bit more than 1 second: " + diff, diff > 999 && diff < 2000);
494407
}
495408

496409
public void testReadme3() throws MustacheException, IOException {
497-
MustacheBuilder c = init();
498-
Mustache m = c.parseFile("items3.html");
410+
MustacheCompiler c = init();
411+
Mustache m = c.compile("items3.html");
499412
StringWriter sw = new StringWriter();
500-
FutureWriter writer = new FutureWriter(sw);
501413
long start = System.currentTimeMillis();
502-
m.execute(writer, new Scope(new Context()));
503-
writer.flush();
414+
m.execute(sw, new Context());
504415
long diff = System.currentTimeMillis() - start;
505416
assertEquals(getContents(root, "items3.txt"), sw.toString());
506417
assertTrue("Should be a little bit more than 1 second: " + diff, diff > 999 && diff < 2000);
@@ -545,7 +456,7 @@ public void run() {
545456
}
546457
}
547458
}
548-
*/
459+
549460
private MustacheCompiler init() {
550461
DefaultCodeFactory cf = new DefaultCodeFactory(root);
551462
return new MustacheCompiler(cf);

0 commit comments

Comments
 (0)