package com.binarythought.picotemplate; import java.util.ArrayList; import java.util.List; import java.util.Stack; import java.util.regex.Pattern; import java.util.regex.Matcher; import java.io.FileReader; import java.io.File; /** * Template is the base class for picotemplate. It it used to generate * html or text from a supplied template and dictionary. *
Basic usage: *
Template template = new Template("I like {{FOOD}}.");
*
* TemplateDictionary dictionary = new TemplateDictionary();
* dictionary.put("food", "cookies");
* String result = template.parse(dictionary);
Value of result : This method is threadsafe.
* @param dict Template dictionary to parse against.
* @return String containing the parsed result of the template.
*/
public String parse(TemplateDictionary dict)
{
StringBuilder b = new StringBuilder();
if(dict != null){ parseTemplate(dict, b, parsedTemplate, 0); }
else { parseTemplate(b, parsedTemplate); }
return b.toString();
}
private static int parseTemplate(StringBuilder output, TemplateNode template[])
{
for(int i = 0; i < template.length; i++){
if(template[i].getNodeType() == TemplateNode.PLAIN){
output.append(template[i].getContent());
}
}
return 0;
}
private static int parseTemplate(TemplateDictionary dict, StringBuilder output, TemplateNode template[], int i)
{
for(;i < template.length; i++){
switch(template[i].getNodeType()){
case TemplateNode.PLAIN:
output.append(template[i].getContent());
break;
case TemplateNode.VARIABLE:
output.append(dict.get(template[i].getContent()));
break;
case TemplateNode.SECTION_START:
if(dict.isShown(template[i].getContent())){
ListI like cookies.
*/
public class Template {
private static final Pattern pattern = Pattern.compile(
"\\{\\{(#|/){0,1}(\\w+)\\}\\}", Pattern.CANON_EQ
);
private TemplateNode parsedTemplate[];
private String originalTemplate;
/**
* Instantiate and compile a new template with the template provided.
* @param template String containing the template.
*/
public Template(String template) throws Exception
{
this.originalTemplate = template;
this.parsedTemplate = initTemplate(template);
}
/**
* Instantiate and compile a new template with the template provided.
* @param file File containing the template.
*/
public Template(File file) throws Exception
{
if(file == null || !file.canRead()){
throw new Exception("Cannot read "+file.getName());
}
FileReader reader = new FileReader(file);
StringBuilder b = new StringBuilder();
char buf[] = new char[1024];
for(int i=0; (i = reader.read(buf)) != -1;){
b.append(buf, 0, i);
}
reader.close();
this.originalTemplate = b.toString();
this.parsedTemplate = initTemplate(this.originalTemplate);
}
private static TemplateNode[] initTemplate(String template) throws Exception
{
ArrayList