8025026: Enhance canonicalization
authorxuelei
Sat, 12 Oct 2013 20:46:35 -0700
changeset 22325 f22d936a0ff5
parent 22324 54a74fc6e85c
child 22326 2fb1fdf0e74e
8025026: Enhance canonicalization Summary: Don't use cached null xmlns definition. Also reviewed by Alexander Fomin <alexander.fomin@oracle.com> Reviewed-by: mullan, hawtin
jdk/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer11.java
jdk/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315.java
jdk/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315Excl.java
jdk/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/CanonicalizerBase.java
--- a/jdk/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer11.java	Sat Oct 12 10:22:43 2013 +0800
+++ b/jdk/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer11.java	Sat Oct 12 20:46:35 2013 -0700
@@ -382,7 +382,8 @@
             } else if (!isVisible(xmlns)) {
                 //There is a definition but the xmlns is not selected by the xpath.
                 //then xmlns=""
-                n = ns.addMappingAndRender(XMLNS, "", nullNode);
+                n = ns.addMappingAndRender(
+                        XMLNS, "", getNullNode(xmlns.getOwnerDocument()));
             }
             //output the xmlns def if needed.
             if (n != null) {
--- a/jdk/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315.java	Sat Oct 12 10:22:43 2013 +0800
+++ b/jdk/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315.java	Sat Oct 12 20:46:35 2013 -0700
@@ -327,7 +327,8 @@
             } else if (!isVisible(xmlns)) {
                 //There is a definition but the xmlns is not selected by the xpath.
                 //then xmlns=""
-                n = ns.addMappingAndRender(XMLNS, "", nullNode);
+                n = ns.addMappingAndRender(
+                        XMLNS, "", getNullNode(xmlns.getOwnerDocument()));
             }
             //output the xmlns def if needed.
             if (n != null) {
--- a/jdk/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315Excl.java	Sat Oct 12 10:22:43 2013 +0800
+++ b/jdk/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315Excl.java	Sat Oct 12 20:46:35 2013 -0700
@@ -292,7 +292,7 @@
             if (xmlns != null && !isVisible(xmlns)) {
                 // There is a definition but the xmlns is not selected by the
                 // xpath. then xmlns=""
-                ns.addMapping(XMLNS, "", nullNode);
+                ns.addMapping(XMLNS, "", getNullNode(xmlns.getOwnerDocument()));
             }
 
             String prefix = null;
--- a/jdk/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/CanonicalizerBase.java	Sat Oct 12 10:22:43 2013 +0800
+++ b/jdk/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/CanonicalizerBase.java	Sat Oct 12 20:46:35 2013 -0700
@@ -34,8 +34,6 @@
 import java.util.Map;
 import java.util.Set;
 
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
@@ -49,6 +47,7 @@
 import org.w3c.dom.Attr;
 import org.w3c.dom.Comment;
 import org.w3c.dom.Element;
+import org.w3c.dom.Document;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.w3c.dom.ProcessingInstruction;
@@ -64,7 +63,6 @@
     public static final String XMLNS = "xmlns";
 
     protected static final AttrCompare COMPARE = new AttrCompare();
-    protected static final Attr nullNode;
 
     private static final byte[] END_PI = {'?','>'};
     private static final byte[] BEGIN_PI = {'<','?'};
@@ -84,21 +82,11 @@
     protected static final int NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT = 0;
     protected static final int NODE_AFTER_DOCUMENT_ELEMENT = 1;
 
-    static {
-        // The null xmlns definition.
-        try {
-            DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
-            nullNode = documentBuilder.newDocument().createAttributeNS(Constants.NamespaceSpecNS, XMLNS);
-            nullNode.setValue("");
-        } catch (Exception e) {
-            throw new RuntimeException("Unable to create nullNode: " + e);
-        }
-    }
-
     private List<NodeFilter> nodeFilter;
 
     private boolean includeComments;
     private Set<Node> xpathNodeSet;
+
     /**
      * The node to be skipped/excluded from the DOM tree
      * in subtree canonicalizations.
@@ -107,6 +95,11 @@
     private OutputStream writer = new ByteArrayOutputStream();
 
     /**
+     * The null xmlns definition.
+     */
+    private Attr nullNode;
+
+    /**
      * Constructor CanonicalizerBase
      *
      * @param includeComments
@@ -641,8 +634,9 @@
         parents.clear();
         Attr nsprefix;
         if (((nsprefix = ns.getMappingWithoutRendered(XMLNS)) != null)
-            && "".equals(nsprefix.getValue())) {
-            ns.addMappingAndRender(XMLNS, "", nullNode);
+                && "".equals(nsprefix.getValue())) {
+            ns.addMappingAndRender(
+                    XMLNS, "", getNullNode(nsprefix.getOwnerDocument()));
         }
     }
 
@@ -879,4 +873,18 @@
         }
     }
 
+    // The null xmlns definition.
+    protected Attr getNullNode(Document ownerDocument) {
+        if (nullNode == null) {
+            try {
+                nullNode = ownerDocument.createAttributeNS(
+                                    Constants.NamespaceSpecNS, XMLNS);
+                nullNode.setValue("");
+            } catch (Exception e) {
+                throw new RuntimeException("Unable to create nullNode: " + e);
+            }
+        }
+        return nullNode;
+    }
+
 }