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.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:
|
||||
* <ul>
|
||||
* <li><b> {{key}} </b><br>
|
||||
* <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>
|
||||
* </ul>
|
||||
*
|
||||
* 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("}}");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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("<HTML>{{test}}</HTML>");
|
||||
assertEquals("<HTML>{{test}}</HTML>", tmpl.compile());
|
||||
}
|
||||
@Test
|
||||
public void simpleAttributeTest() {
|
||||
Templator tmpl = new Templator("<HTML>{{test}}</HTML>");
|
||||
tmpl.setData("test", "1234");
|
||||
assertEquals("<HTML>1234</HTML>", tmpl.compile());
|
||||
}
|
||||
@Test
|
||||
public void emptyAttributeTest(){
|
||||
Templator tmpl = new Templator("<HTML>{{test}}</HTML>");
|
||||
assertEquals("<HTML>{{test}}</HTML>", tmpl.compile());
|
||||
public void objectAttributeTest(){
|
||||
Templator tmpl = new Templator("<HTML>{{test.attr}}</HTML>");
|
||||
TestClass obj = new TestClass();
|
||||
obj.attr = "1234";
|
||||
tmpl.setData("test", obj);
|
||||
assertEquals("<HTML>1234</HTML>", tmpl.compile());
|
||||
}
|
||||
@Test
|
||||
public void incorrectTagsTest(){
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue