1 16 package web.layout; 17 18 import java.io.IOException ; 19 import java.io.Reader ; 20 import java.util.ArrayList ; 21 import java.util.Collections ; 22 import java.util.Hashtable ; 23 import java.util.List ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 import javax.servlet.jsp.JspException ; 28 import javax.servlet.jsp.PageContext ; 29 import javax.servlet.jsp.tagext.BodyTagSupport ; 30 31 import org.xml.sax.SAXException ; 32 33 import dlog4j.util.StringUtils; 34 35 39 public class LayoutTag extends BodyTagSupport { 40 41 protected final static int BUFFER_SIZE = 512; 42 protected final static String ROOT = "{ROOT}"; 43 44 public int doStartTag() throws JspException { 45 return EVAL_BODY_BUFFERED; 46 } 47 public int doEndTag() throws JspException { 48 Reader reader = getBodyContent().getReader(); 49 char[] buf = new char[BUFFER_SIZE]; 50 try{ 51 HttpServletRequest req = (HttpServletRequest )pageContext.getRequest(); 52 HttpServletResponse res = (HttpServletResponse )pageContext.getResponse(); 53 StringBuffer content1 = new StringBuffer (BUFFER_SIZE); 54 do{ 55 int rc = reader.read(buf); 56 if(rc>0) 57 content1.append(buf,0,rc); 58 if(rc<BUFFER_SIZE) 59 break; 60 }while(true); 61 String html = StringUtils.replaceIgnoreCase(content1.toString(), ROOT, req.getContextPath()); 62 List tokens = listTokens(html); 63 int curIdx = 0; 64 for(int i=0;i<tokens.size();i++){ 65 Token token = (Token)tokens.get(i); 66 pageContext.getOut().write(html.substring(curIdx,token.begin)); 67 try{ 68 if(ROOT.equals(token.name)){ 69 pageContext.getOut().write(req.getContextPath()); 70 } 71 else{ 72 String tokenPage = getTokenPage(token.name,token.page); 73 pageContext.include(tokenPage); 74 } 75 }catch (Exception e0) { 76 pageContext.getServletContext().log("LayoutTag.doEndTag page="+token.page,e0); 77 pageContext.setAttribute("excetion", e0, PageContext.REQUEST_SCOPE); 78 if(!errorPage.startsWith("/")) 79 pageContext.include(baseDir + errorPage); 80 else 81 pageContext.include(errorPage); 82 } 83 curIdx = token.begin + token.name.length(); 84 } 85 pageContext.getOut().write(html.substring(curIdx)); 86 }catch(Exception e1){ 87 pageContext.getServletContext().log("LayoutTag.doEndTag",e1); 88 } 89 return EVAL_PAGE; 90 } 91 92 98 protected String getTokenPage(String token, String page){ 99 String sPage; 100 if(mainPage==null || !"$BODY$".equals(token)) 101 sPage = page; 102 else 103 sPage = (String )pageContext.findAttribute(mainPage); 104 return (sPage.startsWith("/"))?sPage:(baseDir + sPage); 105 } 106 107 114 protected synchronized List listTokens(String content) throws Exception { 115 int hashcode = content.toString().hashCode(); 116 Integer layout_hashcode = (Integer )ht_hashcodes.get(id); 117 final int layout_hc = (layout_hashcode!=null)?layout_hashcode.intValue():-1; 118 final List last_tokens = (List )ht_tokens.get(id); 119 if(hashcode==layout_hc && last_tokens!=null) 120 return last_tokens; 121 List tokens = new ArrayList (); 122 LayoutConfig config = LayoutConfig.getConfig(pageContext.getServletContext()); 123 int pc = config.pageSize(); 124 for(int i=0;i<pc;i++){ 125 Page page = config.getPage(i); 126 int begin = content.indexOf(page.getName()); 127 if(begin!=-1){ 128 Token token = new Token(); 129 token.begin = begin; 130 token.name = page.getName(); 131 token.page = page.getUri(); 132 tokens.add(token); 133 } 134 } 135 int begin = content.indexOf(ROOT); 136 if(begin!=-1){ 137 Token token = new Token(); 138 token.begin = begin; 139 token.name = ROOT; 140 tokens.add(token); 141 } 142 Collections.sort(tokens); ht_hashcodes.put(id, new Integer (hashcode)); 145 ht_tokens.put(id, tokens); 146 return tokens; 147 } 148 149 private static Hashtable ht_tokens = new Hashtable (); 150 private static Hashtable ht_hashcodes = new Hashtable (); 151 152 155 private class Token implements Comparable { 156 public int ident; 157 public String name; 158 public int begin; 159 public String page; 160 161 public int compareTo(Object obj) { 162 return begin - ((Token)obj).begin; 163 } 164 public int hashCode() { 165 return begin; 166 } 167 }; 168 169 protected String mainPage; 170 protected String baseDir = "/WEB-INF/jsp/"; 171 protected String errorPage = "error/error.jsp"; 172 173 public String getMainPage() { 174 return mainPage; 175 } 176 public void setMainPage(String mainPage) { 177 this.mainPage = mainPage; 178 } 179 public String getBaseDir() { 180 return baseDir; 181 } 182 public void setBaseDir(String baseDir) { 183 if(!baseDir.endsWith("/")) 184 baseDir += "/"; 185 this.baseDir = baseDir; 186 } 187 public String getErrorPage() { 188 return errorPage; 189 } 190 public void setErrorPage(String errorPage) { 191 this.errorPage = errorPage; 192 } 193 } 194 | Popular Tags |