diff --git a/src/zutil/parser/Templator.java b/src/zutil/parser/Templator.java
index fe384cbf..85289dbe 100644
--- a/src/zutil/parser/Templator.java
+++ b/src/zutil/parser/Templator.java
@@ -41,16 +41,18 @@ import java.util.logging.Logger;
*
* - {{key}}
* {{obj.attr}}
- * will be replaced with the string from the key
+ * Will be replaced with the string from the key.
* - {{#key}}...{{/key}}
- * will display content between the tags if key is defined
+ * Will display content between the tags if key is defined.
+ * - {{^key}}
+ * A negative condition, will display content if key is not set.
+ * - {{! ignore me }}
+ * Comment, will be ignored.
*
*
* TODO:
- * * {{! ignore me }}: Comment
- * * {{^key}}: negative condition
* * {{#person}}: support for boolean
- * * {{#person}}: support for loops
+ * * {{#person}}: support for iterators
* * {{> file}}: include file
* * {{=<% %>=}}: change delimiter
* @author Ziver koc
@@ -65,7 +67,7 @@ public class Templator {
parseTemplate(tmpl);
}
- public void setData(String key, Object data){
+ public void set(String key, Object data){
this.data.put(key, data);
}
@@ -98,7 +100,7 @@ public class Templator {
String d = ""+ c + (m.i+1 0) // Still some text left, add to node
- root.add(new TmplStaticString(data.toString()));
+ root.add(new TemplateStaticString(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));
+ log.severe("Missing closure of tag: " + tagName);
+ root.addFirst(new TemplateStaticString(tagName));
}
return root;
}
+
+
+ /**************************** Utility functions *************************************/
public static Object getFieldValue(Object obj, String attrib){
try {
for (Field field : obj.getClass().getDeclaredFields()) {
@@ -200,7 +210,7 @@ public class Templator {
private String key;
public TemplateCondition(String key){
- this.key = key;
+ this.key = key.trim();
}
public void compile(StringBuilder str) {
@@ -209,10 +219,23 @@ public class Templator {
}
}
- protected class TmplStaticString implements TemplateEntity {
+ protected class TemplateNegativeCondition extends TemplateNode {
+ private String key;
+
+ public TemplateNegativeCondition(String key){
+ this.key = key.trim();
+ }
+
+ public void compile(StringBuilder str) {
+ if( ! data.containsKey(key))
+ super.compile(str);
+ }
+ }
+
+ protected class TemplateStaticString implements TemplateEntity {
private String text;
- public TmplStaticString(String text){
+ public TemplateStaticString(String text){
this.text = text;
}
@@ -221,12 +244,12 @@ public class Templator {
}
}
- protected class TmplDataAttribute implements TemplateEntity {
+ protected class TemplateDataAttribute implements TemplateEntity {
private String key;
private String attrib;
- public TmplDataAttribute(String key){
- String[] s = key.split("\\.", 2);
+ public TemplateDataAttribute(String key){
+ String[] s = key.trim().split("\\.", 2);
this.key = s[0];
if(s.length > 1)
this.attrib = s[1];
diff --git a/src/zutil/test/TemplatorTest.java b/src/zutil/test/TemplatorTest.java
index 0c070e89..1fc41213 100644
--- a/src/zutil/test/TemplatorTest.java
+++ b/src/zutil/test/TemplatorTest.java
@@ -43,7 +43,7 @@ public class TemplatorTest {
@Test
public void simpleAttributeTest() {
Templator tmpl = new Templator("{{test}}");
- tmpl.setData("test", "1234");
+ tmpl.set("test", "1234");
assertEquals("1234", tmpl.compile());
}
@Test
@@ -51,7 +51,7 @@ public class TemplatorTest {
Templator tmpl = new Templator("{{test.attr}}");
TestClass obj = new TestClass();
obj.attr = "1234";
- tmpl.setData("test", obj);
+ tmpl.set("test", obj);
assertEquals("1234", tmpl.compile());
}
@Test
@@ -87,9 +87,9 @@ public class TemplatorTest {
public void simpleConditionTest(){
Templator tmpl = new Templator(
"{{#key}}123456789{{/key}}");
- tmpl.setData("key", true);
+ tmpl.set("key", "set");
assertEquals(
- "",
+ "123456789",
tmpl.compile());
}
@Test
@@ -104,4 +104,32 @@ public class TemplatorTest {
new Templator("{{#key}}{{/key}}").compile());
}
+
+ @Test
+ public void emptyNegativeConditionTest(){
+ Templator tmpl = new Templator(
+ "{{^key}}123456789{{/key}}");
+ assertEquals(
+ "123456789",
+ tmpl.compile());
+ }
+ @Test
+ public void setNegativeConditionTest(){
+ Templator tmpl = new Templator(
+ "{{^key}}123456789{{/key}}");
+ tmpl.set("key", "set");
+ assertEquals(
+ "",
+ tmpl.compile());
+ }
+
+
+ @Test
+ public void commentTest(){
+ Templator tmpl = new Templator(
+ "{{! This is a comment}}");
+ assertEquals(
+ "",
+ tmpl.compile());
+ }
}