comparison gnv/src/main/java/de/intevation/gnv/util/XMLUtils.java @ 962:e7fda0ae8b92

Add more Javadocs gnv/trunk@1106 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 18 May 2010 10:52:12 +0000
parents d49e8695786c
children 3549253ecd7b
comparison
equal deleted inserted replaced
961:d98d3e82118c 962:e7fda0ae8b92
6 import java.io.StringBufferInputStream; 6 import java.io.StringBufferInputStream;
7 import java.io.StringWriter; 7 import java.io.StringWriter;
8 8
9 import javax.xml.namespace.NamespaceContext; 9 import javax.xml.namespace.NamespaceContext;
10 import javax.xml.namespace.QName; 10 import javax.xml.namespace.QName;
11
12 import javax.xml.parsers.DocumentBuilder; 11 import javax.xml.parsers.DocumentBuilder;
13 import javax.xml.parsers.DocumentBuilderFactory; 12 import javax.xml.parsers.DocumentBuilderFactory;
14 import javax.xml.parsers.ParserConfigurationException; 13 import javax.xml.parsers.ParserConfigurationException;
15
16 import javax.xml.transform.Transformer; 14 import javax.xml.transform.Transformer;
17 import javax.xml.transform.TransformerConfigurationException; 15 import javax.xml.transform.TransformerConfigurationException;
18 import javax.xml.transform.TransformerException; 16 import javax.xml.transform.TransformerException;
19 import javax.xml.transform.TransformerFactory; 17 import javax.xml.transform.TransformerFactory;
20 import javax.xml.transform.TransformerFactoryConfigurationError; 18 import javax.xml.transform.TransformerFactoryConfigurationError;
21
22 import javax.xml.transform.dom.DOMSource; 19 import javax.xml.transform.dom.DOMSource;
23
24 import javax.xml.transform.stream.StreamResult; 20 import javax.xml.transform.stream.StreamResult;
25
26 import javax.xml.xpath.XPath; 21 import javax.xml.xpath.XPath;
27 import javax.xml.xpath.XPathConstants; 22 import javax.xml.xpath.XPathConstants;
28 import javax.xml.xpath.XPathExpressionException; 23 import javax.xml.xpath.XPathExpressionException;
29 import javax.xml.xpath.XPathFactory; 24 import javax.xml.xpath.XPathFactory;
30 25
31 import org.apache.log4j.Logger; 26 import org.apache.log4j.Logger;
32
33 import org.w3c.dom.Attr; 27 import org.w3c.dom.Attr;
34 import org.w3c.dom.Document; 28 import org.w3c.dom.Document;
35 import org.w3c.dom.Element; 29 import org.w3c.dom.Element;
36 import org.w3c.dom.Node; 30 import org.w3c.dom.Node;
37 import org.w3c.dom.NodeList; 31 import org.w3c.dom.NodeList;
38
39 import org.xml.sax.SAXException; 32 import org.xml.sax.SAXException;
40 33
41 /** 34 /**
42 * @author Sascha L. Teichmann 35 * This class provides many helper-Methods for handling different kinds of
36 * XML-stuff.
37 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
38 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
43 */ 39 */
44 public class XMLUtils { 40 public class XMLUtils {
41
42 /**
43 * the logger, used to log exceptions and additonaly information
44 */
45 private static Logger logger = Logger.getLogger(XMLUtils.class); 45 private static Logger logger = Logger.getLogger(XMLUtils.class);
46 46
47 /**
48 * Constructor
49 */
47 public XMLUtils() { 50 public XMLUtils() {
48 } 51 }
49 52
53 /**
54 * Class which could be used to create XML-Elements
55 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
56 *
57 */
50 public static class ElementCreator { 58 public static class ElementCreator {
59
60 /**
61 * The document the elements should be placed in.
62 */
51 protected Document document; 63 protected Document document;
64
65 /**
66 * The namespace that should be used.
67 */
52 protected String ns; 68 protected String ns;
69
70 /**
71 * The prefix of the namespace that should be used.
72 */
53 protected String prefix; 73 protected String prefix;
54 74
75 /**
76 * Constructor
77 * @param document the document the elements should be placed in
78 * @param ns the namespace that should be used
79 * @param prefix the prefix of the namespace that should be used
80 */
55 public ElementCreator(Document document, String ns, String prefix) { 81 public ElementCreator(Document document, String ns, String prefix) {
56 this.document = document; 82 this.document = document;
57 this.ns = ns; 83 this.ns = ns;
58 this.prefix = prefix; 84 this.prefix = prefix;
59 } 85 }
60 86
87 /**
88 * Creates a new element using the given name.
89 * @param name the name of the new element.
90 * @return the new element
91 */
61 public Element create(String name) { 92 public Element create(String name) {
62 Element element = document.createElementNS(ns, name); 93 Element element = document.createElementNS(ns, name);
63 element.setPrefix(prefix); 94 element.setPrefix(prefix);
64 return element; 95 return element;
65 } 96 }
66 97
98 /**
99 * Adds a new attribute to the given element.
100 * @param element the element where the attribute should be placed in.
101 * @param name the name of the attribute
102 * @param value the value of the attribute
103 */
67 public void addAttr(Element element, String name, String value) { 104 public void addAttr(Element element, String name, String value) {
68 Attr attr = document.createAttributeNS(ns, name); 105 Attr attr = document.createAttributeNS(ns, name);
69 attr.setValue(value); 106 attr.setValue(value);
70 attr.setPrefix(prefix); 107 attr.setPrefix(prefix);
71 element.setAttributeNode(attr); 108 element.setAttributeNode(attr);
72 } 109 }
73 } // class ElementCreator 110 } // class ElementCreator
74 111
112 /**
113 * Creates a new document.
114 * @return the new document
115 */
75 public static Document newDocument() { 116 public static Document newDocument() {
76 try { 117 try {
77 return DocumentBuilderFactory.newInstance().newDocumentBuilder() 118 return DocumentBuilderFactory.newInstance().newDocumentBuilder()
78 .newDocument(); 119 .newDocument();
79 } catch (ParserConfigurationException pce) { 120 } catch (ParserConfigurationException pce) {
80 logger.error(pce.getLocalizedMessage(), pce); 121 logger.error(pce.getLocalizedMessage(), pce);
81 } 122 }
82 return null; 123 return null;
83 } 124 }
84 125
126 /**
127 * Creates a new <code>XPath</code>-expression
128 * @return the new <code>XPath</code>-expression
129 */
85 public static XPath newXPath() { 130 public static XPath newXPath() {
86 return newXPath(null); 131 return newXPath(null);
87 } 132 }
88 133
134 /**
135 * Creates a new <code>XPath</code>-expression
136 * @param namespaceContext the namespacecontext that should be used.
137 * @return the new <code>XPath</code>-expression
138 */
89 public static XPath newXPath(NamespaceContext namespaceContext) { 139 public static XPath newXPath(NamespaceContext namespaceContext) {
90 XPathFactory factory = XPathFactory.newInstance(); 140 XPathFactory factory = XPathFactory.newInstance();
91 XPath xpath = factory.newXPath(); 141 XPath xpath = factory.newXPath();
92 if (namespaceContext != null) { 142 if (namespaceContext != null) {
93 xpath.setNamespaceContext(namespaceContext); 143 xpath.setNamespaceContext(namespaceContext);
94 } 144 }
95 return xpath; 145 return xpath;
96 } 146 }
97 147
98 public static Object xpath(Object root, String query, QName returnTyp) { 148 /**
99 return xpath(root, query, returnTyp, null); 149 * Fetch the value of an element or attribute from the given resource
100 } 150 * using the query.
101 151 * @param root the source where the value should be fetch from
152 * @param query the query that should be used to fetch the value
153 * @param namespaceContext the namespacecontext that must match to
154 * fetch the value.
155 * @return the value
156 */
102 public static final String xpathString(Object root, String query, 157 public static final String xpathString(Object root, String query,
103 NamespaceContext namespaceContext) { 158 NamespaceContext namespaceContext) {
104 return (String) xpath(root, query, XPathConstants.STRING, 159 return (String) xpath(root, query, XPathConstants.STRING,
105 namespaceContext); 160 namespaceContext);
106 } 161 }
107 162
163 /**
164 * Fetch the object rom the given resource using the query.
165 * @param root the source where the value should be fetch from
166 * @param query the query that should be used to fetch the object
167 * @param returnType the Type that must be used to return the object,
168 * @param namespaceContext the namespacecontext that must match to
169 * fetch the object.
170 * @return the value
171 */
108 public static final Object xpath(Object root, String query, 172 public static final Object xpath(Object root, String query,
109 QName returnType, 173 QName returnType,
110 NamespaceContext namespaceContext) { 174 NamespaceContext namespaceContext) {
111 if (root == null) { 175 if (root == null) {
112 return null; 176 return null;
113 } 177 }
114 178 try {
115 try { 179 XPath xpath = XMLUtils.newXPath(namespaceContext);
116 XPath xpath = new XMLUtils().newXPath(namespaceContext);
117 if (xpath != null) { 180 if (xpath != null) {
118 return xpath.evaluate(query, root, returnType); 181 return xpath.evaluate(query, root, returnType);
119 } 182 }
120 } catch (XPathExpressionException xpee) { 183 } catch (XPathExpressionException xpee) {
121 logger.error(xpee.getLocalizedMessage(), xpee); 184 logger.error(xpee.getLocalizedMessage(), xpee);
122 } 185 }
123
124 return null; 186 return null;
125 } 187 }
126 188
189 /**
190 * Fetch the object rom the given resource using the query
191 * and the default <code>ArtifactNamespaceContext</code>
192 * @param root the source where the value should be fetch from
193 * @param query the query that should be used to fetch the object
194 * @param returnType the Type that must be used to return the object
195 * @return the value
196 */
127 public static Object getXPath(Object root, String query, QName returnType) { 197 public static Object getXPath(Object root, String query, QName returnType) {
128 return getXPath(root,query,returnType,ArtifactNamespaceContext.INSTANCE); 198 return getXPath(root,query,returnType,ArtifactNamespaceContext.INSTANCE);
129 } 199 }
130 200
201 /**
202 * Fetch the object rom the given resource using the query
203 * and the default <code>ArtifactNamespaceContext</code>
204 * @param root the source where the value should be fetch from
205 * @param query the query that should be used to fetch the object
206 * @param returnType the Type that must be used to return the object.
207 * @param namespaceContext the namespacecontext that must match to
208 * fetch the object.
209 * @return the value
210 */
131 public static Object getXPath( 211 public static Object getXPath(
132 Object root, String query, QName returnType, NamespaceContext context 212 Object root, String query, QName returnType, NamespaceContext context
133 ) { 213 ) {
134 return xpath(root, query, returnType, context); 214 return xpath(root, query, returnType, context);
135 } 215 }
136 216
137 public static String getStringXPath(String xpath) { 217 /**
138 return getStringXPath(xpath, null); 218 * Fetch a Nodeset value from a XML-Fragment or XML-Document using the
139 } 219 * given query.
140 220 * @param root the source where the String should be fetched from
221 * @param query the query that should be used,
222 * @return the Nodeset fetched from the source
223 */
141 public static NodeList getNodeSetXPath(Object root, String query) { 224 public static NodeList getNodeSetXPath(Object root, String query) {
142 return (NodeList) getXPath(root, query, XPathConstants.NODESET); 225 return (NodeList) getXPath(root, query, XPathConstants.NODESET);
143 } 226 }
144 227
228 /**
229 * Fetch a Node from a XML-Fragment or XML-Document using the
230 * given query.
231 * @param root the source where the Node should be fetched from
232 * @param query the query that should be used,
233 * @return the Node fetched from the source
234 */
145 public static Node getNodeXPath(Object root, String query) { 235 public static Node getNodeXPath(Object root, String query) {
146 return (Node) getXPath(root, query, XPathConstants.NODE); 236 return (Node) getXPath(root, query, XPathConstants.NODE);
147 } 237 }
148 238
239 /**
240 * Fetch a String value from a XML-Fragment or XML-Document using the
241 * given query.
242 * @param root the source where the String should be fetched from
243 * @param xpath the query that should be used,
244 * @return the String fetched from the source
245 */
149 public static String getStringXPath(Object root, String xpath) { 246 public static String getStringXPath(Object root, String xpath) {
150 return getStringXPath(root, xpath, null); 247 return getStringXPath(root, xpath, null);
151 } 248 }
152 249
250 /**
251 * Fetch a String value from a XML-Fragment or XML-Document using the
252 * given query.
253 * @param root the source where the String should be fetched from
254 * @param query the query that should be used,
255 * @param def the default-value that will be returned id no value was found
256 * @return the String fetched from the source
257 */
153 public static String getStringXPath(Object root, String query, String def) { 258 public static String getStringXPath(Object root, String query, String def) {
154 String s = (String) getXPath(root, query, XPathConstants.STRING); 259 String s = (String) getXPath(root, query, XPathConstants.STRING);
155 return s == null || s.length() == 0 ? def : s; 260 return s == null || s.length() == 0 ? def : s;
156 } 261 }
157 262
263 /**
264 * Reads an XML-document from a given <code>InputStream</code>
265 * @param inputStream the <code>InputStream</code> where the document
266 * should be read from
267 * @return the document that could be read.
268 */
158 public static Document readDocument(InputStream inputStream) { 269 public static Document readDocument(InputStream inputStream) {
159 Document returnValue = null; 270 Document returnValue = null;
160 try { 271 try {
161 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory 272 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
162 .newInstance(); 273 .newInstance();
171 logger.error(e, e); 282 logger.error(e, e);
172 } 283 }
173 return returnValue; 284 return returnValue;
174 } 285 }
175 286
287 /**
288 * Writes an single <code>XML-Node</code> to a string
289 * @param node the <code>XML-Node</code> that should be written
290 * @return the encoded <code>XML-Node</code>
291 */
176 public static String writeNode2String(Node node) { 292 public static String writeNode2String(Node node) {
177 try { 293 try {
178 DOMSource source = new DOMSource(node); 294 DOMSource source = new DOMSource(node);
179 return writeDOMSource2String(source); 295 return writeDOMSource2String(source);
180 } catch (TransformerConfigurationException e) { 296 } catch (TransformerConfigurationException e) {
185 logger.error(e, e); 301 logger.error(e, e);
186 } 302 }
187 return null; 303 return null;
188 } 304 }
189 305
306 /**
307 * Reinitialize the doument.
308 * This is neccessary because the namespace sometimes is invalid
309 * using a document which was created and used in the same step.
310 * @param document the document which should be reinitialize
311 * @return the document
312 */
190 public static Document reInitDocument(Document document) { 313 public static Document reInitDocument(Document document) {
191
192 StringBufferInputStream inputStream = new StringBufferInputStream( 314 StringBufferInputStream inputStream = new StringBufferInputStream(
193 writeDocument2String(document)); 315 writeDocument2String(document));
194 return readDocument(inputStream); 316 return readDocument(inputStream);
195 } 317 }
196 318
319 /**
320 * Writes the given Document into a String.
321 * This is very helpful for debugging.
322 * @param document the document which should be written to the string.
323 * @return the encoded xml-document.
324 */
197 public static String writeDocument2String(Document document) { 325 public static String writeDocument2String(Document document) {
198 try { 326 try {
199 DOMSource source = new DOMSource(document); 327 DOMSource source = new DOMSource(document);
200 return writeDOMSource2String(source); 328 return writeDOMSource2String(source);
201 } catch (TransformerConfigurationException e) { 329 } catch (TransformerConfigurationException e) {
207 } 335 }
208 return null; 336 return null;
209 } 337 }
210 338
211 /** 339 /**
212 * @param source 340 * Writes a given <code>DOMSource</code> into a String
213 * @return 341 * @param source the source that should be written to string
342 * @return the encoded <code>DOMSource</code>
214 * @throws TransformerFactoryConfigurationError 343 * @throws TransformerFactoryConfigurationError
215 * @throws TransformerConfigurationException 344 * @throws TransformerConfigurationException
216 * @throws TransformerException 345 * @throws TransformerException
217 */ 346 */
218 private static String writeDOMSource2String(DOMSource source) 347 private static String writeDOMSource2String(DOMSource source)
226 StreamResult result = new StreamResult(sw); 355 StreamResult result = new StreamResult(sw);
227 transformer.transform(source, result); 356 transformer.transform(source, result);
228 return sw.getBuffer().toString(); 357 return sw.getBuffer().toString();
229 } 358 }
230 359
231 public static boolean toStream(Document document, OutputStream out) { 360 /**
361 * Writes a given Document to an <code>OutputStream</code>
362 * @param document the document that should be written
363 * @param out the stream where the document should be written to,
364 * @return true if it was successful, false if not.
365 */
366 public static boolean toStream(Document document, OutputStream out) {
232 try { 367 try {
233 Transformer transformer = 368 Transformer transformer =
234 TransformerFactory.newInstance().newTransformer(); 369 TransformerFactory.newInstance().newTransformer();
235 DOMSource source = new DOMSource(document); 370 DOMSource source = new DOMSource(document);
236 StreamResult result = new StreamResult(out); 371 StreamResult result = new StreamResult(out);
244 logger.error(tfce.getLocalizedMessage(), tfce); 379 logger.error(tfce.getLocalizedMessage(), tfce);
245 } 380 }
246 catch (TransformerException te) { 381 catch (TransformerException te) {
247 logger.error(te.getLocalizedMessage(), te); 382 logger.error(te.getLocalizedMessage(), te);
248 } 383 }
249
250 return false; 384 return false;
251 } 385 }
252 } 386 }
253 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: 387 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org