ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package jnpf.base.util.custom;
 
 
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.jetbrains.annotations.NotNull;
 
import java.io.*;
import java.util.Map;
import java.util.Properties;
 
public class CustomTemplateEngine extends AbstractTemplateEngine {
 
    private static final String DOT_VM = ".vm";
    private VelocityEngine velocityEngine;
 
    private Map<String, Object> customParams;
 
    private String path;
 
    public CustomTemplateEngine(String path) {
        this.path = path;
    }
 
    public CustomTemplateEngine(Map<String, Object> customParams, String path) {
        this.customParams = customParams;
        this.path = path;
    }
 
    @Override
    public CustomTemplateEngine init(ConfigBuilder configBuilder) {
        super.setConfigBuilder(configBuilder);
        if (null == this.velocityEngine) {
            Properties p = new Properties();
            p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, path);
            p.setProperty("ISO-8859-1", Constants.UTF_8);
            p.setProperty("output.encoding", Constants.UTF_8);
            this.velocityEngine = new VelocityEngine(p);
        }
 
        return this;
    }
 
    @Override
    public String writer(@NotNull Map<String, Object> objectMap, @NotNull String templateName, @NotNull String templateString) throws Exception {
        StringWriter writer = new StringWriter();
        this.velocityEngine.evaluate(new VelocityContext(objectMap), writer, templateName, templateString);
        return writer.toString();
    }
 
    @Override
    public void writer(Map<String, Object> objectMap, String templatePath, File outputFile) throws Exception {
        if (templatePath != null && !"".equals(templatePath.trim())) {
            Template template = this.velocityEngine.getTemplate(templatePath, ConstVal.UTF8);
            FileOutputStream fos = new FileOutputStream(outputFile);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, ConstVal.UTF8));
            if (customParams != null) {
                objectMap.putAll(customParams);
            }
            template.merge(new VelocityContext(objectMap), writer);
            writer.close();
        }
    }
 
    @Override
    public String templateFilePath(String filePath) {
        if (null != filePath && !filePath.contains(".vm")) {
            StringBuilder fp = new StringBuilder();
            fp.append(filePath).append(".vm");
            return fp.toString();
        } else {
            return filePath;
        }
    }
}