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