001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)XMLUtil.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.messaging.util;
030:
031: import java.io.InputStream;
032: import java.io.OutputStream;
033: import java.io.StringWriter;
034:
035: import javax.xml.parsers.DocumentBuilder;
036: import javax.xml.parsers.DocumentBuilderFactory;
037: import javax.xml.transform.Transformer;
038: import javax.xml.transform.TransformerFactory;
039: import javax.xml.transform.dom.DOMSource;
040: import javax.xml.transform.stream.StreamResult;
041:
042: import org.w3c.dom.Document;
043:
044: /** Utility class used to encapsulate XML-related functionality.
045: * @author Sun Microsystems, Inc.
046: */
047: public final class XMLUtil {
048: /** Singleton reference. */
049: private static XMLUtil sMe;
050: /** JAXP XSL transformer. */
051: private Transformer mTransform;
052: /** JAXP DOM builder. */
053: private DocumentBuilder mBuilder;
054:
055: /** Private constructor.
056: * @throws javax.jbi.messaging.MessagingException initialization failed
057: */
058: private XMLUtil() throws javax.jbi.messaging.MessagingException {
059: initBuilder();
060: initTransform();
061: }
062:
063: /** Returns an initialized instance of XMLUtil.
064: * @return xml utility object
065: * @throws javax.jbi.messaging.MessagingException initialization failed
066: */
067: public static XMLUtil getInstance()
068: throws javax.jbi.messaging.MessagingException {
069: if (sMe == null) {
070: sMe = new XMLUtil();
071: }
072:
073: return sMe;
074: }
075:
076: /** Creates a new DOM object.
077: * @return DOM object
078: */
079: public Document newDocument() {
080: // builder frowns on concurrent access :-(
081: synchronized (mBuilder) {
082: return mBuilder.newDocument();
083: }
084: }
085:
086: /** Parses the XML input into a DOM.
087: * @param in input stream containing XML
088: * @return DOM object
089: * @throws java.io.IOException failed to read document from stream
090: */
091: public Document readDocument(InputStream in)
092: throws java.io.IOException {
093: try {
094: // builder frowns on concurrent access :-(
095: synchronized (mBuilder) {
096: return mBuilder.parse(in);
097: }
098: } catch (org.xml.sax.SAXException saxEx) {
099: throw new java.io.IOException(saxEx.getMessage());
100: }
101: }
102:
103: /** Serializes a DOM object to the specified stream.
104: * @param doc DOM reference
105: * @param out output stream
106: * @throws java.io.IOException failed to write document to stream
107: */
108: public void writeDocument(Document doc, OutputStream out)
109: throws java.io.IOException {
110: DOMSource source;
111: StreamResult result;
112:
113: source = new DOMSource(doc);
114: result = new StreamResult(out);
115:
116: try {
117: // tranformer frowns on concurrent access :-(
118: synchronized (mTransform) {
119: mTransform.transform(source, result);
120: }
121: } catch (javax.xml.transform.TransformerException tEx) {
122: throw new java.io.IOException(tEx.getMessage());
123: }
124: }
125:
126: public String asString(Document doc)
127: throws javax.jbi.messaging.MessagingException {
128: DOMSource source;
129: StringWriter writer;
130: StreamResult result;
131:
132: source = new DOMSource(doc);
133: writer = new StringWriter();
134: result = new StreamResult(writer);
135:
136: try {
137: // tranformer frowns on concurrent access :-(
138: synchronized (mTransform) {
139: mTransform.transform(source, result);
140: }
141: } catch (javax.xml.transform.TransformerException tEx) {
142: throw new javax.jbi.messaging.MessagingException(tEx
143: .getMessage());
144: }
145:
146: return writer.toString();
147: }
148:
149: /** Initialize XSL transformer.
150: * @throws javax.jbi.messaging.MessagingException initialization failed
151: */
152: private void initTransform()
153: throws javax.jbi.messaging.MessagingException {
154: TransformerFactory tf;
155:
156: try {
157: // initialize transformer details
158: tf = TransformerFactory.newInstance();
159: mTransform = tf.newTransformer();
160: } catch (javax.xml.transform.TransformerFactoryConfigurationError tfcEx) {
161: throw new javax.jbi.messaging.MessagingException(tfcEx);
162: } catch (javax.xml.transform.TransformerConfigurationException cfgEx) {
163: throw new javax.jbi.messaging.MessagingException(cfgEx);
164: }
165: }
166:
167: /** Initialize DOM builder.
168: * @throws javax.jbi.messaging.MessagingException initialization failed
169: */
170: private void initBuilder()
171: throws javax.jbi.messaging.MessagingException {
172: DocumentBuilderFactory dbf;
173:
174: try {
175: // initialize builder details
176: dbf = DocumentBuilderFactory.newInstance();
177: dbf.setNamespaceAware(true);
178: dbf.setValidating(false);
179:
180: mBuilder = dbf.newDocumentBuilder();
181: } catch (javax.xml.parsers.ParserConfigurationException pEx) {
182: throw new javax.jbi.messaging.MessagingException(pEx);
183: } catch (javax.xml.parsers.FactoryConfigurationError fEx) {
184: throw new javax.jbi.messaging.MessagingException(fEx);
185: }
186:
187: }
188: }
|