KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > html > Page


1 // ========================================================================
2
// $Id: Page.java,v 1.5 2004/09/23 02:15:15 gregwilkins Exp $
3
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.html;
17 import java.io.IOException JavaDoc;
18 import java.io.Writer JavaDoc;
19 import java.util.Dictionary JavaDoc;
20 import java.util.Hashtable JavaDoc;
21
22 /* --------------------------------------------------------------------- */
23 /** HTML Page.
24  * A HTML Page extends composite with the addition of the HTML Header
25  * tags, fields and elements.
26  * Furthermore, individual parts of the page may be written or the
27  * progressive page be output with flush.
28  * <p>
29  * Pages contain parameters and named sections. These are used by
30  * derived Page classes that implement a Look and Feel. Page users
31  * may add to name sections such as "Margin" or "Footer" and set
32  * parameters such as "HelpUrl" without knowledge of how the look and feel
33  * will arrange these. To assist with standard look and feel creation
34  * Page defines a set of standard names for many common parameters
35  * and sections.
36  * <p>
37  * If named sections are used, the page constructor or completeSections
38  * must add the named section to the page in the appropriate places.
39  * If named sections are not added to the page, then they can only be
40  * written with an explicit call to write(out,"section",end);
41  * Changes in behaviour to section creation and adding, should be controlled
42  * via page properties.
43  * <p>
44  * @see Composite
45  * @version $Id: Page.java,v 1.5 2004/09/23 02:15:15 gregwilkins Exp $
46  * @author Greg Wilkins
47  */

48 public class Page extends Composite
49 {
50     /* ----------------------------------------------------------------- */
51     public static final String JavaDoc
52         Request="Request",
53         Response="Response",
54         Header="Header",
55         Title="Title",
56         Section="Section",
57         HeaderSize="HdrSize", // HeaderSize string suitable for FRAMESET
58
Footer="Footer",
59         FooterSize="FtrSize", // FooterSize string suitable for FRAMESET
60
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 JavaDoc properties = new Hashtable JavaDoc(10);
86
87     /* ----------------------------------------------------------------- */
88     Hashtable JavaDoc sections = new Hashtable JavaDoc(10);
89     private Composite head= new Composite();
90     private String JavaDoc 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 JavaDoc title)
102     {
103         title(title);
104     }
105
106     /* ----------------------------------------------------------------- */
107     public Page(String JavaDoc title, String JavaDoc attributes)
108     {
109         title(title);
110         attribute(attributes);
111     }
112
113     /* ----------------------------------------------------------------- */
114     /** Set page title.
115      * @return This Page (for chained commands)
116      */

117     public Page title(String JavaDoc title)
118     {
119         properties.put(Title,title);
120         String JavaDoc heading = (String JavaDoc)properties.get(Heading);
121         if (heading==null||heading.equals(NoTitle))
122             properties.put(Heading,title);
123         return this;
124     }
125
126     /* ----------------------------------------------------------------- */
127     /** Add element or object to the page header.
128      * @param o The Object to add. If it is a String or Element, it is
129      * added directly, otherwise toString() is called.
130      * @return This Page (for chained commands)
131      */

132     public Page addHeader(Object JavaDoc o)
133     {
134         head.add("\n");
135         head.add(o);
136         return this;
137     }
138   
139     /* ----------------------------------------------------------------- */
140     /** Set page background image.
141      * @return This Page (for chained commands)
142      */

143     public final Page setBackGroundImage(String JavaDoc bg)
144     {
145         attribute("background",bg);
146         return this;
147     }
148   
149     /* ----------------------------------------------------------------- */
150     /** Set page background color.
151      * @return This Page (for chained commands)
152      */

153     public final Page setBackGroundColor(String JavaDoc color)
154     {
155         properties.put(BgColour,color);
156         attribute("bgcolor",color);
157         return this;
158     }
159   
160     /* ----------------------------------------------------------------- */
161     /** Set the URL Base for the Page.
162      * @param target Default link target, null if none.
163      * @param href Default absolute href, null if none.
164      * @return This Page (for chained commands)
165      */

166     public final Page setBase(String JavaDoc target, String JavaDoc href)
167     {
168         base="<base " +
169             ((target!=null)?("TARGET=\""+target+"\""):"") +
170             ((href!=null)?("HREF=\""+href+"\""):"") +
171             ">";
172         return this;
173     }
174
175     /* ----------------------------------------------------------------- */
176     /** Write the entire page by calling:<br>
177      * writeHtmlHead(out)<br>
178      * writeBodyTag(out)<br>
179      * writeElements(out)<br>
180      * writeHtmlEnd(out)
181      */

