1 64 65 package com.jcorporate.expresso.services.html; 66 67 import com.jcorporate.expresso.core.misc.ConfigManager; 68 import com.jcorporate.expresso.core.misc.StringUtil; 69 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 70 import com.jcorporate.expresso.services.dbobj.Setup; 71 import org.apache.log4j.Logger; 72 73 import javax.servlet.http.HttpServletRequest ; 74 import javax.servlet.http.HttpServletResponse ; 75 import java.io.IOException ; 76 import java.io.PrintWriter ; 77 import java.util.Enumeration ; 78 79 80 87 public class Page 88 extends HtmlElement { 89 90 private static final String thisClass = Page.class.getName() + "."; 91 private String title = ("No Title"); 92 private String bgColor = null; 93 private String dbName = "default"; 94 private static Logger log = Logger.getLogger(Page.class); 95 96 101 public Page() 102 throws HtmlException { 103 super(); 104 } 105 106 113 public Page(String newTitle) 114 throws HtmlException { 115 super(newTitle); 116 setTitle(newTitle); 117 } 118 119 125 public synchronized void add(HtmlElement newElement) 126 throws HtmlException { 127 String myName = (thisClass + "add(HtmlElement)"); 128 129 if (newElement instanceof Page) { 130 throw new HtmlException(myName + ":Can't add a Page to Page " + 131 getName()); 132 } 133 134 super.add(newElement); 135 } 136 137 138 public synchronized void display(HttpServletRequest req, 139 HttpServletResponse res, String charset) 140 throws HtmlException { 141 if (StringUtil.notNull(charset).equals("")) { 142 charset = "ISO-8859-1"; 143 } 144 145 res.setContentType("text/html; charset=" + charset); 146 147 try { 148 PrintWriter out = res.getWriter(); 149 display(out); 150 } catch (IOException ie) { 151 throw new HtmlException(ie); 152 } 153 } 154 155 156 164 public synchronized void display(PrintWriter out, int depth) 165 throws HtmlException { 166 display(out); 167 } 168 169 179 protected synchronized void display(PrintWriter out) 180 throws HtmlException { 181 String myName = (thisClass + "display(PrintWriter)"); 182 183 if (contents.size() == 0) { 184 throw new HtmlException(myName + ":Page " + getName() + 185 " has no contents"); 186 } 187 188 out.println("<html>"); 189 out.println("<head>"); 190 out.println("<title>" + title + "</title>"); 191 out.println("<META NAME=\"generator\" CONTENT=\"JCorporate's Expresso\">"); 192 193 String contextPath = ConfigManager.getContextPath(); 194 String cssSuffix = null; 195 try { 196 cssSuffix = Setup.getValue(dbName, "defaultCSS"); 197 } catch (com.jcorporate.expresso.core.db.DBException dbe) { 198 log.warn("Unable to get setup value 'defaultCSS'. You may need to re-run DBCreate get the entry added." 199 , dbe); 200 } 201 202 FastStringBuffer styleURL = FastStringBuffer.getInstance(); 203 try { 204 styleURL.append("<link rel=\"stylesheet\" type=\"text/css\" HREF=\""); 205 styleURL.append(contextPath); 206 styleURL.append("/expresso"); 207 styleURL.append("/style/"); 208 styleURL.append("default"); 209 210 if (cssSuffix != null && cssSuffix.length() > 0) { 211 styleURL.append("-"); 212 styleURL.append(cssSuffix); 213 } 214 styleURL.append(".css"); 215 216 styleURL.append("\">"); 217 out.println(styleURL.toString()); 218 } finally { 219 styleURL.release(); 220 styleURL = null; 221 } 222 223 224 251 out.println("</head>"); 252 out.print("<body"); 253 254 if (cSSClass != null) { 255 out.print(" class=\"" + cSSClass + "\""); 256 } 257 if (cSSID != null) { 258 out.print(" id=\"" + cSSID + "\""); 259 } 260 if (bgColor != null) { 261 out.println(" bgcolor=\"" + bgColor + "\""); 262 } 263 264 out.println(">"); 265 266 HtmlElement oneElement = null; 267 268 for (Enumeration e = contents.elements(); e.hasMoreElements();) { 269 oneElement = (HtmlElement) e.nextElement(); 270 oneElement.display(out, 1); 271 } 272 273 out.println("</body>"); 274 out.println("</html>"); 275 setDisplayed(); 276 } 277 278 279 public synchronized void displayPartial(HttpServletRequest req, 280 HttpServletResponse res, 281 String charset) 282 throws HtmlException { 283 if (StringUtil.notNull(charset).equals("")) { 284 charset = "ISO-8859-1"; 285 } 286 287 res.setContentType("text/html; charset=" + charset); 288 289 try { 290 PrintWriter out = res.getWriter(); 291 displayPartial(out); 292 } catch (IOException ie) { 293 throw new HtmlException(ie); 294 } 295 } 296 297 308 protected synchronized void displayPartial(PrintWriter out) 309 throws HtmlException { 310 String myName = (thisClass + "display(PrintWriter)"); 311 312 if (contents.size() == 0) { 313 throw new HtmlException(myName + ":Page " + getName() + 314 " has no contents"); 315 } 316 317 HtmlElement oneElement = null; 318 319 for (Enumeration e = contents.elements(); e.hasMoreElements();) { 320 oneElement = (HtmlElement) e.nextElement(); 321 oneElement.display(out, 0); 322 } 323 324 setDisplayed(); 325 } 326 327 328 334 public synchronized void setBGColor(String newColor) 335 throws HtmlException { 336 String myName = (thisClass + "setBGColor(String)"); 337 338 if (newColor == null) { 339 throw new HtmlException(myName + 340 ":Background color cannot be null"); 341 } 342 343 bgColor = newColor; 344 } 345 346 347 355 public synchronized void setDBName(String newDBName) { 356 if (StringUtil.notNull(newDBName).equals("")) { 357 dbName = "default"; 358 } else { 359 dbName = newDBName; 360 } 361 } 362 363 368 public synchronized void setTitle(String newTitle) { 369 if (newTitle != null) { 370 title = newTitle; 371 } 372 } 373 374 375 } 376 | Popular Tags |