76 lines
2.2 KiB
Java
76 lines
2.2 KiB
Java
1 year ago
|
package com.supervision.util.freemark;
|
||
|
|
||
|
import cn.hutool.core.util.StrUtil;
|
||
|
import cn.hutool.crypto.digest.MD5;
|
||
|
import freemarker.cache.StringTemplateLoader;
|
||
|
import freemarker.template.Configuration;
|
||
|
import freemarker.template.Template;
|
||
|
import freemarker.template.TemplateException;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.io.StringWriter;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
import java.util.Objects;
|
||
|
|
||
|
public class StringTemplateConfig {
|
||
|
|
||
|
private final Configuration configuration;
|
||
|
private final StringTemplateLoader loader;
|
||
|
|
||
|
private final Map<String, String> tempMap = new HashMap<>();
|
||
|
|
||
|
|
||
|
private static class SingletonHolder {
|
||
|
private static final StringTemplateConfig INSTANCE = new StringTemplateConfig();
|
||
|
}
|
||
|
|
||
|
private StringTemplateConfig() {
|
||
|
configuration = new Configuration(Configuration.VERSION_2_3_30);
|
||
|
loader = new StringTemplateLoader();
|
||
|
configuration.setTemplateLoader(loader);
|
||
|
}
|
||
|
|
||
|
public static StringTemplateConfig getInstance() {
|
||
|
return SingletonHolder.INSTANCE;
|
||
|
}
|
||
|
|
||
|
public Template getTemplate(String name) throws IOException {
|
||
|
if (!tempMap.containsKey(name)) {
|
||
|
return null;
|
||
|
}
|
||
|
return this.configuration.getTemplate(name);
|
||
|
}
|
||
|
|
||
|
public void putTemplate(String name, String content){
|
||
|
tempMap.put(name, content);
|
||
|
this.loader.putTemplate(name, content);
|
||
|
}
|
||
|
|
||
|
public boolean removeTemplate(String name){
|
||
|
if (tempMap.remove(name) != null){
|
||
|
return this.loader.removeTemplate(name);
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
public String process(String content, Map<String, Object> data) throws IOException, TemplateException {
|
||
|
if (StrUtil.isEmpty(content)) {
|
||
|
return null;
|
||
|
}
|
||
|
String name = contentToName(content);
|
||
|
if (Objects.isNull(tempMap.get(name))) {
|
||
|
this.putTemplate(name, content);
|
||
|
}
|
||
|
Template template = this.getTemplate(name);
|
||
|
StringWriter out = new StringWriter();
|
||
|
template.process(data, out);
|
||
|
return out.toString();
|
||
|
|
||
|
}
|
||
|
|
||
|
public String contentToName(String content){
|
||
|
return MD5.create().digestHex16(content);
|
||
|
}
|
||
|
}
|