-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e631cd1
commit 2f083f7
Showing
2 changed files
with
66 additions
and
72 deletions.
There are no files selected for viewing
95 changes: 41 additions & 54 deletions
95
pebble/src/main/java/io/pebbletemplates/pebble/loader/MemoryLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters