KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > platform > xml > jaxp > JAXPTransformer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.platform.xml.jaxp;
23
24 import java.io.OutputStream JavaDoc;
25 import java.io.Writer JavaDoc;
26 import java.net.URL JavaDoc;
27 import javax.xml.transform.OutputKeys JavaDoc;
28 import javax.xml.transform.Result JavaDoc;
29 import javax.xml.transform.Source JavaDoc;
30 import javax.xml.transform.Transformer JavaDoc;
31 import javax.xml.transform.TransformerConfigurationException JavaDoc;
32 import javax.xml.transform.TransformerException JavaDoc;
33 import javax.xml.transform.TransformerFactory JavaDoc;
34 import javax.xml.transform.dom.DOMResult JavaDoc;
35 import javax.xml.transform.dom.DOMSource JavaDoc;
36 import javax.xml.transform.sax.SAXResult JavaDoc;
37 import javax.xml.transform.stream.StreamResult JavaDoc;
38 import javax.xml.transform.stream.StreamSource JavaDoc;
39 import oracle.toplink.essentials.platform.xml.XMLPlatformException;
40 import oracle.toplink.essentials.platform.xml.XMLTransformer;
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.Node JavaDoc;
43 import org.xml.sax.ContentHandler JavaDoc;
44
45 public class JAXPTransformer implements XMLTransformer {
46     private boolean fragment;
47     private static final String JavaDoc NO = "no";
48     private static final String JavaDoc YES = "yes";
49     private Transformer JavaDoc transformer;
50
51     public JAXPTransformer() {
52         super();
53         try {
54             TransformerFactory JavaDoc transformerFactory = TransformerFactory.newInstance();
55             transformer = transformerFactory.newTransformer();
56         } catch (TransformerConfigurationException JavaDoc e) {
57             throw XMLPlatformException.xmlPlatformTransformException(e);
58         }
59     }
60
61     public String JavaDoc getEncoding() {
62         return transformer.getOutputProperty(OutputKeys.ENCODING);
63     }
64
65     public void setEncoding(String JavaDoc encoding) {
66         transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
67     }
68
69     public boolean isFormattedOutput() {
70         return transformer.getOutputProperty(OutputKeys.INDENT).equals(YES);
71     }
72
73     public void setFormattedOutput(boolean shouldFormat) {
74         if (shouldFormat) {
75             transformer.setOutputProperty(OutputKeys.INDENT, YES);
76         } else {
77             transformer.setOutputProperty(OutputKeys.INDENT, NO);
78         }
79     }
80
81     public String JavaDoc getVersion() {
82         return transformer.getOutputProperty(OutputKeys.VERSION);
83     }
84
85     public void setVersion(String JavaDoc version) {
86         transformer.setOutputProperty(OutputKeys.VERSION, version);
87     }
88
89     public void transform(Node JavaDoc sourceNode, OutputStream JavaDoc resultOutputStream) throws XMLPlatformException {
90         DOMSource JavaDoc source = new DOMSource JavaDoc(sourceNode);
91         StreamResult JavaDoc result = new StreamResult JavaDoc(resultOutputStream);
92         if (isFragment()) {
93             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
94         }
95         transform(source, result);
96     }
97
98     public void transform(Node JavaDoc sourceNode, ContentHandler JavaDoc resultContentHandler) throws XMLPlatformException {
99         DOMSource JavaDoc source = new DOMSource JavaDoc(sourceNode);
100         SAXResult JavaDoc result = new SAXResult JavaDoc(resultContentHandler);
101
102         transform(source, result);
103     }
104
105     public void transform(Node JavaDoc sourceNode, Result JavaDoc result) throws XMLPlatformException {
106         DOMSource JavaDoc source = null;
107         if ((isFragment()) && (result instanceof SAXResult JavaDoc)) {
108             if (sourceNode instanceof Document JavaDoc) {
109                 source = new DOMSource JavaDoc(((Document JavaDoc)sourceNode).getDocumentElement());
110             }
111         } else {
112             source = new DOMSource JavaDoc(sourceNode);
113         }
114         transform(source, result);
115     }
116
117     public void transform(Node JavaDoc sourceNode, Writer JavaDoc resultWriter) throws XMLPlatformException {
118         DOMSource JavaDoc source = new DOMSource JavaDoc(sourceNode);
119         StreamResult JavaDoc result = new StreamResult JavaDoc(resultWriter);
120
121         if (isFragment()) {
122             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
123         }
124         transform(source, result);
125     }
126
127     public void transform(Source JavaDoc source, Result JavaDoc result) throws XMLPlatformException {
128         try {
129             if ((result instanceof StreamResult JavaDoc) && (isFragment())) {
130                 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
131             }
132             transformer.transform(source, result);
133         } catch (TransformerException JavaDoc e) {
134             throw XMLPlatformException.xmlPlatformTransformException(e);
135         }
136     }
137
138     public void transform(Document JavaDoc sourceDocument, Node JavaDoc resultParentNode, URL JavaDoc stylesheet) throws XMLPlatformException {
139         try {
140             TransformerFactory JavaDoc transformerFactory = TransformerFactory.newInstance();
141             StreamSource JavaDoc stylesheetSource = new StreamSource JavaDoc(stylesheet.openStream());
142             Transformer JavaDoc transformer = transformerFactory.newTransformer(stylesheetSource);
143             DOMSource JavaDoc source = new DOMSource JavaDoc(sourceDocument);
144             DOMResult JavaDoc result = new DOMResult JavaDoc(resultParentNode);
145             transformer.transform(source, result);
146         } catch (Exception JavaDoc e) {
147             throw XMLPlatformException.xmlPlatformTransformException(e);
148         }
149     }
150
151     public void setFragment(boolean fragment) {
152         this.fragment = fragment;
153     }
154
155     public boolean isFragment() {
156         return fragment;
157     }
158 }
159
Popular Tags