annotate gnv/src/main/java/de/intevation/gnv/util/XMLUtils.java @ 35:4405f31bbc30

CodeCleanUP gnv/trunk@171 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Fri, 02 Oct 2009 08:49:18 +0000
parents 3cb753564552
children ad381cc47217
rev   line source
2
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
1 package de.intevation.gnv.util;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
2
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
3 import java.io.IOException;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
4 import java.io.InputStream;
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
5 import java.io.StringBufferInputStream;
2
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
6 import java.io.StringWriter;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
7
35
4405f31bbc30 CodeCleanUP
Tim Englich <tim.englich@intevation.de>
parents: 11
diff changeset
8 import javax.xml.namespace.NamespaceContext;
4405f31bbc30 CodeCleanUP
Tim Englich <tim.englich@intevation.de>
parents: 11
diff changeset
9 import javax.xml.namespace.QName;
2
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
10 import javax.xml.parsers.DocumentBuilder;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
11 import javax.xml.parsers.DocumentBuilderFactory;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
12 import javax.xml.parsers.ParserConfigurationException;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
13 import javax.xml.transform.Transformer;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
14 import javax.xml.transform.TransformerConfigurationException;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
15 import javax.xml.transform.TransformerException;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
16 import javax.xml.transform.TransformerFactory;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
17 import javax.xml.transform.TransformerFactoryConfigurationError;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
18 import javax.xml.transform.dom.DOMSource;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
19 import javax.xml.transform.stream.StreamResult;
35
4405f31bbc30 CodeCleanUP
Tim Englich <tim.englich@intevation.de>
parents: 11
diff changeset
20 import javax.xml.xpath.XPath;
4405f31bbc30 CodeCleanUP
Tim Englich <tim.englich@intevation.de>
parents: 11
diff changeset
21 import javax.xml.xpath.XPathConstants;
4405f31bbc30 CodeCleanUP
Tim Englich <tim.englich@intevation.de>
parents: 11
diff changeset
22 import javax.xml.xpath.XPathExpressionException;
4405f31bbc30 CodeCleanUP
Tim Englich <tim.englich@intevation.de>
parents: 11
diff changeset
23 import javax.xml.xpath.XPathFactory;
2
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
24
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
25 import org.apache.log4j.Logger;
35
4405f31bbc30 CodeCleanUP
Tim Englich <tim.englich@intevation.de>
parents: 11
diff changeset
26 import org.w3c.dom.Attr;
4405f31bbc30 CodeCleanUP
Tim Englich <tim.englich@intevation.de>
parents: 11
diff changeset
27 import org.w3c.dom.Document;
4405f31bbc30 CodeCleanUP
Tim Englich <tim.englich@intevation.de>
parents: 11
diff changeset
28 import org.w3c.dom.Element;
4405f31bbc30 CodeCleanUP
Tim Englich <tim.englich@intevation.de>
parents: 11
diff changeset
29 import org.w3c.dom.Node;
4405f31bbc30 CodeCleanUP
Tim Englich <tim.englich@intevation.de>
parents: 11
diff changeset
30 import org.w3c.dom.NodeList;
4405f31bbc30 CodeCleanUP
Tim Englich <tim.englich@intevation.de>
parents: 11
diff changeset
31 import org.xml.sax.SAXException;
2
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
32
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
33 /**
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
34 * @author Sascha L. Teichmann
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
35 */
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
36 public class XMLUtils
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
37 {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
38 private static Logger logger = Logger.getLogger(XMLUtils.class);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
39
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
40 public XMLUtils() {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
41 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
42
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
43 public static class ElementCreator
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
44 {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
45 protected Document document;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
46 protected String ns;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
47 protected String prefix;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
48
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
49 public ElementCreator(Document document, String ns, String prefix) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
50 this.document = document;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
51 this.ns = ns;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
52 this.prefix = prefix;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
53 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
54
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
55 public Element create(String name) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
56 Element element = document.createElementNS(ns, name);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
57 element.setPrefix(prefix);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
58 return element;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
59 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
60
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
61 public void addAttr(Element element, String name, String value) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
62 Attr attr = document.createAttributeNS(ns, name);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
63 attr.setValue(value);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
64 attr.setPrefix(prefix);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
65 element.setAttributeNode(attr);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
66 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
67 } // class ElementCreator
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
68
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
69 public Document newDocument() {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
70 try {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
71 return DocumentBuilderFactory
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
72 .newInstance()
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
73 .newDocumentBuilder()
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
74 .newDocument();
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
75 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
76 catch (ParserConfigurationException pce) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
77 logger.error(pce.getLocalizedMessage(), pce);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
78 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
79 return null;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
80 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
81
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
82 public XPath newXPath() {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
83 return newXPath(null);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
84 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
85
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
86 public XPath newXPath(NamespaceContext namespaceContext) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
87 XPathFactory factory = XPathFactory.newInstance();
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
88 XPath xpath = factory.newXPath();
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
89 if (namespaceContext != null) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
90 xpath.setNamespaceContext(namespaceContext);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
91 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
92 return xpath;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
93 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
94
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
95 public Object xpath(Object root, String query, QName returnTyp) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
96 return xpath(root, query, returnTyp, null);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
97 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
98
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
99 public final String xpathString(
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
100 Object root, String query, NamespaceContext namespaceContext
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
101 ) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
102 return (String)xpath(root, query, XPathConstants.STRING, namespaceContext);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
103 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
104
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
105 public static final Object xpath(
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
106 Object root,
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
107 String query,
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
108 QName returnType,
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
109 NamespaceContext namespaceContext
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
110 ) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
111 if (root == null) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
112 return null;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
113 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
114
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
115 try {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
116 XPath xpath = new XMLUtils().newXPath(namespaceContext);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
117 if (xpath != null) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
118 return xpath.evaluate(query, root, returnType);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
119 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
120 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
121 catch (XPathExpressionException xpee) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
122 logger.error(xpee.getLocalizedMessage(), xpee);
35
4405f31bbc30 CodeCleanUP
Tim Englich <tim.englich@intevation.de>
parents: 11
diff changeset
123 }
2
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
124
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
125 return null;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
126 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
127
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
128 public Object getXPath(
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
129 Object root, String query, QName returnType
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
130 ) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
131 return xpath(root, query, returnType);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
132 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
133
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
134 public String getStringXPath(String xpath) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
135 return getStringXPath(xpath, null);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
136 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
137
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
138 public NodeList getNodeSetXPath(Object root, String query) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
139 return (NodeList)getXPath(root, query, XPathConstants.NODESET);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
140 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
141
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
142 public Node getNodeXPath(Object root, String query) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
143 return (Node)getXPath(root, query, XPathConstants.NODE);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
144 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
145
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
146 public String getStringXPath(Object root, String xpath) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
147 return getStringXPath(root, xpath, null);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
148 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
149
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
150 public String getStringXPath(
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
151 Object root, String query, String def
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
152 ) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
153 String s = (String)getXPath(root, query, XPathConstants.STRING);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
154 return s == null || s.length() == 0
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
155 ? def
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
156 : s;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
157 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
158
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
159 public Document readDocument(InputStream inputStream){
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
160 Document returnValue = null;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
161 try {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
162 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
163 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
164 returnValue = docBuilder.parse (inputStream);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
165 } catch (ParserConfigurationException e) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
166 logger.error(e,e);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
167 } catch (SAXException e) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
168 logger.error(e,e);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
169 } catch (IOException e) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
170 logger.error(e,e);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
171 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
172 return returnValue;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
173 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
174
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
175 public String writeNode2String(Node node){
2
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
176 try {
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
177 DOMSource source = new DOMSource(node);
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
178 return writeDOMSource2String(source);
2
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
179 } catch (TransformerConfigurationException e) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
180 logger.error(e,e);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
181 } catch (TransformerFactoryConfigurationError e) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
182 logger.error(e,e);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
183 } catch (TransformerException e) {
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
184 logger.error(e,e);
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
185 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
186 return null;
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
187 }
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
188
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
189 public Document reInitDocument(Document document){
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
190
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
191 StringBufferInputStream inputStream = new StringBufferInputStream(this.writeDocument2String(document));
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
192 return this.readDocument(inputStream);
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
193 }
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
194
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
195
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
196 public String writeDocument2String(Document document){
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
197 try {
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
198 DOMSource source = new DOMSource(document);
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
199 return writeDOMSource2String(source);
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
200 } catch (TransformerConfigurationException e) {
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
201 logger.error(e,e);
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
202 } catch (TransformerFactoryConfigurationError e) {
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
203 logger.error(e,e);
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
204 } catch (TransformerException e) {
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
205 logger.error(e,e);
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
206 }
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
207 return null;
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
208 }
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
209
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
210 /**
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
211 * @param source
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
212 * @return
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
213 * @throws TransformerFactoryConfigurationError
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
214 * @throws TransformerConfigurationException
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
215 * @throws TransformerException
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
216 */
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
217 private String writeDOMSource2String(DOMSource source)
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
218 throws TransformerFactoryConfigurationError,
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
219 TransformerConfigurationException, TransformerException {
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
220 TransformerFactory transformerFactory = TransformerFactory.newInstance();
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
221 Transformer transformer = transformerFactory.newTransformer();
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
222 StringWriter sw = new StringWriter();
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
223 StreamResult result = new StreamResult(sw);
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
224 transformer.transform(source, result);
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
225 return sw.getBuffer().toString();
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents: 2
diff changeset
226 }
2
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
227 }
5e94403971af infrastructur for Communicationg with the ArtifactDatabase added.
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
228 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org