annotate flys-aft/src/main/java/de/intevation/utils/XML.java @ 4072:88f801888d85

load configuration an setup db connections. flys-aft/trunk@3401 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 13 Dec 2011 12:33:36 +0000
parents d09adfa90942
children 44dc38ca8492
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) {
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4070
diff changeset
72 return parseDocument(file, Boolean.TRUE);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4070
diff changeset
73 }
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4070
diff changeset
74
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4070
diff changeset
75 public static final Document parseDocument(File file, Boolean namespaceAware) {
4070
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 InputStream inputStream = null;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 try {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 inputStream = new BufferedInputStream(new FileInputStream(file));
4072
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4070
diff changeset
79 return parseDocument(inputStream, namespaceAware);
4070
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 catch (IOException ioe) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 log.error(ioe.getLocalizedMessage(), ioe);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 finally {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 if (inputStream != null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 try { inputStream.close(); }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 catch (IOException ioe) {}
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 return null;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 }
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(InputStream inputStream) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 return parseDocument(inputStream, Boolean.TRUE);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 }
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 public static final Document parseDocument(
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99 InputStream inputStream,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 Boolean namespaceAware
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 ) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
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 if (namespaceAware != null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 factory.setNamespaceAware(namespaceAware.booleanValue());
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
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 try {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 return factory.newDocumentBuilder().parse(inputStream);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 catch (ParserConfigurationException pce) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112 log.error(pce.getLocalizedMessage(), pce);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114 catch (SAXException se) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115 log.error(se.getLocalizedMessage(), se);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
117 catch (IOException ioe) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 log.error(ioe.getLocalizedMessage(), ioe);
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 return null;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
121 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122
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 /**
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125 * Creates a new XPath without a namespace context.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
126 * @return the new XPath.
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 public static final XPath newXPath() {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129 return newXPath(null, null);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
131
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
132 /**
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
133 * Creates a new XPath with a given namespace context.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
134 * @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
135 * if none should be used.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
136 * @return The new XPath
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 public static final XPath newXPath(
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
139 NamespaceContext namespaceContext,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
140 XPathVariableResolver resolver)
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
141 {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
142 XPathFactory factory = XPathFactory.newInstance();
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
143 XPath xpath = factory.newXPath();
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
144 if (namespaceContext != null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
145 xpath.setNamespaceContext(namespaceContext);
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
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
148 if (resolver != null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
149 xpath.setXPathVariableResolver(resolver);
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 return xpath;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
152 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
153
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
154 /**
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
155 * 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
156 * as a given type. No namespace context is used.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
157 * @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
158 * be searched in.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
159 * @param query The XPath query
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
160 * @param returnTyp The type of the result.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
161 * @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
162 * went wrong during XPath evaluation.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
163 */
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
164 public static final Object xpath(
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165 Object root,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
166 String query,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
167 QName returnTyp
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 return xpath(root, query, returnTyp, null);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
170 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
171
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
172 /**
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
173 * 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
174 * 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
175 * @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
176 * be searched in.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
177 * @param query The XPath query
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
178 * @param returnType The type of the result.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
179 * @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
180 * if none should be used.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
181 * @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
182 * went wrong during XPath evaluation.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
183 */
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
184 public static final Object xpath(
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
185 Object root,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
186 String query,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
187 QName returnType,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
188 NamespaceContext namespaceContext
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
189 ) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
190 return xpath(root, query, returnType, namespaceContext, null);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
191 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
192
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
193 public static final Object xpath(
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
194 Object root,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
195 String query,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
196 QName returnType,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
197 NamespaceContext namespaceContext,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
198 Map<String, String> variables)
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 if (root == null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
201 return null;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
202 }
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 XPathVariableResolver resolver = variables != null
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
205 ? new MapXPathVariableResolver(variables)
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
206 : null;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
207
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
208 try {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
209 XPath xpath = newXPath(namespaceContext, resolver);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
210 if (xpath != null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
211 return xpath.evaluate(query, root, returnType);
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 catch (XPathExpressionException xpee) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
215 log.error(xpee.getLocalizedMessage(), xpee);
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
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
218 return null;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
219 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
220 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
221 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org