mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
add templates
This commit is contained in:
parent
6014a47f7c
commit
673df9e76a
@ -5,6 +5,7 @@
|
||||
# 5.7.21 (2022-01-20)
|
||||
|
||||
### 🐣新特性
|
||||
* 【extra 】 增加jetbrick模板支持
|
||||
### 🐞Bug修复
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
@ -119,6 +119,12 @@
|
||||
<version>2.6.0</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.subchen</groupId>
|
||||
<artifactId>jetbrick-template</artifactId>
|
||||
<version>2.1.10</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- 邮件 -->
|
||||
<dependency>
|
||||
|
@ -0,0 +1,109 @@
|
||||
package cn.hutool.extra.template.engine.jetbrick;
|
||||
|
||||
import cn.hutool.extra.template.Template;
|
||||
import cn.hutool.extra.template.TemplateConfig;
|
||||
import cn.hutool.extra.template.TemplateEngine;
|
||||
import jetbrick.template.JetEngine;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Jetbrick模板引擎封装<br>
|
||||
* 见:https://github.com/subchen/jetbrick-template-2x
|
||||
*
|
||||
* @author looly
|
||||
* @since 5.7.21
|
||||
*/
|
||||
public class JetbrickEngine implements TemplateEngine {
|
||||
|
||||
private JetEngine engine;
|
||||
|
||||
// --------------------------------------------------------------------------------- Constructor start
|
||||
/**
|
||||
* 默认构造
|
||||
*/
|
||||
public JetbrickEngine() {}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param config 模板配置
|
||||
*/
|
||||
public JetbrickEngine(TemplateConfig config) {
|
||||
init(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param engine {@link JetEngine}
|
||||
*/
|
||||
public JetbrickEngine(JetEngine engine) {
|
||||
init(engine);
|
||||
}
|
||||
// --------------------------------------------------------------------------------- Constructor end
|
||||
|
||||
|
||||
@Override
|
||||
public TemplateEngine init(TemplateConfig config) {
|
||||
init(createEngine(config));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化引擎
|
||||
* @param engine 引擎
|
||||
*/
|
||||
private void init(JetEngine engine){
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Template getTemplate(String resource) {
|
||||
if(null == this.engine){
|
||||
init(TemplateConfig.DEFAULT);
|
||||
}
|
||||
return JetbrickTemplate.wrap(engine.getTemplate(resource));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建引擎
|
||||
*
|
||||
* @param config 模板配置
|
||||
* @return {@link JetEngine}
|
||||
*/
|
||||
private static JetEngine createEngine(TemplateConfig config) {
|
||||
if (null == config) {
|
||||
config = TemplateConfig.DEFAULT;
|
||||
}
|
||||
|
||||
Properties props = new Properties();
|
||||
props.setProperty("jetx.input.encoding", config.getCharsetStr());
|
||||
props.setProperty("jetx.output.encoding", config.getCharsetStr());
|
||||
props.setProperty("jetx.template.loaders", "$loader");
|
||||
|
||||
switch (config.getResourceMode()){
|
||||
case CLASSPATH:
|
||||
props.setProperty("$loader", "jetbrick.template.loader.ClasspathResourceLoader");
|
||||
props.setProperty("$loader.root", config.getPath());
|
||||
break;
|
||||
case FILE:
|
||||
props.setProperty("$loader", "jetbrick.template.loader.FileSystemResourceLoader");
|
||||
props.setProperty("$loader.root", config.getPath());
|
||||
break;
|
||||
case WEB_ROOT:
|
||||
props.setProperty("$loader", "jetbrick.template.loader.ServletResourceLoader");
|
||||
props.setProperty("$loader.root", config.getPath());
|
||||
break;
|
||||
case STRING:
|
||||
props.setProperty("$loader", "cn.hutool.extra.template.engine.jetbrick.loader.StringResourceLoader");
|
||||
props.setProperty("$loader.charset", config.getCharsetStr());
|
||||
break;
|
||||
default:
|
||||
// 默认
|
||||
return JetEngine.create();
|
||||
}
|
||||
|
||||
return JetEngine.create(props);
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package cn.hutool.extra.template.engine.jetbrick;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import cn.hutool.extra.template.AbstractTemplate;
|
||||
import jetbrick.template.JetTemplate;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.io.Writer;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Jetbrick模板实现<br>
|
||||
* 见:https://github.com/subchen/jetbrick-template-2x
|
||||
*
|
||||
* @author looly
|
||||
* @since 5.7.21
|
||||
*/
|
||||
public class JetbrickTemplate extends AbstractTemplate implements Serializable{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final JetTemplate rawTemplate;
|
||||
|
||||
/**
|
||||
* 包装Jetbrick模板
|
||||
*
|
||||
* @param jetTemplate Jetbrick的模板对象 {@link JetTemplate }
|
||||
* @return JetbrickTemplate
|
||||
*/
|
||||
public static JetbrickTemplate wrap(JetTemplate jetTemplate) {
|
||||
return (null == jetTemplate) ? null : new JetbrickTemplate(jetTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param jetTemplate Jetbrick的模板对象 {@link JetTemplate }
|
||||
*/
|
||||
public JetbrickTemplate(JetTemplate jetTemplate) {
|
||||
this.rawTemplate = jetTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Map<?, ?> bindingMap, Writer writer) {
|
||||
final Map<String, Object> map = Convert.convert(new TypeReference<Map<String, Object>>() {}, bindingMap);
|
||||
rawTemplate.render(map, writer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Map<?, ?> bindingMap, OutputStream out) {
|
||||
final Map<String, Object> map = Convert.convert(new TypeReference<Map<String, Object>>() {}, bindingMap);
|
||||
rawTemplate.render(map, out);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
package cn.hutool.extra.template.engine.jetbrick.loader;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import jetbrick.io.resource.AbstractResource;
|
||||
import jetbrick.io.resource.Resource;
|
||||
import jetbrick.io.resource.ResourceNotFoundException;
|
||||
import jetbrick.template.loader.AbstractResourceLoader;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* 字符串模板加载器
|
||||
*
|
||||
* @author looly
|
||||
* @since 5.7.21
|
||||
*/
|
||||
public class StringResourceLoader extends AbstractResourceLoader {
|
||||
|
||||
private Charset charset;
|
||||
|
||||
@Override
|
||||
public Resource load(String name) {
|
||||
return new StringTemplateResource(name, charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置编码
|
||||
* @param charset 编码
|
||||
*/
|
||||
public void setCharset(Charset charset){
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串模板
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
static class StringTemplateResource extends AbstractResource {
|
||||
|
||||
private final String content;
|
||||
private final Charset charset;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param content 模板内容
|
||||
* @param charset 编码
|
||||
*/
|
||||
public StringTemplateResource(String content, Charset charset){
|
||||
this.content = content;
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream openStream() throws ResourceNotFoundException {
|
||||
return IoUtil.toStream(content, charset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL getURL() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exist() {
|
||||
return StrUtil.isEmpty(content);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long lastModified() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
/**
|
||||
* jetbrick-template实现,模板引擎介绍见:https://github.com/subchen/jetbrick-template-2x
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
package cn.hutool.extra.template.engine.jetbrick;
|
@ -5,3 +5,4 @@ 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.wit.WitEngine
|
||||
cn.hutool.extra.template.engine.jetbrick.JetbrickEngine
|
||||
|
32
hutool-extra/src/test/java/cn/hutool/extra/template/JetbrickTest.java
Executable file
32
hutool-extra/src/test/java/cn/hutool/extra/template/JetbrickTest.java
Executable file
@ -0,0 +1,32 @@
|
||||
package cn.hutool.extra.template;
|
||||
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.template.engine.jetbrick.JetbrickEngine;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JetbrickTest {
|
||||
|
||||
@Test
|
||||
public void jetbrickEngineTest() {
|
||||
//classpath模板
|
||||
TemplateConfig config = new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH)
|
||||
.setCustomEngine(JetbrickEngine.class);
|
||||
TemplateEngine engine = TemplateUtil.createEngine(config);
|
||||
Template template = engine.getTemplate("jetbrick_test.jetx");
|
||||
String result = template.render(Dict.create().set("name", "hutool"));
|
||||
Assert.assertEquals("你好,hutool", StrUtil.trim(result));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jetbrickEngineWithStringTest() {
|
||||
// 字符串模板
|
||||
TemplateConfig config = new TemplateConfig("templates", TemplateConfig.ResourceMode.STRING)
|
||||
.setCustomEngine(JetbrickEngine.class);
|
||||
TemplateEngine engine = TemplateUtil.createEngine(config);
|
||||
Template template = engine.getTemplate("hello,${name}");
|
||||
String result = template.render(Dict.create().set("name", "hutool"));
|
||||
Assert.assertEquals("hello,hutool", StrUtil.trim(result));
|
||||
}
|
||||
}
|
@ -156,7 +156,7 @@ public class TemplateUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void WitEngineTest() {
|
||||
public void witEngineTest() {
|
||||
//classpath模板
|
||||
TemplateConfig config = new TemplateConfig("templates", ResourceMode.CLASSPATH)
|
||||
.setCustomEngine(WitEngine.class);
|
||||
|
1
hutool-extra/src/test/resources/templates/jetbrick_test.jetx
Executable file
1
hutool-extra/src/test/resources/templates/jetbrick_test.jetx
Executable file
@ -0,0 +1 @@
|
||||
你好,${name}
|
Loading…
Reference in New Issue
Block a user