1 16 package org.mortbay.html; 17 import java.io.IOException ; 18 import java.io.Writer ; 19 import java.util.Dictionary ; 20 import java.util.Hashtable ; 21 22 23 48 public class Page extends Composite 49 { 50 51 public static final String 52 Request="Request", 53 Response="Response", 54 Header="Header", 55 Title="Title", 56 Section="Section", 57 HeaderSize="HdrSize", Footer="Footer", 59 FooterSize="FtrSize", Content="Content", 61 ContentSize="CntSize", 62 Margin="Margin", 63 MarginSize="MrgSize", 64 LeftMargin="Left", 65 LeftMarginSize="LMSize", 66 RightMargin="Right", 67 RightMarginSize="RMSize", 68 Help="Help", 69 Home="Home", 70 Heading="Heading", 71 Up="Up", 72 Prev="Prev", 73 Next="Next", 74 Back="Back", 75 Target="Target", 76 BaseUrl="BaseUrl", 77 FgColour="FgColour", 78 BgColour="BgColour", 79 HighlightColour="HlColour", 80 PageType="PageType", 81 NoTitle="No Title" 82 ; 83 84 85 protected Hashtable properties = new Hashtable (10); 86 87 88 Hashtable sections = new Hashtable (10); 89 private Composite head= new Composite(); 90 private String base=""; 91 private boolean writtenHtmlHead = false; 92 private boolean writtenBodyTag = false; 93 94 95 public Page() 96 { 97 this(NoTitle); 98 } 99 100 101 public Page(String title) 102 { 103 title(title); 104 } 105 106 107 public Page(String title, String attributes) 108 { 109 title(title); 110 attribute(attributes); 111 } 112 113 114 117 public Page title(String title) 118 { 119 properties.put(Title,title); 120 String heading = (String )properties.get(Heading); 121 if (heading==null||heading.equals(NoTitle)) 122 properties.put(Heading,title); 123 return this; 124 } 125 126 127 132 public Page addHeader(Object o) 133 { 134 head.add("\n"); 135 head.add(o); 136 return this; 137 } 138 139 140 143 public final Page setBackGroundImage(String bg) 144 { 145 attribute("background",bg); 146 return this; 147 } 148 149 150 153 public final Page setBackGroundColor(String color) 154 { 155 properties.put(BgColour,color); 156 attribute("bgcolor",color); 157 return this; 158 } 159 160 161 166 public final Page setBase(String target, String href) 167 { 168 base="<base " + 169 ((target!=null)?("TARGET=\""+target+"\""):"") + 170 ((href!=null)?("HREF=\""+href+"\""):"") + 171 ">"; 172 return this; 173 } 174 175 176 182 public void write(Writer out) 183 throws IOException 184 { 185 writeHtmlHead(out); 186 writeBodyTag(out); 187 writeElements(out); 188 writeHtmlEnd(out); 189 } 190 191 192 195 public void writeHtmlHead(Writer out) 196 throws IOException 197 { 198 if (!writtenHtmlHead) 199 { 200 writtenHtmlHead=true; 201 completeSections(); 202 out.write("<html><head>"); 203 String title=(String )properties.get(Title); 204 if (title!=null && title.length()>0 && !title.equals(NoTitle)) 205 out.write("<title>"+title+"</title>"); 206 head.write(out); 207 out.write(base); 208 out.write("\n</head>\n"); 209 } 210 } 211 212 213 216 public void writeBodyTag(Writer out) 217 throws IOException 218 { 219 if (!writtenBodyTag) 220 { 221 writtenBodyTag = true; 222 out.write("<body "+attributes()+">\n"); 223 } 224 } 225 226 227 229 public void writeHtmlEnd(Writer out) 230 throws IOException 231 { 232 out.write("\n</body>\n"); 233 out.write("</html>\n"); 234 } 235 236 237 239 public void writeElements(Writer out) 240 throws IOException 241 { 242 super.write(out); 243 } 244 245 246 254 public void write(Writer out, 255 String section, 256 boolean endHtml) 257 throws IOException 258 { 259 writeHtmlHead(out); 260 writeBodyTag(out); 261 Composite s = getSection(section); 262 if (s==null) 263 { 264 if (section.equals(Content)) 265 writeElements(out); 266 } 267 else 268 s.write(out); 269 if (endHtml) 270 writeHtmlEnd(out); 271 out.flush(); 272 } 273 274 275 279 public void flush(Writer out) 280 throws IOException 281 { 282 writeHtmlHead(out); 283 writeBodyTag(out); 284 super.flush(out); 285 } 286 287 288 291 public void rewind() 292 { 293 writtenHtmlHead = false; 294 writtenBodyTag = false; 295 } 296 297 298 301 public Dictionary properties() 302 { 303 return properties; 304 } 305 306 307 312 public FrameSet frameSet() 313 { 314 return null; 315 } 316 317 318 322 public void setSection(String section, Composite composite) 323 { 324 sections.put(section,composite); 325 } 326 327 328 331 public void addSection(String section, Composite composite) 332 { 333 sections.put(section,composite); 334 add(composite); 335 } 336 337 338 340 public Composite getSection(String section) 341 { 342 return (Composite)sections.get(section); 343 } 344 345 346 349 public void addTo(String section, Object element) 350 { 351 Composite s = (Composite)sections.get(section); 352 if (s==null) 353 add(element); 354 else 355 s.add(element); 356 } 357 358 359 364 protected void completeSections() 365 { 366 } 367 } 368 | Popular Tags |