2015-03-23 23:09:56 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2015 Ziver
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* 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 OR COPYRIGHT HOLDERS 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 zutil.parser;
|
|
|
|
|
|
2015-03-25 14:38:49 +00:00
|
|
|
import zutil.log.LogUtil;
|
2015-03-23 23:09:56 +00:00
|
|
|
import zutil.struct.MutableInt;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
2015-03-25 14:38:49 +00:00
|
|
|
import java.util.logging.Logger;
|
2015-03-23 23:09:56 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class for generating dynamic text/code from set data.
|
|
|
|
|
* The syntax is similar to the javascript mustache library.
|
|
|
|
|
*
|
2015-03-24 21:45:23 +00:00
|
|
|
* <br /><br />
|
2015-03-23 23:09:56 +00:00
|
|
|
* Supported tags:
|
2015-03-24 21:45:23 +00:00
|
|
|
* <ul>
|
|
|
|
|
* <li><b> {{key}} </b><br>
|
|
|
|
|
* will be replaced with the string from the key</li>
|
|
|
|
|
* <li><b> {{#key}}...{{/key}} </b><br>
|
|
|
|
|
* will display content between the tags if key is defined</li>
|
|
|
|
|
* </ul>
|
2015-03-23 23:09:56 +00:00
|
|
|
*
|
2015-03-24 21:45:23 +00:00
|
|
|
* @author Ziver koc
|
2015-03-23 23:09:56 +00:00
|
|
|
*/
|
|
|
|
|
public class Templator {
|
2015-03-25 14:38:49 +00:00
|
|
|
private static final Logger log = LogUtil.getLogger();
|
2015-03-23 23:09:56 +00:00
|
|
|
private HashMap<String,Object> data;
|
|
|
|
|
private TemplateEntity tmplRoot;
|
|
|
|
|
|
|
|
|
|
public Templator(String tmpl){
|
|
|
|
|
this.data = new HashMap<String, Object>();
|
|
|
|
|
parseTemplate(tmpl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setData(String key, Object data){
|
|
|
|
|
this.data.put(key, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Will clear all data attributes
|
|
|
|
|
*/
|
|
|
|
|
public void clear(){
|
|
|
|
|
data.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String compile(){
|
|
|
|
|
StringBuilder str = new StringBuilder();
|
|
|
|
|
if(tmplRoot != null)
|
|
|
|
|
tmplRoot.compile(str);
|
|
|
|
|
return str.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Will pare or re-parse the source template.
|
|
|
|
|
*/
|
|
|
|
|
private void parseTemplate(String tmpl){
|
2015-03-25 14:38:49 +00:00
|
|
|
tmplRoot = parseTemplate(new TemplateNode(), tmpl, new MutableInt(), null);
|
2015-03-23 23:09:56 +00:00
|
|
|
}
|
2015-03-25 14:38:49 +00:00
|
|
|
private TemplateNode parseTemplate(TemplateNode root, String tmpl, MutableInt m, String parentTag){
|
2015-03-23 23:09:56 +00:00
|
|
|
StringBuilder data = new StringBuilder();
|
2015-03-25 14:38:49 +00:00
|
|
|
StringBuilder tag = new StringBuilder();
|
2015-03-23 23:09:56 +00:00
|
|
|
boolean tagOpen = false;
|
|
|
|
|
|
|
|
|
|
for(; m.i<tmpl.length(); ++m.i){
|
|
|
|
|
char c = tmpl.charAt(m.i);
|
2015-03-24 13:56:53 +00:00
|
|
|
String d = ""+ c + (m.i+1<tmpl.length() ? tmpl.charAt(m.i+1) : ' ');
|
|
|
|
|
switch( d ){
|
|
|
|
|
case "{{":
|
2015-03-25 14:38:49 +00:00
|
|
|
root.add(new TmplStaticString(data.toString()));
|
2015-03-24 13:56:53 +00:00
|
|
|
data.delete(0, data.length());
|
2015-03-25 14:38:49 +00:00
|
|
|
tag.delete(0, data.length());
|
|
|
|
|
tag.append("{{");
|
2015-03-24 13:56:53 +00:00
|
|
|
tagOpen = true;
|
|
|
|
|
++m.i;
|
2015-03-23 23:09:56 +00:00
|
|
|
break;
|
2015-03-24 13:56:53 +00:00
|
|
|
case "}}":
|
|
|
|
|
if(!tagOpen){ // Tag not opened, incorrect enclosure
|
|
|
|
|
data.append(c);
|
|
|
|
|
continue;
|
2015-03-23 23:09:56 +00:00
|
|
|
}
|
2015-03-24 13:56:53 +00:00
|
|
|
tagOpen = false;
|
|
|
|
|
++m.i;
|
2015-03-24 21:45:23 +00:00
|
|
|
String tagName = data.toString();
|
2015-03-24 13:56:53 +00:00
|
|
|
data.delete(0, data.length());
|
2015-03-25 14:38:49 +00:00
|
|
|
tag.append(tagName).append("}}");
|
2015-03-24 21:45:23 +00:00
|
|
|
switch(tagName.charAt(0)) {
|
|
|
|
|
case '#':
|
2015-03-25 14:38:49 +00:00
|
|
|
++m.i;
|
|
|
|
|
root.add(parseTemplate(new TemplateCondition(tagName),
|
|
|
|
|
tmpl, m, tagName));
|
2015-03-24 21:45:23 +00:00
|
|
|
break;
|
|
|
|
|
case '/':
|
2015-03-25 14:38:49 +00:00
|
|
|
if(parentTag != null && tagName.endsWith(parentTag.substring(1)))
|
|
|
|
|
return root;
|
|
|
|
|
log.severe("Closing non-opened tag: "+tag.toString());
|
|
|
|
|
root.add(new TmplStaticString(tag.toString()));
|
|
|
|
|
break;
|
2015-03-24 21:45:23 +00:00
|
|
|
default:
|
2015-03-25 14:38:49 +00:00
|
|
|
root.add(new TmplDataAttribute(tagName));
|
2015-03-24 21:45:23 +00:00
|
|
|
}
|
2015-03-23 23:09:56 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
2015-03-25 14:38:49 +00:00
|
|
|
if(Character.isWhitespace(c)) // Not a valid tag if it contains whitespace
|
|
|
|
|
tagOpen = false;
|
2015-03-23 23:09:56 +00:00
|
|
|
data.append(c);
|
2015-03-24 13:56:53 +00:00
|
|
|
break;
|
2015-03-23 23:09:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-24 13:56:53 +00:00
|
|
|
if(tagOpen)
|
2015-03-25 14:38:49 +00:00
|
|
|
data.insert(0, tag);
|
2015-03-23 23:09:56 +00:00
|
|
|
if(data.length() > 0)
|
2015-03-25 14:38:49 +00:00
|
|
|
root.add(new TmplStaticString(data.toString()));
|
|
|
|
|
|
|
|
|
|
// If we get to this point means that this node is incorrectly close
|
|
|
|
|
// or this is the end of the file, so we convert it to a normal node
|
|
|
|
|
if(parentTag != null) {
|
|
|
|
|
root = new TemplateNode(root);
|
|
|
|
|
String tagName = "{{"+parentTag+"}}";
|
|
|
|
|
log.severe("Missing closure of tag: "+tagName);
|
|
|
|
|
root.addFirst(new TmplStaticString(tagName));
|
|
|
|
|
}
|
|
|
|
|
return root;
|
2015-03-23 23:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**************************** Template Helper Classes *************************************/
|
|
|
|
|
|
|
|
|
|
protected interface TemplateEntity {
|
|
|
|
|
public void compile(StringBuilder str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected class TemplateNode implements TemplateEntity {
|
|
|
|
|
private List<TemplateEntity> entities;
|
|
|
|
|
|
|
|
|
|
public TemplateNode(){
|
|
|
|
|
this.entities = new ArrayList<TemplateEntity>();
|
|
|
|
|
}
|
2015-03-25 14:38:49 +00:00
|
|
|
public TemplateNode(TemplateNode node){
|
|
|
|
|
this.entities = node.entities;
|
|
|
|
|
}
|
2015-03-23 23:09:56 +00:00
|
|
|
|
2015-03-25 14:38:49 +00:00
|
|
|
public void addFirst(TemplateEntity s){
|
|
|
|
|
entities.add(0, s);
|
|
|
|
|
}
|
|
|
|
|
public void add(TemplateEntity s){
|
2015-03-23 23:09:56 +00:00
|
|
|
entities.add(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void compile(StringBuilder str) {
|
|
|
|
|
for(TemplateEntity sec : entities)
|
|
|
|
|
sec.compile(str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-24 21:45:23 +00:00
|
|
|
protected class TemplateCondition extends TemplateNode {
|
|
|
|
|
private String key;
|
|
|
|
|
|
|
|
|
|
public TemplateCondition(String key){
|
|
|
|
|
this.key = key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void compile(StringBuilder str) {
|
|
|
|
|
if(data.containsKey(key))
|
|
|
|
|
super.compile(str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-23 23:09:56 +00:00
|
|
|
protected class TmplStaticString implements TemplateEntity {
|
|
|
|
|
private String text;
|
|
|
|
|
|
|
|
|
|
public TmplStaticString(String text){
|
|
|
|
|
this.text = text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void compile(StringBuilder str) {
|
|
|
|
|
str.append(text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected class TmplDataAttribute implements TemplateEntity {
|
|
|
|
|
private String key;
|
|
|
|
|
|
|
|
|
|
public TmplDataAttribute(String key){
|
|
|
|
|
this.key = key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void compile(StringBuilder str) {
|
|
|
|
|
if(data.containsKey(key))
|
|
|
|
|
str.append(data.get(key).toString());
|
|
|
|
|
else
|
|
|
|
|
str.append("{{").append(key).append("}}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|