add templates

This commit is contained in:
Looly 2022-01-20 18:23:43 +08:00
parent 6014a47f7c
commit 673df9e76a
10 changed files with 295 additions and 1 deletions

View File

@ -5,6 +5,7 @@
# 5.7.21 (2022-01-20)
### 🐣新特性
* 【extra 】 增加jetbrick模板支持
### 🐞Bug修复
-------------------------------------------------------------------------------------------------------------

View File

@ -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>

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}
}

View File

@ -0,0 +1,6 @@
/**
* jetbrick-template实现模板引擎介绍见https://github.com/subchen/jetbrick-template-2x
*
* @author looly
*/
package cn.hutool.extra.template.engine.jetbrick;

View File

@ -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

View 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));
}
}

View File

@ -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);

View File

@ -0,0 +1 @@
你好,${name}