This commit is contained in:
parent
237acd8c09
commit
e730cee928
2 changed files with 55 additions and 7 deletions
|
|
@ -25,9 +25,11 @@ package zutil.parser;
|
||||||
import zutil.log.LogUtil;
|
import zutil.log.LogUtil;
|
||||||
import zutil.struct.MutableInt;
|
import zutil.struct.MutableInt;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -38,11 +40,19 @@ import java.util.logging.Logger;
|
||||||
* Supported tags:
|
* Supported tags:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li><b> {{key}} </b><br>
|
* <li><b> {{key}} </b><br>
|
||||||
|
* <b> {{obj.attr}} </b><br>
|
||||||
* will be replaced with the string from the key</li>
|
* will be replaced with the string from the key</li>
|
||||||
* <li><b> {{#key}}...{{/key}} </b><br>
|
* <li><b> {{#key}}...{{/key}} </b><br>
|
||||||
* will display content between the tags if key is defined</li>
|
* will display content between the tags if key is defined</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
|
* TODO:
|
||||||
|
* * {{! ignore me }}: Comment
|
||||||
|
* * {{^key}}: negative condition
|
||||||
|
* * {{#person}}: support for boolean
|
||||||
|
* * {{#person}}: support for loops
|
||||||
|
* * {{> file}}: include file
|
||||||
|
* * {{=<% %>=}}: change delimiter
|
||||||
* @author Ziver koc
|
* @author Ziver koc
|
||||||
*/
|
*/
|
||||||
public class Templator {
|
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 *************************************/
|
/**************************** Template Helper Classes *************************************/
|
||||||
|
|
||||||
protected interface TemplateEntity {
|
protected interface TemplateEntity {
|
||||||
|
|
@ -199,14 +223,27 @@ public class Templator {
|
||||||
|
|
||||||
protected class TmplDataAttribute implements TemplateEntity {
|
protected class TmplDataAttribute implements TemplateEntity {
|
||||||
private String key;
|
private String key;
|
||||||
|
private String attrib;
|
||||||
|
|
||||||
public TmplDataAttribute(String key){
|
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) {
|
public void compile(StringBuilder str) {
|
||||||
if(data.containsKey(key))
|
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());
|
str.append(data.get(key).toString());
|
||||||
|
}
|
||||||
else
|
else
|
||||||
str.append("{{").append(key).append("}}");
|
str.append("{{").append(key).append("}}");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,15 @@ import static org.junit.Assert.*;
|
||||||
* Created by Ziver on 2015-03-23.
|
* Created by Ziver on 2015-03-23.
|
||||||
*/
|
*/
|
||||||
public class TemplatorTest {
|
public class TemplatorTest {
|
||||||
|
class TestClass{
|
||||||
|
public String attr;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void emptyAttributeTest(){
|
||||||
|
Templator tmpl = new Templator("<HTML>{{test}}</HTML>");
|
||||||
|
assertEquals("<HTML>{{test}}</HTML>", tmpl.compile());
|
||||||
|
}
|
||||||
@Test
|
@Test
|
||||||
public void simpleAttributeTest() {
|
public void simpleAttributeTest() {
|
||||||
Templator tmpl = new Templator("<HTML>{{test}}</HTML>");
|
Templator tmpl = new Templator("<HTML>{{test}}</HTML>");
|
||||||
|
|
@ -39,9 +47,12 @@ public class TemplatorTest {
|
||||||
assertEquals("<HTML>1234</HTML>", tmpl.compile());
|
assertEquals("<HTML>1234</HTML>", tmpl.compile());
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void emptyAttributeTest(){
|
public void objectAttributeTest(){
|
||||||
Templator tmpl = new Templator("<HTML>{{test}}</HTML>");
|
Templator tmpl = new Templator("<HTML>{{test.attr}}</HTML>");
|
||||||
assertEquals("<HTML>{{test}}</HTML>", tmpl.compile());
|
TestClass obj = new TestClass();
|
||||||
|
obj.attr = "1234";
|
||||||
|
tmpl.setData("test", obj);
|
||||||
|
assertEquals("<HTML>1234</HTML>", tmpl.compile());
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void incorrectTagsTest(){
|
public void incorrectTagsTest(){
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue