Update edition of the jSite homepage.
[jSite.git] / src / de / todesbaum / util / xml / XML.java
1 /*
2  * jSite - XML.java - Copyright © 2006–2012 David Roden
3  *
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
7  * version.
8  *
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
12  * details.
13  *
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.
17  */
18
19 package de.todesbaum.util.xml;
20
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;
27
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;
41
42 import org.w3c.dom.Document;
43
44
45 /**
46  * Contains method to transform DOM XML trees to byte arrays and vice versa.
47  *
48  * @author David Roden <droden@gmail.com>
49  * @version $Id:XML.java 221 2006-03-06 14:46:49Z bombe $
50  */
51 public class XML {
52
53         /** Cached document builder factory. */
54         private static DocumentBuilderFactory documentBuilderFactory = null;
55
56         /** Cached document builder. */
57         private static DocumentBuilder documentBuilder = null;
58
59         /** Cached transformer factory. */
60         private static TransformerFactory transformerFactory = null;
61
62         /** Does nothing. */
63         private XML() {
64         }
65
66         /**
67          * Returns a document builder factory. If possible the cached instance will be returned.
68          *
69          * @return A document builder factory
70          */
71         private static DocumentBuilderFactory getDocumentBuilderFactory() {
72                 if (documentBuilderFactory != null) {
73                         return documentBuilderFactory;
74                 }
75                 documentBuilderFactory = DocumentBuilderFactory.newInstance();
76                 return documentBuilderFactory;
77         }
78
79         /**
80          * Returns a document builder. If possible the cached instance will be returned.
81          *
82          * @return A document builder
83          */
84         private static DocumentBuilder getDocumentBuilder() {
85                 if (documentBuilder != null) {
86                         return documentBuilder;
87                 }
88                 try {
89                         documentBuilder = getDocumentBuilderFactory().newDocumentBuilder();
90                 } catch (ParserConfigurationException e) {
91                 }
92                 return documentBuilder;
93         }
94
95         /**
96          * Returns a transformer factory. If possible the cached instance will be returned.
97          *
98          * @return A transformer factory
99          */
100         private static TransformerFactory getTransformerFactory() {
101                 if (transformerFactory != null) {
102                         return transformerFactory;
103                 }
104                 transformerFactory = TransformerFactory.newInstance();
105                 return transformerFactory;
106         }
107
108         /**
109          * Creates a new XML document.
110          *
111          * @return A new XML document
112          */
113         public static Document createDocument() {
114                 return getDocumentBuilder().newDocument();
115         }
116
117         /**
118          * Transforms the DOM XML document into a byte array.
119          *
120          * @param document
121          *            The document to transform
122          * @return The byte array containing the XML representation
123          */
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);
129                 try {
130                         Transformer transformer = getTransformerFactory().newTransformer();
131                         transformer.transform(documentSource, transformResult);
132                         byteOutput.close();
133                         return byteOutput.toByteArray();
134                 } catch (IOException ioe1) {
135                 } catch (TransformerConfigurationException tce1) {
136                 } catch (TransformerException te1) {
137                 } finally {
138                         try {
139                                 byteOutput.close();
140                         } catch (IOException ioe1) {
141                         }
142                 }
143                 return null;
144         }
145
146         /**
147          * Transforms the byte array into a DOM XML document.
148          *
149          * @param data
150          *            The byte array to parse
151          * @return The DOM XML document
152          */
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();
158                 try {
159                         Transformer transformer = getTransformerFactory().newTransformer();
160                         transformer.transform(xmlSource, xmlResult);
161                         return (Document) ((DOMResult) xmlResult).getNode();
162                 } catch (TransformerConfigurationException tce1) {
163                 } catch (TransformerException te1) {
164                 } finally {
165                         if (byteInput != null)
166                                 try {
167                                         byteInput.close();
168                                 } catch (IOException ioe1) {
169                                 }
170                         if (converter != null)
171                                 try {
172                                         converter.close();
173                                 } catch (IOException ioe1) {
174                                 }
175                 }
176                 return null;
177         }
178
179 }