diff --git a/src/zutil/parser/Templator.java b/src/zutil/parser/Templator.java
index 971453a..e6e0015 100644
--- a/src/zutil/parser/Templator.java
+++ b/src/zutil/parser/Templator.java
@@ -32,11 +32,16 @@ import java.util.List;
* Class for generating dynamic text/code from set data.
* The syntax is similar to the javascript mustache library.
*
+ *
* Supported tags:
- * * {{key}}: will be replaced with the string from the key
+ *
+ * - {{key}}
+ * will be replaced with the string from the key
+ * - {{#key}}...{{/key}}
+ * will display content between the tags if key is defined
+ *
*
- *
- * Created by Ziver on 2015-03-23.
+ * @author Ziver koc
*/
public class Templator {
private HashMap data;
@@ -70,10 +75,10 @@ public class Templator {
*/
private void parseTemplate(String tmpl){
TemplateNode node = new TemplateNode();
- parseTemplate(node, tmpl, new MutableInt());
+ parseTemplate(node, tmpl, new MutableInt(), null);
tmplRoot = node;
}
- private void parseTemplate(TemplateNode root, String tmpl, MutableInt m){
+ private void parseTemplate(TemplateNode root, String tmpl, MutableInt m, String parentTag){
StringBuilder data = new StringBuilder();
StringBuilder tags = new StringBuilder();
boolean tagOpen = false;
@@ -89,7 +94,6 @@ public class Templator {
tags.append("{{");
tagOpen = true;
++m.i;
-
break;
case "}}":
if(!tagOpen){ // Tag not opened, incorrect enclosure
@@ -98,8 +102,20 @@ public class Templator {
}
tagOpen = false;
++m.i;
- root.addEntity(new TmplDataAttribute(data.toString()));
+ String tagName = data.toString();
data.delete(0, data.length());
+ switch(tagName.charAt(0)) {
+ case '#':
+ TemplateCondition conTmpl = new TemplateCondition(tagName);
+ parseTemplate(conTmpl, tmpl, m, tagName.substring(1));
+ root.addEntity(conTmpl);
+ break;
+ case '/':
+ if(tagName.endsWith(parentTag))
+ return;
+ default:
+ root.addEntity(new TmplDataAttribute(tagName));
+ }
break;
default:
data.append(c);
@@ -136,6 +152,19 @@ public class Templator {
}
}
+ 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);
+ }
+ }
+
protected class TmplStaticString implements TemplateEntity {
private String text;
diff --git a/src/zutil/test/JSONSerializerBenchmark.java b/src/zutil/test/JSONSerializerBenchmark.java
index 210b3aa..4885e05 100644
--- a/src/zutil/test/JSONSerializerBenchmark.java
+++ b/src/zutil/test/JSONSerializerBenchmark.java
@@ -25,14 +25,9 @@ package zutil.test;
import com.carrotsearch.junitbenchmarks.BenchmarkRule;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.MethodRule;
-import zutil.io.StringOutputStream;
-import zutil.parser.json.JSONObjectInputStream;
-import zutil.parser.json.JSONObjectOutputStream;
-import zutil.test.JSONSerializerTest.*;
+import zutil.test.JSONSerializerTest.TestClass;
import java.io.*;
-import java.util.Arrays;
import static org.junit.Assert.assertEquals;
diff --git a/src/zutil/test/TemplatorTest.java b/src/zutil/test/TemplatorTest.java
index 6c91c72..eb143ff 100644
--- a/src/zutil/test/TemplatorTest.java
+++ b/src/zutil/test/TemplatorTest.java
@@ -38,13 +38,11 @@ public class TemplatorTest {
tmpl.setData("test", "1234");
assertEquals("1234", tmpl.compile());
}
-
@Test
public void emptyAttributeTest(){
Templator tmpl = new Templator("{{test}}");
assertEquals("{{test}}", tmpl.compile());
}
-
@Test
public void incorrectTagsTest(){
assertEquals("{{",
@@ -64,4 +62,35 @@ public class TemplatorTest {
assertEquals("{test}",
new Templator("{test}").compile());
}
+
+
+ @Test
+ public void emptyConditionTest(){
+ Templator tmpl = new Templator(
+ "{{#key}}123456789{{/key}}");
+ assertEquals(
+ "",
+ tmpl.compile());
+ }
+ @Test
+ public void simpleConditionTest(){
+ Templator tmpl = new Templator(
+ "{{#key}}123456789{{/key}}");
+ tmpl.setData("key", true);
+ assertEquals(
+ "",
+ tmpl.compile());
+ }
+ @Test
+ public void incompleteConditionTest(){
+ assertEquals("{{#key}}",
+ new Templator("{{#key}}").compile());
+ assertEquals("{{/key}}",
+ new Templator("{{/key}}").compile());
+ assertEquals("",
+ new Templator("{{#key}}{{/key}}").compile());
+ assertEquals("",
+ new Templator("{{#key}}{{/key}}").compile());
+ }
+
}