2 * This program is free software; you can redistribute it and/or modify it under
3 * the terms of the GNU General Public License as published by the Free Software
4 * Foundation; either version 2 of the License, or (at your option) any later
7 * This program is distributed in the hope that it will be useful, but WITHOUT
8 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 * You should have received a copy of the GNU General Public License along with
13 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
14 * Place - Suite 330, Boston, MA 02111-1307, USA.
17 package de.todesbaum.util.xml;
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.io.OutputStreamWriter;
24 import java.nio.charset.Charset;
26 import javax.xml.parsers.DocumentBuilder;
27 import javax.xml.parsers.DocumentBuilderFactory;
28 import javax.xml.parsers.ParserConfigurationException;
29 import javax.xml.transform.Result;
30 import javax.xml.transform.Source;
31 import javax.xml.transform.Transformer;
32 import javax.xml.transform.TransformerConfigurationException;
33 import javax.xml.transform.TransformerException;
34 import javax.xml.transform.TransformerFactory;
35 import javax.xml.transform.dom.DOMResult;
36 import javax.xml.transform.dom.DOMSource;
37 import javax.xml.transform.stream.StreamResult;
38 import javax.xml.transform.stream.StreamSource;
40 import org.w3c.dom.Document;
44 * Contains method to transform DOM XML trees to byte arrays and vice versa.
46 * @author David Roden <droden@gmail.com>
47 * @version $Id:XML.java 221 2006-03-06 14:46:49Z bombe $
51 /** Cached document builder factory. */
52 private static DocumentBuilderFactory documentBuilderFactory = null;
54 /** Cached document builder. */
55 private static DocumentBuilder documentBuilder = null;
57 /** Cached transformer factory. */
58 private static TransformerFactory transformerFactory = null;
65 * Returns a document builder factory. If possible the cached instance will be returned.
67 * @return A document builder factory
69 private static DocumentBuilderFactory getDocumentBuilderFactory() {
70 if (documentBuilderFactory != null) {
71 return documentBuilderFactory;
73 documentBuilderFactory = DocumentBuilderFactory.newInstance();
74 return documentBuilderFactory;
78 * Returns a document builder. If possible the cached instance will be returned.
80 * @return A document builder
82 private static DocumentBuilder getDocumentBuilder() {
83 if (documentBuilder != null) {
84 return documentBuilder;
87 documentBuilder = getDocumentBuilderFactory().newDocumentBuilder();
88 } catch (ParserConfigurationException e) {
90 return documentBuilder;
94 * Returns a transformer factory. If possible the cached instance will be returned.
96 * @return A transformer factory
98 private static TransformerFactory getTransformerFactory() {
99 if (transformerFactory != null) {
100 return transformerFactory;
102 transformerFactory = TransformerFactory.newInstance();
103 return transformerFactory;
107 * Creates a new XML document.
109 * @return A new XML document
111 public static Document createDocument() {
112 return getDocumentBuilder().newDocument();
116 * Transforms the DOM XML document into a byte array.
119 * The document to transform
120 * @return The byte array containing the XML representation
122 public static byte[] transformToByteArray(Document document) {
123 ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
124 OutputStreamWriter converter = new OutputStreamWriter(byteOutput, Charset.forName("UTF-8"));
125 Result transformResult = new StreamResult(converter);
126 Source documentSource = new DOMSource(document);
128 Transformer transformer = getTransformerFactory().newTransformer();
129 transformer.transform(documentSource, transformResult);
131 return byteOutput.toByteArray();
132 } catch (IOException ioe1) {
133 } catch (TransformerConfigurationException tce1) {
134 } catch (TransformerException te1) {
138 } catch (IOException ioe1) {
145 * Transforms the byte array into a DOM XML document.
148 * The byte array to parse
149 * @return The DOM XML document
151 public static Document transformToDocument(byte[] data) {
152 ByteArrayInputStream byteInput = new ByteArrayInputStream(data);
153 InputStreamReader converter = new InputStreamReader(byteInput, Charset.forName("UTF-8"));
154 Source xmlSource = new StreamSource(converter);
155 Result xmlResult = new DOMResult();
157 Transformer transformer = getTransformerFactory().newTransformer();
158 transformer.transform(xmlSource, xmlResult);
159 return (Document) ((DOMResult) xmlResult).getNode();
160 } catch (TransformerConfigurationException tce1) {
161 } catch (TransformerException te1) {
163 if (byteInput != null)
166 } catch (IOException ioe1) {
168 if (converter != null)
171 } catch (IOException ioe1) {