KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ecs > XhtmlDocument


1 /*
2  * ====================================================================
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Jakarta Element Construction Set",
29  * "Jakarta ECS" , and "Apache Software Foundation" must not be used
30  * to endorse or promote products derived
31  * from this software without prior written permission. For written
32  * permission, please contact apache@apache.org.
33  *
34  * 5. Products derived from this software may not be called "Apache",
35  * "Jakarta Element Construction Set" nor "Jakarta ECS" nor may "Apache"
36  * appear in their names without prior written permission of the Apache Group.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals on behalf of the Apache Software Foundation. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  *
57  */

58 package org.apache.ecs;
59
60 import java.io.OutputStream JavaDoc;
61 import java.io.PrintWriter JavaDoc;
62 import java.io.Serializable JavaDoc;
63 import org.apache.ecs.xhtml.html;
64 import org.apache.ecs.xhtml.body;
65 import org.apache.ecs.xhtml.title;
66 import org.apache.ecs.xhtml.head;
67 import org.apache.ecs.Doctype;
68
69 /**
70     This class creates an XhtmlDocument container, for convience.
71
72     @version $Id: XhtmlDocument.java,v 1.3 2003/04/27 09:42:34 rdonkin Exp $
73     @author <a HREF="mailto:snagy@servletapi.com">Stephan Nagy</a>
74     @author <a HREF="mailto:jon@clearink.com">Jon S. Stevens</a>
75     @author <a HREF="mailto:bojan@binarix.com">Bojan Smojver</a>
76 */

