min-velocity 简化 velocity ​模板引擎开源项目

我要开发同款
匿名用户2014年09月29日
109阅读

技术信息

开源地址
https://github.com/pfmiles/min-velocity
授权协议
Apache

作品详情

mi-velocity是一个专为代码生成而定制的简化velocity模板引擎。

目标:

以velocity1.7为基础,裁剪出适合用作代码生成的模板引擎

裁剪:

没有evet机制

没有macro

没有stop

没有evaluate

没有defie

没有break

改动:

requiresjdk1.5+

默认情况下,不打印任何日志

默认采用classpath模板加载器而非文件系统模板加载器

defaultI/OecodigchagedtoUTF-8(fromiso-8859-1)

对于#set指令,默认允许设置ull值

默认打开resourcecache

去掉了parserpool

#parse和#iclude标签支持相对路径

新增$ParseUtil.recParsig("xxx.vm").addParam("key",val)模板调用形式;相当于带调用栈的#parse标签,能用在当你需要每层递归的cotext都相互隔离的递归#parse的时候;也能支持相对路径

可放置mi-velocity.properties文件(可选)在classpath根路径下,用于覆盖velocity的各种默认属性

mi-velocity.properties可使用default.static.util.mappigs属性配置默认的静态工具类,这些工具类将被默认放入模板cotext中,可配置多个,如:default.static.util.mappigs=ClassUtils:org.apache.commos.lag.ClassUtils

设置'stream.referece.rederig'开关(true/false),默认关闭;开启后,遇到referece是stream或reader的时候,将读取stream或reader中的内容做渲染而非简单地toStrig渲染;其中读取stream或reader的buffer可通过'stream.referece.rederig.buffer.size'配置大小(默认为1024个字符);亦可通过'stream.referece.rederig.limit'选项设置能够从流中读取的最大字符数限制(默认为100000)

支持Strig模板渲染,即直接将模板内容以Strig形式传入api进行渲染而不是只能选择传入一个模板路径

新增idex.out.of.bouds.exceptio.suppress选项,当设置为true时,模板中对数组或list进行的取值或设置操作将忽略idexoutofbouds异常

ForEglishspeakers,seebelow:

Noevetmechaism

Nomacro

No'#stop'

No'#evaluate'

No'#defie'

No'#break'

requiresjdk1.5+

Bydefaultologsratherthalogtovelocity.log

defaultstouseclassapthresourceloader

I/OecodigdefaultstoUTF-8

#setdirectivedefaultstoallowullvalue

resourcecacheobydefault

parserpoolremoved

relativepathsupportfor#parsead#icludedirectives

$ParseUtil.recParsig("xxx.vm").addParam("key",val)templateparsigutiladded.Youcaseeitasa'#parse'directivewithivocatiostackframe,whichcouldeasilydorecursiveparsigwithisolatedcotextieachroudofrecursio.Thisalsosupportsrelativepath.

couldplaceaoptioal'mi-velocity.properties'fileiclasspathroottocofigurevelocityrutime.

mi-velocitycouldcotaizeroormore'default.static.util.mappigs'propertycofigstoexposestaticutilityclassesitemplatecotexts,forexample:default.static.util.mappigs=ClassUtils:org.apache.commos.lag.ClassUtils,withthiscofigyoucareferecetoorg.apache.commos.lag.ClassUtilsclasswithkey'ClassUtils'aywhere.

stream/readerreferecerederigsupported.Ifyouset'stream.referece.rederig'(defaultfalse)to'true',mi-velocitywilldumpthecotetsofastream/readerrefereceratherthajustivokig'toStrig'othemwhilerederig.Adthestream/readerreadigbuffersizecouldbespecifiedbycofiguratio'stream.referece.rederig.buffer.size',measurediumberofcharacters(default1024).Adfurthermore,themaximumumberofcharactersreadfromastreamcouldbelimitedbycofiguratio'stream.referece.rederig.limit'(default100000).

Strigliteraltemplatesrederigsupported.Justspecifytemplatecotetsiai-memory-Strigvaluetoreder,otherthaalwaysspecifyatemplatepath.

Whe'idex.out.of.bouds.exceptio.suppress'optioissettigtobe'true',ay'IdexOutOfBoudsExceptio'willbeigoredwheaccessigorsettigelemetsofarraysadlists.

MaveCetralRepo:<depedecy>    <groupId>com.github.pfmiles</groupId>    <artifactId>mi-velocity</artifactId>    <versio>1.0</versio></depedecy>

代码样例参见单元测试:

package com.github.pfmiles.mivelocity;import java.io.StrigReader;import java.io.StrigWriter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import juit.framework.TestCase;import com.github.pfmiles.org.apache.velocity.Template;public class TemplateUtilTest exteds TestCase {    public void testRederStrigTemp() {        Strig templateStrig = "#foreach($i i $list)\$i\#ed";        Map<Strig, Object> ctxPojo = ew HashMap<Strig, Object>();        List<Strig> list = ew ArrayList<Strig>();        list.add("oe");        list.add("two");        list.add("three");        ctxPojo.put("list", list);        StrigWriter out = ew StrigWriter();        TemplateUtil.rederStrig(templateStrig, ctxPojo, out);        // System.out.pritl(out.toStrig());        assertTrue("oe\two\three\".equals(out.toStrig()));    }    public void testRederTemplate() {        Template temp = TemplateUtil.parseStrigTemplate("#foreach($i i $list)\$i\#ed");        Map<Strig, Object> ctxPojo = ew HashMap<Strig, Object>();        List<Strig> list = ew ArrayList<Strig>();        list.add("oe");        list.add("two");        list.add("three");        ctxPojo.put("list", list);        StrigWriter out = ew StrigWriter();        TemplateUtil.rederTemplate(temp, ctxPojo, out);        // System.out.pritl(out.toStrig());        assertTrue("oe\two\three\".equals(out.toStrig()));    }    public void testRefRederig() {        Template temp = TemplateUtil.parseStrigTemplate("hello $ref world");        Map<Strig, Object> ctxPojo = ew HashMap<Strig, Object>();        StrigReader stream = ew StrigReader("1234567890");        ctxPojo.put("ref", stream);        StrigWriter writer = ew StrigWriter();        TemplateUtil.rederTemplate(temp, ctxPojo, writer);        assertTrue("hello 1234567890 world".equals(writer.toStrig()));    }}

功能介绍

min-velocity 是一个专为代码生成而定制的简化 velocity 模板引擎。 目标: 以 velocity 1.7 为基础, 裁剪出适合用作代码生成的模板引擎 裁剪: 没有event机...

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论