changeset 546:f099f0b22e62

Resolve entities in artifacts configuration.
author Tom Gottfried <tom@intevation.de>
date Thu, 24 Dec 2015 19:02:11 +0100
parents a173ecf4e537
children 266b599a4819
files artifacts-common/src/main/java/org/dive4elements/artifacts/common/utils/XMLUtils.java
diffstat 1 files changed, 32 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts-common/src/main/java/org/dive4elements/artifacts/common/utils/XMLUtils.java	Thu Dec 10 14:50:56 2015 +0100
+++ b/artifacts-common/src/main/java/org/dive4elements/artifacts/common/utils/XMLUtils.java	Thu Dec 24 19:02:11 2015 +0100
@@ -20,6 +20,7 @@
 import java.io.BufferedInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -29,6 +30,7 @@
 import javax.xml.namespace.QName;
 
 import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.ParserConfigurationException;
 
 import javax.xml.transform.Transformer;
@@ -55,6 +57,9 @@
 import org.w3c.dom.Node;
 
 import org.xml.sax.SAXException;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+
 
 /**
  * Some helper functions to ease work with XML concering namespaces, XPATH
@@ -156,6 +161,21 @@
     } // class ElementCreator
 
     /**
+     * Resolver for entities in artifacts configuration.
+     */
+    private static final EntityResolver CONF_RESOLVER = new EntityResolver() {
+        @Override
+        public InputSource resolveEntity(
+            String publicId,
+            String systemId
+        ) throws SAXException, IOException {
+            return new InputSource(
+                new FileReader(Config.replaceConfigDir(systemId)));
+        }
+    };
+
+
+    /**
      * Creates a new XML document
      * @return the new XML document ot null if something went wrong during
      * creation.
@@ -231,6 +251,15 @@
         InputStream inputStream,
         Boolean     namespaceAware
     ) {
+        return parseDocument(
+            inputStream, namespaceAware, CONF_RESOLVER);
+    }
+
+    public static final Document parseDocument(
+        InputStream    inputStream,
+        Boolean        namespaceAware,
+        EntityResolver entityResolver
+    ) {
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 
         if (namespaceAware != null) {
@@ -238,7 +267,9 @@
         }
 
         try {
-            return factory.newDocumentBuilder().parse(inputStream);
+            DocumentBuilder builder = factory.newDocumentBuilder();
+            builder.setEntityResolver(entityResolver);
+            return builder.parse(inputStream);
         }
         catch (ParserConfigurationException pce) {
             logger.error(pce.getLocalizedMessage(), pce);

http://dive4elements.wald.intevation.org