changeset 5785:d38004f0c52f

Datacage: Always clone XML template for builders. If the first is not cloned they get interlinked somehow. Feels like a bug in Java's DOM implementation.
author Sascha L. Teichmann <teichmann@intevation.de>
date Mon, 22 Apr 2013 17:01:17 +0200
parents efbbfe32e9fe
children 470460bfb5fc
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/BuilderPool.java
diffstat 1 files changed, 18 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/BuilderPool.java	Mon Apr 22 16:18:16 2013 +0200
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/BuilderPool.java	Mon Apr 22 17:01:17 2013 +0200
@@ -7,13 +7,9 @@
 
 import java.sql.SQLException;
 
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-
-import javax.xml.transform.dom.DOMResult;
-import javax.xml.transform.dom.DOMSource;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
 
 import org.apache.log4j.Logger;
 
@@ -46,32 +42,29 @@
 
         pool = new ArrayDeque<Builder>(poolSize);
         for (int i = 0; i < poolSize; ++i) {
-            Document doc = i > 0 // Clone all but the first.
-                ? cloneDocument(document)
-                : document;
-            pool.add(new Builder(doc));
+            pool.add(new Builder(cloneDocument(document)));
         }
     }
 
     private final static Document cloneDocument(Document document) {
+
         try {
-            TransformerFactory tfactory = TransformerFactory.newInstance();
-            Transformer xformer = tfactory.newTransformer();
-            DOMSource src = new DOMSource(document);
-            DOMResult dst = new DOMResult();
-            xformer.transform(src, dst);
-            return (Document)dst.getNode();
+            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+            DocumentBuilder db = dbf.newDocumentBuilder();
+
+            Node origRoot = document.getDocumentElement();
+
+            Document copy = db.newDocument();
+            Node copyRoot = copy.importNode(origRoot, true);
+            copy.appendChild(copyRoot);
+
+            return copy;
         }
-        catch (TransformerConfigurationException tce) {
-            log.error(tce);
-        }
-        catch (TransformerException te) {
-            log.error(te);
+        catch (ParserConfigurationException pce) {
+            log.error(pce);
         }
 
-        log.error(
-            "Returning original DOM document. " +
-            "This will result in threading errors!");
+        log.error("Returning original document. This will lead to threading issues.");
 
         return document;
     }

http://dive4elements.wald.intevation.org