# HG changeset patch # User xuelei # Date 1381800940 25200 # Node ID c31a79eedba256f61771de7e0e209fb0be3d9af5 # Parent 8903edf477cf86e42251eb5de90177d98aec9655 8025026: Enhance canonicalization Summary: Don't use cached null xmlns definition. Also reviewed by Alexander Fomin Reviewed-by: mullan, hawtin diff -r 8903edf477cf -r c31a79eedba2 src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer11.java --- a/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer11.java Fri Sep 13 15:17:31 2013 +0800 +++ 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) { diff -r 8903edf477cf -r c31a79eedba2 src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315.java --- a/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315.java Fri Sep 13 15:17:31 2013 +0800 +++ 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) { diff -r 8903edf477cf -r c31a79eedba2 src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315Excl.java --- a/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315Excl.java Fri Sep 13 15:17:31 2013 +0800 +++ 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) { diff -r 8903edf477cf -r c31a79eedba2 src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/CanonicalizerBase.java --- a/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/CanonicalizerBase.java Fri Sep 13 15:17:31 2013 +0800 +++ 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; @@ -86,22 +86,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. @@ -110,6 +98,11 @@ OutputStream _writer = new UnsyncByteArrayOutputStream();//null; /** + * The null xmlns definition. + */ + private Attr nullNode; + + /** * Constructor CanonicalizerBase * * @param includeComments @@ -613,7 +606,8 @@ Attr nsprefix; if (((nsprefix=ns.getMappingWithoutRendered("xmlns"))!=null) && "".equals(nsprefix.getValue())) { - ns.addMappingAndRender("xmlns","",nullNode); + ns.addMappingAndRender("xmlns", "", + getNullNode(nsprefix.getOwnerDocument())); } } /** @@ -841,4 +835,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; + } + }