forked from lxm_tools/screw
优化从类路径加载velocity模板.
This commit is contained in:
@@ -55,4 +55,8 @@ public class EngineConfig implements Serializable {
|
|||||||
* 文件名称
|
* 文件名称
|
||||||
*/
|
*/
|
||||||
private String fileName;
|
private String fileName;
|
||||||
|
/**
|
||||||
|
* 模板内容
|
||||||
|
*/
|
||||||
|
private String templateContent;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.Assert;
|
||||||
import cn.smallbun.screw.core.util.ExceptionUtils;
|
import cn.smallbun.screw.core.util.ExceptionUtils;
|
||||||
import cn.smallbun.screw.core.util.StringUtils;
|
import cn.smallbun.screw.core.util.StringUtils;
|
||||||
|
import java.util.HashMap;
|
||||||
import org.apache.velocity.Template;
|
import org.apache.velocity.Template;
|
||||||
import org.apache.velocity.VelocityContext;
|
import org.apache.velocity.VelocityContext;
|
||||||
import org.apache.velocity.app.Velocity;
|
import org.apache.velocity.app.Velocity;
|
||||||
@@ -33,6 +34,7 @@ import java.io.BufferedWriter;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStreamWriter;
|
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.constant.DefaultConstants.DEFAULT_ENCODING;
|
||||||
import static cn.smallbun.screw.core.engine.EngineTemplateType.velocity;
|
import static cn.smallbun.screw.core.engine.EngineTemplateType.velocity;
|
||||||
@@ -74,6 +76,7 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
|||||||
velocityEngine.setProperty("file.resource.loader.class",
|
velocityEngine.setProperty("file.resource.loader.class",
|
||||||
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
|
"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.FILE_RESOURCE_LOADER_PATH, "");
|
||||||
velocityEngine.setProperty(Velocity.ENCODING_DEFAULT, DEFAULT_ENCODING);
|
velocityEngine.setProperty(Velocity.ENCODING_DEFAULT, DEFAULT_ENCODING);
|
||||||
velocityEngine.setProperty(Velocity.INPUT_ENCODING, DEFAULT_ENCODING);
|
velocityEngine.setProperty(Velocity.INPUT_ENCODING, DEFAULT_ENCODING);
|
||||||
@@ -89,21 +92,24 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
|||||||
@Override
|
@Override
|
||||||
public void produce(DataModel info, String docName) throws ProduceException {
|
public void produce(DataModel info, String docName) throws ProduceException {
|
||||||
Assert.notNull(info, "DataModel can not be empty!");
|
Assert.notNull(info, "DataModel can not be empty!");
|
||||||
Template template;
|
Template template = null;
|
||||||
try {
|
try {
|
||||||
// get template path
|
String templateContent = getEngineConfig().getTemplateContent();
|
||||||
String path = getEngineConfig().getCustomTemplate();
|
if (StringUtils.isBlank(templateContent)) {
|
||||||
//如果自定义了模板
|
// get template path
|
||||||
if (StringUtils.isNotBlank(path)) {
|
String path = getEngineConfig().getCustomTemplate();
|
||||||
template = velocityEngine.getTemplate(path, DEFAULT_ENCODING);
|
//如果自定义了模板
|
||||||
}
|
if (StringUtils.isNotBlank(path)) {
|
||||||
//没有自定义模板,使用核心包自带
|
template = velocityEngine.getTemplate(path, DEFAULT_ENCODING);
|
||||||
else {
|
}
|
||||||
template = velocityEngine
|
//没有自定义模板,使用核心包自带
|
||||||
.getTemplate(velocity.getTemplateDir()
|
else {
|
||||||
+ getEngineConfig().getFileType().getTemplateNamePrefix()
|
template = velocityEngine
|
||||||
+ velocity.getSuffix(),
|
.getTemplate(velocity.getTemplateDir()
|
||||||
DEFAULT_ENCODING);
|
+ getEngineConfig().getFileType().getTemplateNamePrefix()
|
||||||
|
+ velocity.getSuffix(),
|
||||||
|
DEFAULT_ENCODING);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// output
|
// output
|
||||||
try (FileOutputStream outStream = new FileOutputStream(getFile(docName));
|
try (FileOutputStream outStream = new FileOutputStream(getFile(docName));
|
||||||
@@ -112,8 +118,21 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
|||||||
//put data
|
//put data
|
||||||
VelocityContext context = new VelocityContext();
|
VelocityContext context = new VelocityContext();
|
||||||
context.put(DATA, info);
|
context.put(DATA, info);
|
||||||
|
context.put("markdown", new HashMap<String, String>() {
|
||||||
|
{
|
||||||
|
put("h1", "#");
|
||||||
|
put("h2", "##");
|
||||||
|
put("h3", "###");
|
||||||
|
put("h4", "####");
|
||||||
|
put("h5", "#####");
|
||||||
|
}
|
||||||
|
});
|
||||||
//generate
|
//generate
|
||||||
template.merge(context, sw);
|
if (template != null) {
|
||||||
|
template.merge(context, sw);
|
||||||
|
} else {
|
||||||
|
velocityEngine.evaluate(context, sw, "test", templateContent);
|
||||||
|
}
|
||||||
// open the output directory
|
// open the output directory
|
||||||
openOutputDir();
|
openOutputDir();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,4 +47,4 @@
|
|||||||
#foreach($c in $t.columns)
|
#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} |
|
| $!{velocityCount} | $!{c.columnName} | $!{c.columnType} | #if($!{c.decimalDigits})$!{c.decimalDigits}#else 0#end | $!{c.nullable}| $!{c.primaryKey} | $!{c.columnDef} | $!{c.remarks} |
|
||||||
#end
|
#end
|
||||||
#end
|
#end
|
||||||
|
|||||||
Reference in New Issue
Block a user