Mercurial > dive4elements > framework
annotate artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java @ 57:cf9848c85755 0.2
RELEASE 0.2: Summarized NEWS and Changes
artifacts/trunk@338 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Hans Plum <hans.plum@intevation.de> |
---|---|
date | Fri, 13 Nov 2009 18:37:51 +0000 |
parents | 4ae4dc99127d |
children | 76abf0c64d3b |
rev | line source |
---|---|
20
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.artifactdatabase; |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import org.w3c.dom.Document; |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 import org.w3c.dom.Element; |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 import org.w3c.dom.Attr; |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 import javax.xml.parsers.DocumentBuilderFactory; |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 import javax.xml.parsers.ParserConfigurationException; |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 import org.apache.log4j.Logger; |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 |
23
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
13 import javax.xml.xpath.XPathFactory; |
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
14 import javax.xml.xpath.XPath; |
25
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
15 import javax.xml.xpath.XPathExpressionException; |
26
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
25
diff
changeset
|
16 import javax.xml.xpath.XPathConstants; |
23
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
17 |
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
18 import javax.xml.namespace.NamespaceContext; |
25
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
19 import javax.xml.namespace.QName; |
23
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
20 |
20
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 /** |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 * @author Sascha L. Teichmann |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 */ |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 public final class XMLUtils |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 { |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 private static Logger logger = Logger.getLogger(XMLUtils.class); |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 private XMLUtils() { |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 } |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 public static class ElementCreator |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 { |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 protected Document document; |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 protected String ns; |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 protected String prefix; |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 public ElementCreator(Document document, String ns, String prefix) { |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 this.document = document; |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 this.ns = ns; |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 this.prefix = prefix; |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 } |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 public Element create(String name) { |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 Element element = document.createElementNS(ns, name); |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 element.setPrefix(prefix); |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 return element; |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 } |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 public void addAttr(Element element, String name, String value) { |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 Attr attr = document.createAttributeNS(ns, name); |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 attr.setValue(value); |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 attr.setPrefix(prefix); |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 element.setAttributeNode(attr); |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 } |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 } // class ElementCreator |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 public static final Document newDocument() { |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 try { |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 return DocumentBuilderFactory |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 .newInstance() |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 .newDocumentBuilder() |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 .newDocument(); |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 } |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 catch (ParserConfigurationException pce) { |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 logger.error(pce.getLocalizedMessage(), pce); |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 } |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 return null; |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 } |
23
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
69 |
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
70 public static final XPath newXPath() { |
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
71 return newXPath(null); |
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
72 } |
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
73 |
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
74 public static final XPath newXPath(NamespaceContext namespaceContext) { |
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
75 XPathFactory factory = XPathFactory.newInstance(); |
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
76 XPath xpath = factory.newXPath(); |
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
77 if (namespaceContext != null) { |
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
78 xpath.setNamespaceContext(namespaceContext); |
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
79 } |
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
80 return xpath; |
00596a591a2f
Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
20
diff
changeset
|
81 } |
25
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
82 |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
83 public static final Object xpath(Object root, String query, QName returnTyp) { |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
84 return xpath(root, query, returnTyp, null); |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
85 } |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
86 |
26
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
25
diff
changeset
|
87 public static final String xpathString( |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
25
diff
changeset
|
88 Object root, String query, NamespaceContext namespaceContext |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
25
diff
changeset
|
89 ) { |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
25
diff
changeset
|
90 return (String)xpath(root, query, XPathConstants.STRING, namespaceContext); |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
25
diff
changeset
|
91 } |
83a059c204f8
Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
25
diff
changeset
|
92 |
25
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
93 public static final Object xpath( |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
94 Object root, |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
95 String query, |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
96 QName returnType, |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
97 NamespaceContext namespaceContext |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
98 ) { |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
99 if (root == null) { |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
100 return null; |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
101 } |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
102 |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
103 try { |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
104 XPath xpath = newXPath(namespaceContext); |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
105 if (xpath != null) { |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
106 return xpath.evaluate(query, root, returnType); |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
107 } |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
108 } |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
109 catch (XPathExpressionException xpee) { |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
110 logger.error(xpee.getLocalizedMessage(), xpee); |
47
4ae4dc99127d
Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
26
diff
changeset
|
111 } |
25
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
112 |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
113 return null; |
ccc6aae25585
Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
23
diff
changeset
|
114 } |
20
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
115 } |
ff666592c1c3
Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
116 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: |