首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

StringTemplate学习札记(二) 语法

2012-07-26 
StringTemplate学习笔记(二) 语法一,分隔符,注释...貌似只能在StringTemplateGroup文件中使用 $...$ 貌

StringTemplate学习笔记(二) 语法

一,分隔符,注释

<...>貌似只能在StringTemplateGroup文件中使用 $...$ 貌似只能在StringTemplate文件和程序中使用??? <! comment !>$! comment !$


二,保留字

?

?

for (int index = 0; index < attribute.length; index++) { int templateIndex = (index+1)%N == 0 ? N, (index+1)%N; attribute[index].t[templateIndex]();}package com.siyuan.st.entity;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class Student {private String name;private String $optional;private List<String> classes = new ArrayList<String>();private Map<String, String> teachers = new HashMap<String, String>();/** * @return the teachers */public Map<String, String> getTeachers() {return teachers;}/** * @param teachers the teachers to set */public void setTeachers(Map<String, String> teachers) {this.teachers = teachers;}/** * @return the $optional */public String get$optional() {return $optional;}/** * @param $optional the $optional to set */public void set$optional(String $optional) {this.$optional = $optional;}/** * @return the classes */public List<String> getClasses() {return classes;}/** * @param classes the classes to set */public void setClasses(List<String> classes) {this.classes = classes;}/** * @return the name */public String getName() {return name;}/** * @param name the name to set */public void setName(String name) {this.name = name;}}

?? STTest.java

package com.siyuan.st.test;import java.util.Arrays;import java.util.HashMap;import java.util.Map;import org.antlr.stringtemplate.StringTemplate;import org.antlr.stringtemplate.StringTemplateGroup;import com.siyuan.st.entity.Student;public class STTest {/** * @param args */public static void main(String[] args) {String simple = "SimpleST $attribute;separator=" "$ \n" +"$first(attribute);separator=" "$ \n" +"$last(attribute);separator=" "$ \n" +"$rest(attribute);separator=" "$ \n" +"$trunc(attribute);separator=" "$ \n" +"$strip(attribute);separator=" "$ \n" +"$length(attribute)$ \n" +"$attr.name$=$attr.value$ \n" +"$[attribute, attr];separator=" "$ \n" +"$\\u2021$";//new SimpltTemplate 对象StringTemplate simpleST = new StringTemplate(simple);//attribute设置simpleST.setAttribute("attribute", null);//注意:不同于MAP,对同一个attribute多次赋值时,不会覆盖,而是将新旧值合并成一个数组,并且会忽略null值simpleST.setAttribute("attribute", "attr1");simpleST.setAttribute("attribute", "attr2");//可以比较两种方式的区别//simpleST.setAttribute("attribute", Arrays.asList(new String[]{null, "attr1", "attr2"}));Map<String, String> attr = new HashMap<String, String>();//可以通过map.key的方式获取map中的值attr.put("name", "attr");attr.put("value", "val");simpleST.setAttribute("attr", attr);//输出System.out.println(simpleST);//综合应用System.out.println("\n---------------------------------------\n");String template = "Student[name=$stu.name$ \n" +"\t \\$optional=$stu.("\\$optional")$ \n" +"\t classes=$stu.classes;null="XXX",separator=" "$ \n" +"\t teachers=$stu.teachers;null="...",separator=","$ ]";StringTemplate st = new StringTemplate(template);Student stu = new Student();stu.setName("student1");stu.set$optional("false");stu.setClasses(Arrays.asList(new String[]{"Math", "English", null}));//注意Map的迭代结果只输出valueMap<String, String> teachers = new HashMap<String, String>();teachers.put("Math", "Mr. M");teachers.put("English", "Miss E");teachers.put(null, null);stu.setTeachers(teachers);st.setAttribute("stu", stu);System.out.println(st);//模板调用 必须在同一个StringTemplateGroup中的StringTemplate才能相互调用System.out.println("\n---------------------------------------\n");//new StringTemplateGroup 对象StringTemplateGroup stGroup = new StringTemplateGroup("test");//在stGroup中定义一个名字为method的StringTemplatestGroup.defineTemplate("method", "\n method invoke $attr$");stGroup.defineTemplate("main", "main $method(attr=attribute)$");stGroup.defineTemplate("element", "\n $i$ $i0$ $it$");//stGroup.defineTemplate("iterator", "Iterator $list:element()$ \n $list:{it1| \n $it1$ }$");//stGroup.defineTemplate("iterator", "Iterator $list:element():method()$");//stGroup.defineTemplate("iterator", "Iterator $list:element(),method()$");stGroup.defineTemplate("iterator", "Iterator $list,list1:{arg1,arg2| \n $arg1$ $arg2$}$");//获得stGroup中名为main的StringTemplateStringTemplate main = stGroup.getInstanceOf("main");main.setAttribute("attribute", "attri");System.out.println(main);StringTemplate iterator = stGroup.getInstanceOf("iterator");iterator.setAttribute("list", Arrays.asList(new String[]{null, "a", "b", "c", "d"}));iterator.setAttribute("list1", Arrays.asList(new String[]{"a1", "b1", "c1"}));System.out.println(iterator);}}

六,参考资料

http://www.antlr.org/wiki/display/ST/StringTemplate+cheat+sheet?

热点排行