Skip to content

Commit

Permalink
Fix multithreading usage of template rendering #31
Browse files Browse the repository at this point in the history
  • Loading branch information
chbloemer committed Dec 16, 2024
1 parent ec25035 commit ff1095a
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 140 deletions.
8 changes: 2 additions & 6 deletions src/main/java/de/neuland/pug4j/Pug4J.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

import de.neuland.pug4j.exceptions.PugCompilerException;
Expand Down Expand Up @@ -42,8 +40,7 @@ public static void render(String filename, Map<String, Object> model, Writer wri
public static void render(String filename, Map<String, Object> model, Writer writer, boolean pretty) throws IOException,
PugCompilerException {
PugTemplate template = getTemplate(filename);
template.setPrettyPrint(pretty);
template.process(new PugModel(model), writer);
render(template,model,writer,pretty);
}

public static String render(PugTemplate template, Map<String, Object> model) throws PugCompilerException {
Expand Down Expand Up @@ -112,10 +109,9 @@ private static PugTemplate getTemplate(Reader reader, String name, String extens
private static PugTemplate createTemplate(String filename, TemplateLoader loader, ExpressionHandler expressionHandler) throws IOException {
Parser parser = new Parser(filename, loader, expressionHandler);
Node root = parser.parse();
PugTemplate template = new PugTemplate();
PugTemplate template = new PugTemplate(root);
template.setExpressionHandler(expressionHandler);
template.setTemplateLoader(loader);
template.setRootNode(root);
return template;
}

Expand Down
6 changes: 1 addition & 5 deletions src/main/java/de/neuland/pug4j/PugConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import de.neuland.pug4j.exceptions.PugCompilerException;
import de.neuland.pug4j.exceptions.PugException;
import de.neuland.pug4j.expression.ExpressionHandler;
import de.neuland.pug4j.expression.GraalJsExpressionHandler;
import de.neuland.pug4j.expression.JexlExpressionHandler;
import de.neuland.pug4j.filter.CDATAFilter;
import de.neuland.pug4j.filter.CssFilter;
Expand Down Expand Up @@ -38,7 +37,6 @@ public class PugConfiguration {
private Map<String, Object> sharedVariables = new HashMap<String, Object>();
private TemplateLoader templateLoader = new FileTemplateLoader();
private ExpressionHandler expressionHandler = new JexlExpressionHandler();
//private ExpressionHandler expressionHandler = new GraalJsExpressionHandler();
protected static final int MAX_ENTRIES = 1000;

public PugConfiguration() {
Expand Down Expand Up @@ -103,14 +101,12 @@ public String renderTemplate(PugTemplate template, Map<String, Object> model) th
}

private PugTemplate createTemplate(String name) throws PugException, IOException {
PugTemplate template = new PugTemplate();
Parser parser = new Parser(name, templateLoader, expressionHandler);
Node root = parser.parse();
PugTemplate template = new PugTemplate(root,getMode());
template.setTemplateLoader(templateLoader);
template.setExpressionHandler(expressionHandler);
template.setRootNode(root);
template.setPrettyPrint(prettyPrint);
template.setMode(getMode());
return template;
}

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/de/neuland/pug4j/PugConfigurationCaffeine.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,13 @@ public String renderTemplate(PugTemplate template, Map<String, Object> model) th
}

private PugTemplate createTemplate(String name) throws PugException, IOException {
PugTemplate template = new PugTemplate();

Parser parser = new Parser(name, templateLoader, expressionHandler);
Node root = parser.parse();
PugTemplate template = new PugTemplate(root,getMode());
template.setTemplateLoader(templateLoader);
template.setExpressionHandler(expressionHandler);
template.setRootNode(root);
template.setPrettyPrint(prettyPrint);
template.setMode(getMode());
return template;
}

Expand Down
19 changes: 11 additions & 8 deletions src/main/java/de/neuland/pug4j/compiler/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
import java.io.Writer;

import de.neuland.pug4j.exceptions.PugCompilerException;
import de.neuland.pug4j.expression.ExpressionHandler;
import de.neuland.pug4j.model.PugModel;
import de.neuland.pug4j.parser.node.Node;
import de.neuland.pug4j.template.PugTemplate;

public class Compiler {

private final Node rootNode;
private boolean prettyPrint;
private PugTemplate template = new PugTemplate();
private PugTemplate template;

public Compiler(PugTemplate pugTemplate) {
this.template = pugTemplate;
}
//@TODO: Deprecated: Remove in 3.0.0
public Compiler(Node rootNode) {
this.rootNode = rootNode;
template = new PugTemplate(rootNode);
}

public String compileToString(PugModel model) throws PugCompilerException {
Expand All @@ -27,14 +28,16 @@ public String compileToString(PugModel model) throws PugCompilerException {

public void compile(PugModel model, Writer w) throws PugCompilerException {
IndentWriter writer = new IndentWriter(w);
writer.setUseIndent(prettyPrint);
rootNode.execute(writer, model, template);
writer.setUseIndent(template.isPrettyPrint());
template.getRootNode().execute(writer, model, template);
}

//@TODO: Deprecated: Remove in 3.0.0
public void setPrettyPrint(boolean prettyPrint) {
this.prettyPrint = prettyPrint;
this.template.setPrettyPrint(prettyPrint);
}

//@TODO: Deprecated: Remove in 3.0.0
public void setTemplate(PugTemplate pugTemplate) {
this.template = pugTemplate;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/de/neuland/pug4j/lexer/token/Doctypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ public class Doctypes {
private static Map<String, String> doctypes = new HashMap<String, String>();

static {
doctypes.put("default", "<!DOCTYPE html>");
doctypes.put("default", "<!DOCTYPE html>"); // Fallback
doctypes.put("html", "<!DOCTYPE html>");
doctypes.put("xml", "<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
doctypes.put("transitional", "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
doctypes.put("strict", "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
doctypes.put("frameset", "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">");
doctypes.put("1.1", "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
doctypes.put("basic", "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML Basic 1.1//EN\" \"http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd\">");
doctypes.put("mobile", "<!DOCTYPE html PUBLIC \"-//WAPFORUM//DTD XHTML Mobile 1.2//EN\" \"http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd\">");
doctypes.put("plist", "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
}

public static String get(String pugDoctype) {
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/de/neuland/pug4j/parser/node/DoctypeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

import de.neuland.pug4j.compiler.IndentWriter;
import de.neuland.pug4j.exceptions.PugCompilerException;
import de.neuland.pug4j.lexer.token.Doctypes;
import de.neuland.pug4j.model.PugModel;
import de.neuland.pug4j.template.PugTemplate;
import org.apache.commons.lang3.StringUtils;

public class DoctypeNode extends Node {
@Override
public void execute(IndentWriter writer, PugModel model, PugTemplate template) throws PugCompilerException {
String name = getValue();
template.setDoctype(name);
writer.append(template.getDoctypeLine());
if (name == null || StringUtils.isBlank(name)) {
name = "html";
}
String doctypeLine = Doctypes.get(name);
if (doctypeLine == null) {
doctypeLine = "<!DOCTYPE " + name + ">";
}
writer.append(doctypeLine);
writer.setCompiledDoctype(true);
}

}
54 changes: 24 additions & 30 deletions src/main/java/de/neuland/pug4j/template/PugTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import de.neuland.pug4j.compiler.Compiler;
import de.neuland.pug4j.exceptions.PugCompilerException;
import de.neuland.pug4j.expression.ExpressionHandler;
import de.neuland.pug4j.lexer.token.Doctypes;
import de.neuland.pug4j.model.PugModel;
import de.neuland.pug4j.parser.node.DoctypeNode;
import de.neuland.pug4j.parser.node.Node;
import org.apache.commons.lang3.StringUtils;

public class PugTemplate {

Expand All @@ -19,12 +18,21 @@ public class PugTemplate {
private boolean xml = false;
private TemplateLoader templateLoader;
private ExpressionHandler expressionHandler;
private String doctypeLine;

public PugTemplate() {
}

public PugTemplate(final Node rootNode) {
setRootNode(rootNode);
}

public PugTemplate(final Node rootNode, final Mode mode) {
setMode(mode);
setRootNode(rootNode);
}

public void process(PugModel model, Writer writer) throws PugCompilerException {
Compiler compiler = new Compiler(rootNode);
compiler.setPrettyPrint(prettyPrint);
compiler.setTemplate(this);
Compiler compiler = new Compiler(this);
compiler.compile(model, writer);
}

Expand All @@ -40,7 +48,13 @@ public Node getRootNode() {
return rootNode;
}

//@TODO: Deprecated: Remove in 3.0.0
public void setRootNode(Node rootNode) {
final Node peek = rootNode.getNodes().peek();
if(peek instanceof DoctypeNode){
DoctypeNode doctypeNode = (DoctypeNode) peek;
setDoctype(doctypeNode.getValue());
}
this.rootNode = rootNode;
}

Expand All @@ -61,33 +75,13 @@ public TemplateLoader getTemplateLoader() {
}

public void setDoctype(String name){
if (name == null || StringUtils.isBlank(name)) {
name = "default";
}
doctypeLine = Doctypes.get(name);
if (doctypeLine == null) {
doctypeLine = "<!DOCTYPE " + name + ">";
}

this.terse = "<!doctype html>".equals(this.doctypeLine.toLowerCase());
this.xml = doctypeLine.startsWith("<?xml");
this.terse = "html".equals(name);
this.xml = "xml".equals(name);
}

public String getDoctypeLine() {
return doctypeLine;
}

//@TODO: Deprecated: Remove in 3.0.0, use constructor
public void setMode(Mode mode) {
xml = false;
terse = false;
switch (mode) {
case HTML:
terse = true;
break;
case XML:
xml = true;
break;
}
setDoctype(mode.name().toLowerCase());
}

public void setExpressionHandler(ExpressionHandler expressionHandler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;

import de.neuland.pug4j.expression.JexlExpressionHandler;
import de.neuland.pug4j.template.PugTemplate;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

Expand Down Expand Up @@ -46,8 +47,9 @@ private void run(String testName, boolean pretty, PugModel model) throws IOExcep
"jade");
parser = new Parser(testName, loader, new JexlExpressionHandler());
Node root = parser.parse();
Compiler compiler = new Compiler(root);
compiler.setPrettyPrint(pretty);
PugTemplate pugTemplate = new PugTemplate(root);
pugTemplate.setPrettyPrint(pretty);
Compiler compiler = new Compiler(pugTemplate);
String expected = readFile(testName + ".html");
model.addFilter("markdown", new MarkdownFilter());
model.addFilter("plain", new PlainFilter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;

import de.neuland.pug4j.expression.JexlExpressionHandler;
import de.neuland.pug4j.template.PugTemplate;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

Expand Down Expand Up @@ -46,8 +47,8 @@ private void run(String testName, boolean pretty, PugModel model) throws IOExcep
"jade");
parser = new Parser(testName, loader, new JexlExpressionHandler());
Node root = parser.parse();
Compiler compiler = new Compiler(root);
compiler.setPrettyPrint(pretty);
PugTemplate pugTemplate = new PugTemplate(root);
Compiler compiler = new Compiler(pugTemplate);
String expected = readFile(testName + ".html");
model.addFilter("markdown", new MarkdownFilter());
model.addFilter("plain", new PlainFilter());
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/de/neuland/pug4j/compiler/CompilerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,11 @@ private void run(String testName, boolean pretty, PugModel model) {


Node root = parser.parse();
Compiler compiler = new Compiler(root);
PugTemplate pugTemplate = new PugTemplate();
PugTemplate pugTemplate = new PugTemplate(root);
pugTemplate.setPrettyPrint(pretty);
pugTemplate.setTemplateLoader(loader);
pugTemplate.setExpressionHandler(expressionHandler);
compiler.setTemplate(pugTemplate);
compiler.setPrettyPrint(pretty);
Compiler compiler = new Compiler(pugTemplate);

String expected = readFile(testName + expectedFileNameExtension);
model.addFilter("markdown", new MarkdownFilter());
Expand Down
Loading

0 comments on commit ff1095a

Please sign in to comment.