Skip to content

Commit

Permalink
added loader that works from memory
Browse files Browse the repository at this point in the history
  • Loading branch information
acole76 authored and ebussieres committed Oct 28, 2022
1 parent f6f7c88 commit e631cd1
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package io.pebbletemplates.pebble.loader;

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

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

private String suffix;

private String charset = "UTF-8";

ArrayList<Hashtable<String, String>> fileTable = new ArrayList<>();

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;
}
}
}

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

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 setSuffix(String suffix) {
this.suffix = suffix;
}

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

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

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

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

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

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;
}
}
}
return false;
}
}
25 changes: 19 additions & 6 deletions pebble/src/test/java/io/pebbletemplates/pebble/LoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@
import io.pebbletemplates.pebble.loader.StringLoader;
import io.pebbletemplates.pebble.template.PebbleTemplate;

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

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
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 @@ -108,6 +105,22 @@ void testDelegatingLoader() throws PebbleException, IOException {
assertEquals("LOADER ONE", writer.toString());
}

@Test
void testMemoryLoader() throws PebbleException, IOException, URISyntaxException {
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);

loader.addFile("main", content);
PebbleTemplate template1 = engine.getTemplate("main");
Writer writer1 = new StringWriter();
template1.evaluate(writer1);
assertEquals("SUCCESS", writer1.toString());

}

@Test
void testGetLiteralTemplate() throws IOException {
PebbleEngine engine = new PebbleEngine.Builder().build();
Expand Down

0 comments on commit e631cd1

Please sign in to comment.