min-velocity 介绍
min-veLocity 是一个专为代码生成而定制的简化 velocity
模板引擎。
[](https://github.com/pfmiles/min-
veLocity/blob/master/README.md#%E7%9B%AE%E6%A0%87)
目标:
以 veLocity 1.7 为基础, 裁剪出适合用作代码生成的模板引擎
裁剪:
-
没有event机制
-
没有macro
-
没有stop
-
没有evaluate
-
没有define
-
没有break
改动:
-
requires jdk1.5+
-
默认情况下,不打印任何日志
-
default I/O encoding changed to UTF-8(from iso-8859-1)
-
对于#set指令,默认允许设置null值
-
默认打开resource cache
-
去掉了parser pool
-
parse和#include标签支持相对路径
-
新增$ParseUtil.recParsing(“xxx.vm”).addParam(“key”, val)模板调用形式;相当于带调用栈的#parse标签,能用在当你需要每层递归的context都相互隔离的递归#parse的时候;也能支持相对路径
-
可放置min-veLocity.properties文件(可选)在classpath根路径下,用于覆盖veLocity的各种默认属性
-
min-veLocity.properties可使用default.static.util.mappings属性配置默认的静态工具类,这 些工具类将被默认放入模板context中,可配置多个,如:default.static.util.mappings = ClassUtils:org.apache.commons.lang.ClassUtils
-
设置’stream.reference.rendering’开关(true/false),默认关闭; 开启后,遇到reference是stream或reader的时候, 将读取stream或reader中的内容做渲染而非简单地toString渲染; 其中读取stream或reader的buffer可通过’stream.reference.rendering.buffer.size’配置大小 (默认为1024个字符); 亦可通过’stream.reference.rendering.limit’选项设置能够从流中读取的最大字符数限制(默认为100000)
-
新增index.out.of.bounds.exception.suppress选项,当设置为true时,模板中对数组或list进行的取值或设置操作将忽略index out of bounds异常
[](https://github.com/pfmiles/min-velocity#for-english-speakers-see-
below)For English speakers, see below:
-
No event mechanism
-
No macro
-
No ‘#stop’
-
No ‘#evaluate’
-
No ‘#define’
-
No ‘#break’
-
requires jdk1.5+
-
By default no logs rather than log to veLocity.log
-
defaults to use classapth resource loader
-
I/O encoding defaults to UTF-8
-
set directive defaults to allow null value
-
resource cache on by default
-
parser pool removed
-
relative path support for #parse and #include directives
-
$ParseUtil.recParsing(“xxx.vm”).addParam(“key”, val) template parsing util added. You can see it as a ‘#parse’ directive with invocation stack frame,
which Could easily do recursive parsing with isolated context in each round of
recursion. This also supports relative path. -
Could place an optional ‘min-veLocity.properties’ file in classpath root to configure veLocity runtime.
-
min-veLocity Could contain zero or more ‘default.static.util.mappings’ property configs to expose static utility classes in template contexts, for example: default.static.util.mappings = ClassUtils:org.apache.commons.lang.ClassUtils, with this config you can reference to org.apache.commons.lang.ClassUtils class with key ‘ClassUtils’ anywhere.
-
stream/reader reference rendering supported. If you set ‘stream.reference.rendering’(default false) to ‘true’, min-veLocity will dump the contents of a stream/reader reference rather than just invoking ‘toString’ on them while rendering. And the stream/reader reading buffer size Could be specified by configuration ‘stream.reference.rendering.buffer.size’, measured in number of characters(default 1024). And further more, the maximum number of characters read from a stream Could be limited by configuration ‘stream.reference.rendering.limit’(default 100000).
-
String literal templates rendering supported. Just specify template contents in a in-memory-String value to render, other than always specify a template path.
-
When ‘index.out.of.bounds.exception.suppress’ option is setting to be ‘true’,any ‘indexoutofboundsexception’ will be ignored when accessing or setting elements of arrays and lists.
Maven Central
Repo:
<dependency> <groupId>com.github.pfmiles</groupId> <artifactId>min-veLocity</artifactId> <version>1.0</version> </dependency>
代码样例参见单元测试:
package com.github.pfmiles.minveLocity; import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import junit.framework.TestCase; import com.github.pfmiles.org.apache.veLocity.Template; public class TemplateUtilTest extends TestCase { public void testRenderStringTemp() { String templateString = "#foreach($i in $list)\n$i\n#end"; Map<String, Object> ctxPojo = new HashMap<String, Object>(); List<String> list = new ArrayList<String>(); list.add("one"); list.add("two"); list.add("three"); ctxPojo.put("list", list); StringWriter out = new StringWriter(); TemplateUtil.renderString(templateString, ctxPojo, out); // System.out.println(out.toString()); assertTrue("one\ntwo\nthree\n".equals(out.toString())); } public void testRenderTemplate() { Template temp = TemplateUtil.parseStringTemplate("#foreach($i in $list)\n$i\n#end"); Map<String, Object> ctxPojo = new HashMap<String, Object>(); List<String> list = new ArrayList<String>(); list.add("one"); list.add("two"); list.add("three"); ctxPojo.put("list", list); StringWriter out = new StringWriter(); TemplateUtil.renderTemplate(temp, ctxPojo, out); // System.out.println(out.toString()); assertTrue("one\ntwo\nthree\n".equals(out.toString())); } public void testRefRendering() { Template temp = TemplateUtil.parseStringTemplate("hello $ref world"); Map<String, Object> ctxPojo = new HashMap<String, Object>(); StringReader stream = new StringReader("1234567890"); ctxPojo.put("ref", stream); StringWriter writer = new StringWriter(); TemplateUtil.renderTemplate(temp, ctxPojo, writer); assertTrue("hello 1234567890 world".equals(writer.toString())); } }
min-velocity 官网
https://github.com/pfmiles/min-velocity
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。