/* * Copyright 2012-2013 The Haohui Network Corporation */ package com.haohui.baidamei.client.web.json.template; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.URISyntaxException; import java.net.URL; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * <pre> * 模板资源读取工具 * </pre> * * @project baidamei * @author cevencheng <cevencheng@gmail.com> * @create 2012-11-24 下午9:21:51 */ public class TemplateUtils { public static URL getResource(String name) { return TemplateUtils.class.getResource(name); } /** * t1: 支持模板注释 和 jsp 代码级别注释 * <#-- freemarker 注释 --> * <%!-- jsp 代码级别 注释 --> * * @param templateName * @param encoding * @return * @throws URISyntaxException */ public static String processTemplateIntoString(String templateName, String encoding) { URL url = TemplateUtils.class.getResource(templateName); if (null == url) { throw new RuntimeException("模板文件[com/haohui/baidamei/client/web/json/template" + templateName + "]不存在"); } if (null == encoding) { encoding = "utf-8"; } encoding = encoding.trim(); StringBuilder str = new StringBuilder(); FileInputStream fs = null; InputStreamReader isr = null; String content = null; try { fs = new FileInputStream(new File(url.toURI())); isr = new InputStreamReader(fs, encoding); // 方法2:自己实现 buffer char[] buffer = new char[1024]; int len = 0; while ((len = isr.read(buffer)) > 0) { str.append(buffer, 0, len); } content = str.toString(); String parttern = "<#--[\\w\\W\r\\n]*?-->"; Pattern p1 = Pattern.compile(parttern); Matcher m1 = p1.matcher(content); content = m1.replaceAll(""); //去掉模板注释 return content; } catch (Exception e) { throw new RuntimeException(e.getMessage(), e.getCause()); } finally { try { isr.close(); fs.close(); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e.getCause()); } } } public static String processTemplateIntoString(String templateName, String encoding, Map<Object, Object> model) { URL url = TemplateUtils.class.getResource(templateName); if (null == url) { throw new RuntimeException("模板文件[com/haohui/baidamei/client/web/json/template" + templateName + "]不存在"); } if (null == encoding) { encoding = "utf-8"; } encoding = encoding.trim(); StringBuilder str = new StringBuilder(); FileInputStream fs = null; InputStreamReader isr = null; String content = null; try { fs = new FileInputStream(new File(url.toURI())); isr = new InputStreamReader(fs, encoding); // 方法2:自己实现 buffer char[] buffer = new char[1024]; int len = 0; while ((len = isr.read(buffer)) > 0) { str.append(buffer, 0, len); } content = str.toString(); String parttern = "<#--[\\w\\W\r\\n]*?-->"; Pattern p1 = Pattern.compile(parttern); Matcher m1 = p1.matcher(content); content = m1.replaceAll(""); //去掉模板注释 // 处理模板变量 #{name} -->> 成功 if (model.size() > 0) { for (Map.Entry<Object, Object> entry : model.entrySet()) { String regex = "#\\{" + entry.getKey() + "\\}"; content = content.replaceAll(regex, entry.getValue().toString()); } } return content; } catch (Exception e) { throw new RuntimeException(e.getMessage(), e.getCause()); } finally { try { isr.close(); fs.close(); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e.getCause()); } } } public static void main(String[] args) { processTemplateIntoString("sss", null); } }
最近下载更多