[PATCH] Resolve entities in artifacts configuration

Wald Commits scm-commit at wald.intevation.org
Thu Dec 24 19:02:34 CET 2015


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1450980131 -3600
# Node ID f099f0b22e62b69a48e0dbb6bf1af8f36fbd6d3f
# Parent  a173ecf4e537a97c16c97084d5ef80b0e161f37b
Resolve entities in artifacts configuration.

diff -r a173ecf4e537 -r f099f0b22e62 artifacts-common/src/main/java/org/dive4elements/artifacts/common/utils/XMLUtils.java
--- 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);


More information about the Dive4Elements-commits mailing list