# HG changeset patch # User xuelei # Date 1381635995 25200 # Node ID 62a8a26dca0962feaa13cf09caf1c0053a9823c9 # Parent eafa41f4e9fd8816c1ce793086fd98c883a8da88 8025026: Enhance canonicalization Summary: Don't use cached null xmlns definition. Also reviewed by Alexander Fomin Reviewed-by: mullan, hawtin diff -r eafa41f4e9fd -r 62a8a26dca09 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 Sat Oct 12 10:22:43 2013 +0800 +++ b/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) { diff -r eafa41f4e9fd -r 62a8a26dca09 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 Sat Oct 12 10:22:43 2013 +0800 +++ b/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) { diff -r eafa41f4e9fd -r 62a8a26dca09 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 Sat Oct 12 10:22:43 2013 +0800 +++ b/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; diff -r eafa41f4e9fd -r 62a8a26dca09 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 Sat Oct 12 10:22:43 2013 +0800 +++ b/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; private boolean includeComments; private Set 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; + } + }