Mercurial > mxd2map
annotate src/java/de/intevation/mxd/utils/XMLUtils.java @ 335:4cf94b919725
* src/java/de/intevation/mxd/writer/MapScriptWriter.java:
Fix Label handling according to mapscript 6.2
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Fri, 26 Oct 2012 12:46:41 +0200 |
parents | df4e0946ef02 |
children |
rev | line source |
---|---|
27
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
1 /* |
243 | 2 * Copyright (c) 2011 by Intevation GmbH, Germany <info@intevation.de> |
3 * | |
4 * This file is part of MXD2map. | |
27
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
5 * |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
6 * This program is free software under the LGPL (>=v2.1) |
243 | 7 * Read the file LICENCE.txt coming with the software for details |
27
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
8 * or visit http://www.gnu.org/licenses/ if it does not exist. |
243 | 9 * |
10 * MXD2map has been developed on behalf of the | |
11 * Bundesamt fuer Seeschifffahrt und Hydrographie (BSH) in Hamburg | |
12 * by Intevation GmbH. | |
13 * | |
14 * Authors: | |
15 * Raimund Renkert <raimund.renkert@intevation.de> | |
16 * Bjoern Schilberg <bjoern.schilberg@intevation.de> | |
17 * Stephan Holl <stephan.holl@intevation.de> | |
27
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
18 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
19 |
33
c51376f8e24c
Separated converter components into packages.
Raimund Renkert <rrenkert@intevation.de>
parents:
27
diff
changeset
|
20 package de.intevation.mxd.utils; |
27
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
21 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
22 import java.io.ByteArrayInputStream; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
23 import java.io.FileInputStream; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
24 import java.io.BufferedInputStream; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
25 import java.io.ByteArrayOutputStream; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
26 import java.io.File; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
27 import java.io.IOException; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
28 import java.io.InputStream; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
29 import java.io.OutputStream; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
30 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
31 import javax.xml.namespace.NamespaceContext; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
32 import javax.xml.namespace.QName; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
33 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
34 import javax.xml.parsers.DocumentBuilderFactory; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
35 import javax.xml.parsers.ParserConfigurationException; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
36 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
37 import javax.xml.transform.Transformer; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
38 import javax.xml.transform.TransformerConfigurationException; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
39 import javax.xml.transform.TransformerException; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
40 import javax.xml.transform.TransformerFactory; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
41 import javax.xml.transform.TransformerFactoryConfigurationError; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
42 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
43 import javax.xml.transform.dom.DOMSource; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
44 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
45 import javax.xml.transform.stream.StreamResult; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
46 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
47 import javax.xml.xpath.XPath; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
48 import javax.xml.xpath.XPathConstants; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
49 import javax.xml.xpath.XPathExpressionException; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
50 import javax.xml.xpath.XPathFactory; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
51 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
52 import org.apache.log4j.Logger; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
53 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
54 import org.w3c.dom.Attr; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
55 import org.w3c.dom.Document; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
56 import org.w3c.dom.Element; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
57 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
58 import org.xml.sax.SAXException; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
59 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
60 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
61 * Some helper functions to ease work with XML concering namespaces, XPATH |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
62 * and so on. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
63 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
64 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
65 public final class XMLUtils |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
66 { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
67 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
68 * W3C URL of XForms |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
69 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
70 public static final String XFORM_URL = "http://www.w3.org/2002/xforms"; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
71 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
72 * W3C prefix of XForms |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
73 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
74 public static final String XFORM_PREFIX = "xform"; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
75 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
76 private static Logger logger = Logger.getLogger(XMLUtils.class); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
77 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
78 private XMLUtils() { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
79 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
80 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
81 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
82 * Helper class to generate elements and attributes with |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
83 * namespaces. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
84 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
85 public static class ElementCreator |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
86 { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
87 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
88 * owner document of the elements to be created |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
89 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
90 protected Document document; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
91 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
92 * namespace to be used |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
93 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
94 protected String ns; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
95 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
96 * prefix to be used |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
97 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
98 protected String prefix; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
99 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
100 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
101 * Constructor to create an element/attribute creator |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
102 * with a given namespace and namespace prefix using a |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
103 * given owner document. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
104 * @param document The owning document |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
105 * @param ns The namespace |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
106 * @param prefix The namespace prefix |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
107 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
108 public ElementCreator(Document document, String ns, String prefix) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
109 this.document = document; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
110 this.ns = ns; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
111 this.prefix = prefix; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
112 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
113 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
114 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
115 * Creates a new element using the owning document with |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
116 * the this creators namespace and namespace prefix. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
117 * @param name The name of the element |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
118 * @return The new element |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
119 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
120 public Element create(String name) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
121 Element element = document.createElementNS(ns, name); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
122 element.setPrefix(prefix); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
123 return element; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
124 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
125 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
126 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
127 * Adds a new attribute and its value to a given element. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
128 * It does not set the namespace prefix. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
129 * @param element The element to add the attribute to |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
130 * @param name The name of the attribute |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
131 * @param value The value of the attribute |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
132 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
133 public void addAttr(Element element, String name, String value) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
134 addAttr(element, name, value, false); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
135 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
136 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
137 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
138 * Adds a new attribute and its value to a given element. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
139 * If the namespace prefix is used is decided by the 'addPrefix' flag. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
140 * @param element The element to add the attribute to |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
141 * @param name The name of the attribute |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
142 * @param value The value of the attribute |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
143 * @param addPrefix If true the creators namespace prefix is |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
144 * set on the attribute. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
145 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
146 public void addAttr( |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
147 Element element, |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
148 String name, |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
149 String value, |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
150 boolean addPrefix |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
151 ) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
152 if (addPrefix) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
153 Attr attr = document.createAttributeNS(ns, name); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
154 attr.setValue(value); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
155 attr.setPrefix(prefix); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
156 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
157 element.setAttributeNode(attr); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
158 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
159 else { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
160 element.setAttribute(name, value); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
161 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
162 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
163 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
164 } // class ElementCreator |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
165 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
166 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
167 * Creates a new XML document |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
168 * @return the new XML document ot null if something went wrong during |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
169 * creation. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
170 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
171 public static final Document newDocument() { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
172 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
173 factory.setNamespaceAware(true); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
174 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
175 try { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
176 return factory.newDocumentBuilder().newDocument(); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
177 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
178 catch (ParserConfigurationException pce) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
179 logger.error(pce.getLocalizedMessage(), pce); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
180 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
181 return null; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
182 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
183 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
184 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
185 * Loads a XML document namespace aware from a file |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
186 * @param file The file to load. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
187 * @return the XML document or null if something went wrong |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
188 * during loading. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
189 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
190 public static final Document parseDocument(File file) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
191 InputStream inputStream = null; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
192 try { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
193 inputStream = new BufferedInputStream(new FileInputStream(file)); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
194 return parseDocument(inputStream); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
195 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
196 catch (IOException ioe) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
197 logger.error(ioe.getLocalizedMessage(), ioe); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
198 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
199 finally { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
200 if (inputStream != null) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
201 try { inputStream.close(); } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
202 catch (IOException ioe) {} |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
203 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
204 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
205 return null; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
206 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
207 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
208 public static final Document parseDocument(InputStream inputStream) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
209 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
210 factory.setNamespaceAware(true); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
211 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
212 try { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
213 return factory.newDocumentBuilder().parse(inputStream); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
214 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
215 catch (ParserConfigurationException pce) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
216 logger.error(pce.getLocalizedMessage(), pce); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
217 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
218 catch (SAXException se) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
219 logger.error(se.getLocalizedMessage(), se); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
220 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
221 catch (IOException ioe) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
222 logger.error(ioe.getLocalizedMessage(), ioe); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
223 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
224 return null; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
225 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
226 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
227 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
228 * Creates a new XPath without a namespace context. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
229 * @return the new XPath. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
230 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
231 public static final XPath newXPath() { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
232 return newXPath(null); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
233 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
234 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
235 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
236 * Creates a new XPath with a given namespace context. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
237 * @param namespaceContext The namespace context to be used or null |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
238 * if none should be used. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
239 * @return The new XPath |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
240 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
241 public static final XPath newXPath(NamespaceContext namespaceContext) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
242 XPathFactory factory = XPathFactory.newInstance(); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
243 XPath xpath = factory.newXPath(); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
244 if (namespaceContext != null) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
245 xpath.setNamespaceContext(namespaceContext); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
246 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
247 return xpath; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
248 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
249 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
250 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
251 * Evaluates an XPath query on a given object and returns the result |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
252 * as a given type. No namespace context is used. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
253 * @param root The object which is used as the root of the tree to |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
254 * be searched in. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
255 * @param query The XPath query |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
256 * @param returnTyp The type of the result. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
257 * @return The result of type 'returnTyp' or null if something |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
258 * went wrong during XPath evaluation. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
259 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
260 public static final Object xpath( |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
261 Object root, |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
262 String query, |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
263 QName returnTyp |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
264 ) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
265 return xpath(root, query, returnTyp, null); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
266 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
267 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
268 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
269 * Evaluates an XPath query on a given object and returns the result |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
270 * as a string. A given namespace context is used. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
271 * @param root The object which is used as the root of the tree to |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
272 * be searched in. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
273 * @param query The XPath query |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
274 * @param namespaceContext The namespace context to be used or null |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
275 * if none should be used. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
276 * @return The result of the query or null if something went wrong |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
277 * during XPath evaluation. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
278 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
279 public static final String xpathString( |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
280 Object root, String query, NamespaceContext namespaceContext |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
281 ) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
282 return (String)xpath( |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
283 root, query, XPathConstants.STRING, namespaceContext); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
284 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
285 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
286 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
287 * Evaluates an XPath query on a given object and returns the result |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
288 * as a given type. Optionally a namespace context is used. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
289 * @param root The object which is used as the root of the tree to |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
290 * be searched in. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
291 * @param query The XPath query |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
292 * @param returnType The type of the result. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
293 * @param namespaceContext The namespace context to be used or null |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
294 * if none should be used. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
295 * @return The result of type 'returnTyp' or null if something |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
296 * went wrong during XPath evaluation. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
297 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
298 public static final Object xpath( |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
299 Object root, |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
300 String query, |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
301 QName returnType, |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
302 NamespaceContext namespaceContext |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
303 ) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
304 if (root == null) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
305 return null; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
306 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
307 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
308 try { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
309 XPath xpath = newXPath(namespaceContext); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
310 if (xpath != null) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
311 return xpath.evaluate(query, root, returnType); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
312 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
313 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
314 catch (XPathExpressionException xpee) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
315 logger.error(xpee.getLocalizedMessage(), xpee); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
316 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
317 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
318 return null; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
319 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
320 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
321 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
322 * Streams out an XML document to a given output stream. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
323 * @param document The document to be streamed out. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
324 * @param out The output stream to be used. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
325 * @return true if operation succeeded else false. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
326 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
327 public static boolean toStream(Document document, OutputStream out) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
328 try { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
329 Transformer transformer = |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
330 TransformerFactory.newInstance().newTransformer(); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
331 DOMSource source = new DOMSource(document); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
332 StreamResult result = new StreamResult(out); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
333 transformer.transform(source, result); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
334 return true; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
335 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
336 catch (TransformerConfigurationException tce) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
337 logger.error(tce.getLocalizedMessage(), tce); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
338 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
339 catch (TransformerFactoryConfigurationError tfce) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
340 logger.error(tfce.getLocalizedMessage(), tfce); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
341 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
342 catch (TransformerException te) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
343 logger.error(te.getLocalizedMessage(), te); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
344 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
345 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
346 return false; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
347 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
348 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
349 /** |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
350 * Transforms an XML document into a byte array. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
351 * @param document The document to be streamed out. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
352 * @return the byte array or null if operation failed or |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
353 * document is null. |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
354 */ |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
355 public static byte [] toByteArray(Document document) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
356 if (document == null) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
357 return null; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
358 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
359 ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
360 return toStream(document, baos) |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
361 ? baos.toByteArray() |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
362 : null; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
363 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
364 |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
365 public static Document fromByteArray(byte [] data) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
366 if (data == null) { |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
367 return null; |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
368 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
369 return parseDocument(new ByteArrayInputStream(data)); |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
370 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
371 } |
e5fdc37f8f94
Added XMLUtils to store map information.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
372 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |