KickJava   Java API By Example, From Geeks To Geeks.

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


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.html.Html;
64 import org.apache.ecs.html.Body;
65 import org.apache.ecs.html.Title;
66 import org.apache.ecs.html.Head;
67 import org.apache.ecs.Doctype;
68
69 /**
70     This class creates a Document container, for convience.
71
72     @version $Id: Document.java,v 1.10 2003/04/27 09:43:30 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 */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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