Skip to content

Commit

Permalink
Refactor MemoryLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
ebussieres committed Oct 28, 2022
1 parent e631cd1 commit 2f083f7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,95 +1,82 @@
package io.pebbletemplates.pebble.loader;

import io.pebbletemplates.pebble.error.LoaderException;

import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Hashtable;
import com.mitchellbosecke.pebble.error.LoaderException;
import java.util.List;

public class MemoryLoader implements Loader<String> {
private String prefix;

private String suffix;

private String charset = "UTF-8";

ArrayList<Hashtable<String, String>> fileTable = new ArrayList<>();
private final List<TemplateDefinition> templateDefinitions = new ArrayList<>();

public Reader getReader(String templateName)
{
@Override
public Reader getReader(String templateName) {
String content = "";
for (Hashtable<String, String> hashtable : fileTable)
{
if(hashtable.containsKey("templateName"))
{
if(hashtable.get("templateName").equals(templateName))
{
content = hashtable.get("content");
break;
}
for (TemplateDefinition templateDefinition : this.templateDefinitions) {
if (templateDefinition.templateName.equals(templateName)) {
content = templateDefinition.content;
break;
}
}

if(content.isEmpty())
{
throw new LoaderException(null, "Could not find template \"" + templateName + "\""); //XSS?
if (content.isEmpty()) {
throw new LoaderException(null, "Could not find template \"" + templateName + "\"");
}

return new StringReader(content);
}

public void addFile(String templateName, String content)
{
Hashtable<String, String> table = new Hashtable<>();
table.put("templateName", templateName);
table.put("content", content);
fileTable.add(table);
}

public String getSuffix() {
return this.suffix;
public void addTemplate(String templateName, String content) {
if (templateName == null) {
throw new IllegalArgumentException("templateName cannot be null");
}
if (content == null) {
throw new IllegalArgumentException("content cannot be null");
}
this.templateDefinitions.add(new TemplateDefinition(templateName, content));
}

@Override
public void setSuffix(String suffix) {
this.suffix = suffix;
}

public String getPrefix() {
return this.prefix;
}

@Override
public void setPrefix(String prefix) {
this.prefix = prefix;
}

public String getCharset() {
return this.charset;
}

@Override
public void setCharset(String charset) {
this.charset = charset;
}

@Override
public String resolveRelativePath(String relativePath, String anchorPath) {
return relativePath; // hierarchy is flat
}

@Override
public String createCacheKey(String templateName) {
return templateName;
}

public boolean resourceExists(String templateName)
{
for (Hashtable<String, String> hashtable : fileTable)
{
if(hashtable.containsKey("templateName"))
{
if(hashtable.get("templateName").equals(templateName))
{
return true;
}
@Override
public boolean resourceExists(String templateName) {
for (TemplateDefinition templateDefinition : this.templateDefinitions) {
if (templateDefinition.templateName.equals(templateName)) {
return true;
}
}
return false;
}

private static class TemplateDefinition {
public final String templateName;
public final String content;

public TemplateDefinition(String templateName,
String content) {
this.templateName = templateName;
this.content = content;
}
}
}
43 changes: 25 additions & 18 deletions pebble/src/test/java/io/pebbletemplates/pebble/LoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,14 @@

import io.pebbletemplates.pebble.error.LoaderException;
import io.pebbletemplates.pebble.error.PebbleException;
import io.pebbletemplates.pebble.loader.ClasspathLoader;
import io.pebbletemplates.pebble.loader.DelegatingLoader;
import io.pebbletemplates.pebble.loader.FileLoader;
import io.pebbletemplates.pebble.loader.Loader;
import io.pebbletemplates.pebble.loader.StringLoader;
import io.pebbletemplates.pebble.loader.*;
import io.pebbletemplates.pebble.template.PebbleTemplate;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -106,19 +99,33 @@ void testDelegatingLoader() throws PebbleException, IOException {
}

@Test
void testMemoryLoader() throws PebbleException, IOException, URISyntaxException {
void testMemoryLoader() throws PebbleException, IOException {
MemoryLoader loader = new MemoryLoader();
loader.setSuffix(".suffix");
PebbleEngine engine = new PebbleEngine.Builder().loader(loader).strictVariables(false).build();
URL url = this.getClass().getResource("/templates/template.loaderTest.peb");
String content = FileUtils.readFileToString(new File(url.toURI()), StandardCharsets.UTF_8);
PebbleEngine pebble = new PebbleEngine.Builder().loader(loader).strictVariables(false).build();

loader.addFile("main", content);
PebbleTemplate template1 = engine.getTemplate("main");
Writer writer1 = new StringWriter();
template1.evaluate(writer1);
assertEquals("SUCCESS", writer1.toString());
loader.addTemplate("home.html", "{% extends \"layout.html\" %}{% block title %} Home {% endblock %}"
+ "{% block content %}"
+ "<h1> Home </h1>"
+ "<p> Welcome to my home page. My name is {{ name }}.</p>"
+ "{% endblock %}");
loader.addTemplate("layout.html", "<html>"
+ "<head>"
+ "<title>Hello Pebble</title>"
+ "</head>"
+ "<body>"
+ "{% block content %}{% endblock %}"
+ "</body>"
+ "</html>");

PebbleTemplate template = pebble.getTemplate("home.html");

Map<String, Object> context = new HashMap<>();
context.put("name", "Bob");

Writer writer = new StringWriter();
template.evaluate(writer, context);

assertEquals("<html><head><title>Hello Pebble</title></head><body><h1> Home </h1><p> Welcome to my home page. My name is Bob.</p></body></html>", writer.toString());
}

@Test
Expand Down

0 comments on commit 2f083f7

Please sign in to comment.