Skip to content

Commit

Permalink
优化从类路径加载velocity模板.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkqm committed May 7, 2021
1 parent 212dbad commit 64cbe18
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ public class EngineConfig implements Serializable {
* 文件名称
*/
private String fileName;
/**
* 模板内容
*/
private String templateContent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand All @@ -33,6 +34,7 @@
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;
Expand Down Expand Up @@ -74,6 +76,7 @@ public VelocityTemplateEngine(EngineConfig templateConfig) {
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);
Expand All @@ -89,21 +92,24 @@ public VelocityTemplateEngine(EngineConfig templateConfig) {
@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));
Expand All @@ -112,8 +118,21 @@ public void produce(DataModel info, String docName) throws ProduceException {
//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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 64cbe18

Please sign in to comment.