1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- package cn.nosum.framework.mvc.v4.servlet;
- import cn.nosum.util.StringUtils;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.File;
- import java.io.RandomAccessFile;
- import java.util.Map;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * Created by Tom.
- */
- public class View {
- private File viewFile;
- public View(File templateFile) {
- this.viewFile = templateFile;
- }
- public void render(Map<String, ?> model, HttpServletRequest req, HttpServletResponse resp) throws Exception {
- StringBuffer sb = new StringBuffer();
- RandomAccessFile ra = new RandomAccessFile(this.viewFile,"r");
- String line = null;
- while (null != (line = ra.readLine())){
- line = new String(line.getBytes("ISO-8859-1"),"utf-8");
- Pattern pattern = Pattern.compile("¥\\{[^\\}]+\\}",Pattern.CASE_INSENSITIVE);
- Matcher matcher = pattern.matcher(line);
- while (matcher.find()){
- String paramName = matcher.group();
- paramName = paramName.replaceAll("¥\\{|\\}","");
- String paramValue = StringUtils.getString(model.get(paramName),"");
- line = matcher.replaceFirst(makeStringForRegExp(paramValue));
- matcher = pattern.matcher(line);
- }
- sb.append(line);
- }
- resp.setCharacterEncoding("utf-8");
- resp.getWriter().write(sb.toString());
- }
- //处理特殊字符
- public static String makeStringForRegExp(String str) {
- return str.replace("\\", "\\\\").replace("*", "\\*")
- .replace("+", "\\+").replace("|", "\\|")
- .replace("{", "\\{").replace("}", "\\}")
- .replace("(", "\\(").replace(")", "\\)")
- .replace("^", "\\^").replace("$", "\\$")
- .replace("[", "\\[").replace("]", "\\]")
- .replace("?", "\\?").replace(",", "\\,")
- .replace(".", "\\.").replace("&", "\\&");
- }
- }
|