use of cn.hutool.extra.template.TemplateConfig in project hutool by looly.
the class FreemarkerEngine method createCfg.
/**
* 创建配置项
*
* @param config 模板配置
* @return {@link Configuration }
*/
private static Configuration createCfg(TemplateConfig config) {
if (null == config) {
config = new TemplateConfig();
}
final Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
cfg.setLocalizedLookup(false);
cfg.setDefaultEncoding(config.getCharset().toString());
switch(config.getResourceMode()) {
case CLASSPATH:
cfg.setTemplateLoader(new ClassTemplateLoader(ClassUtil.getClassLoader(), config.getPath()));
break;
case FILE:
try {
cfg.setTemplateLoader(new FileTemplateLoader(FileUtil.file(config.getPath())));
} catch (IOException e) {
throw new IORuntimeException(e);
}
break;
case WEB_ROOT:
try {
cfg.setTemplateLoader(new FileTemplateLoader(FileUtil.file(FileUtil.getWebRoot(), config.getPath())));
} catch (IOException e) {
throw new IORuntimeException(e);
}
break;
case STRING:
cfg.setTemplateLoader(new SimpleStringTemplateLoader());
break;
default:
break;
}
return cfg;
}
use of cn.hutool.extra.template.TemplateConfig in project hutool by looly.
the class ThymeleafEngine method createEngine.
/**
* 创建引擎
*
* @param config 模板配置
* @return {@link TemplateEngine}
*/
private static org.thymeleaf.TemplateEngine createEngine(TemplateConfig config) {
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;
}
final org.thymeleaf.TemplateEngine engine = new org.thymeleaf.TemplateEngine();
engine.setTemplateResolver(resolver);
return engine;
}
use of cn.hutool.extra.template.TemplateConfig in project hutool by looly.
the class VelocityEngine method createEngine.
/**
* 创建引擎
*
* @param config 模板配置
* @return {@link org.apache.velocity.app.VelocityEngine}
*/
private static org.apache.velocity.app.VelocityEngine createEngine(TemplateConfig config) {
if (null == config) {
config = new TemplateConfig();
}
final org.apache.velocity.app.VelocityEngine ve = new org.apache.velocity.app.VelocityEngine();
// 编码
final String charsetStr = config.getCharset().toString();
ve.setProperty(Velocity.INPUT_ENCODING, charsetStr);
// ve.setProperty(Velocity.OUTPUT_ENCODING, charsetStr);
// 使用缓存
ve.setProperty(Velocity.FILE_RESOURCE_LOADER_CACHE, true);
// loader
switch(config.getResourceMode()) {
case CLASSPATH:
// 新版Velocity弃用
// ve.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
ve.setProperty("resource.loader.file.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
break;
case FILE:
// path
final String path = config.getPath();
if (null != path) {
ve.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
}
break;
case WEB_ROOT:
ve.setProperty(Velocity.RESOURCE_LOADER, "webapp");
ve.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.servlet.WebappLoader");
ve.setProperty("webapp.resource.loader.path", StrUtil.nullToDefault(config.getPath(), StrUtil.SLASH));
break;
case STRING:
ve.setProperty(Velocity.RESOURCE_LOADER, "str");
ve.setProperty("str.resource.loader.class", SimpleStringResourceLoader.class.getName());
break;
default:
break;
}
ve.init();
return ve;
}
use of cn.hutool.extra.template.TemplateConfig in project hutool by looly.
the class RythmEngine method createEngine.
/**
* 创建引擎
*
* @param config 模板配置
* @return {@link org.rythmengine.RythmEngine}
*/
private static org.rythmengine.RythmEngine createEngine(TemplateConfig config) {
if (null == config) {
config = new TemplateConfig();
}
final Properties props = new Properties();
final String path = config.getPath();
if (null != path) {
props.put("home.template", path);
}
return new org.rythmengine.RythmEngine(props);
}
Aggregations