cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject [cxf-fediz] 01/02: Revert "Switching static DocumentBuilderFactory to use a ThreadLocal instead"
Date Mon, 11 Jun 2018 09:42:33 GMT
This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch 1.4.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf-fediz.git

commit 5fe31259af670295d77a55c136c3e13ba572835d
Author: Colm O hEigeartaigh <coheigea@apache.org>
AuthorDate: Mon Jun 11 10:22:24 2018 +0100

    Revert "Switching static DocumentBuilderFactory to use a ThreadLocal instead"
    
    This reverts commit f4d8caaaccd0ea0428b7def64d8d0607a915d28f.
---
 .../org/apache/cxf/fediz/core/util/DOMUtils.java   | 60 ++++++++++++----------
 1 file changed, 33 insertions(+), 27 deletions(-)

diff --git a/plugins/core/src/main/java/org/apache/cxf/fediz/core/util/DOMUtils.java b/plugins/core/src/main/java/org/apache/cxf/fediz/core/util/DOMUtils.java
index fc43aef..9c533fc 100644
--- a/plugins/core/src/main/java/org/apache/cxf/fediz/core/util/DOMUtils.java
+++ b/plugins/core/src/main/java/org/apache/cxf/fediz/core/util/DOMUtils.java
@@ -65,7 +65,23 @@ public final class DOMUtils {
 
     private static final String XMLNAMESPACE = "xmlns";
 
-    private static final ThreadLocal<DocumentBuilder> DOC_BUILDER_TL = new MyThreadLocal();
+    private static final DocumentBuilderFactory DBF = DocumentBuilderFactory.newInstance();
+
+    static {
+        try {
+            DBF.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
+            DBF.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
+
+            DBF.setValidating(false);
+            DBF.setIgnoringComments(false);
+            DBF.setIgnoringElementContentWhitespace(true);
+            DBF.setNamespaceAware(true);
+            // DBF.setCoalescing(true);
+            // DBF.setExpandEntityReferences(true);
+        } catch (ParserConfigurationException ex) {
+            LOG.error("Error configuring DocumentBuilderFactory", ex);
+        }
+    }
 
     private DOMUtils() {
     }
@@ -412,12 +428,14 @@ public final class DOMUtils {
      */
     public static Document readXml(InputStream is) throws SAXException, IOException,
         ParserConfigurationException {
-        return createDocumentBuilder().parse(is);
+        DocumentBuilder db = DBF.newDocumentBuilder();
+        return db.parse(is);
     }
 
     public static Document readXml(Reader is) throws SAXException, IOException, ParserConfigurationException
{
         InputSource ips = new InputSource(is);
-        return createDocumentBuilder().parse(ips);
+        DocumentBuilder db = DBF.newDocumentBuilder();
+        return db.parse(ips);
     }
 
     public static Document readXml(StreamSource is) throws SAXException, IOException,
@@ -427,7 +445,8 @@ public final class DOMUtils {
         is2.setByteStream(is.getInputStream());
         is2.setCharacterStream(is.getReader());
 
-        return createDocumentBuilder().parse(is2);
+        DocumentBuilder db = DBF.newDocumentBuilder();
+        return db.parse(is2);
     }
 
     public static void writeXml(Node n, OutputStream os) throws TransformerException {
@@ -440,13 +459,19 @@ public final class DOMUtils {
     }
 
     public static DocumentBuilder createDocumentBuilder() {
-        DocumentBuilder documentBuilder = DOC_BUILDER_TL.get();
-        documentBuilder.reset();
-        return documentBuilder;
+        try {
+            return DBF.newDocumentBuilder();
+        } catch (ParserConfigurationException e) {
+            throw new RuntimeException("Couldn't find a DOM parser.", e);
+        }
     }
 
     public static Document createDocument() {
-        return createDocumentBuilder().newDocument();
+        try {
+            return DBF.newDocumentBuilder().newDocument();
+        } catch (ParserConfigurationException e) {
+            throw new RuntimeException("Couldn't find a DOM parser.", e);
+        }
     }
 
     public static String getPrefixRecursive(Element el, String ns) {
@@ -633,23 +658,4 @@ public final class DOMUtils {
     public static void addNamespacePrefix(Element element, String namespaceUri, String prefix)
{
         element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + prefix, namespaceUri);
     }
-
-    private static final class MyThreadLocal extends ThreadLocal<DocumentBuilder> {
-
-        @Override
-        protected DocumentBuilder initialValue() {
-            try {
-                DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
-                dfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
-                dfactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl",
true);
-                dfactory.setValidating(false);
-                dfactory.setNamespaceAware(true);
-
-                return dfactory.newDocumentBuilder();
-            } catch (ParserConfigurationException e) {
-                LOG.error("Error configuring DocumentBuilderFactory", e);
-                throw new RuntimeException(e);
-            }
-        }
-    }
 }

-- 
To stop receiving notification emails like this one, please contact
coheigea@apache.org.

Mime
View raw message