cn.hutool.extra.template.engine.jetbrick.JetbrickEngine Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hutool-all Show documentation
Show all versions of hutool-all Show documentation
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。
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模板引擎封装
* 见: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));
}
/**
* 获取原始引擎的钩子方法,用于自定义特殊属性,如插件等
*
* @return {@link JetEngine}
* @since 5.8.7
*/
public JetEngine getRawEngine() {
return this.engine;
}
/**
* 创建引擎
*
* @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);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy