comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/ArtifactXMLUtilities.java @ 875:5e9efdda6894

merged gnv-artifacts/1.0
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:56 +0200
parents 05bf8534a35a
children f953c9a559d8
comparison
equal deleted inserted replaced
722:bb3ffe7d719e 875:5e9efdda6894
1 package de.intevation.gnv.utils;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.Serializable;
7 import java.io.StringWriter;
8 import java.io.UnsupportedEncodingException;
9
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
13 import javax.xml.transform.Transformer;
14 import javax.xml.transform.TransformerConfigurationException;
15 import javax.xml.transform.TransformerException;
16 import javax.xml.transform.TransformerFactory;
17 import javax.xml.transform.TransformerFactoryConfigurationError;
18 import javax.xml.transform.dom.DOMSource;
19 import javax.xml.transform.stream.StreamResult;
20
21 import org.apache.log4j.Logger;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24 import org.w3c.dom.Node;
25 import org.xml.sax.SAXException;
26
27 import de.intevation.artifacts.ArtifactNamespaceContext;
28
29 /**
30 * This class provides some methods for creating and working with xml documents.
31 *
32 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
33 *
34 */
35 public class ArtifactXMLUtilities implements Serializable {
36 /**
37 *
38 */
39 private static final long serialVersionUID = -6236340358303411758L;
40
41 /**
42 * the logger, used to log exceptions and additonaly information
43 */
44 private static Logger log = Logger
45 .getLogger(ArtifactXMLUtilities.class);
46
47 public static final String XFORM_URL = "http://www.w3.org/2002/xforms";
48 public static final String XFORM_PREFIX = "xform";
49
50 /**
51 * Constructor.<br>
52 * <b>Note:</b> It should not be necessary to create an object of this
53 * class - which is a helper class. Call static methods instead.
54 */
55 public ArtifactXMLUtilities() {
56 }
57
58 /**
59 * Creates an <code>Element</code> and returns it.
60 *
61 * @param document A document
62 * @param name Name of a node.
63 * @return an Element.
64 */
65 public static Element createArtifactElement(Document document, String name) {
66 Element node = document.createElementNS(
67 ArtifactNamespaceContext.NAMESPACE_URI, name);
68 node.setPrefix(ArtifactNamespaceContext.NAMESPACE_PREFIX);
69 return node;
70 }
71
72
73 /**
74 * Turns an xml document into a string.
75 *
76 * @param document An xml document.
77 * @return String representation of <i>document</i>.
78 */
79 public static String writeDocument2String(Document document) {
80 try {
81 TransformerFactory transformerFactory = TransformerFactory
82 .newInstance();
83 Transformer transformer = transformerFactory.newTransformer();
84 DOMSource source = new DOMSource(document);
85 StringWriter sw = new StringWriter();
86 StreamResult result = new StreamResult(sw);
87 transformer.transform(source, result);
88 return sw.getBuffer().toString();
89 } catch (TransformerConfigurationException e) {
90 log.error(e, e);
91 } catch (TransformerFactoryConfigurationError e) {
92 log.error(e, e);
93 } catch (TransformerException e) {
94 log.error(e, e);
95 }
96 return null;
97 }
98
99 /**
100 * Read a document from input stream.
101 *
102 * @param inputStream The input stream.
103 * @return the document read from stream.
104 */
105 public static Document readDocument(InputStream inputStream) {
106 Document returnValue = null;
107 try {
108 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
109 .newInstance();
110 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
111 returnValue = docBuilder.parse(inputStream);
112 } catch (ParserConfigurationException e) {
113 log.error(e, e);
114 } catch (SAXException e) {
115 log.error(e, e);
116 } catch (IOException e) {
117 log.error(e, e);
118 }
119 return returnValue;
120 }
121
122
123 public Document reInitDocument(Document document) {
124 try {
125 byte[] barray = ArtifactXMLUtilities.writeDocument2String(document).getBytes(
126 "UTF-8");
127 InputStream inputStream = new ByteArrayInputStream(barray);
128 return ArtifactXMLUtilities.readDocument(inputStream);
129 } catch (UnsupportedEncodingException e) {
130 log.error(e, e);
131 }
132 return document;
133 }
134
135
136 /**
137 * Creates an <code>Element</code> with {@link #XFORM_PREFIX} and <i>name
138 * </i>.
139 *
140 * @param document A document.
141 * @param name The node name.
142 * @return the created element.
143 */
144 public static Element createXFormElement(Document document, String name) {
145 Element node = document.createElementNS(XFORM_URL, name);
146 node.setPrefix(XFORM_PREFIX);
147 return node;
148 }
149
150 /**
151 * Creates an exception node.
152 *
153 * @param message The message in the node.
154 * @param document A document.
155 * @return the document containing the exception node.
156 */
157 public static Document createExceptionReport(String message, Document document) {
158 log.debug("ArtifactXMLUtilities.createExceptionReport");
159 Element exceptionReportNode = createArtifactElement(document,
160 "exceptionreport");
161 document.appendChild(exceptionReportNode);
162 Element exceptionNode = createArtifactElement(document,
163 "exception");
164 exceptionNode.setTextContent(message);
165 exceptionReportNode.appendChild(exceptionNode);
166 return document;
167 }
168
169 /**
170 * Creates an input exception node.
171 *
172 * @param msg The message in the node.
173 * @param doc A document.
174 * @return the document containing the exception node.
175 */
176 public static Document createInputExceptionReport(String msg, Document doc) {
177 Element exceptionReportNode = createArtifactElement(
178 doc,"exceptionreport");
179 Element exceptionNode = createArtifactElement(
180 doc,"exception");
181 Element inputNode = createArtifactElement(
182 doc, "input");
183 inputNode.setTextContent(msg);
184 exceptionNode.appendChild(inputNode);
185 exceptionReportNode.appendChild(exceptionNode);
186 doc.appendChild(exceptionReportNode);
187 return doc;
188 }
189
190 /**
191 * Creates a success node.
192 *
193 * @param message The message.
194 * @param document A document.
195 * @return the document containing the success node.
196 */
197 public static Document createSuccessReport(String message, Document document) {
198 log.debug("ArtifactXMLUtilities.creatSuccessReport");
199 Element reportNode = createArtifactElement(document, "result");
200 document.appendChild(reportNode);
201 Element successNode = createArtifactElement(document, "success");
202 successNode.setTextContent(message);
203 reportNode.appendChild(successNode);
204 return document;
205 }
206
207 /**
208 * Read <i>fileName</i> and return the first child node.
209 *
210 * @param fileName An xml document.
211 * @return the first child node in this document.
212 */
213 public Node readConfiguration(String fileName){
214 try {
215 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
216 factory.setValidating(false);
217 return factory.newDocumentBuilder().parse(fileName).getChildNodes().item(0);
218 } catch (SAXException e) {
219 log.error(e,e);
220 return null;
221 } catch (IOException e) {
222 log.error(e,e);
223 return null;
224 } catch (ParserConfigurationException e) {
225 log.error(e,e);
226 return null;
227 }
228 }
229 }
230 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org