182     public void write(Writer JavaDoc out)
183          throws IOException JavaDoc
184     {
185         writeHtmlHead(out);
186         writeBodyTag(out);
187         writeElements(out);
188         writeHtmlEnd(out);
189     }
190     
191     /* ------------------------------------------------------------ */
192     /** Write HTML page head tags.
193      * Write tags &ltHTML&gt&lthead&gt .... &lt/head&gt
194      */

195     public void writeHtmlHead(Writer JavaDoc out)
196          throws IOException JavaDoc
197     {
198         if (!writtenHtmlHead)
199         {
200             writtenHtmlHead=true;
201             completeSections();
202             out.write("<html><head>");
203             String JavaDoc title=(String JavaDoc)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     /** Write HTML page body tag.
214      * Write tags &ltBODY page attributes&gt.
215      */

216     public void writeBodyTag(Writer JavaDoc out)
217          throws IOException JavaDoc
218     {
219         if (!writtenBodyTag)
220         {
221             writtenBodyTag = true;
222             out.write("<body "+attributes()+">\n");
223         }
224     }
225
226     /* ------------------------------------------------------------ */
227     /** Write end BODY and end HTML tags.
228      */

229     public void writeHtmlEnd(Writer JavaDoc out)
230          throws IOException JavaDoc
231     {
232         out.write("\n</body>\n");
233         out.write("</html>\n");
234     }
235     
236     /* ------------------------------------------------------------ */
237     /** Write any body elements of the page.
238      */

239     public void writeElements(Writer JavaDoc out)
240          throws IOException JavaDoc
241     {
242         super.write(out);
243     }
244     
245     /* ------------------------------------------------------------ */
246     /** Write page section.
247      * The page is written containing only the named section.
248      * If a head and bodyTag have not been written, then they
249      * are written before the section. If endHtml is true, the
250      * end HTML tag is also written.
251      * If the named section is Content and it cannot be found,
252      * then the normal page contents are written.
253      */

254     public void write(Writer JavaDoc out,
255                       String JavaDoc section,
256                       boolean endHtml)
257          throws IOException JavaDoc
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     /* Flush the current contents of the page.
276      * writeHtmlEnd() is not called and should either be
277      * explicitly called or called via an eventual call to write()
278      */

279     public void flush(Writer JavaDoc out)
280          throws IOException JavaDoc
281     {
282         writeHtmlHead(out);
283         writeBodyTag(out);
284         super.flush(out);
285     }
286     
287     /* ------------------------------------------------------------ */
288     /* Reset the page status to not written.
289      * This is useful if you want to send a page more than once.
290      */

291      public void rewind()
292     {
293         writtenHtmlHead = false;
294         writtenBodyTag = false;
295     }
296     
297     /* ------------------------------------------------------------ */
298     /** Access the page properties. It is up to a derived Page class
299      * to interpret these properties.
300      */

301     public Dictionary JavaDoc properties()
302     {
303         return properties;
304     }
305
306     /* ------------------------------------------------------------ */
307     /** Return the preferred FrameSet to be used with a specialized Page.
308      * The Frames will be named after the sections they are to
309      * contain.
310      * The default implementation returns null
311      */

312     public FrameSet frameSet()
313     {
314         return null;
315     }
316
317     /* ------------------------------------------------------------ */
318     /** Set a composite as a named section. Other Page users may.
319      * add to the section by calling addTo(). It is up to the section
320      * creator to add the section to the page in it appropriate position.
321      */

322     public void setSection(String JavaDoc section, Composite composite)
323     {
324         sections.put(section,composite);
325     }
326     
327     /* ------------------------------------------------------------ */
328     /** Set a composite as a named section and add it to the.
329      * contents of the page
330      */

331     public void addSection(String JavaDoc section, Composite composite)
332     {
333         sections.put(section,composite);
334         add(composite);
335     }
336     
337     /* ------------------------------------------------------------ */
338     /** Get a composite as a named section.
339      */

340     public Composite getSection(String JavaDoc section)
341     {
342         return (Composite)sections.get(section);
343     }
344
345     /* ------------------------------------------------------------ */
346     /** Add content to a named sections. If the named section cannot.
347      * be found, the content is added to the page.
348      */

349     public void addTo(String JavaDoc section, Object JavaDoc 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     /** This call back is called just before writeHeaders() actually
360      * writes the HTML page headers. It can be implemented by a derived
361      * Page class to complete a named section after the rest of the Page
362      * has been created and appropriate properties set.
363      */

364     protected void completeSections()
365     {
366     }
367 }
368
Popular Tags