Skip to content

Commit 90f868b

Browse files
committed
remove unnecessary reader() method from TemplateSource, fix #285
1 parent 915a99b commit 90f868b

File tree

8 files changed

+51
-100
lines changed

8 files changed

+51
-100
lines changed

handlebars/src/main/java/com/github/jknack/handlebars/internal/HbsParserFactory.java

+41-51
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package com.github.jknack.handlebars.internal;
1919

2020
import java.io.IOException;
21-
import java.io.Reader;
2221

2322
import org.antlr.v4.runtime.ANTLRErrorListener;
2423
import org.antlr.v4.runtime.ANTLRInputStream;
@@ -47,7 +46,6 @@
4746
*/
4847
public class HbsParserFactory implements ParserFactory {
4948

50-
5149
/**
5250
* The logging system.
5351
*/
@@ -69,55 +67,47 @@ public Parser create(final Handlebars handlebars,
6967

7068
@Override
7169
public Template parse(final TemplateSource source) throws IOException {
72-
Reader reader = null;
73-
try {
74-
logger.debug("About to parse: {}", source);
75-
final ANTLRErrorListener errorReporter = new HbsErrorReporter(source.filename());
76-
77-
reader = source.reader();
78-
// 1. Lexer
79-
final HbsLexer lexer = newLexer(newStream(source.filename(), reader),
80-
startDelimiter, endDelimiter);
81-
configure(lexer, errorReporter);
82-
83-
// 2. Parser
84-
final HbsParser parser = newParser(lexer);
85-
configure(parser, errorReporter);
86-
87-
logger.debug("Building AST");
88-
// 3. Parse
89-
ParseTree tree = parser.template();
90-
91-
// remove unnecessary spaces and new lines?
92-
if (handlebars.prettyPrint()) {
93-
logger.debug("Applying Mustache spec");
94-
new ParseTreeWalker().walk(new MustacheSpec(), tree);
95-
}
70+
logger.debug("About to parse: {}", source);
71+
final ANTLRErrorListener errorReporter = new HbsErrorReporter(source.filename());
72+
73+
// 1. Lexer
74+
final HbsLexer lexer = newLexer(newStream(source.filename(), source.content()),
75+
startDelimiter, endDelimiter);
76+
configure(lexer, errorReporter);
77+
78+
// 2. Parser
79+
final HbsParser parser = newParser(lexer);
80+
configure(parser, errorReporter);
81+
82+
logger.debug("Building AST");
83+
// 3. Parse
84+
ParseTree tree = parser.template();
85+
86+
// remove unnecessary spaces and new lines?
87+
if (handlebars.prettyPrint()) {
88+
logger.debug("Applying Mustache spec");
89+
new ParseTreeWalker().walk(new MustacheSpec(), tree);
90+
}
9691

97-
if (lexer.whiteSpaceControl) {
98-
logger.debug("Applying white spaces control");
99-
new ParseTreeWalker().walk(new WhiteSpaceControl(), tree);
100-
}
92+
if (lexer.whiteSpaceControl) {
93+
logger.debug("Applying white spaces control");
94+
new ParseTreeWalker().walk(new WhiteSpaceControl(), tree);
95+
}
10196

102-
/**
103-
* Build the AST.
104-
*/
105-
TemplateBuilder builder = new TemplateBuilder(handlebars, source) {
106-
@Override
107-
protected void reportError(final CommonToken offendingToken, final int line,
108-
final int column,
109-
final String message) {
110-
errorReporter.syntaxError(parser, offendingToken, line, column, message, null);
111-
}
112-
};
113-
logger.debug("Creating templates");
114-
Template template = builder.visit(tree);
115-
return template;
116-
} finally {
117-
if (reader != null) {
118-
reader.close();
97+
/**
98+
* Build the AST.
99+
*/
100+
TemplateBuilder builder = new TemplateBuilder(handlebars, source) {
101+
@Override
102+
protected void reportError(final CommonToken offendingToken, final int line,
103+
final int column,
104+
final String message) {
105+
errorReporter.syntaxError(parser, offendingToken, line, column, message, null);
119106
}
120-
}
107+
};
108+
logger.debug("Creating templates");
109+
Template template = builder.visit(tree);
110+
return template;
121111
}
122112

123113
};
@@ -127,13 +117,13 @@ protected void reportError(final CommonToken offendingToken, final int line,
127117
* Creates a new {@link ANTLRInputStream}.
128118
*
129119
* @param filename The file's name.
130-
* @param reader A reader.
120+
* @param content A content.
131121
* @return A new {@link ANTLRInputStream}.
132122
* @throws IOException If the reader can't be open.
133123
*/
134-
private ANTLRInputStream newStream(final String filename, final Reader reader)
124+
private ANTLRInputStream newStream(final String filename, final String content)
135125
throws IOException {
136-
ANTLRInputStream stream = new ANTLRInputStream(reader);
126+
ANTLRInputStream stream = new ANTLRInputStream(content);
137127
stream.name = filename;
138128
return stream;
139129
}

handlebars/src/main/java/com/github/jknack/handlebars/internal/Partial.java

-7
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import static org.apache.commons.lang3.Validate.notNull;
2323

2424
import java.io.IOException;
25-
import java.io.Reader;
26-
import java.io.StringReader;
2725
import java.io.Writer;
2826
import java.util.Collections;
2927
import java.util.LinkedList;
@@ -171,11 +169,6 @@ private static boolean exists(final List<TemplateSource> invocationStack,
171169
*/
172170
private static TemplateSource partial(final TemplateSource source, final String indent) {
173171
return new TemplateSource() {
174-
@Override
175-
public Reader reader() throws IOException {
176-
return new StringReader(content());
177-
}
178-
179172
@Override
180173
public long lastModified() {
181174
return source.lastModified();

handlebars/src/main/java/com/github/jknack/handlebars/io/ForwardingTemplateSource.java

-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.apache.commons.lang3.Validate.notNull;
2121

2222
import java.io.IOException;
23-
import java.io.Reader;
2423

2524
/**
2625
* A template source which forwards all its method calls to another template source..
@@ -49,11 +48,6 @@ public String content() throws IOException {
4948
return source.content();
5049
}
5150

52-
@Override
53-
public Reader reader() throws IOException {
54-
return source.reader();
55-
}
56-
5751
@Override
5852
public String filename() {
5953
return source.filename();

handlebars/src/main/java/com/github/jknack/handlebars/io/StringTemplateSource.java

-7
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import static org.apache.commons.lang3.Validate.notNull;
2121

2222
import java.io.IOException;
23-
import java.io.Reader;
24-
import java.io.StringReader;
2523

2624

2725
/**
@@ -64,11 +62,6 @@ public String content() throws IOException {
6462
return content;
6563
}
6664

67-
@Override
68-
public Reader reader() throws IOException {
69-
return new StringReader(content);
70-
}
71-
7265
@Override
7366
public String filename() {
7467
return filename;

handlebars/src/main/java/com/github/jknack/handlebars/io/TemplateSource.java

-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package com.github.jknack.handlebars.io;
1919

2020
import java.io.IOException;
21-
import java.io.Reader;
2221

2322
/**
2423
* The template source. Implementation of {@link TemplateSource} must implement
@@ -38,15 +37,6 @@ public interface TemplateSource {
3837
*/
3938
String content() throws IOException;
4039

41-
/**
42-
* The template content as a {@link Reader}. Clients of this method must close the {@link Reader}.
43-
*
44-
* @return The template content as a {@link Reader}.Clients of this method must close the
45-
* {@link Reader}.
46-
* @throws IOException If the template can't read.
47-
*/
48-
Reader reader() throws IOException;
49-
5040
/**
5141
* The file's name.
5242
*

handlebars/src/main/java/com/github/jknack/handlebars/io/URLTemplateSource.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,13 @@ public long lastModified() {
9494
return lastModified;
9595
}
9696

97-
@Override
98-
public Reader reader() throws IOException {
97+
/**
98+
* Open the stream under the URL.
99+
*
100+
* @return A reader.
101+
* @throws IOException If the stream can't be opened.
102+
*/
103+
private Reader reader() throws IOException {
99104
InputStream in = resource.openStream();
100105
return new InputStreamReader(in, "UTF-8");
101106
}

handlebars/src/test/java/com/github/jknack/handlebars/io/ForwardingTemplateSourceTest.java

-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import static org.junit.Assert.assertEquals;
88

99
import java.io.IOException;
10-
import java.io.Reader;
1110

1211
import org.junit.Test;
1312

@@ -25,20 +24,6 @@ public void content() throws IOException {
2524
verify(source);
2625
}
2726

28-
@Test
29-
public void reader() throws IOException {
30-
Reader reader = createMock(Reader.class);
31-
32-
TemplateSource source = createMock(TemplateSource.class);
33-
expect(source.reader()).andReturn(reader);
34-
35-
replay(source, reader);
36-
37-
assertEquals(reader, new ForwardingTemplateSource(source).reader());
38-
39-
verify(source, reader);
40-
}
41-
4227
@Test
4328
public void filename() throws IOException {
4429
String filename = "filename";

handlebars/src/test/java/com/github/jknack/handlebars/io/URLTemplateSourceTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
public class URLTemplateSourceTest {
2323

2424
@Test
25-
public void content() throws IOException {
25+
public void content() throws Exception {
2626
String content = "...";
2727

2828
URLTemplateSource templateSource = PowerMock.createPartialMockForAllMethodsExcept(
2929
URLTemplateSource.class, "content");
30-
expect(templateSource.reader()).andReturn(new StringReader(content));
30+
31+
PowerMock.expectPrivate(templateSource, "reader").andReturn(new StringReader(content));
3132

3233
Object[] mocks = {templateSource };
3334

0 commit comments

Comments
 (0)