77 public class XhtmlDocument implements Serializable JavaDoc,Cloneable JavaDoc
78 {
79     /** @serial html html */
80     private html html; // this is the actual container for head and body
81
/** @serial head head */
82     private head head;
83     /** @serial body body */
84     private body body;
85     /** @serial title title */
86     private title title;
87     /** @serial codeset codeset */
88     private String JavaDoc codeset = null;
89     /** @serial doctype doctype */
90     private Doctype doctype = null;
91     
92     {
93         html = new html();
94         head = new head();
95         title = new title();
96         body = new body();
97         
98         head.addElement("title",title);
99         html.addElement("head",head);
100         html.addElement("body",body);
101     }
102     
103     /**
104         Basic constructor.
105     */

106     public XhtmlDocument()
107     {
108     }
109
110     /**
111         Basic constructor. Sets the codeset for the page output.
112     */

113     public XhtmlDocument(String JavaDoc codeset)
114     {
115         setCodeset(codeset);
116     }
117
118     /**
119         Get the doctype element for this XhtmlDocument container.
120     */

121     public Doctype getDoctype()
122     {
123         return(doctype);
124     }
125     
126     /**
127         Set the doctype element for this XhtmlDocument container.
128     */

129     public XhtmlDocument setDoctype(Doctype set_doctype)
130     {
131         this.doctype = set_doctype;
132         return(this);
133     }
134
135     /**
136         Get the html element for this XhtmlDocument container.
137     */

138     public html getHtml()
139     {
140         return(html);
141     }
142     
143     /**
144         Set the html element for this XhtmlDocument container.
145     */

146     public XhtmlDocument setHtml(html set_html)
147     {
148         this.html = set_html;
149         return(this);
150     }
151     
152     /**
153         Get the head element for this XhtmlDocument container.
154     */

155     public head getHead()
156     {
157         return(head);
158     }
159     
160     /**
161         Set the head element for this XhtmlDocument container.
162     */

163     public XhtmlDocument setHead(head set_head)
164     {
165         html.addElement("head",set_head);
166         return(this);
167     }
168     
169     /**
170         Append to the head element for this XhtmlDocument container.
171         @param value adds to the value between the head tags
172     */

173     public XhtmlDocument appendHead(Element value)
174     {
175         head.addElement(value);
176         return(this);
177     }
178
179     /**
180         Append to the head element for this XhtmlDocument container.
181         @param value adds to the value between the head tags
182     */

183     public XhtmlDocument appendHead(String JavaDoc value)
184     {
185         head.addElement(value);
186         return(this);
187     }
188
189     /**
190         Get the body element for this XhtmlDocument container.
191     */

192     public body getBody()
193     {
194         return(body);
195     }
196
197     /**
198         Set the body element for this XhtmlDocument container.
199     */

200     public XhtmlDocument setBody(body set_body)
201     {
202         html.addElement("body",set_body);
203         return(this);
204     }
205     
206     /**
207         Append to the body element for this XhtmlDocument container.
208         @param value adds to the value between the body tags
209     */

210     public XhtmlDocument appendBody(Element value)
211     {
212         body.addElement(value);
213         return(this);
214     }
215
216     /**
217         Append to the body element for this XhtmlDocument container.
218         @param value adds to the value between the body tags
219     */

220     public XhtmlDocument appendBody(String JavaDoc value)
221     {
222         body.addElement(value);
223         return(this);
224     }
225
226     /**
227         Get the title element for this XhtmlDocument container.
228     */

229     public title getTitle()
230     {
231         return(title);
232     }
233
234     /**
235         Set the title element for this XhtmlDocument container.
236     */

237     public XhtmlDocument setTitle(title set_title)
238     {
239         head.addElement("title",set_title);
240         return(this);
241     }
242     
243     /**
244         Append to the title element for this XhtmlDocument container.
245         @param value adds to the value between the title tags
246     */

247     public XhtmlDocument appendTitle(Element value)
248     {
249         title.addElement(value);
250         return(this);
251     }
252
253     /**
254         Append to the title element for this XhtmlDocument container.
255         @param value adds to the value between the title tags
256     */

257     public XhtmlDocument appendTitle(String JavaDoc value)
258     {
259         title.addElement(value);
260         return(this);
261     }
262
263     /**
264      * Sets the codeset for this XhtmlDocument
265      */

266     public void setCodeset ( String JavaDoc codeset )
267     {
268         this.codeset = codeset;
269     }
270
271     /**
272      * Gets the codeset for this XhtmlDocument
273      *
274      * @return the codeset
275      */

276     public String JavaDoc getCodeset()
277     {
278         return this.codeset;
279     }
280     
281     /**
282         Write the container to the OutputStream
283     */

284     public void output(OutputStream JavaDoc out)
285     {
286         if (doctype != null)
287         {
288             doctype.output(out);
289             try
290             {
291                 out.write('\n');
292             }
293             catch ( Exception JavaDoc e)
294             {}
295         }
296         // XhtmlDocument is just a convient wrapper for html call html.output
297
html.output(out);
298     }
299
300     /**
301         Write the container to the PrintWriter
302     */

303     public void output(PrintWriter JavaDoc out)
304     {
305         if (doctype != null)
306         {
307             doctype.output(out);
308             try
309             {
310                 out.write('\n');
311             }
312             catch ( Exception JavaDoc e)
313             {}
314         }
315         // XhtmlDocument is just a convient wrapper for html call html.output
316
html.output(out);
317     }
318
319     /**
320         Override the toString() method so that it prints something meaningful.
321     */

322     public final String JavaDoc toString()
323     {
324         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
325         if ( getCodeset() != null )
326         {
327             if (doctype != null)
328                 sb.append (doctype.toString(getCodeset()));
329             sb.append (html.toString(getCodeset()));
330             return (sb.toString());
331         }
332         else
333         {
334             if (doctype != null)
335                 sb.append (doctype.toString());
336             sb.append (html.toString());
337             return(sb.toString());
338         }
339     }
340
341     /**
342         Override the toString() method so that it prints something meaningful.
343     */

344     public final String JavaDoc toString(String JavaDoc codeset)
345     {
346         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
347         if (doctype != null)
348             sb.append (doctype.toString(getCodeset()));
349         sb.append (html.toString(getCodeset()));
350         return(sb.toString());
351     }
352     
353     /**
354         Allows the document to be cloned.
355         Doesn't return an instance of document returns instance of html.
356         NOTE: If you have a doctype set, then it will be lost. Feel free
357         to submit a patch to fix this. It isn't trivial.
358     */

359     public Object JavaDoc clone()
360     {
361         return(html.clone());
362     }
363 }
364
Popular Tags