annotate flys-aft/src/main/java/de/intevation/utils/XML.java @ 4070:d09adfa90942

Added XML/XPath support. flys-aft/trunk@3397 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 13 Dec 2011 10:01:44 +0000
parents
children 88f801888d85
rev   line source
4070
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.utils;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import java.io.FileInputStream;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 import java.io.BufferedInputStream;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 import java.io.File;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 import java.io.IOException;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 import java.io.InputStream;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 import org.w3c.dom.Document;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 import javax.xml.parsers.DocumentBuilderFactory;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 import javax.xml.parsers.ParserConfigurationException;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 import org.xml.sax.SAXException;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import org.apache.log4j.Logger;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 import java.util.HashMap;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 import java.util.Map;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 import javax.xml.namespace.NamespaceContext;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 import javax.xml.namespace.QName;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 import javax.xml.xpath.XPath;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 import javax.xml.xpath.XPathExpressionException;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 import javax.xml.xpath.XPathFactory;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 import javax.xml.xpath.XPathVariableResolver;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 public final class XML
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 /** Logger for this class. */
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 private static Logger log = Logger.getLogger(XML.class);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 public static class MapXPathVariableResolver
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 implements XPathVariableResolver
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 protected Map<String, String> variables;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 public MapXPathVariableResolver() {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 this.variables = new HashMap<String, String>();
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 public MapXPathVariableResolver(Map<String, String> variables) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 this.variables = variables;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 public void addVariable(String name, String value) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 variables.put(name, value);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 @Override
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 public Object resolveVariable(QName variableName) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 String key = variableName.getLocalPart();
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 return variables.get(key);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 } // class MapXPathVariableResolver
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 private XML() {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 /**
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 * Loads a XML document namespace aware from a file
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 * @param file The file to load.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 * @return the XML document or null if something went wrong
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 * during loading.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 */
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 public static final Document parseDocument(File file) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 InputStream inputStream = null;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 try {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 inputStream = new BufferedInputStream(new FileInputStream(file));
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 return parseDocument(inputStream);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 catch (IOException ioe) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 log.error(ioe.getLocalizedMessage(), ioe);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 finally {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 if (inputStream != null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 try { inputStream.close(); }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 catch (IOException ioe) {}
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 return null;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 public static final Document parseDocument(InputStream inputStream) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 return parseDocument(inputStream, Boolean.TRUE);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 public static final Document parseDocument(
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 InputStream inputStream,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 Boolean namespaceAware
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97 ) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 if (namespaceAware != null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 factory.setNamespaceAware(namespaceAware.booleanValue());
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104 try {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 return factory.newDocumentBuilder().parse(inputStream);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 catch (ParserConfigurationException pce) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 log.error(pce.getLocalizedMessage(), pce);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 catch (SAXException se) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 log.error(se.getLocalizedMessage(), se);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113 catch (IOException ioe) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114 log.error(ioe.getLocalizedMessage(), ioe);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116 return null;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
117 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120 /**
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
121 * Creates a new XPath without a namespace context.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 * @return the new XPath.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 */
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
124 public static final XPath newXPath() {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125 return newXPath(null, null);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
126 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
127
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128 /**
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129 * Creates a new XPath with a given namespace context.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 * @param namespaceContext The namespace context to be used or null
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
131 * if none should be used.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
132 * @return The new XPath
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
133 */
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
134 public static final XPath newXPath(
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
135 NamespaceContext namespaceContext,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
136 XPathVariableResolver resolver)
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
137 {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
138 XPathFactory factory = XPathFactory.newInstance();
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
139 XPath xpath = factory.newXPath();
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
140 if (namespaceContext != null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
141 xpath.setNamespaceContext(namespaceContext);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
142 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
143
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
144 if (resolver != null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
145 xpath.setXPathVariableResolver(resolver);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
146 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
147 return xpath;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
148 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
149
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
150 /**
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
151 * Evaluates an XPath query on a given object and returns the result
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
152 * as a given type. No namespace context is used.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
153 * @param root The object which is used as the root of the tree to
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
154 * be searched in.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
155 * @param query The XPath query
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
156 * @param returnTyp The type of the result.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
157 * @return The result of type 'returnTyp' or null if something
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
158 * went wrong during XPath evaluation.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
159 */
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
160 public static final Object xpath(
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
161 Object root,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
162 String query,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
163 QName returnTyp
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
164 ) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165 return xpath(root, query, returnTyp, null);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
166 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
167
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
168 /**
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
169 * Evaluates an XPath query on a given object and returns the result
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
170 * as a given type. Optionally a namespace context is used.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
171 * @param root The object which is used as the root of the tree to
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
172 * be searched in.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
173 * @param query The XPath query
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
174 * @param returnType The type of the result.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
175 * @param namespaceContext The namespace context to be used or null
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
176 * if none should be used.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
177 * @return The result of type 'returnTyp' or null if something
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
178 * went wrong during XPath evaluation.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
179 */
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
180 public static final Object xpath(
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
181 Object root,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
182 String query,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
183 QName returnType,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
184 NamespaceContext namespaceContext
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
185 ) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
186 return xpath(root, query, returnType, namespaceContext, null);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
187 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
188
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
189 public static final Object xpath(
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
190 Object root,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
191 String query,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
192 QName returnType,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
193 NamespaceContext namespaceContext,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
194 Map<String, String> variables)
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
195 {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
196 if (root == null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
197 return null;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
198 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
199
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
200 XPathVariableResolver resolver = variables != null
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
201 ? new MapXPathVariableResolver(variables)
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
202 : null;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
203
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
204 try {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
205 XPath xpath = newXPath(namespaceContext, resolver);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
206 if (xpath != null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
207 return xpath.evaluate(query, root, returnType);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
208 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
209 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
210 catch (XPathExpressionException xpee) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
211 log.error(xpee.getLocalizedMessage(), xpee);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
212 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
213
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
214 return null;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
215 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
216 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
217 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org