Mercurial > dive4elements > framework
comparison artifacts-common/src/main/java/org/dive4elements/artifacts/common/utils/XMLUtils.java @ 546:f099f0b22e62
Resolve entities in artifacts configuration.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Thu, 24 Dec 2015 19:02:11 +0100 |
parents | 415df0fc4fa1 |
children | e781b847fdca |
comparison
equal
deleted
inserted
replaced
545:a173ecf4e537 | 546:f099f0b22e62 |
---|---|
18 import java.io.ByteArrayInputStream; | 18 import java.io.ByteArrayInputStream; |
19 import java.io.FileInputStream; | 19 import java.io.FileInputStream; |
20 import java.io.BufferedInputStream; | 20 import java.io.BufferedInputStream; |
21 import java.io.ByteArrayOutputStream; | 21 import java.io.ByteArrayOutputStream; |
22 import java.io.File; | 22 import java.io.File; |
23 import java.io.FileReader; | |
23 import java.io.IOException; | 24 import java.io.IOException; |
24 import java.io.InputStream; | 25 import java.io.InputStream; |
25 import java.io.OutputStream; | 26 import java.io.OutputStream; |
26 import java.io.StringWriter; | 27 import java.io.StringWriter; |
27 | 28 |
28 import javax.xml.namespace.NamespaceContext; | 29 import javax.xml.namespace.NamespaceContext; |
29 import javax.xml.namespace.QName; | 30 import javax.xml.namespace.QName; |
30 | 31 |
31 import javax.xml.parsers.DocumentBuilderFactory; | 32 import javax.xml.parsers.DocumentBuilderFactory; |
33 import javax.xml.parsers.DocumentBuilder; | |
32 import javax.xml.parsers.ParserConfigurationException; | 34 import javax.xml.parsers.ParserConfigurationException; |
33 | 35 |
34 import javax.xml.transform.Transformer; | 36 import javax.xml.transform.Transformer; |
35 import javax.xml.transform.TransformerConfigurationException; | 37 import javax.xml.transform.TransformerConfigurationException; |
36 import javax.xml.transform.TransformerException; | 38 import javax.xml.transform.TransformerException; |
53 import org.w3c.dom.Document; | 55 import org.w3c.dom.Document; |
54 import org.w3c.dom.Element; | 56 import org.w3c.dom.Element; |
55 import org.w3c.dom.Node; | 57 import org.w3c.dom.Node; |
56 | 58 |
57 import org.xml.sax.SAXException; | 59 import org.xml.sax.SAXException; |
60 import org.xml.sax.EntityResolver; | |
61 import org.xml.sax.InputSource; | |
62 | |
58 | 63 |
59 /** | 64 /** |
60 * Some helper functions to ease work with XML concering namespaces, XPATH | 65 * Some helper functions to ease work with XML concering namespaces, XPATH |
61 * and so on. | 66 * and so on. |
62 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> | 67 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> |
154 } | 159 } |
155 } | 160 } |
156 } // class ElementCreator | 161 } // class ElementCreator |
157 | 162 |
158 /** | 163 /** |
164 * Resolver for entities in artifacts configuration. | |
165 */ | |
166 private static final EntityResolver CONF_RESOLVER = new EntityResolver() { | |
167 @Override | |
168 public InputSource resolveEntity( | |
169 String publicId, | |
170 String systemId | |
171 ) throws SAXException, IOException { | |
172 return new InputSource( | |
173 new FileReader(Config.replaceConfigDir(systemId))); | |
174 } | |
175 }; | |
176 | |
177 | |
178 /** | |
159 * Creates a new XML document | 179 * Creates a new XML document |
160 * @return the new XML document ot null if something went wrong during | 180 * @return the new XML document ot null if something went wrong during |
161 * creation. | 181 * creation. |
162 */ | 182 */ |
163 public static final Document newDocument() { | 183 public static final Document newDocument() { |
229 | 249 |
230 public static final Document parseDocument( | 250 public static final Document parseDocument( |
231 InputStream inputStream, | 251 InputStream inputStream, |
232 Boolean namespaceAware | 252 Boolean namespaceAware |
233 ) { | 253 ) { |
254 return parseDocument( | |
255 inputStream, namespaceAware, CONF_RESOLVER); | |
256 } | |
257 | |
258 public static final Document parseDocument( | |
259 InputStream inputStream, | |
260 Boolean namespaceAware, | |
261 EntityResolver entityResolver | |
262 ) { | |
234 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | 263 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
235 | 264 |
236 if (namespaceAware != null) { | 265 if (namespaceAware != null) { |
237 factory.setNamespaceAware(namespaceAware.booleanValue()); | 266 factory.setNamespaceAware(namespaceAware.booleanValue()); |
238 } | 267 } |
239 | 268 |
240 try { | 269 try { |
241 return factory.newDocumentBuilder().parse(inputStream); | 270 DocumentBuilder builder = factory.newDocumentBuilder(); |
271 builder.setEntityResolver(entityResolver); | |
272 return builder.parse(inputStream); | |
242 } | 273 } |
243 catch (ParserConfigurationException pce) { | 274 catch (ParserConfigurationException pce) { |
244 logger.error(pce.getLocalizedMessage(), pce); | 275 logger.error(pce.getLocalizedMessage(), pce); |
245 } | 276 } |
246 catch (SAXException se) { | 277 catch (SAXException se) { |