2 * jSite - XML.java - Copyright © 2006–2012 David Roden
4 * This program is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License as published by the Free Software
6 * Foundation; either version 2 of the License, or (at your option) any later
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.util.xml;
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.io.OutputStreamWriter;
26 import java.nio.charset.Charset;
28 import javax.xml.parsers.DocumentBuilder;
29 import javax.xml.parsers.DocumentBuilderFactory;
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.transform.Result;
32 import javax.xml.transform.Source;
33 import javax.xml.transform.Transformer;
34 import javax.xml.transform.TransformerConfigurationException;
35 import javax.xml.transform.TransformerException;
36 import javax.xml.transform.TransformerFactory;
37 import javax.xml.transform.dom.DOMResult;
38 import javax.xml.transform.dom.DOMSource;
39 import javax.xml.transform.stream.StreamResult;
40 import javax.xml.transform.stream.StreamSource;
42 import org.w3c.dom.Document;
46 * Contains method to transform DOM XML trees to byte arrays and vice versa.
48 * @author David Roden <droden@gmail.com>
49 * @version $Id:XML.java 221 2006-03-06 14:46:49Z bombe $
53 /** Cached document builder factory. */
54 private static DocumentBuilderFactory documentBuilderFactory = null;
56 /** Cached document builder. */
57 private static DocumentBuilder documentBuilder = null;
59 /** Cached transformer factory. */
60 private static TransformerFactory transformerFactory = null;
67 * Returns a document builder factory. If possible the cached instance will be returned.
69 * @return A document builder factory
71 private static DocumentBuilderFactory getDocumentBuilderFactory() {
72 if (documentBuilderFactory != null) {
73 return documentBuilderFactory;
75 documentBuilderFactory = DocumentBuilderFactory.newInstance();
76 return documentBuilderFactory;
80 * Returns a document builder. If possible the cached instance will be returned.
82 * @return A document builder
84 private static DocumentBuilder getDocumentBuilder() {
85 if (documentBuilder != null) {
86 return documentBuilder;
89 documentBuilder = getDocumentBuilderFactory().newDocumentBuilder();
90 } catch (ParserConfigurationException e) {
92 return documentBuilder;
96 * Returns a transformer factory. If possible the cached instance will be returned.
98 * @return A transformer factory
100 private static TransformerFactory getTransformerFactory() {
101 if (transformerFactory != null) {
102 return transformerFactory;
104 transformerFactory = TransformerFactory.newInstance();
105 return transformerFactory;
109 * Creates a new XML document.
111 * @return A new XML document
113 public static Document createDocument() {
114 return getDocumentBuilder().newDocument();
118 * Transforms the DOM XML document into a byte array.
121 * The document to transform
122 * @return The byte array containing the XML representation
124 public static byte[] transformToByteArray(Document document) {
125 ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
126 OutputStreamWriter converter = new OutputStreamWriter(byteOutput, Charset.forName("UTF-8"));
127 Result transformResult = new StreamResult(converter);
128 Source documentSource = new DOMSource(document);
130 Transformer transformer = getTransformerFactory().newTransformer();
131 transformer.transform(documentSource, transformResult);
133 return byteOutput.toByteArray();
134 } catch (IOException ioe1) {
135 } catch (TransformerConfigurationException tce1) {
136 } catch (TransformerException te1) {
140 } catch (IOException ioe1) {
147 * Transforms the byte array into a DOM XML document.
150 * The byte array to parse
151 * @return The DOM XML document
153 public static Document transformToDocument(byte[] data) {
154 ByteArrayInputStream byteInput = new ByteArrayInputStream(data);
155 InputStreamReader converter = new InputStreamReader(byteInput, Charset.forName("UTF-8"));
156 Source xmlSource = new StreamSource(converter);
157 Result xmlResult = new DOMResult();
159 Transformer transformer = getTransformerFactory().newTransformer();
160 transformer.transform(xmlSource, xmlResult);
161 return (Document) ((DOMResult) xmlResult).getNode();
162 } catch (TransformerConfigurationException tce1) {
163 } catch (TransformerException te1) {
165 if (byteInput != null)
168 } catch (IOException ioe1) {
170 if (converter != null)
173 } catch (IOException ioe1) {