原文:自己在项目中写的简单的仿freemarker模板引擎工具
源代码下载地址:http://www.zuidaima.com/share/1550463381490688.htm
?
自己在项目中写的简单的仿freemarker模板引擎工具
1. 支持 类 freemarker 风格注释
<#-- 我是注释,你看不到我,你看不到我 *^_^* -->
2. 支持模型数据的自动填充替换, 模板中的变量名定义为 #{val}
例如: register-success-mail.tpl 恭喜: #{account} 注册成功 点击<a href="#{url}">这里</a> 激活账号
3. 暂时不支持 迭代、日期格式化 等等....
4. 祝您学习愉快 ....
大家有兴趣可以自行扩展,然后在 javaniu 上面分享
class="java" name="code">/* * 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> www.zuidaima.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/zuidaima/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("www.zuidaima.com", null); } }
?