changeset 6862:6b3c195c73b0 jdk7u51-b03

8025026: Enhance canonicalization Summary: Don't use cached null xmlns definition. Also reviewed by Alexander Fomin <alexander.fomin@oracle.com> Reviewed-by: mullan, hawtin
author xuelei
date Mon, 14 Oct 2013 18:35:40 -0700
parents 232d21f4de76
children 0797c957fdb9
files src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer11.java src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315.java src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315Excl.java src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/CanonicalizerBase.java
diffstat 4 files changed, 28 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer11.java	Thu Oct 03 17:32:01 2013 +0400
+++ b/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer11.java	Mon Oct 14 18:35:40 2013 -0700
@@ -358,7 +358,8 @@
             } else if (!isVisible(xmlns)) {
                 // There is a defn 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/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315.java	Thu Oct 03 17:32:01 2013 +0400
+++ b/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315.java	Mon Oct 14 18:35:40 2013 -0700
@@ -302,7 +302,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/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315Excl.java	Thu Oct 03 17:32:01 2013 +0400
+++ b/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315Excl.java	Mon Oct 14 18:35:40 2013 -0700
@@ -300,7 +300,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()));
             }
 
                         if (E.getNamespaceURI() != null) {
--- a/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/CanonicalizerBase.java	Thu Oct 03 17:32:01 2013 +0400
+++ b/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/CanonicalizerBase.java	Mon Oct 14 18:35:40 2013 -0700
@@ -34,7 +34,6 @@
 import java.util.Map;
 import java.util.Set;
 
-import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
@@ -48,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;
@@ -82,22 +82,10 @@
    static final int NODE_BEFORE_DOCUMENT_ELEMENT = -1;
    static final int NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT = 0;
    static final int NODE_AFTER_DOCUMENT_ELEMENT = 1;
-   //The null xmlns definiton.
-   protected static final Attr nullNode;
-   static {
-    try {
-        nullNode=DocumentBuilderFactory.newInstance().
-            newDocumentBuilder().newDocument().createAttributeNS(Constants.NamespaceSpecNS,XMLNS);
-        nullNode.setValue("");
-    } catch (Exception e) {
-        throw new RuntimeException("Unable to create nullNode"/*,*/+e);
-    }
-   }
-
    List nodeFilter;
-
    boolean _includeComments;
    Set _xpathNodeSet = null;
+
    /**
     * The node to be skiped/excluded from the DOM tree
     * in subtree canonicalizations.
@@ -106,6 +94,11 @@
    OutputStream _writer = new UnsyncByteArrayOutputStream();//null;
 
    /**
+    * The null xmlns definition.
+    */
+   private Attr nullNode;
+
+   /**
     * Constructor CanonicalizerBase
     *
     * @param includeComments
@@ -609,7 +602,8 @@
         Attr nsprefix;
         if (((nsprefix=ns.getMappingWithoutRendered("xmlns"))!=null)
                 && "".equals(nsprefix.getValue())) {
-             ns.addMappingAndRender("xmlns","",nullNode);
+             ns.addMappingAndRender("xmlns", "",
+                        getNullNode(nsprefix.getOwnerDocument()));
         }
         }
    /**
@@ -837,4 +831,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;
+    }
+
 }