001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
005: *
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common Development
009: * and Distribution License("CDDL") (collectively, the "License"). You
010: * may not use this file except in compliance with the License. You can obtain
011: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
012: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
013: * language governing permissions and limitations under the License.
014: *
015: * When distributing the software, include this License Header Notice in each
016: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
017: * Sun designates this particular file as subject to the "Classpath" exception
018: * as provided by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the License
020: * Header, with the fields enclosed by brackets [] replaced by your own
021: * identifying information: "Portions Copyrighted [year]
022: * [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * If you wish your version of this file to be governed by only the CDDL or
027: * only the GPL Version 2, indicate your decision by adding "[Contributor]
028: * elects to include this software in this distribution under the [CDDL or GPL
029: * Version 2] license." If you don't indicate a single choice of license, a
030: * recipient has the option to distribute your version of this file under
031: * either the CDDL, the GPL Version 2 or to extend the choice of license to
032: * its licensees as provided above. However, if you add GPL Version 2 code
033: * and therefore, elected the GPL Version 2 license, then the option applies
034: * only if the new code is made subject to such option by the copyright
035: * holder.
036: */
037: package oracle.toplink.essentials.platform.xml.jaxp;
038:
039: import java.io.OutputStream;
040: import java.io.Writer;
041: import java.net.URL;
042: import javax.xml.transform.OutputKeys;
043: import javax.xml.transform.Result;
044: import javax.xml.transform.Source;
045: import javax.xml.transform.Transformer;
046: import javax.xml.transform.TransformerConfigurationException;
047: import javax.xml.transform.TransformerException;
048: import javax.xml.transform.TransformerFactory;
049: import javax.xml.transform.dom.DOMResult;
050: import javax.xml.transform.dom.DOMSource;
051: import javax.xml.transform.sax.SAXResult;
052: import javax.xml.transform.stream.StreamResult;
053: import javax.xml.transform.stream.StreamSource;
054: import oracle.toplink.essentials.platform.xml.XMLPlatformException;
055: import oracle.toplink.essentials.platform.xml.XMLTransformer;
056: import org.w3c.dom.Document;
057: import org.w3c.dom.Node;
058: import org.xml.sax.ContentHandler;
059:
060: public class JAXPTransformer implements XMLTransformer {
061: private boolean fragment;
062: private static final String NO = "no";
063: private static final String YES = "yes";
064: private Transformer transformer;
065:
066: public JAXPTransformer() {
067: super ();
068: try {
069: TransformerFactory transformerFactory = TransformerFactory
070: .newInstance();
071: transformer = transformerFactory.newTransformer();
072: } catch (TransformerConfigurationException e) {
073: throw XMLPlatformException.xmlPlatformTransformException(e);
074: }
075: }
076:
077: public String getEncoding() {
078: return transformer.getOutputProperty(OutputKeys.ENCODING);
079: }
080:
081: public void setEncoding(String encoding) {
082: transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
083: }
084:
085: public boolean isFormattedOutput() {
086: return transformer.getOutputProperty(OutputKeys.INDENT).equals(
087: YES);
088: }
089:
090: public void setFormattedOutput(boolean shouldFormat) {
091: if (shouldFormat) {
092: transformer.setOutputProperty(OutputKeys.INDENT, YES);
093: } else {
094: transformer.setOutputProperty(OutputKeys.INDENT, NO);
095: }
096: }
097:
098: public String getVersion() {
099: return transformer.getOutputProperty(OutputKeys.VERSION);
100: }
101:
102: public void setVersion(String version) {
103: transformer.setOutputProperty(OutputKeys.VERSION, version);
104: }
105:
106: public void transform(Node sourceNode,
107: OutputStream resultOutputStream)
108: throws XMLPlatformException {
109: DOMSource source = new DOMSource(sourceNode);
110: StreamResult result = new StreamResult(resultOutputStream);
111: if (isFragment()) {
112: transformer.setOutputProperty(
113: OutputKeys.OMIT_XML_DECLARATION, "yes");
114: }
115: transform(source, result);
116: }
117:
118: public void transform(Node sourceNode,
119: ContentHandler resultContentHandler)
120: throws XMLPlatformException {
121: DOMSource source = new DOMSource(sourceNode);
122: SAXResult result = new SAXResult(resultContentHandler);
123:
124: transform(source, result);
125: }
126:
127: public void transform(Node sourceNode, Result result)
128: throws XMLPlatformException {
129: DOMSource source = null;
130: if ((isFragment()) && (result instanceof SAXResult)) {
131: if (sourceNode instanceof Document) {
132: source = new DOMSource(((Document) sourceNode)
133: .getDocumentElement());
134: }
135: } else {
136: source = new DOMSource(sourceNode);
137: }
138: transform(source, result);
139: }
140:
141: public void transform(Node sourceNode, Writer resultWriter)
142: throws XMLPlatformException {
143: DOMSource source = new DOMSource(sourceNode);
144: StreamResult result = new StreamResult(resultWriter);
145:
146: if (isFragment()) {
147: transformer.setOutputProperty(
148: OutputKeys.OMIT_XML_DECLARATION, "yes");
149: }
150: transform(source, result);
151: }
152:
153: public void transform(Source source, Result result)
154: throws XMLPlatformException {
155: try {
156: if ((result instanceof StreamResult) && (isFragment())) {
157: transformer.setOutputProperty(
158: OutputKeys.OMIT_XML_DECLARATION, "yes");
159: }
160: transformer.transform(source, result);
161: } catch (TransformerException e) {
162: throw XMLPlatformException.xmlPlatformTransformException(e);
163: }
164: }
165:
166: public void transform(Document sourceDocument,
167: Node resultParentNode, URL stylesheet)
168: throws XMLPlatformException {
169: try {
170: TransformerFactory transformerFactory = TransformerFactory
171: .newInstance();
172: StreamSource stylesheetSource = new StreamSource(stylesheet
173: .openStream());
174: Transformer transformer = transformerFactory
175: .newTransformer(stylesheetSource);
176: DOMSource source = new DOMSource(sourceDocument);
177: DOMResult result = new DOMResult(resultParentNode);
178: transformer.transform(source, result);
179: } catch (Exception e) {
180: throw XMLPlatformException.xmlPlatformTransformException(e);
181: }
182: }
183:
184: public void setFragment(boolean fragment) {
185: this .fragment = fragment;
186: }
187:
188: public boolean isFragment() {
189: return fragment;
190: }
191: }
|