Added support for boolean and lists
This commit is contained in:
parent
9f5922a0be
commit
51967859b6
2 changed files with 215 additions and 88 deletions
|
|
@ -27,6 +27,7 @@ import zutil.struct.MutableInt;
|
|||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
|
@ -43,18 +44,26 @@ import java.util.logging.Logger;
|
|||
* <b> {{obj.attr}} </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>
|
||||
* <li><b>{{^key}}</b><br>
|
||||
* A negative condition, will display content if key is not set.</li>
|
||||
* <b> {{#obj.attr}}...{{/obj.attr}} </b><br>
|
||||
* Will display content between the tags if:
|
||||
* key is defined,
|
||||
* if the key references a list the content will be iterated
|
||||
* for every element, the element can be referenced by the tag {{.}},
|
||||
* if key is a true boolean (false will not display content).</li>
|
||||
* <li><b> {{^key}}</b><br>
|
||||
* <b> {{^obj.attr}}...{{/obj.attr}} </b><br>
|
||||
* A negative condition, will display content if:
|
||||
* the key is undefined,
|
||||
* the key is a empty list,
|
||||
* the key is a false boolean.</li>
|
||||
* <li><b>{{! ignore me }}</b><br>
|
||||
* Comment, will be ignored.</li>
|
||||
* </ul>
|
||||
*
|
||||
* TODO:
|
||||
* * {{#person}}: support for boolean
|
||||
* * {{#person}}: support for iterators
|
||||
* * {{> file}}: include file
|
||||
* * {{=<% %>=}}: change delimiter
|
||||
* TODO: {{#key}}: support for boolean
|
||||
* TODO: {{> file}}: include file
|
||||
* TODO: {{=<% %>=}}: change delimiter
|
||||
*
|
||||
* @author Ziver koc
|
||||
*/
|
||||
public class Templator {
|
||||
|
|
@ -160,23 +169,6 @@ public class Templator {
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**************************** Utility functions *************************************/
|
||||
public static Object getFieldValue(Object obj, String attrib){
|
||||
try {
|
||||
for (Field field : obj.getClass().getDeclaredFields()) {
|
||||
if(field.getName().equals(attrib)) {
|
||||
field.setAccessible(true);
|
||||
return field.get(obj);
|
||||
}
|
||||
}
|
||||
}catch (IllegalAccessException e){
|
||||
log.log(Level.WARNING, null, e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**************************** Template Helper Classes *************************************/
|
||||
|
||||
protected interface TemplateEntity {
|
||||
|
|
@ -207,28 +199,53 @@ public class Templator {
|
|||
}
|
||||
|
||||
protected class TemplateCondition extends TemplateNode {
|
||||
private String key;
|
||||
private TemplateDataAttribute attrib;
|
||||
|
||||
public TemplateCondition(String key){
|
||||
this.key = key.trim();
|
||||
this.attrib = new TemplateDataAttribute(key);
|
||||
}
|
||||
|
||||
public void compile(StringBuilder str) {
|
||||
if(data.containsKey(key))
|
||||
super.compile(str);
|
||||
Object obj = attrib.getObject();
|
||||
if(obj != null) {
|
||||
if(obj instanceof Boolean){
|
||||
if ((Boolean) obj)
|
||||
super.compile(str);
|
||||
}
|
||||
else if(obj instanceof Iterable){
|
||||
for(Object o : (Iterable)obj){ // Iterate through the whole list
|
||||
set(".", o);
|
||||
super.compile(str);
|
||||
}
|
||||
set(".", null);
|
||||
}
|
||||
else
|
||||
super.compile(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected class TemplateNegativeCondition extends TemplateNode {
|
||||
private String key;
|
||||
private TemplateDataAttribute attrib;
|
||||
|
||||
public TemplateNegativeCondition(String key){
|
||||
this.key = key.trim();
|
||||
this.attrib = new TemplateDataAttribute(key);
|
||||
}
|
||||
|
||||
public void compile(StringBuilder str) {
|
||||
if( ! data.containsKey(key))
|
||||
Object obj = attrib.getObject();
|
||||
if(obj == null)
|
||||
super.compile(str);
|
||||
else {
|
||||
if(obj instanceof Boolean) {
|
||||
if ( ! (Boolean) obj)
|
||||
super.compile(str);
|
||||
}
|
||||
else if(obj instanceof Collection) {
|
||||
if (((Collection) obj).isEmpty())
|
||||
super.compile(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -245,30 +262,54 @@ public class Templator {
|
|||
}
|
||||
|
||||
protected class TemplateDataAttribute implements TemplateEntity {
|
||||
private String tag;
|
||||
private String key;
|
||||
private String attrib;
|
||||
|
||||
public TemplateDataAttribute(String key){
|
||||
String[] s = key.trim().split("\\.", 2);
|
||||
public TemplateDataAttribute(String tag){
|
||||
this.tag = tag;
|
||||
String[] s = tag.trim().split("\\.", 2);
|
||||
this.key = s[0];
|
||||
if(s.length > 1)
|
||||
this.attrib = s[1];
|
||||
}
|
||||
|
||||
public void compile(StringBuilder str) {
|
||||
if (data.containsKey(key)) {
|
||||
|
||||
public Object getObject(){
|
||||
if (data.containsKey(tag))
|
||||
return data.get(tag);
|
||||
else if (data.containsKey(key)) {
|
||||
if (attrib != null) {
|
||||
Object obj = getFieldValue(data.get(key), attrib);
|
||||
if(obj != null)
|
||||
str.append(obj.toString());
|
||||
else
|
||||
str.append("{{").append(key).append(".").append(attrib).append("}}");
|
||||
return obj;
|
||||
}
|
||||
else
|
||||
str.append(data.get(key).toString());
|
||||
return data.get(key);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
protected Object getFieldValue(Object obj, String attrib){
|
||||
try {
|
||||
for (Field field : obj.getClass().getDeclaredFields()) {
|
||||
if(field.getName().equals(attrib)) {
|
||||
field.setAccessible(true);
|
||||
return field.get(obj);
|
||||
}
|
||||
}
|
||||
}catch (IllegalAccessException e){
|
||||
log.log(Level.WARNING, null, e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public void compile(StringBuilder str) {
|
||||
Object obj = getObject();
|
||||
if(obj != null)
|
||||
str.append(obj.toString());
|
||||
else
|
||||
str.append("{{").append(key).append("}}");
|
||||
str.append("{{").append(tag).append("}}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue