001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.webrender.output;
031:
032: import java.io.IOException;
033: import java.io.PrintWriter;
034: import java.util.Properties;
035:
036: import javax.xml.transform.Transformer;
037: import javax.xml.transform.TransformerException;
038: import javax.xml.transform.TransformerFactory;
039: import javax.xml.transform.dom.DOMSource;
040: import javax.xml.transform.stream.StreamResult;
041:
042: import nextapp.echo2.webrender.util.DomUtil;
043:
044: import org.w3c.dom.DOMImplementation;
045: import org.w3c.dom.Document;
046: import org.w3c.dom.DocumentType;
047:
048: /**
049: * A simple wrapper around JAXP/W3C DOM APIs to generate and render an XML
050: * document.
051: */
052: public class XmlDocument {
053:
054: private Document document;
055: private Properties outputProperties;
056:
057: /**
058: * Creates a new <code>XmlDocument</code>.
059: *
060: * @param qualifiedName the qualified name of the document type to be
061: * created
062: * @param publicId the external subset public identifier
063: * @param systemId the external subset system identifier
064: * @param namespaceUri the namespace URI of the document element to create
065: */
066: public XmlDocument(String qualifiedName, String publicId,
067: String systemId, String namespaceUri) {
068: super ();
069: DOMImplementation dom = DomUtil.getDocumentBuilder()
070: .getDOMImplementation();
071: DocumentType docType = dom.createDocumentType(qualifiedName,
072: publicId, systemId);
073: document = dom.createDocument(namespaceUri, qualifiedName,
074: docType);
075: if (namespaceUri != null) {
076: document.getDocumentElement().setAttribute("xmlns",
077: namespaceUri);
078: }
079: }
080:
081: /**
082: * Returns the W3C DOM implementation <code>Document</code> object.
083: *
084: * @return the <code>Document</code> object
085: */
086: public Document getDocument() {
087: return document;
088: }
089:
090: /**
091: * Renders the document to a <code>PrintWriter</code>.
092: *
093: * @param pw the <code>PrintWriter</code>
094: */
095: public void render(PrintWriter pw) throws IOException {
096: try {
097: TransformerFactory tFactory = DomUtil
098: .getTransformerFactory();
099: Transformer transformer = tFactory.newTransformer();
100: if (outputProperties != null) {
101: transformer.setOutputProperties(outputProperties);
102: }
103: DOMSource source = new DOMSource(document);
104: StreamResult result = new StreamResult(pw);
105: transformer.transform(source, result);
106: } catch (TransformerException ex) {
107: throw new IOException(
108: "Unable to write document to OutputStream: "
109: + ex.toString());
110: }
111: }
112:
113: /**
114: * Sets the output properties which will be used by the rendering
115: * <code>javax.xml.transform.Transformer</code>.
116: *
117: * @param newValue the new output properties
118: */
119: public void setOutputProperties(Properties newValue) {
120: outputProperties = newValue;
121: }
122: }
|