annotate artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java @ 67:ed03cc0e5800

Added method to XMLUtils to copy an XML document to an output stream. artifacts/trunk@553 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 17 Jan 2010 14:00:06 +0000
parents 8b72676698b5
children 48d1a9a082c2
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
67
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
3 import java.io.File;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
4 import java.io.IOException;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
5 import java.io.OutputStream;
23
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
6
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
7 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
8 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
9
67
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
10 import javax.xml.parsers.DocumentBuilderFactory;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
11 import javax.xml.parsers.ParserConfigurationException;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
12
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
13 import javax.xml.transform.Transformer;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
14 import javax.xml.transform.TransformerConfigurationException;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
15 import javax.xml.transform.TransformerException;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
16 import javax.xml.transform.TransformerFactory;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
17 import javax.xml.transform.TransformerFactoryConfigurationError;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
18
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
19 import javax.xml.transform.dom.DOMSource;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
20
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
21 import javax.xml.transform.stream.StreamResult;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
22
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
23 import javax.xml.xpath.XPath;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
24 import javax.xml.xpath.XPathConstants;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
25 import javax.xml.xpath.XPathExpressionException;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
26 import javax.xml.xpath.XPathFactory;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
27
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
28 import org.apache.log4j.Logger;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
29
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
30 import org.w3c.dom.Attr;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
31 import org.w3c.dom.Document;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
32 import org.w3c.dom.Element;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
33
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
34 import org.xml.sax.SAXException;
62
76abf0c64d3b Added XML utils method to load XML file from file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
35
20
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 /**
67
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
37 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
20
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 */
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 public final class XMLUtils
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 {
64
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
41 public static final String XFORM_URL = "http://www.w3.org/2002/xforms";
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
42 public static final String XFORM_PREFIX = "xform";
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
43
20
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 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
45
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 private XMLUtils() {
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 static class ElementCreator
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 {
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 protected Document document;
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 protected String ns;
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 protected String prefix;
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 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
56 this.document = document;
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 this.ns = ns;
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 this.prefix = prefix;
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 }
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 public Element create(String name) {
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 Element element = document.createElementNS(ns, name);
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 element.setPrefix(prefix);
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 return element;
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 }
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 public void addAttr(Element element, String name, String value) {
64
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
68 addAttr(element, name, value, false);
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
69 }
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
70
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
71 public void addAttr(
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
72 Element element,
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
73 String name,
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
74 String value,
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
75 boolean addPrefix
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
76 ) {
20
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 Attr attr = document.createAttributeNS(ns, name);
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 attr.setValue(value);
64
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
79
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
80 if (addPrefix)
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
81 attr.setPrefix(prefix);
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
82
20
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 element.setAttributeNode(attr);
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 }
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 } // class ElementCreator
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 public static final Document newDocument() {
64
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
88 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
89 factory.setNamespaceAware(true);
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
90
20
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 try {
64
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
92 return factory.newDocumentBuilder().newDocument();
20
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 }
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 catch (ParserConfigurationException pce) {
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 logger.error(pce.getLocalizedMessage(), pce);
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 }
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97 return null;
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98 }
23
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
99
62
76abf0c64d3b Added XML utils method to load XML file from file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
100 public static final Document parseDocument(File file) {
64
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
101 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
102 factory.setNamespaceAware(true);
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
103
62
76abf0c64d3b Added XML utils method to load XML file from file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
104 try {
64
8b72676698b5 Create a namespace aware document out of the request. We consider namespaces now. Adapted xpath to find action.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 62
diff changeset
105 return factory.newDocumentBuilder().parse(file);
62
76abf0c64d3b Added XML utils method to load XML file from file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
106 }
76abf0c64d3b Added XML utils method to load XML file from file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
107 catch (ParserConfigurationException pce) {
76abf0c64d3b Added XML utils method to load XML file from file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
108 logger.error(pce.getLocalizedMessage(), pce);
76abf0c64d3b Added XML utils method to load XML file from file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
109 }
76abf0c64d3b Added XML utils method to load XML file from file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
110 catch (SAXException se) {
76abf0c64d3b Added XML utils method to load XML file from file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
111 logger.error(se.getLocalizedMessage(), se);
76abf0c64d3b Added XML utils method to load XML file from file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
112 }
76abf0c64d3b Added XML utils method to load XML file from file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
113 catch (IOException ioe) {
76abf0c64d3b Added XML utils method to load XML file from file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
114 logger.error(ioe.getLocalizedMessage(), ioe);
76abf0c64d3b Added XML utils method to load XML file from file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
115 }
76abf0c64d3b Added XML utils method to load XML file from file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
116 return null;
76abf0c64d3b Added XML utils method to load XML file from file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
117 }
76abf0c64d3b Added XML utils method to load XML file from file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
118
23
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
119 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
120 return newXPath(null);
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
121 }
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
122
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
123 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
124 XPathFactory factory = XPathFactory.newInstance();
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
125 XPath xpath = factory.newXPath();
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
126 if (namespaceContext != null) {
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
127 xpath.setNamespaceContext(namespaceContext);
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
128 }
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
129 return xpath;
00596a591a2f Added possibibilty to ceate XPaths with namespace contextes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 20
diff changeset
130 }
25
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
131
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
132 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
133 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
134 }
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
135
26
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 25
diff changeset
136 public static final String xpathString(
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 25
diff changeset
137 Object root, String query, NamespaceContext namespaceContext
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 25
diff changeset
138 ) {
67
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
139 return (String)xpath(
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
140 root, query, XPathConstants.STRING, namespaceContext);
26
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 25
diff changeset
141 }
83a059c204f8 Connected /create with artifact db.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 25
diff changeset
142
25
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
143 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
144 Object root,
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
145 String query,
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
146 QName returnType,
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
147 NamespaceContext namespaceContext
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
148 ) {
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
149 if (root == null) {
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
150 return null;
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
151 }
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
152
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
153 try {
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
154 XPath xpath = newXPath(namespaceContext);
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
155 if (xpath != null) {
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
156 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
157 }
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
158 }
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
159 catch (XPathExpressionException xpee) {
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
160 logger.error(xpee.getLocalizedMessage(), xpee);
47
4ae4dc99127d Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 26
diff changeset
161 }
25
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
162
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
163 return null;
ccc6aae25585 Moved some XPath stuff from Config into XMLUtils.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 23
diff changeset
164 }
67
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
165
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
166 public static boolean toStream(Document document, OutputStream out) {
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
167 try {
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
168 Transformer transformer =
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
169 TransformerFactory.newInstance().newTransformer();
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
170 DOMSource source = new DOMSource(document);
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
171 StreamResult result = new StreamResult(out);
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
172 transformer.transform(source, result);
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
173 return true;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
174 }
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
175 catch (TransformerConfigurationException tce) {
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
176 logger.error(tce.getLocalizedMessage(), tce);
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
177 }
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
178 catch (TransformerFactoryConfigurationError tfce) {
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
179 logger.error(tfce.getLocalizedMessage(), tfce);
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
180 }
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
181 catch (TransformerException te) {
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
182 logger.error(te.getLocalizedMessage(), te);
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
183 }
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
184
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
185 return false;
ed03cc0e5800 Added method to XMLUtils to copy an XML document to an output stream.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 64
diff changeset
186 }
20
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
187 }
ff666592c1c3 Added factory listing '/factories' to web app.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
188 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org