Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/ArtifactXMLUtilities.java @ 657:af3f56758f59
merged gnv-artifacts/0.5
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:53 +0200 |
parents | b8080695ea62 |
children | dde7f51dbe1e |
comparison
equal
deleted
inserted
replaced
590:5f5f273c8566 | 657:af3f56758f59 |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package de.intevation.gnv.utils; | |
5 | |
6 import java.io.ByteArrayInputStream; | |
7 import java.io.IOException; | |
8 import java.io.InputStream; | |
9 import java.io.Serializable; | |
10 import java.io.StringWriter; | |
11 import java.io.UnsupportedEncodingException; | |
12 | |
13 import javax.xml.parsers.DocumentBuilder; | |
14 import javax.xml.parsers.DocumentBuilderFactory; | |
15 import javax.xml.parsers.ParserConfigurationException; | |
16 import javax.xml.transform.Transformer; | |
17 import javax.xml.transform.TransformerConfigurationException; | |
18 import javax.xml.transform.TransformerException; | |
19 import javax.xml.transform.TransformerFactory; | |
20 import javax.xml.transform.TransformerFactoryConfigurationError; | |
21 import javax.xml.transform.dom.DOMSource; | |
22 import javax.xml.transform.stream.StreamResult; | |
23 | |
24 import org.apache.log4j.Logger; | |
25 import org.w3c.dom.Document; | |
26 import org.w3c.dom.Element; | |
27 import org.w3c.dom.Node; | |
28 import org.xml.sax.SAXException; | |
29 | |
30 import de.intevation.artifacts.ArtifactNamespaceContext; | |
31 | |
32 /** | |
33 * @author Tim Englich <tim.englich@intevation.de> | |
34 * | |
35 */ | |
36 public class ArtifactXMLUtilities implements Serializable { | |
37 /** | |
38 * | |
39 */ | |
40 private static final long serialVersionUID = -6236340358303411758L; | |
41 | |
42 /** | |
43 * the logger, used to log exceptions and additonaly information | |
44 */ | |
45 private static Logger log = Logger | |
46 .getLogger(ArtifactXMLUtilities.class); | |
47 | |
48 public static final String XFORM_URL = "http://www.w3.org/2002/xforms"; | |
49 public static final String XFORM_PREFIX = "xform"; | |
50 | |
51 /** | |
52 * Constructor | |
53 */ | |
54 public ArtifactXMLUtilities() { | |
55 } | |
56 | |
57 /** | |
58 * @param document | |
59 * @return | |
60 */ | |
61 public Element createArtifactElement(Document document, String name) { | |
62 Element node = document.createElementNS( | |
63 ArtifactNamespaceContext.NAMESPACE_URI, name); | |
64 node.setPrefix(ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
65 return node; | |
66 } | |
67 | |
68 public String writeDocument2String(Document document) { | |
69 try { | |
70 TransformerFactory transformerFactory = TransformerFactory | |
71 .newInstance(); | |
72 Transformer transformer = transformerFactory.newTransformer(); | |
73 DOMSource source = new DOMSource(document); | |
74 StringWriter sw = new StringWriter(); | |
75 StreamResult result = new StreamResult(sw); | |
76 transformer.transform(source, result); | |
77 return sw.getBuffer().toString(); | |
78 } catch (TransformerConfigurationException e) { | |
79 log.error(e, e); | |
80 } catch (TransformerFactoryConfigurationError e) { | |
81 log.error(e, e); | |
82 } catch (TransformerException e) { | |
83 log.error(e, e); | |
84 } | |
85 return null; | |
86 } | |
87 | |
88 public static Document readDocument(InputStream inputStream) { | |
89 Document returnValue = null; | |
90 try { | |
91 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory | |
92 .newInstance(); | |
93 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); | |
94 returnValue = docBuilder.parse(inputStream); | |
95 } catch (ParserConfigurationException e) { | |
96 log.error(e, e); | |
97 } catch (SAXException e) { | |
98 log.error(e, e); | |
99 } catch (IOException e) { | |
100 log.error(e, e); | |
101 } | |
102 return returnValue; | |
103 } | |
104 | |
105 public Document reInitDocument(Document document) { | |
106 try { | |
107 byte[] barray = this.writeDocument2String(document).getBytes( | |
108 "UTF-8"); | |
109 InputStream inputStream = new ByteArrayInputStream(barray); | |
110 return this.readDocument(inputStream); | |
111 } catch (UnsupportedEncodingException e) { | |
112 log.error(e, e); | |
113 } | |
114 return document; | |
115 } | |
116 | |
117 public static Element createXFormElement(Document document, String name) { | |
118 Element node = document.createElementNS(XFORM_URL, name); | |
119 node.setPrefix(XFORM_PREFIX); | |
120 return node; | |
121 } | |
122 | |
123 public Document createExceptionReport(String message, Document document) { | |
124 log.debug("ArtifactXMLUtilities.createExceptionReport"); | |
125 Element exceptionReportNode = this.createArtifactElement(document, | |
126 "exceptionreport"); | |
127 document.appendChild(exceptionReportNode); | |
128 Element exceptionNode = this.createArtifactElement(document, | |
129 "exception"); | |
130 exceptionNode.setTextContent(message); | |
131 exceptionReportNode.appendChild(exceptionNode); | |
132 return document; | |
133 } | |
134 | |
135 public Document createSuccessReport(String message, Document document) { | |
136 log.debug("ArtifactXMLUtilities.creatSuccessReport"); | |
137 Element reportNode = this.createArtifactElement(document, "result"); | |
138 document.appendChild(reportNode); | |
139 Element successNode = this.createArtifactElement(document, "success"); | |
140 successNode.setTextContent(message); | |
141 reportNode.appendChild(successNode); | |
142 return document; | |
143 } | |
144 | |
145 public Node readConfiguration(String fileName){ | |
146 try { | |
147 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
148 factory.setValidating(false); | |
149 return factory.newDocumentBuilder().parse(fileName).getChildNodes().item(0); | |
150 } catch (SAXException e) { | |
151 log.error(e,e); | |
152 return null; | |
153 } catch (IOException e) { | |
154 log.error(e,e); | |
155 return null; | |
156 } catch (ParserConfigurationException e) { | |
157 log.error(e,e); | |
158 return null; | |
159 } | |
160 } | |
161 | |
162 } |