- 浏览: 347494 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
孙大圣123:
大神,你分享的castor dtd文件中没有field-han ...
Castor (二) -- 自定义映射 -
ananeye:
建议举些通俗的例子,说明下。光这样讲,不好懂。
ORACLE 索引原理 -
度_java:
1.不在不同的机器上实现集群功能,除非他们的时钟同步精确到秒。 ...
Quartzs -- Quartz.properties 配置 -
2047699523:
可以参考最新的文档:如何在eclipse jee中检出项目并转 ...
MAVEN3--(三)创建MAVEN项目 -
qgm168:
写的不错,借用啦,哈哈,多谢!
自定义POI EXCEL模板
一,分隔符,注释
- <...>貌似只能在StringTemplateGroup文件中使用
- $...$ 貌似只能在StringTemplate文件和程序中使用
- <! comment !>
- $! comment !$
二,保留字
default first group if |
implements interface last length |
optional rest strip super |
trunc else endif elseif |
三,转义字符
- \$ or \<
- <\ >, <\n>, <\t>, <\r>
四,属性表达式
<attribute> | 如果attribute存在且不为null,输出attribute.toString(),否则输出"" |
<i>, <i0>, <it> |
一般出现在<attribute:template...>的template中 i0和i为迭代次数的计数器,i0从0开始计数,i则从1开始计数, 而it则为当前子元素的值 |
<attribute.property> |
1.如果attribute不存在,输出"" 2.attribute存在,采用attribute.getProperty()/attribute.isProperty()----> attribute.property的优先方式获取property的值value,然后输出value.toString(), 获取value失败则报错 |
<attribute.(expr)> |
同上,但是当属性名有$,<特殊字符时必须使用这种方式 如:$stu.(\"\\$optional\")$ |
<multi-valued-attribute> |
迭代输出集合子元素的toString()值(null忽略),并将它们连接起来 注意:Map只输出key值 |
<multi-valued-attribute; null=expr0,separator=expr1> |
同上,区别在于会用expr0的值取代null,用expr1的值将子元素分割 |
<[mine, yours,...]> |
将mine和yours合并成新的集合,并且mine的元素字yours之前 (子元素类型不一致时需注意) |
<template(argument-list)> |
模板调用,argument-list为参数列表,格式为arg-of-template=expr 其中arg-of-template为template定义中的形参名 expr为实参求值表达式 |
<(expr)(argument-list)> | 同上,区别在于,template的名字为expr的值 |
<attribute:template(argument-list)> |
相当于JAVA中的attribute.template(argument-list), 与<template(argument-list)>类似, 区别:1.template定义中可以通过it引用attribute 2.当attribute为集合时,迭代调用,it引用的为迭代时的子元素 |
<attribute:(expr)(argument-list)> | 同上,区别在于,template的名字为expr的值 |
<attribute:t1(argument-list): ... :tN(argument-list)> |
相当于JAVA中的 var result1 = attribute.t1(...); var result2 = result1.t2(...); ... |
<attribute:{anonymous-template}> |
与<attribute:template(argument-list)>类似 {...}为匿名模板,可以使用it,也可以使用外层st中的attributes |
<attribute:{argument-name_ | _anonymous-template}> |
同上,区别: 向匿名模板中传递了一个名为argument-name的attribute,并且attribute的value与it一致,并且此时it将失效。 |
<a1,a2,...,aN:{argument-list_ | _anonymous-template}> |
同时迭代a1...aN 迭代次数为maxLength(a1...aN) argument-list不能省略,格式为arg1,arg2...argN anonymous-template中不能使用it,通过arg来获取对应的值 |
<attribute:t1(),t2(),...,tN()> |
想当于 for (int index = 0; index < attribute.length; index++) { int templateIndex = (index+1)%N == 0 ? N, (index+1)%N; attribute[index].t[templateIndex](); } |
<first(attr)> |
如果attr为集合,返回它的第一个元素,否则返回它本身 |
<last(attr)> |
如果attr为集合,返回它的最后一个元素,否则返回它本身 |
<rest(attr)> |
如果attr为集合,返回index>=1的所有子元素,否则返回"" |
<trunc(attr)> |
如果attr为集合,返回index<length(attr)-1的所有子元素,否则返回"" |
<strip(attr)> |
如果attr为集合,返回除null之外元素的集合,否则返回attri本身 |
<length(attr)> |
如果attr为集合,返回集合元素数量,否则返回1 |
<\uXXXX> | Unicode字符 |
五,条件语句
<if(attribute)>subtemplate <else>subtemplate2 <endif> |
关于条件的真假判定: 1.attribute存在 2.attribute!=null 3.!(attribute instanceof Boolean) || attribute 仅当上述3条件均为true时,条件判定才为true |
<if(x)>subtemplate <elseif(y)>subtemplate2 <elseif(z)>subtemplate3 <else>subtemplate4 <endif> |
|
<if(!attribute)>subtemplate<endif> |
五,测试代码
Student.java
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的迭代结果只输出value Map<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的StringTemplate stGroup.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的StringTemplate StringTemplate 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
发表评论
-
自定义POI EXCEL模板
2013-07-01 17:51 23091.背景 项目中需要将相关数据导出为EXCEL文件,该文 ... -
POI操作EXCEL
2013-07-01 15:55 9741.简介 Apache POI是Apache软件基金会的开 ... -
JSON-LIB
2013-06-30 15:44 16291.关于JSON JSON(JavaScript Obj ... -
Jasperreport应用小结
2013-06-19 23:15 14241.简介 JasperReport是一 ... -
JAVA实现FTP
2013-06-09 09:05 151001.关于FTP FTP:File Transfe ... -
Digester解析的顺序
2013-03-11 18:07 14461.说明 本文将通过一段代码来说明Digester解析X ... -
ONGL介绍
2012-10-16 17:02 58891.简介 OGNL:Object Graph Navi ... -
MyBatis(一)----基础使用
2012-06-10 22:24 9201.简介 --MyBatis是支持普通SQL查询,存储 ... -
SMOOKS--XSL
2012-06-07 13:33 30851.简介 Smooks是一个用于出力XML和非XML数 ... -
Log4j
2012-05-25 15:28 10771。简介 Log4j是一个应 ... -
commons-logging
2012-05-23 22:25 1054一。简介 commons-logging是Apache下的一 ... -
JFreeChart -- Web应用
2012-01-06 00:26 10171) Model index.jsp < ... -
JUnit -- 分析
2011-12-11 18:08 9901.类结构图 Assert : 大量的ass ... -
Quartzs -- JDBC-JobStore - Oracle 实现 + Cluster
2011-09-13 16:44 4485数据库 从%QUARTZ_HOME%/docs/dbTa ... -
Quartzs -- Quartz.properties 配置
2011-09-13 11:23 46252文件加载位置 默认:优先顺序 Classpath: ... -
StringTemplate学习笔记(四) StringTemplateGroup文件
2011-08-27 10:31 3500一,简介 StringTemplateGroup ... -
StringTemplate学习笔记(三) st文件加载
2011-08-22 22:40 4368一,简介 ST通过StringTemplateGr ... -
StringTemplate学习笔记(一) 简介
2011-08-21 09:47 2601一,简介 StringTemplate(简称ST ... -
Castor (二) -- 自定义映射
2011-08-03 23:33 39031.概述 Castor的自定义映射关系通过XML设置。 ... -
Castor (一) -- 默认绑定
2011-05-26 23:39 1352在MQ的数据传输过程中,往往将JAVA BEAN与XML进行相 ...
相关推荐
NULL 博文链接:https://orange5458.iteye.com/blog/1154339
ANtlr的StringTemplate的PPT,Antlr的网站上也能下载,但是比较慢。所以,放在这里希望能有用:)
StringTemplate.NetStringTemplate.NetStringTemplate.Net
前端开源库-stringtemplate-js用于JavaScript的StringTemplate JS、StringTemplate V4
系列博文 使用Antlr+Stringtemplate生成method chaining,一个不太简单的案例 的源代码。
正文 使用Antlr+Stringtemplate生成method chaining 在这里 http://blog.csdn.net/younggift/article/details/7028932
stringTemplate是一个模板引擎,同时支持java,C#,Python.
antlrcs, ANTLR 3 StringTemplate 3和 StringTemplate 4的C# 端口 ANTLR 3 C# 目标 这里知识库包含 3个主要项目的C# 版本,其中有些项目具有多个生成构件:ANTLR 3Antlr3: ANTLR 3的代码生成器Antlr3.Runtime: ANTLR...
maven-stringtemplate-plugin-1.0.0.jar
StringTemplate的源代码-vs2005版本的源代码文件
ST(StringTemplate)是一个Java模板引擎(带有用于C#,Python和Objective-C的端口),用于生成源代码,网页,电子邮件或任何其他格式的文本输出。 ST特别擅长于多目标代码生成器,多个站点皮肤以及国际化/本地化。...
主要是用于做静态页面的模块设计,很方便做不同的语言版本的网站开发,此上传的只支持C#
ANTLR 3 C#目标 平台建置状态视窗 Linux 该存储库包含3个主要项目的C#版本,其中一些具有多个构建工件: ANTLR 3 :ANTLR 3的代码生成器 :ANTLR 3的运行时库 :ANTLR 3的运行时库调试工具StringTemplate 3 :运行...
StringTemplate一个快速的Rails的模板引擎,使用Ruby的字符串插值语法专注于速度
maven-stringtemplate-plugin-1.0.0-sources.jar
使用 Terence Parr 提供的强大的 StringTemplate V4 模板引擎呈现 Excel 工作簿和工作表的内容(请参阅 http://www.stringtemplate.org/)。 解析器将您的工作簿转换为数据结构,然后传递给模板引擎进行渲染。 您的...
使用Antlr+Stringtemplate生成method chaining,一个不太简单的案例(1) 因为一直上传失败,猜是因为附件太小,所以包含了antlr-3.4-complete-no-antlrv2.jar和antlrworks-1.4.3.jar。对浪费你的带宽抱歉。
df-stringtemplate-runner StringTemplate Runner 是 Actian DataFlow 的运算符,用于运行模板引擎以从数据流管道中的数据生成文本。 配置 在构建 stringtemplate 之前,您需要定义以下环境变量以指向本地 DataFlow...