mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
add wit
This commit is contained in:
parent
6d5ca7051b
commit
14ee7985a8
@ -3,11 +3,12 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.6.2 (2021-03-19)
|
||||
# 5.6.2 (2021-03-20)
|
||||
|
||||
### 新特性
|
||||
* 【core 】 Validator增加车架号(车辆识别码)验证、驾驶证(驾驶证档案编号)的正则校验(pr#280@Gitee)
|
||||
* 【core 】 CopyOptions增加propertiesFilter(pr#281@Gitee)
|
||||
* 【extra 】 增加Wit模板引擎支持
|
||||
### Bug修复
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
@ -112,6 +112,12 @@
|
||||
</exclusions>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.febit.wit</groupId>
|
||||
<artifactId>wit-core</artifactId>
|
||||
<version>2.6.0</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- 邮件 -->
|
||||
<dependency>
|
||||
|
@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Beetl实现
|
||||
*
|
||||
* Beetl实现,模板引擎介绍见:http://ibeetl.com/
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.extra.template.engine.beetl;
|
||||
package cn.hutool.extra.template.engine.beetl;
|
||||
|
@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Jfinal家的Enjoy模板引擎实现
|
||||
*
|
||||
* Jfinal家的Enjoy模板引擎实现,见:https://jfinal.com/doc/6-1
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.extra.template.engine.enjoy;
|
||||
package cn.hutool.extra.template.engine.enjoy;
|
||||
|
@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Rythm实现
|
||||
*
|
||||
* Rythm实现,见:http://www.rythmengine.org/
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.extra.template.engine.rythm;
|
||||
package cn.hutool.extra.template.engine.rythm;
|
||||
|
@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Thymeleaf实现
|
||||
*
|
||||
* Thymeleaf实现,见:https://www.thymeleaf.org/
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.extra.template.engine.thymeleaf;
|
||||
package cn.hutool.extra.template.engine.thymeleaf;
|
||||
|
@ -16,7 +16,7 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* Velocity模板包装
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
@ -25,20 +25,20 @@ public class VelocityTemplate extends AbstractTemplate implements Serializable {
|
||||
|
||||
private final org.apache.velocity.Template rawTemplate;
|
||||
private String charset;
|
||||
|
||||
|
||||
/**
|
||||
* 包装Velocity模板
|
||||
*
|
||||
*
|
||||
* @param template Velocity的模板对象 {@link org.apache.velocity.Template}
|
||||
* @return {@link VelocityTemplate}
|
||||
* @return VelocityTemplate
|
||||
*/
|
||||
public static VelocityTemplate wrap(org.apache.velocity.Template template) {
|
||||
return (null == template) ? null : new VelocityTemplate(template);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param rawTemplate Velocity模板对象
|
||||
*/
|
||||
public VelocityTemplate(org.apache.velocity.Template rawTemplate) {
|
||||
@ -61,7 +61,7 @@ public class VelocityTemplate extends AbstractTemplate implements Serializable {
|
||||
|
||||
/**
|
||||
* 将Map转为VelocityContext
|
||||
*
|
||||
*
|
||||
* @param bindingMap 参数绑定的Map
|
||||
* @return {@link VelocityContext}
|
||||
*/
|
||||
@ -69,7 +69,7 @@ public class VelocityTemplate extends AbstractTemplate implements Serializable {
|
||||
final Map<String, Object> map = Convert.convert(new TypeReference<Map<String, Object>>() {}, bindingMap);
|
||||
return new VelocityContext(map);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 加载可用的Velocity中预定义的编码
|
||||
*/
|
||||
|
@ -0,0 +1,84 @@
|
||||
package cn.hutool.extra.template.engine.wit;
|
||||
|
||||
import cn.hutool.extra.template.Template;
|
||||
import cn.hutool.extra.template.TemplateConfig;
|
||||
import cn.hutool.extra.template.TemplateEngine;
|
||||
import cn.hutool.extra.template.TemplateException;
|
||||
import org.febit.wit.Engine;
|
||||
import org.febit.wit.exceptions.ResourceNotFoundException;
|
||||
|
||||
/**
|
||||
* Wit(http://zqq90.github.io/webit-script/)模板引擎封装
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
public class WitEngine implements TemplateEngine {
|
||||
|
||||
private Engine engine;
|
||||
|
||||
// --------------------------------------------------------------------------------- Constructor start
|
||||
/**
|
||||
* 默认构造
|
||||
*/
|
||||
public WitEngine() {}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param config 模板配置
|
||||
*/
|
||||
public WitEngine(TemplateConfig config) {
|
||||
init(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param engine {@link Engine}
|
||||
*/
|
||||
public WitEngine(Engine engine) {
|
||||
init(engine);
|
||||
}
|
||||
// --------------------------------------------------------------------------------- Constructor end
|
||||
|
||||
|
||||
@Override
|
||||
public TemplateEngine init(TemplateConfig config) {
|
||||
init(createEngine(config));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化引擎
|
||||
* @param engine 引擎
|
||||
*/
|
||||
private void init(Engine engine){
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Template getTemplate(String resource) {
|
||||
if(null == this.engine){
|
||||
init(TemplateConfig.DEFAULT);
|
||||
}
|
||||
try {
|
||||
return WitTemplate.wrap(engine.getTemplate(resource));
|
||||
} catch (ResourceNotFoundException e) {
|
||||
throw new TemplateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建引擎
|
||||
*
|
||||
* @param config 模板配置
|
||||
* @return {@link Engine}
|
||||
*/
|
||||
private static Engine createEngine(TemplateConfig config) {
|
||||
if (null == config) {
|
||||
config = TemplateConfig.DEFAULT;
|
||||
}
|
||||
|
||||
return Engine.create();
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package cn.hutool.extra.template.engine.wit;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import cn.hutool.extra.template.AbstractTemplate;
|
||||
import org.febit.wit.Template;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.io.Writer;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Wit模板实现
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
public class WitTemplate extends AbstractTemplate implements Serializable{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final Template rawTemplate;
|
||||
|
||||
/**
|
||||
* 包装Wit模板
|
||||
*
|
||||
* @param witTemplate Wit的模板对象 {@link Template}
|
||||
* @return WitTemplate
|
||||
*/
|
||||
public static WitTemplate wrap(Template witTemplate) {
|
||||
return (null == witTemplate) ? null : new WitTemplate(witTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param witTemplate Wit的模板对象 {@link Template}
|
||||
*/
|
||||
public WitTemplate(Template witTemplate) {
|
||||
this.rawTemplate = witTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Map<?, ?> bindingMap, Writer writer) {
|
||||
final Map<String, Object> map = Convert.convert(new TypeReference<Map<String, Object>>() {}, bindingMap);
|
||||
rawTemplate.merge(map, writer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Map<?, ?> bindingMap, OutputStream out) {
|
||||
final Map<String, Object> map = Convert.convert(new TypeReference<Map<String, Object>>() {}, bindingMap);
|
||||
rawTemplate.merge(map, out);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
/**
|
||||
* Wit实现,见:http://zqq90.github.io/webit-script/
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
package cn.hutool.extra.template.engine.wit;
|
@ -3,4 +3,5 @@ cn.hutool.extra.template.engine.freemarker.FreemarkerEngine
|
||||
cn.hutool.extra.template.engine.velocity.VelocityEngine
|
||||
cn.hutool.extra.template.engine.rythm.RythmEngine
|
||||
cn.hutool.extra.template.engine.enjoy.EnjoyEngine
|
||||
cn.hutool.extra.template.engine.thymeleaf.ThymeleafEngine
|
||||
cn.hutool.extra.template.engine.thymeleaf.ThymeleafEngine
|
||||
cn.hutool.extra.template.engine.wit.WitEngine
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cn.hutool.extra.template;
|
||||
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.template.TemplateConfig.ResourceMode;
|
||||
import cn.hutool.extra.template.engine.beetl.BeetlEngine;
|
||||
import cn.hutool.extra.template.engine.enjoy.EnjoyEngine;
|
||||
@ -8,6 +9,7 @@ import cn.hutool.extra.template.engine.freemarker.FreemarkerEngine;
|
||||
import cn.hutool.extra.template.engine.rythm.RythmEngine;
|
||||
import cn.hutool.extra.template.engine.thymeleaf.ThymeleafEngine;
|
||||
import cn.hutool.extra.template.engine.velocity.VelocityEngine;
|
||||
import cn.hutool.extra.template.engine.wit.WitEngine;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
@ -18,7 +20,7 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 模板引擎单元测试
|
||||
*
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
@ -77,7 +79,7 @@ public class TemplateUtilTest {
|
||||
Template template = engine.getTemplate("hello,${name}");
|
||||
String result = template.render(Dict.create().set("name", "hutool"));
|
||||
Assert.assertEquals("hello,hutool", result);
|
||||
|
||||
|
||||
//ClassPath模板
|
||||
engine = TemplateUtil.createEngine(
|
||||
new TemplateConfig("templates", ResourceMode.CLASSPATH).setCustomEngine(FreemarkerEngine.class));
|
||||
@ -85,7 +87,7 @@ public class TemplateUtilTest {
|
||||
result = template.render(Dict.create().set("name", "hutool"));
|
||||
Assert.assertEquals("hello,hutool", result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void velocityEngineTest() {
|
||||
// 字符串模板
|
||||
@ -94,7 +96,7 @@ public class TemplateUtilTest {
|
||||
Template template = engine.getTemplate("你好,$name");
|
||||
String result = template.render(Dict.create().set("name", "hutool"));
|
||||
Assert.assertEquals("你好,hutool", result);
|
||||
|
||||
|
||||
//ClassPath模板
|
||||
engine = TemplateUtil.createEngine(
|
||||
new TemplateConfig("templates", ResourceMode.CLASSPATH).setCustomEngine(VelocityEngine.class));
|
||||
@ -132,7 +134,7 @@ public class TemplateUtilTest {
|
||||
Template template = engine.getTemplate("<h3 th:text=\"${message}\"></h3>");
|
||||
String result = template.render(Dict.create().set("message", "Hutool"));
|
||||
Assert.assertEquals("<h3>Hutool</h3>", result);
|
||||
|
||||
|
||||
//ClassPath模板
|
||||
engine = TemplateUtil.createEngine(
|
||||
new TemplateConfig("templates", ResourceMode.CLASSPATH).setCustomEngine(ThymeleafEngine.class));
|
||||
@ -140,7 +142,7 @@ public class TemplateUtilTest {
|
||||
result = template.render(Dict.create().set("message", "Hutool"));
|
||||
Assert.assertEquals("<h3>Hutool</h3>", result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void renderToFileTest() {
|
||||
@ -152,4 +154,13 @@ public class TemplateUtilTest {
|
||||
File outputFile = new File("e:/test.txt");
|
||||
template.render(bindingMap, outputFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void WitEngineTest() {
|
||||
TemplateEngine engine = TemplateUtil.createEngine(
|
||||
new TemplateConfig("templates", ResourceMode.CLASSPATH).setCustomEngine(WitEngine.class));
|
||||
Template template = engine.getTemplate("/templates/wit_test.wit");
|
||||
String result = template.render(Dict.create().set("name", "hutool"));
|
||||
Assert.assertEquals("hello,hutool", StrUtil.trim(result));
|
||||
}
|
||||
}
|
||||
|
4
hutool-extra/src/test/resources/templates/wit_test.wit
Normal file
4
hutool-extra/src/test/resources/templates/wit_test.wit
Normal file
@ -0,0 +1,4 @@
|
||||
<%
|
||||
var name;
|
||||
%>
|
||||
hello,${name}
|
Loading…
Reference in New Issue
Block a user