001: // ========================================================================
002: // $Id: Page.java,v 1.5 2004/09/23 02:15:15 gregwilkins Exp $
003: // Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
004: // ------------------------------------------------------------------------
005: // Licensed under the Apache License, Version 2.0 (the "License");
006: // you may not use this file except in compliance with the License.
007: // You may obtain a copy of the License at
008: // http://www.apache.org/licenses/LICENSE-2.0
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014: // ========================================================================
015:
016: package org.mortbay.html;
017:
018: import java.io.IOException;
019: import java.io.Writer;
020: import java.util.Dictionary;
021: import java.util.Hashtable;
022:
023: /* --------------------------------------------------------------------- */
024: /** HTML Page.
025: * A HTML Page extends composite with the addition of the HTML Header
026: * tags, fields and elements.
027: * Furthermore, individual parts of the page may be written or the
028: * progressive page be output with flush.
029: * <p>
030: * Pages contain parameters and named sections. These are used by
031: * derived Page classes that implement a Look and Feel. Page users
032: * may add to name sections such as "Margin" or "Footer" and set
033: * parameters such as "HelpUrl" without knowledge of how the look and feel
034: * will arrange these. To assist with standard look and feel creation
035: * Page defines a set of standard names for many common parameters
036: * and sections.
037: * <p>
038: * If named sections are used, the page constructor or completeSections
039: * must add the named section to the page in the appropriate places.
040: * If named sections are not added to the page, then they can only be
041: * written with an explicit call to write(out,"section",end);
042: * Changes in behaviour to section creation and adding, should be controlled
043: * via page properties.
044: * <p>
045: * @deprecated Unless somebody steps forward to update and maintain this package
046: * @see Composite
047: * @version $Id: Page.java,v 1.5 2004/09/23 02:15:15 gregwilkins Exp $
048: * @author Greg Wilkins
049: */
050: public class Page extends Composite {
051: /* ----------------------------------------------------------------- */
052: public static final String Request = "Request",
053: Response = "Response",
054: Header = "Header",
055: Title = "Title",
056: Section = "Section",
057: HeaderSize = "HdrSize", // HeaderSize string suitable for FRAMESET
058: Footer = "Footer",
059: FooterSize = "FtrSize", // FooterSize string suitable for FRAMESET
060: Content = "Content", ContentSize = "CntSize",
061: Margin = "Margin", MarginSize = "MrgSize",
062: LeftMargin = "Left", LeftMarginSize = "LMSize",
063: RightMargin = "Right", RightMarginSize = "RMSize",
064: Help = "Help", Home = "Home", Heading = "Heading",
065: Up = "Up", Prev = "Prev", Next = "Next", Back = "Back",
066: Target = "Target", BaseUrl = "BaseUrl",
067: FgColour = "FgColour", BgColour = "BgColour",
068: HighlightColour = "HlColour", PageType = "PageType",
069: NoTitle = "No Title";
070:
071: /* ----------------------------------------------------------------- */
072: protected Hashtable properties = new Hashtable(10);
073:
074: /* ----------------------------------------------------------------- */
075: Hashtable sections = new Hashtable(10);
076: private Composite head = new Composite();
077: private String base = "";
078: private boolean writtenHtmlHead = false;
079: private boolean writtenBodyTag = false;
080:
081: /* ----------------------------------------------------------------- */
082: public Page() {
083: this (NoTitle);
084: }
085:
086: /* ----------------------------------------------------------------- */
087: public Page(String title) {
088: title(title);
089: }
090:
091: /* ----------------------------------------------------------------- */
092: public Page(String title, String attributes) {
093: title(title);
094: attribute(attributes);
095: }
096:
097: /* ----------------------------------------------------------------- */
098: /** Set page title.
099: * @return This Page (for chained commands)
100: */
101: public Page title(String title) {
102: properties.put(Title, title);
103: String heading = (String) properties.get(Heading);
104: if (heading == null || heading.equals(NoTitle))
105: properties.put(Heading, title);
106: return this ;
107: }
108:
109: /* ----------------------------------------------------------------- */
110: /** Add element or object to the page header.
111: * @param o The Object to add. If it is a String or Element, it is
112: * added directly, otherwise toString() is called.
113: * @return This Page (for chained commands)
114: */
115: public Page addHeader(Object o) {
116: head.add("\n");
117: head.add(o);
118: return this ;
119: }
120:
121: /* ----------------------------------------------------------------- */
122: /** Set page background image.
123: * @return This Page (for chained commands)
124: */
125: public final Page setBackGroundImage(String bg) {
126: attribute("background", bg);
127: return this ;
128: }
129:
130: /* ----------------------------------------------------------------- */
131: /** Set page background color.
132: * @return This Page (for chained commands)
133: */
134: public final Page setBackGroundColor(String color) {
135: properties.put(BgColour, color);
136: attribute("bgcolor", color);
137: return this ;
138: }
139:
140: /* ----------------------------------------------------------------- */
141: /** Set the URL Base for the Page.
142: * @param target Default link target, null if none.
143: * @param href Default absolute href, null if none.
144: * @return This Page (for chained commands)
145: */
146: public final Page setBase(String target, String href) {
147: base = "<base "
148: + ((target != null) ? ("TARGET=\"" + target + "\"")
149: : "")
150: + ((href != null) ? ("HREF=\"" + href + "\"") : "")
151: + ">";
152: return this ;
153: }
154:
155: /* ----------------------------------------------------------------- */
156: /** Write the entire page by calling:<br>
157: * writeHtmlHead(out)<br>
158: * writeBodyTag(out)<br>
159: * writeElements(out)<br>
160: * writeHtmlEnd(out)
161: */
162: public void write(Writer out) throws IOException {
163: writeHtmlHead(out);
164: writeBodyTag(out);
165: writeElements(out);
166: writeHtmlEnd(out);
167: }
168:
169: /* ------------------------------------------------------------ */
170: /** Write HTML page head tags.
171: * Write tags <HTML><head> .... </head>
172: */
173: public void writeHtmlHead(Writer out) throws IOException {
174: if (!writtenHtmlHead) {
175: writtenHtmlHead = true;
176: completeSections();
177: out.write("<html><head>");
178: String title = (String) properties.get(Title);
179: if (title != null && title.length() > 0
180: && !title.equals(NoTitle))
181: out.write("<title>" + title + "</title>");
182: head.write(out);
183: out.write(base);
184: out.write("\n</head>\n");
185: }
186: }
187:
188: /* ------------------------------------------------------------ */
189: /** Write HTML page body tag.
190: * Write tags <BODY page attributes>.
191: */
192: public void writeBodyTag(Writer out) throws IOException {
193: if (!writtenBodyTag) {
194: writtenBodyTag = true;
195: out.write("<body " + attributes() + ">\n");
196: }
197: }
198:
199: /* ------------------------------------------------------------ */
200: /** Write end BODY and end HTML tags.
201: */
202: public void writeHtmlEnd(Writer out) throws IOException {
203: out.write("\n</body>\n");
204: out.write("</html>\n");
205: }
206:
207: /* ------------------------------------------------------------ */
208: /** Write any body elements of the page.
209: */
210: public void writeElements(Writer out) throws IOException {
211: super .write(out);
212: }
213:
214: /* ------------------------------------------------------------ */
215: /** Write page section.
216: * The page is written containing only the named section.
217: * If a head and bodyTag have not been written, then they
218: * are written before the section. If endHtml is true, the
219: * end HTML tag is also written.
220: * If the named section is Content and it cannot be found,
221: * then the normal page contents are written.
222: */
223: public void write(Writer out, String section, boolean endHtml)
224: throws IOException {
225: writeHtmlHead(out);
226: writeBodyTag(out);
227: Composite s = getSection(section);
228: if (s == null) {
229: if (section.equals(Content))
230: writeElements(out);
231: } else
232: s.write(out);
233: if (endHtml)
234: writeHtmlEnd(out);
235: out.flush();
236: }
237:
238: /* ------------------------------------------------------------ */
239: /* Flush the current contents of the page.
240: * writeHtmlEnd() is not called and should either be
241: * explicitly called or called via an eventual call to write()
242: */
243: public void flush(Writer out) throws IOException {
244: writeHtmlHead(out);
245: writeBodyTag(out);
246: super .flush(out);
247: }
248:
249: /* ------------------------------------------------------------ */
250: /* Reset the page status to not written.
251: * This is useful if you want to send a page more than once.
252: */
253: public void rewind() {
254: writtenHtmlHead = false;
255: writtenBodyTag = false;
256: }
257:
258: /* ------------------------------------------------------------ */
259: /** Access the page properties. It is up to a derived Page class
260: * to interpret these properties.
261: */
262: public Dictionary properties() {
263: return properties;
264: }
265:
266: /* ------------------------------------------------------------ */
267: /** Return the preferred FrameSet to be used with a specialized Page.
268: * The Frames will be named after the sections they are to
269: * contain.
270: * The default implementation returns null
271: */
272: public FrameSet frameSet() {
273: return null;
274: }
275:
276: /* ------------------------------------------------------------ */
277: /** Set a composite as a named section. Other Page users may.
278: * add to the section by calling addTo(). It is up to the section
279: * creator to add the section to the page in it appropriate position.
280: */
281: public void setSection(String section, Composite composite) {
282: sections.put(section, composite);
283: }
284:
285: /* ------------------------------------------------------------ */
286: /** Set a composite as a named section and add it to the.
287: * contents of the page
288: */
289: public void addSection(String section, Composite composite) {
290: sections.put(section, composite);
291: add(composite);
292: }
293:
294: /* ------------------------------------------------------------ */
295: /** Get a composite as a named section.
296: */
297: public Composite getSection(String section) {
298: return (Composite) sections.get(section);
299: }
300:
301: /* ------------------------------------------------------------ */
302: /** Add content to a named sections. If the named section cannot.
303: * be found, the content is added to the page.
304: */
305: public void addTo(String section, Object element) {
306: Composite s = (Composite) sections.get(section);
307: if (s == null)
308: add(element);
309: else
310: s.add(element);
311: }
312:
313: /* ------------------------------------------------------------ */
314: /** This call back is called just before writeHeaders() actually
315: * writes the HTML page headers. It can be implemented by a derived
316: * Page class to complete a named section after the rest of the Page
317: * has been created and appropriate properties set.
318: */
319: protected void completeSections() {
320: }
321: }
|