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