diff --git a/src/zutil/parser/Templator.java b/src/zutil/parser/Templator.java
index 49ef1d1..fe384cb 100644
--- a/src/zutil/parser/Templator.java
+++ b/src/zutil/parser/Templator.java
@@ -25,9 +25,11 @@ package zutil.parser;
import zutil.log.LogUtil;
import zutil.struct.MutableInt;
+import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
+import java.util.logging.Level;
import java.util.logging.Logger;
/**
@@ -38,11 +40,19 @@ import java.util.logging.Logger;
* Supported tags:
*
* - {{key}}
+ * {{obj.attr}}
* will be replaced with the string from the key
* - {{#key}}...{{/key}}
* will display content between the tags if key is defined
*
*
+ * TODO:
+ * * {{! ignore me }}: Comment
+ * * {{^key}}: negative condition
+ * * {{#person}}: support for boolean
+ * * {{#person}}: support for loops
+ * * {{> file}}: include file
+ * * {{=<% %>=}}: change delimiter
* @author Ziver koc
*/
public class Templator {
@@ -143,6 +153,20 @@ public class Templator {
}
+ 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 {
@@ -199,14 +223,27 @@ public class Templator {
protected class TmplDataAttribute implements TemplateEntity {
private String key;
+ private String attrib;
public TmplDataAttribute(String key){
- this.key = key;
+ String[] s = key.split("\\.", 2);
+ this.key = s[0];
+ if(s.length > 1)
+ this.attrib = s[1];
}
public void compile(StringBuilder str) {
- if(data.containsKey(key))
- str.append(data.get(key).toString());
+ 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("}}");
+ }
+ else
+ str.append(data.get(key).toString());
+ }
else
str.append("{{").append(key).append("}}");
}
diff --git a/src/zutil/test/TemplatorTest.java b/src/zutil/test/TemplatorTest.java
index eb143ff..0c070e8 100644
--- a/src/zutil/test/TemplatorTest.java
+++ b/src/zutil/test/TemplatorTest.java
@@ -31,17 +31,28 @@ import static org.junit.Assert.*;
* Created by Ziver on 2015-03-23.
*/
public class TemplatorTest {
+ class TestClass{
+ public String attr;
+ }
@Test
- public void simpleAttributeTest(){
+ public void emptyAttributeTest(){
+ Templator tmpl = new Templator("{{test}}");
+ assertEquals("{{test}}", tmpl.compile());
+ }
+ @Test
+ public void simpleAttributeTest() {
Templator tmpl = new Templator("{{test}}");
tmpl.setData("test", "1234");
assertEquals("1234", tmpl.compile());
}
@Test
- public void emptyAttributeTest(){
- Templator tmpl = new Templator("{{test}}");
- assertEquals("{{test}}", tmpl.compile());
+ public void objectAttributeTest(){
+ Templator tmpl = new Templator("{{test.attr}}");
+ TestClass obj = new TestClass();
+ obj.attr = "1234";
+ tmpl.setData("test", obj);
+ assertEquals("1234", tmpl.compile());
}
@Test
public void incorrectTagsTest(){