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