FreeMarkerUtils

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;


public class FreeMarkerUtils {



private static final Logger logger = LoggerFactory
.getLogger(FreeMarkerUtils.class);


private static boolean isInit = false;


private static String appPath = null;


private static final String ENCODING = "UTF-8";


private static Configuration config = new Configuration();


public static final String PATH_SEPARATOR = "/";


public static void initFreeMarker(String applicationPath) {
if (!(isInit)) {
try {
appPath = applicationPath;
// 加载模版
File file = new File(new StringBuffer(appPath).append(
File.separator).toString());
// 设置要解析的模板所在的目录,并加载模板文件
config.setDirectoryForTemplateLoading(file);

// 设置文件编码为UTF-8
config.setEncoding(Locale.getDefault(), ENCODING);
isInit = true;
} catch (IOException e) {
logger.error("初始化FreeMarker配置出错", e);
}
}
}


public static void crateFile(Map, Object> data,
String templateFileName, String outFileName, boolean isAbsPath) {
if (!isInit) {
System.out
.println("FreeMarker模板引擎未初始化,请确认已经调用initFreeMarker()方法对其进行了初始化");
}
Writer out = null;
FileOutputStream fileOPStream = null;
try {

// 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致
Template template = config.getTemplate(templateFileName, ENCODING);
template.setEncoding(ENCODING);
// 生成文件路径
// 如果是绝对路径则直接使用
if (isAbsPath) {
outFileName = new StringBuffer(outFileName).toString();
} else {
// 相对路径则使用默认的appPath加上输入的文件路径
outFileName = new StringBuffer(appPath).append(File.separator)
.append(outFileName).toString();
}
String outPath = outFileName.substring(0,
outFileName.lastIndexOf(PATH_SEPARATOR));
// 创建文件目录
FileUtils.forceMkdir(new File(outPath));
File outFile = new File(outFileName);
fileOPStream = new FileOutputStream(outFile);
out = new OutputStreamWriter(fileOPStream, ENCODING);

// 处理模版
template.process(data, out);

out.flush();
logger.info("由模板文件" + templateFileName + "生成" + outFileName + "成功.");
} catch (Exception e) {
logger.error("由模板文件" + templateFileName + "生成" + outFileName
+ "出错.", e);
} finally {
try {
if (out != null) {
out.close();
}
if(fileOPStream != null){
fileOPStream.close();
}

} catch (IOException e) {
logger.error("关闭Write对象出错", e);
}
}
}


public static String tpl2String(String tpl, Object model) throws IOException, TemplateException {
Configuration cfg = new Configuration();
StringTemplateLoader stringLoader = new StringTemplateLoader();
stringLoader.putTemplate("DRL_Template", tpl);
cfg.setTemplateLoader(stringLoader);
StringWriter out = new StringWriter();
try {
Template template = cfg.getTemplate("DRL_Template", ENCODING);
template.process(model, out);
} catch (IOException e) {
logger.error("读取模板文件错误。", e);
throw new IOException("读取模板文件错误。",e);
} catch (TemplateException e) {
logger.error("freemarker 处理错误。", e);
throw new TemplateException("freemarker 处理错误。", null);
}
return out.toString();
}


public static String tpl2String(String tpl, HashMap, Object> model,String encoding) {
Configuration cfg = new Configuration();
StringTemplateLoader stringLoader = new StringTemplateLoader();
stringLoader.putTemplate("DRL_Template", tpl);
cfg.setTemplateLoader(stringLoader);
StringWriter out = new StringWriter();
try {
Template template = cfg.getTemplate("DRL_Template", encoding);
template.process(model, out);
} catch (IOException e) {
logger.error("读取模板文件错误。", e);
} catch (TemplateException e) {
logger.error("freemarker 处理错误。", e);
}
return out.toString();
}




public static Template getFtlTemplate(String tpl) throws IOException, TemplateException {
Configuration cfg = new Configuration();
StringTemplateLoader stringLoader = new StringTemplateLoader();
stringLoader.putTemplate("DRL_Template", tpl);
cfg.setTemplateLoader(stringLoader);
StringWriter out = new StringWriter();
Template template ;
try {
template = cfg.getTemplate("DRL_Template", ENCODING);
//template.process(model, out);
} catch (IOException e) {
logger.error("读取模板文件错误。", e);
throw new IOException("读取模板文件错误。",e);
} 
return template;
}

}

@Override
public String creditReport(String event_id) {
Map, Object> eventInfo = caseDao.queryAppIdAndScenarioByEventId(event_id);
logger.info("信用报告查询结果:{}", eventInfo);
if (eventInfo == null || eventInfo.isEmpty()) {
throw new CaseException("无效事件号");
}
JSONObject riskDataMap = caseDao.queryRiskData((Integer) eventInfo.get("appId")
, (String) eventInfo.get("scenario"), event_id);
logger.info("creditReport----riskDataMap:" + riskDataMap);

HashMap, Object> dcsMap = processData(riskDataMap);
processBasicData(dcsMap, eventInfo);

//将对应码表填入map中
String jc = new ParserUtil().ReadFile("bstTable", "BASIC_MSC");
JSONObject jo = JSONObject.parseObject(jc);
dcsMap.put("BASIC_MSC", jo);
if (StringUtils.isBlank(DCSServiceId)) {
DCSServiceId = bstSingleSearchService.getDcsServiceId();
}
for (String id : DCSServiceId.split(",")) {
String JsonContext = new ParserUtil().ReadFile("bstTable", id);
JSONObject jsonObj = JSONObject.parseObject(JsonContext);
dcsMap.put("table_" + id, jsonObj);
}
logger.info("Freemarker开始转换,dcsMap:" + dcsMap);
String html = this.getFtlTemplate(dcsMap, "UTF-8");
logger.info("Freemarker转换完成,html:" + html);
return html;
}

private String getFtlTemplate(HashMap, Object> map, String encoding) {
String tplString = "";
try {
tplString = FreeMarkerUtils.tpl2String(creditBstTpl, map);

} catch (Exception e) {
logger.info("Freemarker转换有问题:" + e.getMessage());
}
return tplString;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值