annotate flys-aft/src/main/java/de/intevation/utils/XML.java @ 4092:b3fc044f75ba

Added code to store W/Q values into FLYS database. flys-aft/trunk@3602 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 05 Jan 2012 17:06:29 +0000
parents 44dc38ca8492
children da9df3641578
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
4083
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
29 import javax.xml.transform.Transformer;
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
30 import javax.xml.transform.TransformerFactory;
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
31 import javax.xml.transform.TransformerConfigurationException;
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
32 import javax.xml.transform.TransformerException;
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
33
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
34 import javax.xml.transform.stream.StreamSource;
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
35
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
36 import javax.xml.transform.dom.DOMSource;
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
37 import javax.xml.transform.dom.DOMResult;
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
38
4070
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 public final class XML
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 /** Logger for this class. */
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 private static Logger log = Logger.getLogger(XML.class);
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 public static class MapXPathVariableResolver
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 implements XPathVariableResolver
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 protected Map<String, String> variables;
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 MapXPathVariableResolver() {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 this.variables = new HashMap<String, String>();
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 public MapXPathVariableResolver(Map<String, String> variables) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 this.variables = variables;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58
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 public void addVariable(String name, String value) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 variables.put(name, value);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 }
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 @Override
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 public Object resolveVariable(QName variableName) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 String key = variableName.getLocalPart();
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 return variables.get(key);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 } // class MapXPathVariableResolver
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 private XML() {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 /**
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 * Loads a XML document namespace aware from a file
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 * @param file The file to load.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 * @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
79 * during loading.
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 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
82 return parseDocument(file, Boolean.TRUE);
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4070
diff changeset
83 }
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4070
diff changeset
84
88f801888d85 load configuration an setup db connections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4070
diff changeset
85 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
86 InputStream inputStream = null;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 try {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 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
89 return parseDocument(inputStream, namespaceAware);
4070
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 catch (IOException ioe) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 log.error(ioe.getLocalizedMessage(), ioe);
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 finally {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 if (inputStream != null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 try { inputStream.close(); }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97 catch (IOException ioe) {}
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98 }
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 return null;
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
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 public static final Document parseDocument(InputStream inputStream) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 return parseDocument(inputStream, Boolean.TRUE);
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 public static final Document parseDocument(
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 InputStream inputStream,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 Boolean namespaceAware
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 ) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
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 if (namespaceAware != null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115 factory.setNamespaceAware(namespaceAware.booleanValue());
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
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 try {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 return factory.newDocumentBuilder().parse(inputStream);
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 catch (ParserConfigurationException pce) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 log.error(pce.getLocalizedMessage(), pce);
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 catch (SAXException se) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125 log.error(se.getLocalizedMessage(), se);
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 catch (IOException ioe) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128 log.error(ioe.getLocalizedMessage(), ioe);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 return null;
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
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
134 /**
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
135 * Creates a new XPath without a namespace context.
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 return newXPath(null, null);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
140 }
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 /**
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
143 * Creates a new XPath with a given namespace context.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
144 * @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
145 * if none should be used.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
146 * @return The new XPath
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 public static final XPath newXPath(
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
149 NamespaceContext namespaceContext,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
150 XPathVariableResolver resolver)
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
151 {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
152 XPathFactory factory = XPathFactory.newInstance();
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
153 XPath xpath = factory.newXPath();
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
154 if (namespaceContext != null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
155 xpath.setNamespaceContext(namespaceContext);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
156 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
157
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
158 if (resolver != null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
159 xpath.setXPathVariableResolver(resolver);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
160 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
161 return xpath;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
162 }
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 /**
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165 * 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
166 * as a given type. No namespace context is used.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
167 * @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
168 * be searched in.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
169 * @param query The XPath query
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
170 * @param returnTyp The type of the result.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
171 * @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
172 * went wrong during XPath evaluation.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
173 */
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
174 public static final Object xpath(
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
175 Object root,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
176 String query,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
177 QName returnTyp
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
178 ) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
179 return xpath(root, query, returnTyp, null);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
180 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
181
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
182 /**
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
183 * 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
184 * 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
185 * @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
186 * be searched in.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
187 * @param query The XPath query
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
188 * @param returnType The type of the result.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
189 * @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
190 * if none should be used.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
191 * @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
192 * went wrong during XPath evaluation.
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
193 */
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
194 public static final Object xpath(
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
195 Object root,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
196 String query,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
197 QName returnType,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
198 NamespaceContext namespaceContext
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 return xpath(root, query, returnType, namespaceContext, null);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
201 }
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 public static final Object xpath(
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
204 Object root,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
205 String query,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
206 QName returnType,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
207 NamespaceContext namespaceContext,
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
208 Map<String, String> variables)
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 if (root == null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
211 return null;
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 XPathVariableResolver resolver = variables != null
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
215 ? new MapXPathVariableResolver(variables)
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
216 : null;
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 try {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
219 XPath xpath = newXPath(namespaceContext, resolver);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
220 if (xpath != null) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
221 return xpath.evaluate(query, root, returnType);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
222 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
223 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
224 catch (XPathExpressionException xpee) {
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
225 log.error(xpee.getLocalizedMessage(), xpee);
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
226 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
227
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
228 return null;
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
229 }
4083
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
230
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
231 public static Document transform(
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
232 Document document,
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
233 File xformFile
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
234 ) {
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
235 try {
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
236 Transformer transformer =
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
237 TransformerFactory
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
238 .newInstance()
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
239 .newTransformer(
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
240 new StreamSource(xformFile));
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
241
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
242 DOMResult result = new DOMResult();
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
243
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
244 transformer.transform(new DOMSource(document), result);
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
245
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
246 return (Document)result.getNode();
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
247 }
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
248 catch (TransformerConfigurationException tce) {
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
249 log.error(tce, tce);
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
250 }
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
251 catch (TransformerException te) {
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
252 log.error(te, te);
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
253 }
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
254
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
255 return null;
44dc38ca8492 Added an optional XSL transformation to fix the raw DIPS files.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 4072
diff changeset
256 }
4070
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
257 }
d09adfa90942 Added XML/XPath support.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
258 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org