KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webrender > output > XmlDocument


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.webrender.output;
31
32 import java.io.IOException JavaDoc;
33 import java.io.PrintWriter JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 import javax.xml.transform.Transformer JavaDoc;
37 import javax.xml.transform.TransformerException JavaDoc;
38 import javax.xml.transform.TransformerFactory JavaDoc;
39 import javax.xml.transform.dom.DOMSource JavaDoc;
40 import javax.xml.transform.stream.StreamResult JavaDoc;
41
42 import nextapp.echo2.webrender.util.DomUtil;
43
44 import org.w3c.dom.DOMImplementation JavaDoc;
45 import org.w3c.dom.Document JavaDoc;
46 import org.w3c.dom.DocumentType JavaDoc;
47
48 /**
49  * A simple wrapper around JAXP/W3C DOM APIs to generate and render an XML
50  * document.
51  */

52 public class XmlDocument {
53     
54     private Document JavaDoc document;
55     private Properties JavaDoc outputProperties;
56     
57     /**
58      * Creates a new <code>XmlDocument</code>.
59      *
60      * @param qualifiedName the qualified name of the document type to be
61      * created
62      * @param publicId the external subset public identifier
63      * @param systemId the external subset system identifier
64      * @param namespaceUri the namespace URI of the document element to create
65      */

66     public XmlDocument(String JavaDoc qualifiedName, String JavaDoc publicId, String JavaDoc systemId, String JavaDoc namespaceUri) {
67         super();
68         DOMImplementation JavaDoc dom = DomUtil.getDocumentBuilder().getDOMImplementation();
69         DocumentType JavaDoc docType = dom.createDocumentType(qualifiedName, publicId, systemId);
70         document = dom.createDocument(namespaceUri, qualifiedName, docType);
71         if (namespaceUri != null) {
72             document.getDocumentElement().setAttribute("xmlns", namespaceUri);
73         }
74     }
75     
76     /**
77      * Returns the W3C DOM implementation <code>Document</code> object.
78      *
79      * @return the <code>Document</code> object
80      */

81     public Document JavaDoc getDocument() {
82         return document;
83     }
84     
85     /**
86      * Renders the document to a <code>PrintWriter</code>.
87      *
88      * @param pw the <code>PrintWriter</code>
89      */

90     public void render(PrintWriter JavaDoc pw)
91     throws IOException JavaDoc {
92         try {
93             TransformerFactory JavaDoc tFactory = DomUtil.getTransformerFactory();
94             Transformer JavaDoc transformer = tFactory.newTransformer();
95             if (outputProperties != null) {
96                 transformer.setOutputProperties(outputProperties);
97             }
98             DOMSource JavaDoc source = new DOMSource JavaDoc(document);
99             StreamResult JavaDoc result = new StreamResult JavaDoc(pw);
100             transformer.transform(source, result);
101         } catch (TransformerException JavaDoc ex) {
102             throw new IOException JavaDoc("Unable to write document to OutputStream: " + ex.toString());
103         }
104     }
105
106     /**
107      * Sets the output properties which will be used by the rendering
108      * <code>javax.xml.transform.Transformer</code>.
109      *
110      * @param newValue the new output properties
111      */

112     public void setOutputProperties(Properties JavaDoc newValue) {
113         outputProperties = newValue;
114     }
115 }
116
Popular Tags