You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by co...@apache.org on 2011/06/29 18:30:15 UTC

svn commit: r1141152 - in /santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations: Canonicalizer20010315.java Canonicalizer20010315Excl.java CanonicalizerBase.java

Author: coheigea
Date: Wed Jun 29 16:30:14 2011
New Revision: 1141152

URL: http://svn.apache.org/viewvc?rev=1141152&view=rev
Log:
Some more c14n refactoring.

Modified:
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java?rev=1141152&r1=1141151&r2=1141152&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315.java Wed Jun 29 16:30:14 2011
@@ -54,7 +54,6 @@ public abstract class Canonicalizer20010
     private static final String XML_LANG_URI = Constants.XML_LANG_SPACE_SpecNS;
     
     private boolean firstCall = true;
-    private final SortedSet<Attr> result = new TreeSet<Attr>(COMPARE);
     
     private static class XmlAttrStack {
         static class XmlsStackElement {
@@ -150,9 +149,39 @@ public abstract class Canonicalizer20010
     public Canonicalizer20010315(boolean includeComments) {
         super(includeComments);
     }
+    
+    /**
+     * Always throws a CanonicalizationException because this is inclusive c14n.
+     *
+     * @param xpathNodeSet
+     * @param inclusiveNamespaces
+     * @return none it always fails
+     * @throws CanonicalizationException always
+     */
+    public byte[] engineCanonicalizeXPathNodeSet(Set<Node> xpathNodeSet, String inclusiveNamespaces)
+        throws CanonicalizationException {
+
+        /** $todo$ well, should we throw UnsupportedOperationException ? */
+        throw new CanonicalizationException("c14n.Canonicalizer.UnsupportedOperation");
+    }
 
     /**
-     * Returns the Attr[]s to be outputted for the given element.
+     * Always throws a CanonicalizationException because this is inclusive c14n.
+     *
+     * @param rootNode
+     * @param inclusiveNamespaces
+     * @return none it always fails
+     * @throws CanonicalizationException
+     */
+    public byte[] engineCanonicalizeSubTree(Node rootNode, String inclusiveNamespaces)
+        throws CanonicalizationException {
+
+        /** $todo$ well, should we throw UnsupportedOperationException ? */
+        throw new CanonicalizationException("c14n.Canonicalizer.UnsupportedOperation");
+    }
+
+    /**
+     * Returns the Attr[]s to be output for the given element.
      * <br>
      * The code of this method is a copy of {@link #handleAttributes(Element,
      * NameSpaceSymbTable)},
@@ -160,51 +189,49 @@ public abstract class Canonicalizer20010
      * So if the element in question isRoot of c14n, it's parent is not in the
      * node set, as well as all other ancestors.
      *
-     * @param E
+     * @param element
      * @param ns
      * @return the Attr[]s to be output
      * @throws CanonicalizationException
      */
-    protected Iterator<Attr> handleAttributesSubtree(Element E,  NameSpaceSymbTable ns )
+    @Override
+    protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
         throws CanonicalizationException {
-        if (!E.hasAttributes() && !firstCall) {
+        if (!element.hasAttributes() && !firstCall) {
             return null; 
         }
-        // result will contain the attrs which have to be outputted   	  
-        final SortedSet<Attr> result = this.result;       
-        result.clear();
-        NamedNodeMap attrs = E.getAttributes();
-        int attrsLength = attrs.getLength();      
+        // result will contain the attrs which have to be output	  
+        SortedSet<Attr> result = new TreeSet<Attr>(COMPARE);
 
-        for (int i = 0; i < attrsLength; i++) {
-            Attr N = (Attr) attrs.item(i);
-            String NUri = N.getNamespaceURI();
-
-            if (!XMLNS_URI.equals(NUri)) {
-                //It's not a namespace attr node. Add to the result and continue.
-                result.add(N);
-                continue;
-            }
-
-            String NName = N.getLocalName();
-            String NValue = N.getValue();        
-            if (XML.equals(NName) && XML_LANG_URI.equals(NValue)) {
-                //The default mapping for xml must not be output.
-                continue;
-            }
-
-            Node n = ns.addMappingAndRender(NName, NValue, N);          		 
-
-            if (n != null) {
-                //Render the ns definition
-                result.add((Attr)n);
-                if (C14nHelper.namespaceIsRelative(N)) {
-                    Object exArgs[] = { E.getTagName(), NName, N.getNodeValue() };
-                    throw new CanonicalizationException(
-                        "c14n.Canonicalizer.RelativeNamespace", exArgs
-                    );
+        if (element.hasAttributes()) {
+            NamedNodeMap attrs = element.getAttributes();
+            int attrsLength = attrs.getLength();      
+    
+            for (int i = 0; i < attrsLength; i++) {
+                Attr attribute = (Attr) attrs.item(i);
+                String NUri = attribute.getNamespaceURI();
+                String NName = attribute.getLocalName();
+                String NValue = attribute.getValue();        
+    
+                if (!XMLNS_URI.equals(NUri)) {
+                    //It's not a namespace attr node. Add to the result and continue.
+                    result.add(attribute);
+                } else if (!(XML.equals(NName) && XML_LANG_URI.equals(NValue))) {
+                    //The default mapping for xml must not be output.
+                    Node n = ns.addMappingAndRender(NName, NValue, attribute);          		 
+        
+                    if (n != null) {
+                        //Render the ns definition
+                        result.add((Attr)n);
+                        if (C14nHelper.namespaceIsRelative(attribute)) {
+                            Object exArgs[] = { element.getTagName(), NName, attribute.getNodeValue() };
+                            throw new CanonicalizationException(
+                                "c14n.Canonicalizer.RelativeNamespace", exArgs
+                            );
+                        }
+                    }
                 }
-            }        
+            }
         }
 
         if (firstCall) {
@@ -220,85 +247,75 @@ public abstract class Canonicalizer20010
     }
 
     /**
-     * Returns the Attr[]s to be outputted for the given element.
+     * Returns the Attr[]s to be output for the given element.
      * <br>
      * IMPORTANT: This method expects to work on a modified DOM tree, i.e. a DOM which has
      * been prepared using {@link org.apache.xml.security.utils.XMLUtils#circumventBug2650(
      * org.w3c.dom.Document)}.
      * 
-     * @param E
+     * @param element
      * @param ns
-     * @return the Attr[]s to be outputted
+     * @return the Attr[]s to be output
      * @throws CanonicalizationException
      */
-    protected Iterator<Attr> handleAttributes(Element E,  NameSpaceSymbTable ns) 
+    @Override
+    protected Iterator<Attr> handleAttributes(Element element, NameSpaceSymbTable ns) 
         throws CanonicalizationException {    
-        // result will contain the attrs which have to be outputted
+        // result will contain the attrs which have to be output
         xmlattrStack.push(ns.getLevel());
-        boolean isRealVisible = isVisibleDO(E, ns.getLevel()) == 1;    
-        NamedNodeMap attrs = null;
-        int attrsLength = 0;
-        if (E.hasAttributes()) {
-            attrs = E.getAttributes();
-            attrsLength = attrs.getLength();
-        }
-
-        SortedSet<Attr> result = this.result;       
-        result.clear();
-
-        for (int i = 0; i < attrsLength; i++) {
-            Attr N = (Attr) attrs.item(i);
-            String NUri = N.getNamespaceURI();
-
-            if (!XMLNS_URI.equals(NUri)) {
-                //A non namespace definition node.
-                if (XML_LANG_URI.equals(NUri)) {
-                    xmlattrStack.addXmlnsAttr(N);
-                } else if (isRealVisible){
-                    //The node is visible add the attribute to the list of output attributes.
-                    result.add(N);
-                } 
-                //keep working
-                continue;
-            }
-
-            String NName = N.getLocalName();
-            String NValue = N.getValue();              
-            if ("xml".equals(NName) && XML_LANG_URI.equals(NValue)) {
-                /* except omit namespace node with local name xml, which defines
-                 * the xml prefix, if its string value is http://www.w3.org/XML/1998/namespace.
-                 */
-                continue;
-            }
-            //add the prefix binding to the ns symb table.
-            //ns.addInclusiveMapping(NName,NValue,N,isRealVisible);          
-            if (isVisible(N))  {
-                if (!isRealVisible && ns.removeMappingIfRender(NName)) {
-                    continue;
-                } 
-                //The xpath select this node output it if needed.
-                //Node n=ns.addMappingAndRenderXNodeSet(NName,NValue,N,isRealVisible);
-                Node n = ns.addMappingAndRender(NName, NValue, N);
-                if (n != null) {
-                    result.add((Attr)n);
-                    if (C14nHelper.namespaceIsRelative(N)) {
-                        Object exArgs[] = { E.getTagName(), NName, N.getNodeValue() };
-                        throw new CanonicalizationException(
-                            "c14n.Canonicalizer.RelativeNamespace", exArgs
-                        );
+        boolean isRealVisible = isVisibleDO(element, ns.getLevel()) == 1;
+        SortedSet<Attr> result = new TreeSet<Attr>(COMPARE);
+        
+        if (element.hasAttributes()) {
+            NamedNodeMap attrs = element.getAttributes();
+            int attrsLength = attrs.getLength();
+
+            for (int i = 0; i < attrsLength; i++) {
+                Attr attribute = (Attr) attrs.item(i);
+                String NUri = attribute.getNamespaceURI();
+                String NName = attribute.getLocalName();
+                String NValue = attribute.getValue();
+    
+                if (!XMLNS_URI.equals(NUri)) {
+                    //A non namespace definition node.
+                    if (XML_LANG_URI.equals(NUri)) {
+                        xmlattrStack.addXmlnsAttr(attribute);
+                    } else if (isRealVisible){
+                        //The node is visible add the attribute to the list of output attributes.
+                        result.add(attribute);
+                    } 
+                } else if (!"xml".equals(NName) || !XML_LANG_URI.equals(NValue)) {
+                    /* except omit namespace node with local name xml, which defines
+                     * the xml prefix, if its string value is http://www.w3.org/XML/1998/namespace.
+                     */
+                    //add the prefix binding to the ns symb table.
+                    if (isVisible(attribute))  {
+                        if (isRealVisible || !ns.removeMappingIfRender(NName)) {
+                            //The xpath select this node output it if needed.
+                            Node n = ns.addMappingAndRender(NName, NValue, attribute);
+                            if (n != null) {
+                                result.add((Attr)n);
+                                if (C14nHelper.namespaceIsRelative(attribute)) {
+                                    Object exArgs[] = { element.getTagName(), NName, attribute.getNodeValue() };
+                                    throw new CanonicalizationException(
+                                        "c14n.Canonicalizer.RelativeNamespace", exArgs
+                                    );
+                                }
+                            }
+                        }
+                    } else {
+                        if (isRealVisible && !XMLNS.equals(NName)) {
+                            ns.removeMapping(NName);	
+                        } else {
+                            ns.addMapping(NName, NValue, attribute);
+                        }
                     }
                 }
-            } else {
-                if (isRealVisible && !XMLNS.equals(NName)) {
-                    ns.removeMapping(NName);	
-                } else {
-                    ns.addMapping(NName,NValue,N);
-                }
             }
         }
         if (isRealVisible) {    	           
             //The element is visible, handle the xmlns definition        
-            Attr xmlns = E.getAttributeNodeNS(XMLNS_URI, XMLNS);
+            Attr xmlns = element.getAttributeNodeNS(XMLNS_URI, XMLNS);
             Node n = null;
             if (xmlns == null) {
                 //No xmlns def just get the already defined.
@@ -313,7 +330,6 @@ public abstract class Canonicalizer20010
                 result.add((Attr)n);
             }
             //Float all xml:* attributes of the unselected parent elements to this one. 
-            //addXmlAttributes(E,result);
             xmlattrStack.getXmlnsAttr(result);
             ns.getUnrenderedNodes(result);
         }
@@ -321,36 +337,6 @@ public abstract class Canonicalizer20010
         return result.iterator();
     }
     
-    /**
-     * Always throws a CanonicalizationException because this is inclusive c14n.
-     *
-     * @param xpathNodeSet
-     * @param inclusiveNamespaces
-     * @return none it always fails
-     * @throws CanonicalizationException always
-     */
-    public byte[] engineCanonicalizeXPathNodeSet(Set<Node> xpathNodeSet, String inclusiveNamespaces)
-        throws CanonicalizationException {
-
-        /** $todo$ well, should we throw UnsupportedOperationException ? */
-        throw new CanonicalizationException("c14n.Canonicalizer.UnsupportedOperation");
-    }
-
-    /**
-     * Always throws a CanonicalizationException because this is inclusive c14n.
-     *
-     * @param rootNode
-     * @param inclusiveNamespaces
-     * @return none it always fails
-     * @throws CanonicalizationException
-     */
-    public byte[] engineCanonicalizeSubTree(Node rootNode, String inclusiveNamespaces)
-        throws CanonicalizationException {
-
-        /** $todo$ well, should we throw UnsupportedOperationException ? */
-        throw new CanonicalizationException("c14n.Canonicalizer.UnsupportedOperation");
-    }
-    
     protected void circumventBugIfNeeded(XMLSignatureInput input) 
         throws CanonicalizationException, ParserConfigurationException, IOException, SAXException {
         if (!input.isNeedsToBeExpanded()) {
@@ -365,6 +351,7 @@ public abstract class Canonicalizer20010
         XMLUtils.circumventBug2650(doc);
     }
 
+    @Override
     protected void handleParent(Element e, NameSpaceSymbTable ns) {
         if (!e.hasAttributes() && e.getNamespaceURI() == null) {
             return;
@@ -373,21 +360,17 @@ public abstract class Canonicalizer20010
         NamedNodeMap attrs = e.getAttributes();
         int attrsLength = attrs.getLength();
         for (int i = 0; i < attrsLength; i++) {
-            Attr N = (Attr) attrs.item(i);
-            if (!Constants.NamespaceSpecNS.equals(N.getNamespaceURI())) {
-                //Not a namespace definition, ignore.
-                if (XML_LANG_URI.equals(N.getNamespaceURI())) {
-                    xmlattrStack.addXmlnsAttr(N);
+            Attr attribute = (Attr) attrs.item(i);
+            String NName = attribute.getLocalName();
+            String NValue = attribute.getNodeValue();
+            
+            if (Constants.NamespaceSpecNS.equals(attribute.getNamespaceURI())) {
+                if (!XML.equals(NName) || !Constants.XML_LANG_SPACE_SpecNS.equals(NValue)) {
+                    ns.addMapping(NName, NValue, attribute);
                 }
-                continue;
+            } else if (XML_LANG_URI.equals(attribute.getNamespaceURI())) {
+                xmlattrStack.addXmlnsAttr(attribute);
             }
-
-            String NName = N.getLocalName();
-            String NValue = N.getNodeValue();
-            if (XML.equals(NName) && Constants.XML_LANG_SPACE_SpecNS.equals(NValue)) {
-                continue;
-            }            
-            ns.addMapping(NName, NValue, N);             
         }
         if (e.getNamespaceURI() != null) {
             String NName = e.getPrefix();

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java?rev=1141152&r1=1141151&r2=1141152&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/Canonicalizer20010315Excl.java Wed Jun 29 16:30:14 2011
@@ -143,6 +143,7 @@ public abstract class Canonicalizer20010
         return super.engineCanonicalizeXPathNodeSet(xpathNodeSet);
     }
     
+    @Override
     protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
         throws CanonicalizationException {
         // result will contain the attrs which have to be output
@@ -208,9 +209,10 @@ public abstract class Canonicalizer20010
         
     /**
      * @inheritDoc
-     * @param E
+     * @param element
      * @throws CanonicalizationException
      */
+    @Override
     protected final Iterator<Attr> handleAttributes(Element element, NameSpaceSymbTable ns)
         throws CanonicalizationException {
         // result will contain the attrs which have to be output

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java?rev=1141152&r1=1141151&r2=1141152&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java Wed Jun 29 16:30:14 2011
@@ -574,18 +574,14 @@ public abstract class CanonicalizerBase 
         NamedNodeMap attrs = e.getAttributes();
         int attrsLength = attrs.getLength();
         for (int i = 0; i < attrsLength; i++) {
-            Attr N = (Attr) attrs.item(i);
-            if (!Constants.NamespaceSpecNS.equals(N.getNamespaceURI())) {
-                //Not a namespace definition, ignore.
-                continue;
+            Attr attribute = (Attr) attrs.item(i);
+            String NName = attribute.getLocalName();
+            String NValue = attribute.getNodeValue();
+            
+            if (Constants.NamespaceSpecNS.equals(attribute.getNamespaceURI())
+                && (!XML.equals(NName) || !Constants.XML_LANG_SPACE_SpecNS.equals(NValue))) {
+                ns.addMapping(NName, NValue, attribute);
             }
-
-            String NName = N.getLocalName();
-            String NValue = N.getNodeValue();
-            if (XML.equals(NName) && Constants.XML_LANG_SPACE_SpecNS.equals(NValue)) {
-                continue;
-            }            
-            ns.addMapping(NName, NValue, N);             
         }
         if (e.getNamespaceURI() != null) {
             String NName = e.getPrefix();
@@ -640,23 +636,23 @@ public abstract class CanonicalizerBase 
     /**
      * Obtain the attributes to output for this node in XPathNodeSet c14n. 
      *
-     * @param E
+     * @param element
      * @param ns
      * @return the attributes nodes to output.
      * @throws CanonicalizationException
      */
-    abstract Iterator<Attr> handleAttributes(Element E, NameSpaceSymbTable ns)
+    abstract Iterator<Attr> handleAttributes(Element element, NameSpaceSymbTable ns)
         throws CanonicalizationException;
 
     /**
      * Obtain the attributes to output for this node in a Subtree c14n.
      *
-     * @param E
+     * @param element
      * @param ns
      * @return the attributes nodes to output.
      * @throws CanonicalizationException
      */
-    abstract Iterator<Attr> handleAttributesSubtree(Element E, NameSpaceSymbTable ns)
+    abstract Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
         throws CanonicalizationException;
 
     abstract void circumventBugIfNeeded(XMLSignatureInput input)