KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ecs > xml > XMLDocument


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.xml;
59
60 import java.io.Serializable JavaDoc;
61 import java.io.OutputStream JavaDoc;
62 import java.io.PrintWriter JavaDoc;
63 import java.io.IOException JavaDoc;
64 import java.util.*;
65
66 import org.apache.ecs.MultiPartElement;
67 import org.apache.ecs.ConcreteElement;
68 import org.apache.ecs.xml.XML;
69 import org.apache.ecs.xml.PI;
70
71 /**
72  * XMLDocument
73  *
74  * This is the container for XML elements that can be used similar to
75  * org.apache.ecs.Document. However, it correctly handles XML elements
76  * and doesn't have any notion of a head, body, etc., that is associated
77  * with HTML documents.
78  *
79  * @author <a HREF="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
80  */

81 public class XMLDocument implements Serializable JavaDoc, Cloneable JavaDoc {
82     
83     /** Default Version */
84     private static final float DEFAULT_XML_VERSION = 1.0f;
85     
86     /** Version Declaration - FIXME!! */
87     private String JavaDoc versionDecl;
88     
89     /** Prolog */
90     private Vector prolog;
91     
92     /** "Body" of document */
93     private XML content;
94     
95     /** @serial codeset codeset */
96     private String JavaDoc codeset = null;
97     
98     /**
99      * This sets the document up. Since an XML document can be
100      * pretty much anything you want, all it does is create an
101      * XML Instruction for the default version and sets the
102      * document to be standalone.
103      */

104     public XMLDocument() {
105         this(DEFAULT_XML_VERSION, true);
106     }
107     
108     /**
109      * This sets the document up. Since an XML document can
110      * be pretty much anything, all this does is create the
111      * XML Instruction for the version specified and set the
112      * document to be standalone.
113      *
114      * @param version - version of XML this document is
115      */

116     public XMLDocument(double version) {
117         this(version, true);
118     }
119     
120     /**
121      * This sets the document up. Since an XML document can be
122      * pretty much anything, all this does is create the
123      * XML Instruction with the version specified, and
124      * identifies the document as standalone if set
125      *
126      * @param version - version of XML document is
127      * @param standalone - boolean: <code>true</code> if standalone, else false
128      */

129     public XMLDocument(double version, boolean standalone) {
130         prolog = new Vector(2);
131         StringBuffer JavaDoc versionStr = new StringBuffer JavaDoc();
132         versionStr.append("<?xml version=\"");
133         versionStr.append(version);
134         versionStr.append("\" standalone=\"");
135         if (standalone)
136             versionStr.append("yes\"?>");
137         else
138             versionStr.append("no\"?>\n");
139             
140         this.versionDecl = versionStr.toString();
141         
142         /**
143          * FIXME: ECS currently does not do any ordering of attributes.
144          * Although about 99% of the time, this has no problems,
145          * in the initial XML declaration, it can be a problem in
146          * certain frameworks (e.g. Cocoon/Xerces/Xalan). So instead
147          * of adding an element here, we have to store this first command
148          * in a String and add it to the output at output time.
149          */

150         /**
151         PI versionDecl = new PI().setTarget("xml");
152                 
153         if (standalone)
154             versionDecl.addInstruction("standalone", "yes");
155         else
156             versionDecl.addInstruction("standalone", "no");
157             
158         versionDecl.setVersion(version);
159         
160         prolog.addElement(versionDecl);
161         */

162     }
163     
164     /**
165      * This sets the document up. Since an XML document can be
166      * pretty much anything, all this does is create the
167      * XML Instruction with the version specified, and
168      * identifies the document as standalone if set. This also
169      * allows the codeset to be set as well.
170      *
171      * @param version - version of XML document is
172      * @param standalone - boolean: <code>true</code if standalone, else false
173      * @param codeset - String codeset to use
174      */

175     public XMLDocument(double version, boolean standalone, String JavaDoc codeset) {
176         this(version, standalone);
177         setCodeset(codeset);
178     }
179     
180     /**
181      * This sets the codeset for this document
182      *
183      * @param codeset - String representation of codeset for this
184      * document
185      */

186     public void setCodeset(String JavaDoc codeset) {
187         this.codeset = codeset;
188     }
189     
190     /**
191      * This gets the codeset for this document
192      *
193      * @return String the codeset for this document
194      */

195     public String JavaDoc getCodeset() {
196         return codeset;
197     }
198     
199     /**
200      * This adds a stylesheet to the XML document.
201      *
202      * @param href - String reference to stylesheet
203      * @param type - String type of stylesheet
204      */

205     public XMLDocument addStylesheet(String JavaDoc href, String JavaDoc type) {
206         PI pi = new PI();
207         pi.setTarget("xml-stylesheet")
208           .addInstruction("href", href)
209           .addInstruction("type", type);
210         prolog.addElement(pi);
211         
212         return(this);
213     }
214     
215     /**
216      * This adds a stylesheet to the XML document, and assumes
217      * the default <code>text/xsl</code> type.
218      *
219      * @param href = String reference to stylesheet
220      */

221     public XMLDocument addStylesheet(String JavaDoc href) {
222         return addStylesheet(href, "text/xsl");
223     }
224     
225     /**
226      * This adds the specified element to the prolog of the document
227      *
228      * @param element - Element to add
229      */

230     public XMLDocument addToProlog(ConcreteElement element) {
231         prolog.addElement(element);
232         return(this);
233     }
234     
235     /**
236      * This adds an element to the XML document. If the
237      * document is empty, it sets the passed in element
238      * as the root element.
239      *
240      * @param element - XML Element to add
241      * @return XMLDocument - modified document
242      */

243     public XMLDocument addElement(XML element) {
244         if (content == null)
245             content = element;
246         else
247             content.addElement(element);
248             
249         return(this);
250     }
251     
252     /**
253      * Write the document to the OutputStream
254      *
255      * @param out - OutputStream to write to
256      */

257     public void output(OutputStream JavaDoc out)
258     {
259         /**
260          * FIXME: The other part of the version hack!
261          * Add the version declaration to the beginning of the document.
262          */

263         try {
264             out.write(versionDecl.getBytes());
265         } catch (Exception JavaDoc e) { }
266         
267         for (int i=0; i<prolog.size(); i++) {
268             ConcreteElement e = (ConcreteElement)prolog.elementAt(i);
269             e.output(out);
270             // XXX really this should use line separator!
271
// XXX should also probably check for pretty print
272
// XXX also probably have difficulties with encoding
273
try
274             {
275                 out.write('\n');
276             }
277             catch(IOException JavaDoc ioe)
278             {
279                 ioe.printStackTrace(new PrintWriter JavaDoc(out));
280             }
281         }
282          
283         if (content != null)
284             content.output(out);
285     }
286
287     /**
288      * Write the document to the PrintWriter
289      *
290      * @param out - PrintWriter to write to
291      */

292     public void output(PrintWriter JavaDoc out)
293     {
294         
295         /**
296          * FIXME: The other part of the version hack!
297          * Add the version declaration to the beginning of the document.
298          */

299         out.write(versionDecl);
300         
301         for (int i=0; i<prolog.size(); i++) {
302             ConcreteElement e = (ConcreteElement)prolog.elementAt(i);
303             e.output(out);
304             // XXX really this should use line separator!
305
// XXX should also probably check for pretty print
306
out.println();
307         }
308         
309         if (content != null)
310             content.output(out);
311     }
312     
313     /**
314      * Override toString so it does something useful
315      *
316      * @return String - representation of the document
317      */

318     public final String JavaDoc toString() {
319         StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
320         
321         if (codeset != null) {
322             for (int i=0; i<prolog.size(); i++) {
323                 ConcreteElement e = (ConcreteElement)prolog.elementAt(i);
324                 retVal.append(e.toString(getCodeset()) + "\n");
325             }
326             
327             if (content != null)
328                 retVal.append(content.toString(getCodeset()));
329         } else {
330             for (int i=0; i<prolog.size(); i++) {
331                 ConcreteElement e = (ConcreteElement)prolog.elementAt(i);
332                 retVal.append(e.toString() + "\n");
333             }
334             
335             if (content != null)
336                 retVal.append(content.toString());
337         }
338         
339         /**
340          * FIXME: The other part of the version hack!
341          * Add the version declaration to the beginning of the document.
342          */

343         return versionDecl + retVal.toString();
344     }
345     
346     /**
347      * Override toString so it prints something useful
348      *
349      * @param codeset - String codeset to use
350      * @return String - representation of the document
351      */

352     public final String JavaDoc toString(String JavaDoc codeset) {
353         StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
354         
355         for (int i=0; i<prolog.size(); i++) {
356             ConcreteElement e = (ConcreteElement)prolog.elementAt(i);
357             retVal.append(e.toString(getCodeset()) + "\n");
358         }
359         if (content != null)
360             retVal.append(content.toString(getCodeset()) + "\n");
361         
362         /**
363          * FIXME: The other part of the version hack!
364          * Add the version declaration to the beginning of the document.
365          */

366         return versionDecl + retVal.toString();
367     }
368     
369     /**
370      * Clone this document
371      *
372      * @return Object - cloned XMLDocument
373      */

374     public Object JavaDoc clone() {
375         return content.clone();
376     }
377     
378 }
379     
380
Popular Tags