优化从类路径加载velocity模板.

This commit is contained in:
Mario Luo
2021-05-07 10:59:32 +08:00
parent 212dbada18
commit 64cbe185d2
3 changed files with 39 additions and 16 deletions

View File

@@ -55,4 +55,8 @@ public class EngineConfig implements Serializable {
* 文件名称
*/
private String fileName;
/**
* 模板内容
*/
private String templateContent;
}

View File

@@ -24,6 +24,7 @@ import cn.smallbun.screw.core.metadata.model.DataModel;
import cn.smallbun.screw.core.util.Assert;
import cn.smallbun.screw.core.util.ExceptionUtils;
import cn.smallbun.screw.core.util.StringUtils;
import java.util.HashMap;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
@@ -33,6 +34,7 @@ import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import org.apache.velocity.runtime.log.NullLogChute;
import static cn.smallbun.screw.core.constant.DefaultConstants.DEFAULT_ENCODING;
import static cn.smallbun.screw.core.engine.EngineTemplateType.velocity;
@@ -74,6 +76,7 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
velocityEngine.setProperty("file.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
}
velocityEngine.setProperty("runtime.log.logsystem.class", NullLogChute.class.getName());
velocityEngine.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "");
velocityEngine.setProperty(Velocity.ENCODING_DEFAULT, DEFAULT_ENCODING);
velocityEngine.setProperty(Velocity.INPUT_ENCODING, DEFAULT_ENCODING);
@@ -89,21 +92,24 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
@Override
public void produce(DataModel info, String docName) throws ProduceException {
Assert.notNull(info, "DataModel can not be empty!");
Template template;
Template template = null;
try {
// get template path
String path = getEngineConfig().getCustomTemplate();
//如果自定义了模板
if (StringUtils.isNotBlank(path)) {
template = velocityEngine.getTemplate(path, DEFAULT_ENCODING);
}
//没有自定义模板,使用核心包自带
else {
template = velocityEngine
.getTemplate(velocity.getTemplateDir()
+ getEngineConfig().getFileType().getTemplateNamePrefix()
+ velocity.getSuffix(),
DEFAULT_ENCODING);
String templateContent = getEngineConfig().getTemplateContent();
if (StringUtils.isBlank(templateContent)) {
// get template path
String path = getEngineConfig().getCustomTemplate();
//如果自定义了模板
if (StringUtils.isNotBlank(path)) {
template = velocityEngine.getTemplate(path, DEFAULT_ENCODING);
}
//没有自定义模板,使用核心包自带
else {
template = velocityEngine
.getTemplate(velocity.getTemplateDir()
+ getEngineConfig().getFileType().getTemplateNamePrefix()
+ velocity.getSuffix(),
DEFAULT_ENCODING);
}
}
// output
try (FileOutputStream outStream = new FileOutputStream(getFile(docName));
@@ -112,8 +118,21 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
//put data
VelocityContext context = new VelocityContext();
context.put(DATA, info);
context.put("markdown", new HashMap<String, String>() {
{
put("h1", "#");
put("h2", "##");
put("h3", "###");
put("h4", "####");
put("h5", "#####");
}
});
//generate
template.merge(context, sw);
if (template != null) {
template.merge(context, sw);
} else {
velocityEngine.evaluate(context, sw, "test", templateContent);
}
// open the output directory
openOutputDir();
}

View File

@@ -47,4 +47,4 @@
#foreach($c in $t.columns)
| $!{velocityCount} | $!{c.columnName} | $!{c.columnType} | #if($!{c.decimalDigits})$!{c.decimalDigits}#else 0#end | $!{c.nullable}| $!{c.primaryKey} | $!{c.columnDef} | $!{c.remarks} |
#end
#end
#end