base.template.Template Maven / Gradle / Ivy
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
package base.template;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import org.stringtemplate.v4.AttributeRenderer;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
/**
* A collection of resources used to display a web page.
*
* @see TemplateManager
* @see Resource
*/
public class Template {
private String site;
private String name;
protected Map resources = new Hashtable<>();
protected Collection resourceList = new LinkedList<>();
/** The set of text (not binary) resources */
private Set pageNames = new HashSet<>();
/** The set of text (not binary) resources compiled with String Template */
private STGroup st;
public Template(String site, String name) {
this.site = site;
this.name = name;
this.st = new STGroup('$', '$');
this.st.registerRenderer(String.class, new StringAttributeRenderer());
this.st.registerRenderer(Date.class, new DateAttributeRenderer());
}
public String getSite() {
return site;
}
public String getName() {
return name;
}
public Collection getResources() {
return resourceList;
}
public Resource getResource(String name) {
return resources.get(name);
}
/**
* Associate a resource with this template.
*
* @param name
* @param data
* @throws UnsupportedEncodingException
*/
public void defineResource(String name, byte[] data, boolean binary, boolean builtin) throws UnsupportedEncodingException {
Resource resource = new Resource(name, data, binary, builtin);
resources.put(name, resource);
resourceList = resources.values();
if(!binary) {
definePage(name, data);
}
}
protected void definePage(String name, byte[] content) throws UnsupportedEncodingException {
try {
if(name.contains(".")) {
name = name.substring(0, name.lastIndexOf("."));
}
if(pageNames.contains(name)) {
System.out.println("Overriding template file: " + name);
st.undefineTemplate("/" + name);
st.defineTemplate(name, new String(content, "UTF-8")).hasFormalArgs = false;
} else {
pageNames.add(name);
st.defineTemplate(name, new String(content, "UTF-8")).hasFormalArgs = false;
}
} catch(Exception e) {
//TODO: This is a hack. Must handle properly handle when a template file is malformed.
System.err.println("Issue while processing page: " + name);
}
}
public ST getInstanceOf(String name) {
ST page = st.getInstanceOf(name);
if(page == null) {
System.err.println("Invalid String Template resource: " + name);
}
return page;
}
/**
* Allow any users of the base library to add or ammend String Template renderers
*
* @param name Class, ie String.class
* @param renderer Implementation of the renderer.
*/
public void setRenderer(Class name, AttributeRenderer renderer) {
st.registerRenderer(name, renderer);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy