TemplateUtil的实现类增加getRawEngine方法

This commit is contained in:
Looly 2022-09-15 00:34:06 +08:00
parent bce8d93ba7
commit f9f0a5ed1a
9 changed files with 150 additions and 61 deletions

View File

@ -10,6 +10,7 @@
* 【jwt 】 优化JWT自动识别header中的算法并可自定义header中key的顺序issue#I5QRUO@Gitee
* 【core 】 IdcardUtil增加convert18To15方法issue#I5QYCP@Gitee
* 【core 】 新增AnsiColors(改自Spring Boot)、AnsiColorWrapper优化QrCodeUtilpr#778@Gitee
* 【core 】 TemplateUtil的实现类增加getRawEngine方法issues#2530@Github
### 🐞Bug修复
* 【core 】 修复ObjectUtil.defaultIfXXX中NPE问题pr#2603@Github

View File

@ -17,7 +17,7 @@ import java.io.IOException;
/**
* Beetl模板引擎封装
*
*
* @author looly
*/
public class BeetlEngine implements TemplateEngine {
@ -32,7 +32,7 @@ public class BeetlEngine implements TemplateEngine {
/**
* 构造
*
*
* @param config 模板配置
*/
public BeetlEngine(TemplateConfig config) {
@ -41,7 +41,7 @@ public class BeetlEngine implements TemplateEngine {
/**
* 构造
*
*
* @param engine {@link GroupTemplate}
*/
public BeetlEngine(GroupTemplate engine) {
@ -72,9 +72,19 @@ public class BeetlEngine implements TemplateEngine {
return BeetlTemplate.wrap(engine.getTemplate(resource));
}
/**
* 获取原始引擎的钩子方法用于自定义特殊属性如插件等
*
* @return {@link GroupTemplate}
* @since 5.8.7
*/
public GroupTemplate getRawEngine() {
return this.engine;
}
/**
* 创建引擎
*
*
* @param config 模板配置
* @return {@link GroupTemplate}
*/
@ -103,7 +113,7 @@ public class BeetlEngine implements TemplateEngine {
/**
* 创建自定义的模板组 {@link GroupTemplate}配置文件使用全局默认<br>
* 此时自定义的配置文件可在ClassPath中放入beetl.properties配置
*
*
* @param loader {@link ResourceLoader}资源加载器
* @return {@link GroupTemplate}
* @since 3.2.0
@ -118,7 +128,7 @@ public class BeetlEngine implements TemplateEngine {
/**
* 创建自定义的 {@link GroupTemplate}
*
*
* @param loader {@link ResourceLoader}资源加载器
* @param conf {@link Configuration} 配置文件
* @return {@link GroupTemplate}

View File

@ -77,6 +77,16 @@ public class EnjoyEngine implements TemplateEngine {
return EnjoyTemplate.wrap(this.engine.getTemplate(resource));
}
/**
* 获取原始引擎的钩子方法用于自定义特殊属性如插件等
*
* @return {@link com.jfinal.template.Engine}
* @since 5.8.7
*/
public com.jfinal.template.Engine getRawEngine() {
return this.engine;
}
/**
* 创建引擎
*

View File

@ -15,7 +15,7 @@ import java.io.IOException;
/**
* FreeMarker模板引擎封装<br>
* https://freemarker.apache.org/
* <a href="https://freemarker.apache.org/">https://freemarker.apache.org/</a>
*
* @author looly
*/
@ -82,6 +82,16 @@ public class FreemarkerEngine implements TemplateEngine {
}
}
/**
* 获取原始引擎的钩子方法用于自定义特殊属性如插件等
*
* @return {@link Configuration}
* @since 5.8.7
*/
public Configuration getConfiguration() {
return this.cfg;
}
/**
* 创建配置项
*

View File

@ -66,6 +66,16 @@ public class JetbrickEngine implements TemplateEngine {
return JetbrickTemplate.wrap(engine.getTemplate(resource));
}
/**
* 获取原始引擎的钩子方法用于自定义特殊属性如插件等
*
* @return {@link JetEngine}
* @since 5.8.7
*/
public JetEngine getRawEngine() {
return this.engine;
}
/**
* 创建引擎
*

View File

@ -1,15 +1,15 @@
package cn.hutool.extra.template.engine.rythm;
import java.util.Properties;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import java.util.Properties;
/**
* Rythm模板引擎<br>
* 文档http://rythmengine.org/doc/index
*
*
* @author looly
*
*/
@ -25,7 +25,7 @@ public class RythmEngine implements TemplateEngine {
/**
* 构造
*
*
* @param config 模板配置
*/
public RythmEngine(TemplateConfig config) {
@ -34,7 +34,7 @@ public class RythmEngine implements TemplateEngine {
/**
* 构造
*
*
* @param engine {@link org.rythmengine.RythmEngine}
*/
public RythmEngine(org.rythmengine.RythmEngine engine) {
@ -51,6 +51,16 @@ public class RythmEngine implements TemplateEngine {
return this;
}
/**
* 获取原始引擎的钩子方法用于自定义特殊属性如插件等
*
* @return {@link org.rythmengine.RythmEngine}
* @since 5.8.7
*/
public org.rythmengine.RythmEngine getRawEngine() {
return this.engine;
}
/**
* 初始化引擎
* @param engine 引擎
@ -69,7 +79,7 @@ public class RythmEngine implements TemplateEngine {
/**
* 创建引擎
*
*
* @param config 模板配置
* @return {@link org.rythmengine.RythmEngine}
*/
@ -77,7 +87,7 @@ public class RythmEngine implements TemplateEngine {
if (null == config) {
config = new TemplateConfig();
}
final Properties props = new Properties();
final String path = config.getPath();
if (null != path) {

View File

@ -15,7 +15,7 @@ import cn.hutool.extra.template.TemplateEngine;
/**
* Thymeleaf模板引擎实现
*
*
* @author looly
* @since 4.1.11
*/
@ -25,14 +25,16 @@ public class ThymeleafEngine implements TemplateEngine {
TemplateConfig config;
// --------------------------------------------------------------------------------- Constructor start
/**
* 默认构造
*/
public ThymeleafEngine() {}
public ThymeleafEngine() {
}
/**
* 构造
*
*
* @param config 模板配置
*/
public ThymeleafEngine(TemplateConfig config) {
@ -41,7 +43,7 @@ public class ThymeleafEngine implements TemplateEngine {
/**
* 构造
*
*
* @param engine {@link org.thymeleaf.TemplateEngine}
*/
public ThymeleafEngine(org.thymeleaf.TemplateEngine engine) {
@ -51,7 +53,7 @@ public class ThymeleafEngine implements TemplateEngine {
@Override
public TemplateEngine init(TemplateConfig config) {
if(null == config){
if (null == config) {
config = TemplateConfig.DEFAULT;
}
this.config = config;
@ -61,23 +63,34 @@ public class ThymeleafEngine implements TemplateEngine {
/**
* 初始化引擎
*
* @param engine 引擎
*/
private void init(org.thymeleaf.TemplateEngine engine){
private void init(org.thymeleaf.TemplateEngine engine) {
this.engine = engine;
}
@Override
public Template getTemplate(String resource) {
if(null == this.engine){
if (null == this.engine) {
init(TemplateConfig.DEFAULT);
}
return ThymeleafTemplate.wrap(this.engine, resource, (null == this.config) ? null : this.config.getCharset());
}
/**
* 获取原始引擎的钩子方法用于自定义特殊属性如插件等
*
* @return {@link org.thymeleaf.TemplateEngine}
* @since 5.8.7
*/
public org.thymeleaf.TemplateEngine getRawEngine() {
return this.engine;
}
/**
* 创建引擎
*
*
* @param config 模板配置
* @return {@link TemplateEngine}
*/
@ -85,38 +98,38 @@ public class ThymeleafEngine implements TemplateEngine {
if (null == config) {
config = new TemplateConfig();
}
ITemplateResolver resolver;
switch (config.getResourceMode()) {
case CLASSPATH:
final ClassLoaderTemplateResolver classLoaderResolver = new ClassLoaderTemplateResolver();
classLoaderResolver.setCharacterEncoding(config.getCharsetStr());
classLoaderResolver.setTemplateMode(TemplateMode.HTML);
classLoaderResolver.setPrefix(StrUtil.addSuffixIfNot(config.getPath(), "/"));
resolver = classLoaderResolver;
break;
case FILE:
final FileTemplateResolver fileResolver = new FileTemplateResolver();
fileResolver.setCharacterEncoding(config.getCharsetStr());
fileResolver.setTemplateMode(TemplateMode.HTML);
fileResolver.setPrefix(StrUtil.addSuffixIfNot(config.getPath(), "/"));
resolver = fileResolver;
break;
case WEB_ROOT:
final FileTemplateResolver webRootResolver = new FileTemplateResolver();
webRootResolver.setCharacterEncoding(config.getCharsetStr());
webRootResolver.setTemplateMode(TemplateMode.HTML);
webRootResolver.setPrefix(StrUtil.addSuffixIfNot(FileUtil.getAbsolutePath(FileUtil.file(FileUtil.getWebRoot(), config.getPath())), "/"));
resolver = webRootResolver;
break;
case STRING:
resolver = new StringTemplateResolver();
break;
default:
resolver = new DefaultTemplateResolver();
break;
case CLASSPATH:
final ClassLoaderTemplateResolver classLoaderResolver = new ClassLoaderTemplateResolver();
classLoaderResolver.setCharacterEncoding(config.getCharsetStr());
classLoaderResolver.setTemplateMode(TemplateMode.HTML);
classLoaderResolver.setPrefix(StrUtil.addSuffixIfNot(config.getPath(), "/"));
resolver = classLoaderResolver;
break;
case FILE:
final FileTemplateResolver fileResolver = new FileTemplateResolver();
fileResolver.setCharacterEncoding(config.getCharsetStr());
fileResolver.setTemplateMode(TemplateMode.HTML);
fileResolver.setPrefix(StrUtil.addSuffixIfNot(config.getPath(), "/"));
resolver = fileResolver;
break;
case WEB_ROOT:
final FileTemplateResolver webRootResolver = new FileTemplateResolver();
webRootResolver.setCharacterEncoding(config.getCharsetStr());
webRootResolver.setTemplateMode(TemplateMode.HTML);
webRootResolver.setPrefix(StrUtil.addSuffixIfNot(FileUtil.getAbsolutePath(FileUtil.file(FileUtil.getWebRoot(), config.getPath())), "/"));
resolver = webRootResolver;
break;
case STRING:
resolver = new StringTemplateResolver();
break;
default:
resolver = new DefaultTemplateResolver();
break;
}
final org.thymeleaf.TemplateEngine engine = new org.thymeleaf.TemplateEngine();
engine.setTemplateResolver(resolver);
return engine;

View File

@ -73,6 +73,16 @@ public class WitEngine implements TemplateEngine {
}
}
/**
* 获取原始引擎的钩子方法用于自定义特殊属性如插件等
*
* @return {@link Engine}
* @since 5.8.7
*/
public Engine getRawEngine() {
return this.engine;
}
/**
* 创建引擎
*

View File

@ -1,5 +1,15 @@
package cn.hutool.extra.template;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.extra.template.engine.thymeleaf.ThymeleafEngine;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.thymeleaf.context.Context;
import org.thymeleaf.standard.StandardDialect;
import org.thymeleaf.templateresolver.StringTemplateResolver;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
@ -8,23 +18,28 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.StringTemplateResolver;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.extra.template.engine.thymeleaf.ThymeleafEngine;
/**
* Thymeleaf单元测试
*
*
* @author looly
*
*/
public class ThymeleafTest {
/**
* <a href="https://github.com/dromara/hutool/issues/2530">...</a>
* 自定义操作原始引擎
*/
@Test
@Ignore
public void addDialectTest(){
final TemplateEngine engine = TemplateUtil.createEngine();
if(engine instanceof ThymeleafEngine){
final org.thymeleaf.TemplateEngine rawEngine = ((ThymeleafEngine) engine).getRawEngine();
rawEngine.addDialect(new StandardDialect());
}
}
@Test
public void thymeleafEngineTest() {
Map<String, Object> map1 = new HashMap<>();