comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/ArtifactXMLUtilities.java @ 1119:7c4f81f74c47

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

http://dive4elements.wald.intevation.org