freemark 工具封装
parent
67bc3237dc
commit
a0909c6c00
@ -1,83 +0,0 @@
|
||||
package com.supervision.util.freemark;
|
||||
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import com.supervision.domain.IrKnowledge;
|
||||
import com.supervision.service.IrKnowledgeService;
|
||||
import freemarker.cache.TemplateLoader;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
|
||||
/**
|
||||
* Created by xiaoj on 2016/9/8.
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class DatabaseTemplateLoader implements TemplateLoader {
|
||||
|
||||
private final IrKnowledgeService irKnowledgeService;
|
||||
|
||||
@Override
|
||||
public Object findTemplateSource(String templateId) {
|
||||
try {
|
||||
IrKnowledge irKnowledge = irKnowledgeService.getById(templateId);
|
||||
long lastModified = ObjUtil.isNull(irKnowledge.getUpdateTime()) ? 0L : irKnowledge.getUpdateTime().getNano();
|
||||
return new StringTemplateSource(templateId, irKnowledge.getResultTemplate(),lastModified);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLastModified(Object templateSource) {
|
||||
return ((StringTemplateSource) templateSource).lastModified;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reader getReader(Object templateSource, String encoding) {
|
||||
return new StringReader(((StringTemplateSource) templateSource).source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeTemplateSource(Object templateSource) {
|
||||
//do nothing
|
||||
}
|
||||
|
||||
private static class StringTemplateSource {
|
||||
private final String name;
|
||||
private final String source;
|
||||
private final long lastModified;
|
||||
|
||||
StringTemplateSource(String name, String source, long lastModified) {
|
||||
if (name == null) {
|
||||
throw new IllegalArgumentException("name == null");
|
||||
}
|
||||
if (source == null) {
|
||||
throw new IllegalArgumentException("source == null");
|
||||
}
|
||||
if (lastModified < -1L) {
|
||||
throw new IllegalArgumentException("lastModified < -1L");
|
||||
}
|
||||
this.name = name;
|
||||
this.source = source;
|
||||
this.lastModified = lastModified;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof StringTemplateSource) {
|
||||
return name.equals(((StringTemplateSource) obj).name);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package com.supervision.util.freemark;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class FreemarkerConfiguration {
|
||||
@Bean("databaseFreemarkerConfiguration")
|
||||
public freemarker.template.Configuration databaseTemplateLoader(DatabaseTemplateLoader databaseTemplateLoader) {
|
||||
freemarker.template.Configuration configuration = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_30);
|
||||
configuration.setTemplateLoader(databaseTemplateLoader);
|
||||
return configuration;
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue