Skip to content

Commit

Permalink
remove unnecessary reader() method from TemplateSource, fix #285
Browse files Browse the repository at this point in the history
  • Loading branch information
jknack committed Mar 10, 2014
1 parent 915a99b commit 90f868b
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package com.github.jknack.handlebars.internal;

import java.io.IOException;
import java.io.Reader;

import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.ANTLRInputStream;
Expand Down Expand Up @@ -47,7 +46,6 @@
*/
public class HbsParserFactory implements ParserFactory {


/**
* The logging system.
*/
Expand All @@ -69,55 +67,47 @@ public Parser create(final Handlebars handlebars,

@Override
public Template parse(final TemplateSource source) throws IOException {
Reader reader = null;
try {
logger.debug("About to parse: {}", source);
final ANTLRErrorListener errorReporter = new HbsErrorReporter(source.filename());

reader = source.reader();
// 1. Lexer
final HbsLexer lexer = newLexer(newStream(source.filename(), reader),
startDelimiter, endDelimiter);
configure(lexer, errorReporter);

// 2. Parser
final HbsParser parser = newParser(lexer);
configure(parser, errorReporter);

logger.debug("Building AST");
// 3. Parse
ParseTree tree = parser.template();

// remove unnecessary spaces and new lines?
if (handlebars.prettyPrint()) {
logger.debug("Applying Mustache spec");
new ParseTreeWalker().walk(new MustacheSpec(), tree);
}
logger.debug("About to parse: {}", source);
final ANTLRErrorListener errorReporter = new HbsErrorReporter(source.filename());

// 1. Lexer
final HbsLexer lexer = newLexer(newStream(source.filename(), source.content()),
startDelimiter, endDelimiter);
configure(lexer, errorReporter);

// 2. Parser
final HbsParser parser = newParser(lexer);
configure(parser, errorReporter);

logger.debug("Building AST");
// 3. Parse
ParseTree tree = parser.template();

// remove unnecessary spaces and new lines?
if (handlebars.prettyPrint()) {
logger.debug("Applying Mustache spec");
new ParseTreeWalker().walk(new MustacheSpec(), tree);
}

if (lexer.whiteSpaceControl) {
logger.debug("Applying white spaces control");
new ParseTreeWalker().walk(new WhiteSpaceControl(), tree);
}
if (lexer.whiteSpaceControl) {
logger.debug("Applying white spaces control");
new ParseTreeWalker().walk(new WhiteSpaceControl(), tree);
}

/**
* Build the AST.
*/
TemplateBuilder builder = new TemplateBuilder(handlebars, source) {
@Override
protected void reportError(final CommonToken offendingToken, final int line,
final int column,
final String message) {
errorReporter.syntaxError(parser, offendingToken, line, column, message, null);
}
};
logger.debug("Creating templates");
Template template = builder.visit(tree);
return template;
} finally {
if (reader != null) {
reader.close();
/**
* Build the AST.
*/
TemplateBuilder builder = new TemplateBuilder(handlebars, source) {
@Override
protected void reportError(final CommonToken offendingToken, final int line,
final int column,
final String message) {
errorReporter.syntaxError(parser, offendingToken, line, column, message, null);
}
}
};
logger.debug("Creating templates");
Template template = builder.visit(tree);
return template;
}

};
Expand All @@ -127,13 +117,13 @@ protected void reportError(final CommonToken offendingToken, final int line,
* Creates a new {@link ANTLRInputStream}.
*
* @param filename The file's name.
* @param reader A reader.
* @param content A content.
* @return A new {@link ANTLRInputStream}.
* @throws IOException If the reader can't be open.
*/
private ANTLRInputStream newStream(final String filename, final Reader reader)
private ANTLRInputStream newStream(final String filename, final String content)
throws IOException {
ANTLRInputStream stream = new ANTLRInputStream(reader);
ANTLRInputStream stream = new ANTLRInputStream(content);
stream.name = filename;
return stream;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import static org.apache.commons.lang3.Validate.notNull;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.util.Collections;
import java.util.LinkedList;
Expand Down Expand Up @@ -171,11 +169,6 @@ private static boolean exists(final List<TemplateSource> invocationStack,
*/
private static TemplateSource partial(final TemplateSource source, final String indent) {
return new TemplateSource() {
@Override
public Reader reader() throws IOException {
return new StringReader(content());
}

@Override
public long lastModified() {
return source.lastModified();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static org.apache.commons.lang3.Validate.notNull;

import java.io.IOException;
import java.io.Reader;

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

@Override
public Reader reader() throws IOException {
return source.reader();
}

@Override
public String filename() {
return source.filename();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import static org.apache.commons.lang3.Validate.notNull;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;


/**
Expand Down Expand Up @@ -64,11 +62,6 @@ public String content() throws IOException {
return content;
}

@Override
public Reader reader() throws IOException {
return new StringReader(content);
}

@Override
public String filename() {
return filename;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package com.github.jknack.handlebars.io;

import java.io.IOException;
import java.io.Reader;

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

/**
* The template content as a {@link Reader}. Clients of this method must close the {@link Reader}.
*
* @return The template content as a {@link Reader}.Clients of this method must close the
* {@link Reader}.
* @throws IOException If the template can't read.
*/
Reader reader() throws IOException;

/**
* The file's name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,13 @@ public long lastModified() {
return lastModified;
}

@Override
public Reader reader() throws IOException {
/**
* Open the stream under the URL.
*
* @return A reader.
* @throws IOException If the stream can't be opened.
*/
private Reader reader() throws IOException {
InputStream in = resource.openStream();
return new InputStreamReader(in, "UTF-8");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.Reader;

import org.junit.Test;

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

@Test
public void reader() throws IOException {
Reader reader = createMock(Reader.class);

TemplateSource source = createMock(TemplateSource.class);
expect(source.reader()).andReturn(reader);

replay(source, reader);

assertEquals(reader, new ForwardingTemplateSource(source).reader());

verify(source, reader);
}

@Test
public void filename() throws IOException {
String filename = "filename";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
public class URLTemplateSourceTest {

@Test
public void content() throws IOException {
public void content() throws Exception {
String content = "...";

URLTemplateSource templateSource = PowerMock.createPartialMockForAllMethodsExcept(
URLTemplateSource.class, "content");
expect(templateSource.reader()).andReturn(new StringReader(content));

PowerMock.expectPrivate(templateSource, "reader").andReturn(new StringReader(content));

Object[] mocks = {templateSource };

Expand Down

0 comments on commit 90f868b

Please sign in to comment.