1 9 10 package com.opensymphony.module.sitemesh.parser; 11 12 import com.opensymphony.module.sitemesh.Page; 13 14 import javax.servlet.http.HttpServletRequest ; 15 import javax.servlet.http.HttpServletRequestWrapper ; 16 import java.io.*; 17 import java.util.HashMap ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 37 public abstract class AbstractPage implements Page { 38 42 private Map properties = new HashMap (); 43 44 45 protected char[] pageData = new char[0]; 46 47 48 private HttpServletRequest request; 49 50 public void writePage(Writer out) throws IOException { 51 out.write(pageData); 52 } 53 54 public String getPage() { 55 try { 56 StringWriter writer = new StringWriter(); 57 writePage(writer); 58 return writer.toString(); 59 } catch (IOException e) { 60 throw new IllegalStateException ("Could not get page " + e.getMessage()); 61 } 62 } 63 64 70 public abstract void writeBody(Writer out) throws IOException; 71 72 public String getBody() { 73 try { 74 StringWriter writer = new StringWriter(); 75 writeBody(writer); 76 return writer.toString(); 77 } catch (IOException e) { 78 throw new IllegalStateException ("Could not get body " + e.getMessage()); 79 } 80 } 81 82 83 public String getTitle() { 84 return noNull(getProperty("title")); 85 } 86 87 public int getContentLength() { 88 return pageData.length; 89 } 90 91 public String getProperty(String name) { 92 if (!isPropertySet(name)) return null; 93 return (String )properties.get(name); 94 } 95 96 public int getIntProperty(String name) { 97 try { 98 return Integer.parseInt(noNull(getProperty(name))); 99 } 100 catch (NumberFormatException e) { 101 return 0; 102 } 103 } 104 105 public long getLongProperty(String name) { 106 try { 107 return Long.parseLong(noNull(getProperty(name))); 108 } 109 catch (NumberFormatException e) { 110 return 0; 111 } 112 } 113 114 public boolean getBooleanProperty(String name) { 115 String property = getProperty(name); 116 if (property == null || property.trim().length() == 0) return false; 117 switch (property.charAt(0)) { 118 case '1': 119 case 't': 120 case 'T': 121 case 'y': 122 case 'Y': 123 return true; 124 default: 125 return false; 126 } 127 } 128 129 public boolean isPropertySet(String name) { 130 return properties.containsKey(name); 131 } 132 133 public String [] getPropertyKeys() { 134 synchronized(properties) { 135 Set keys = properties.keySet(); 136 return (String [])keys.toArray(new String [keys.size()]); 137 } 138 } 139 140 public Map getProperties() { 141 return properties; 142 } 143 144 145 public HttpServletRequest getRequest() { 146 return request; 147 } 148 149 154 public void setRequest(HttpServletRequest request) { 155 this.request = new PageRequest(request); 156 } 157 158 164 public void addProperty(String name, String value) { 165 properties.put(name, value); 166 } 167 168 169 protected static String noNull(String in) { 170 return in == null ? "" : in; 171 } 172 } 173 174 class PageRequest extends HttpServletRequestWrapper { 175 176 private String requestURI, method, pathInfo, pathTranslated, queryString, servletPath; 177 178 public PageRequest(HttpServletRequest request) { 179 super(request); 180 requestURI = request.getRequestURI(); 181 method = request.getMethod(); 182 pathInfo = request.getPathInfo(); 183 pathTranslated = request.getPathTranslated(); 184 queryString = request.getQueryString(); 185 servletPath = request.getServletPath(); 186 } 187 188 public String getRequestURI() { 189 return requestURI; 190 } 191 192 public String getMethod() { 193 return method; 194 } 195 196 public String getPathInfo() { 197 return pathInfo; 198 } 199 200 public String getPathTranslated() { 201 return pathTranslated; 202 } 203 204 public String getQueryString() { 205 return queryString; 206 } 207 208 public String getServletPath() { 209 return servletPath; 210 } 211 } 212 | Popular Tags |