Skip to content

Commit

Permalink
Removed another dependency on apaches IOUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
mbosecke committed Jan 19, 2014
1 parent 64e847b commit 6fe8457
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 29 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,6 @@
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/com/mitchellbosecke/pebble/PebbleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Semaphore;

import org.apache.commons.io.IOUtils;

import com.mitchellbosecke.pebble.cache.DefaultTemplateLoadingCache;
import com.mitchellbosecke.pebble.cache.TemplateLoadingCache;
import com.mitchellbosecke.pebble.compiler.Compiler;
Expand Down Expand Up @@ -50,6 +48,7 @@
import com.mitchellbosecke.pebble.tokenParser.TokenParser;
import com.mitchellbosecke.pebble.tokenParser.TokenParserBroker;
import com.mitchellbosecke.pebble.tokenParser.TokenParserBrokerImpl;
import com.mitchellbosecke.pebble.utils.IOUtils;

public class PebbleEngine {

Expand Down Expand Up @@ -153,18 +152,25 @@ public PebbleTemplate call() throws PebbleException, InterruptedException {

// load it
Reader templateReader = loader.getReader(templateName);
String templateSource = "";

/*
* load template into a String.
*
* TODO: Pass the reader to the Lexer and just let the lexer
* iterate through the characters without having to use an
* intermediary string.
*/
String templateSource = null;
try {
templateSource = IOUtils.toString(templateReader);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new LoaderException("Could not load template");
}

TokenStream tokenStream = getLexer().tokenize(templateSource, templateName);
NodeRoot root = getParser().parse(tokenStream);
String javaSource = getCompiler().compile(root).getSource();
instance = getCompiler().instantiateTemplate(javaSource, className, templateSource);
instance = getCompiler().instantiateTemplate(javaSource, className);

// we are now done with the non-thread-safe objects, so release
// the compilation mutex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public interface Compiler {
* @return The final PebbleTemplate instance
* @throws PebbleException
*/
PebbleTemplate instantiateTemplate(String javaSource, String className, String templateSource)
PebbleTemplate instantiateTemplate(String javaSource, String className)
throws PebbleException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ public PebbleEngine getEngine() {
}

@Override
public PebbleTemplate instantiateTemplate(String javaSource, String className, String templateSource)
throws PebbleException {
public PebbleTemplate instantiateTemplate(String javaSource, String className) throws PebbleException {

String fullClassName = PebbleTemplate.COMPILED_PACKAGE_NAME + "." + className;

Expand Down Expand Up @@ -216,14 +215,15 @@ public PebbleTemplate instantiateTemplate(String javaSource, String className, S
try {

ClassLoader cl = fileManager.getClassLoader(null);
Constructor<?> constructor = cl.loadClass(fullClassName).getDeclaredConstructor(String.class, String.class,
Constructor<?> constructor = cl.loadClass(fullClassName).getDeclaredConstructor(String.class,
PebbleEngine.class);

// constructor.setAccessible(true);
template = (PebbleTemplate) constructor.newInstance(javaSource, templateSource, engine);
constructor.setAccessible(true);
template = (PebbleTemplate) constructor.newInstance(javaSource, engine);

} catch (IllegalAccessException | NoSuchMethodException | SecurityException | InstantiationException
| InvocationTargetException | IllegalArgumentException e) {
e.printStackTrace();
throw new PebbleException("Compilation error occurred");
} catch (ClassNotFoundException e) {
throw new PebbleException(String.format("Could not find generated class: %s", fullClassName));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/mitchellbosecke/pebble/node/NodeRoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ private void compileClassHeader(Compiler compiler, String className) {
}

private void compileConstructor(Compiler compiler, String className) {
compiler.newline(2).write("public ").raw(className).raw(" (String javaCode, String source, ")
compiler.newline(2).write("public ").raw(className).raw(" (String javaCode, ")
.raw(PebbleEngine.class.getName()).raw(" engine) {").newline();

compiler.indent().write("super(javaCode, source, engine);").newline();
compiler.indent().write("super(javaCode, engine);").newline();

compiler.outdent().write("}").newline(2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public abstract class PebbleTemplate {
public final static String COMPILED_PACKAGE_NAME = "com.mitchellbosecke.pebble.template.compiled";

private final String generatedJavaCode;
private final String source;

protected final PebbleEngine engine;

Expand All @@ -50,9 +49,8 @@ public abstract class PebbleTemplate {
private final Map<String, Block> blocks = new HashMap<>();
private final Map<String, Map<Integer, Macro>> macros = new HashMap<>();

public PebbleTemplate(String generatedJavaCode, String source, PebbleEngine engine) {
public PebbleTemplate(String generatedJavaCode, PebbleEngine engine) {
this.generatedJavaCode = generatedJavaCode;
this.source = source;
this.engine = engine;
}

Expand Down Expand Up @@ -242,7 +240,7 @@ protected Object applyFilter(String filterName, Context context, Object... args)
Filter filter = filters.get(filterName);

if (filter instanceof LocaleAware) {
((LocaleAware) filter).setLocale((Locale)context.get(Context.GLOBAL_VARIABLE_LOCALE));
((LocaleAware) filter).setLocale((Locale) context.get(Context.GLOBAL_VARIABLE_LOCALE));
}

if (filter == null) {
Expand All @@ -266,7 +264,7 @@ private Object applyFunction(SimpleFunction function, Context context, Object...
Collections.addAll(arguments, args);

if (function instanceof LocaleAware) {
((LocaleAware) function).setLocale((Locale)context.get(Context.GLOBAL_VARIABLE_LOCALE));
((LocaleAware) function).setLocale((Locale) context.get(Context.GLOBAL_VARIABLE_LOCALE));
}
return function.execute(arguments);
}
Expand Down Expand Up @@ -305,10 +303,6 @@ public String getGeneratedJavaCode() {
return generatedJavaCode;
}

public String getSource() {
return this.source;
}

public PebbleTemplate getParent() {
return parent;
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/mitchellbosecke/pebble/utils/IOUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mitchellbosecke.pebble.utils;

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

public class IOUtils {

public static String toString(Reader reader) throws IOException {
StringWriter writer = new StringWriter();

char[] buffer = new char[1024 * 4];
int n = 0;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
return writer.toString();
}

}

0 comments on commit 6fe8457

Please sign in to comment.