annotate artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java @ 23:00596a591a2f

Added possibibilty to ceate XPaths with namespace contextes. artifacts/trunk@57 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 10 Sep 2009 08:14:03 +0000
parents ff666592c1c3
children ccc6aae25585
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;
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
15
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
16 import javax.xml.namespace.NamespaceContext;
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
20
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 /**
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 * @author Sascha L. Teichmann
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 public final class XMLUtils
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 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
25
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 private XMLUtils() {
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
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 public static class ElementCreator
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 protected Document document;
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 protected String ns;
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 protected String prefix;
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 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
36 this.document = document;
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 this.ns = ns;
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 this.prefix = prefix;
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 }
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 public Element create(String name) {
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 Element element = document.createElementNS(ns, name);
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 element.setPrefix(prefix);
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 return element;
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 }
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 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
48 Attr attr = document.createAttributeNS(ns, name);
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 attr.setValue(value);
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 attr.setPrefix(prefix);
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 element.setAttributeNode(attr);
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 }
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 } // class ElementCreator
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 public static final Document newDocument() {
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 try {
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 return DocumentBuilderFactory
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 .newInstance()
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 .newDocumentBuilder()
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 .newDocument();
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 }
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 catch (ParserConfigurationException pce) {
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 logger.error(pce.getLocalizedMessage(), pce);
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 }
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 return null;
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 }
23
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
67
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
68 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
69 return newXPath(null);
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
70 }
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
71
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
72 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
73 XPathFactory factory = XPathFactory.newInstance();
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
74 XPath xpath = factory.newXPath();
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
75 if (namespaceContext != null) {
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
76 xpath.setNamespaceContext(namespaceContext);
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
77 }
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
78 return xpath;
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
79 }
20
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 }
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org