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 17:02:12 UTC

svn commit: r1141125 - in /santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security: c14n/implementations/Canonicalizer20010315Excl.java c14n/implementations/CanonicalizerBase.java transforms/params/InclusiveNamespaces.java

Author: coheigea
Date: Wed Jun 29 15:02:12 2011
New Revision: 1141125

URL: http://svn.apache.org/viewvc?rev=1141125&view=rev
Log:
Some refactoring of the 20010315Excl c14n algorithm

Modified:
    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
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/params/InclusiveNamespaces.java

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=1141125&r1=1141124&r2=1141125&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 15:02:12 2011
@@ -63,7 +63,7 @@ public abstract class Canonicalizer20010
       * This Set contains the names (Strings like "xmlns" or "xmlns:foo") of
       * the inclusive namespaces.
       */
-    private SortedSet<String> inclusiveNSSet = new TreeSet<String>();
+    private SortedSet<String> inclusiveNSSet;
 
     /**
      * Constructor Canonicalizer20010315Excl
@@ -130,89 +130,80 @@ public abstract class Canonicalizer20010
     }
  
     /**
-     * Method handleAttributesSubtree
+     * Method engineCanonicalizeXPathNodeSet
      * @inheritDoc
-     * @param E
+     * @param xpathNodeSet
+     * @param inclusiveNamespaces
      * @throws CanonicalizationException
      */
-    protected Iterator<Attr> handleAttributesSubtree(Element E, NameSpaceSymbTable ns)
+    public byte[] engineCanonicalizeXPathNodeSet(
+        Set<Node> xpathNodeSet, String inclusiveNamespaces
+    ) throws CanonicalizationException {
+        inclusiveNSSet = InclusiveNamespaces.prefixStr2Set(inclusiveNamespaces);
+        return super.engineCanonicalizeXPathNodeSet(xpathNodeSet);
+    }
+    
+    protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
         throws CanonicalizationException {
         // result will contain the attrs which have to be output
         SortedSet<Attr> result = new TreeSet<Attr>(COMPARE);
-        NamedNodeMap attrs = null;
-
-        int attrsLength = 0;
-        if (E.hasAttributes()) {
-            attrs = E.getAttributes();
-            attrsLength = attrs.getLength();
-        }
-        // The prefix visibly utilized(in the attribute or in the name) in 
+    
+        // The prefix visibly utilized (in the attribute or in the name) in 
         // the element
-        SortedSet<String> visiblyUtilized = new TreeSet<String>(inclusiveNSSet);
-
-        for (int i = 0; i < attrsLength; i++) {
-            Attr N = (Attr) attrs.item(i);
-
-            if (!XMLNS_URI.equals(N.getNamespaceURI())) {
-                // Not a namespace definition.
-                // The Element is output element, add the prefix (if used) to 
-                // visibyUtilized
-                String prefix = N.getPrefix();
-                if (prefix != null && (!prefix.equals(XML) && !prefix.equals(XMLNS))) {
-                    visiblyUtilized.add(prefix);
-                }					
-                // Add to the result.
-                result.add(N);				
-                continue;
-            }
-            String NName = N.getLocalName();
-            String NNodeValue = N.getNodeValue();
-            if (XML.equals(NName) && XML_LANG_URI.equals(NNodeValue)) {
-                // The default mapping for xml must not be output.
-                continue;
-            }
-
-            if (ns.addMapping(NName, NNodeValue, N) 
-                && C14nHelper.namespaceIsRelative(NNodeValue)) {
-                // New definition check if it is relative.
-                Object exArgs[] = {E.getTagName(), NName, N.getNodeValue()};
-                throw new CanonicalizationException(
-                    "c14n.Canonicalizer.RelativeNamespace", exArgs);
-            }
-        }		
-        String prefix;
-        if (E.getNamespaceURI() != null) {
-            prefix = E.getPrefix();
-            if (prefix == null || prefix.length() == 0) {
-                prefix = XMLNS;
+        SortedSet<String> visiblyUtilized = null;
+        if (inclusiveNSSet == null) {
+            visiblyUtilized = new TreeSet<String>();
+        } else {
+            visiblyUtilized = new TreeSet<String>(inclusiveNSSet);
+        }
+    
+        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 NName = attribute.getLocalName();
+                String NNodeValue = attribute.getNodeValue();
+                
+                if (!XMLNS_URI.equals(attribute.getNamespaceURI())) {
+                    // Not a namespace definition.
+                    // The Element is output element, add the prefix (if used) to 
+                    // visiblyUtilized
+                    String prefix = attribute.getPrefix();
+                    if (prefix != null && !(prefix.equals(XML) || prefix.equals(XMLNS))) {
+                        visiblyUtilized.add(prefix);
+                    }                                   
+                    // Add to the result.
+                    result.add(attribute);
+                } else if (!(XML.equals(NName) && XML_LANG_URI.equals(NNodeValue))
+                    && ns.addMapping(NName, NNodeValue, attribute) 
+                    && C14nHelper.namespaceIsRelative(NNodeValue)) {
+                    // The default mapping for xml must not be output.
+                    // New definition check if it is relative.
+                    Object exArgs[] = {element.getTagName(), NName, attribute.getNodeValue()};
+                    throw new CanonicalizationException(
+                        "c14n.Canonicalizer.RelativeNamespace", exArgs
+                    );
+                }
             }
+        }
+        String prefix = null;
+        if (element.getNamespaceURI() != null
+            && !(element.getPrefix() == null || element.getPrefix().length() == 0)) {
+            prefix = element.getPrefix();
         } else {
             prefix = XMLNS;
         }
         visiblyUtilized.add(prefix);
-
+    
         for (String s : visiblyUtilized) {
             Attr key = ns.getMapping(s);
             if (key != null) {
                 result.add(key);
             }
         }
-
-        return result.iterator(); 		
-    }
-
-    /**
-     * Method engineCanonicalizeXPathNodeSet
-     * @inheritDoc
-     * @param xpathNodeSet
-     * @param inclusiveNamespaces
-     * @throws CanonicalizationException
-     */
-    public byte[] engineCanonicalizeXPathNodeSet(
-        Set<Node> xpathNodeSet, String inclusiveNamespaces
-    ) throws CanonicalizationException {
-        inclusiveNSSet = InclusiveNamespaces.prefixStr2Set(inclusiveNamespaces);
-        return super.engineCanonicalizeXPathNodeSet(xpathNodeSet);
+    
+        return result.iterator();               
     }
         
     /**
@@ -220,97 +211,92 @@ public abstract class Canonicalizer20010
      * @param E
      * @throws CanonicalizationException
      */
-    protected final Iterator<Attr> handleAttributes(Element E, NameSpaceSymbTable ns)
+    protected final Iterator<Attr> handleAttributes(Element element, NameSpaceSymbTable ns)
         throws CanonicalizationException {
         // result will contain the attrs which have to be output
         SortedSet<Attr> result = new TreeSet<Attr>(COMPARE);
-        NamedNodeMap attrs = null;
-        int attrsLength = 0;
-        if (E.hasAttributes()) {
-            attrs = E.getAttributes();           
-            attrsLength = attrs.getLength();
-        }
+
         // The prefix visibly utilized (in the attribute or in the name) in 
         // the element
         Set<String> visiblyUtilized = null;
         // It's the output selected.
-        boolean isOutputElement = isVisibleDO(E, ns.getLevel()) == 1;
+        boolean isOutputElement = isVisibleDO(element, ns.getLevel()) == 1;
         if (isOutputElement) {
-            visiblyUtilized = new TreeSet<String>(inclusiveNSSet);
+            if (inclusiveNSSet == null) {
+                visiblyUtilized = new TreeSet<String>();
+            } else {
+                visiblyUtilized = new TreeSet<String>(inclusiveNSSet);
+            }
         }
 
-        for (int i = 0; i < attrsLength; i++) {
-            Attr N = (Attr) attrs.item(i);
-
-            if (!XMLNS_URI.equals(N.getNamespaceURI())) {
-                if (!isVisible(N)) {
-                    // The node is not in the nodeset(if there is a nodeset)
-                    continue;
-                }
-                // Not a namespace definition.
-                if (isOutputElement) {
-                    // The Element is output element, add the prefix (if used) 
-                    // to visibyUtilized
-                    String prefix = N.getPrefix();
-                    if (prefix != null && (!prefix.equals(XML) && !prefix.equals(XMLNS))) {
-                        visiblyUtilized.add(prefix);
-                    }					
-                    // Add to the result.
-                    result.add(N);
-                }
-                continue;
-            }
-            String NName = N.getLocalName();
-            if (isOutputElement && !isVisible(N) && !XMLNS.equals(NName)) {
-                ns.removeMappingIfNotRender(NName);
-                continue;
-            }
-            String NNodeValue = N.getNodeValue();
+        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 NName = attribute.getLocalName();
+                String NNodeValue = attribute.getNodeValue();
+    
+                if (!XMLNS_URI.equals(attribute.getNamespaceURI())) {
+                    if (isVisible(attribute) && isOutputElement) {
+                        // The Element is output element, add the prefix (if used) 
+                        // to visibyUtilized
+                        String prefix = attribute.getPrefix();
+                        if (prefix != null && !(prefix.equals(XML) || prefix.equals(XMLNS))) {
+                            visiblyUtilized.add(prefix);
+                        }					
+                        // Add to the result.
+                        result.add(attribute);
+                    }
+                } else if (isOutputElement && !isVisible(attribute) && !XMLNS.equals(NName)) {
+                    ns.removeMappingIfNotRender(NName);
+                } else {
+                    if (!isOutputElement && isVisible(attribute) 
+                        && inclusiveNSSet.contains(NName) 
+                        && !ns.removeMappingIfRender(NName)) {
+                        Node n = ns.addMappingAndRender(NName, NNodeValue, 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
+                                );
+                            }
+                        }
+                    }
 
-            if (!isOutputElement && isVisible(N) 
-                && inclusiveNSSet.contains(NName) 
-                && !ns.removeMappingIfRender(NName)) {
-                Node n = ns.addMappingAndRender(NName, NNodeValue, N);
-                if (n != null) {
-                    result.add((Attr)n);
-                    if (C14nHelper.namespaceIsRelative(N)) {
-                        Object exArgs[] = 
-                            { E.getTagName(), NName, N.getNodeValue() };
+                    if (ns.addMapping(NName, NNodeValue, attribute)
+                        && C14nHelper.namespaceIsRelative(NNodeValue)) {
+                        // New definition check if it is relative
+                        Object exArgs[] = { element.getTagName(), NName, attribute.getNodeValue() };
                         throw new CanonicalizationException(
-                            "c14n.Canonicalizer.RelativeNamespace", exArgs);
+                            "c14n.Canonicalizer.RelativeNamespace", exArgs
+                        );
                     }
                 }
             }
-
-            if (ns.addMapping(NName, NNodeValue, N)
-                && C14nHelper.namespaceIsRelative(NNodeValue)) {
-                // New definition check if it is relative
-                Object exArgs[] = 
-                    { E.getTagName(), NName, N.getNodeValue() };
-                throw new CanonicalizationException(
-                    "c14n.Canonicalizer.RelativeNamespace", exArgs);
-            }
         }
 
         if (isOutputElement) {	               
             // The element is visible, handle the xmlns definition    
-            Attr xmlns = E.getAttributeNodeNS(XMLNS_URI, XMLNS);
+            Attr xmlns = element.getAttributeNodeNS(XMLNS_URI, XMLNS);
             if (xmlns != null && !isVisible(xmlns)) {
                 // There is a definition but the xmlns is not selected by the 
                 // xpath. then xmlns=""
                 ns.addMapping(XMLNS, "", nullNode);
             }
 
-            if (E.getNamespaceURI() != null) {
-                String prefix = E.getPrefix();
-                if (prefix == null || prefix.length() == 0) {
-                    visiblyUtilized.add(XMLNS);
-                } else {
-                    visiblyUtilized.add(prefix);
-                }
+            String prefix = null;
+            if (element.getNamespaceURI() != null
+                && !(element.getPrefix() == null || element.getPrefix().length() == 0)) {
+                prefix = element.getPrefix();
             } else {
-                visiblyUtilized.add(XMLNS);
-            }									
+                prefix = XMLNS;
+            }
+            visiblyUtilized.add(prefix);
+        
             for (String s : visiblyUtilized) {
                 Attr key = ns.getMapping(s);
                 if (key != null) {

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=1141125&r1=1141124&r2=1141125&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 15:02:12 2011
@@ -30,6 +30,7 @@ import java.util.ListIterator;
 import java.util.Map;
 import java.util.Set;
 
+import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
@@ -81,8 +82,8 @@ public abstract class CanonicalizerBase 
     static {
         // The null xmlns definiton.
         try {
-            nullNode = DocumentBuilderFactory.newInstance().
-            newDocumentBuilder().newDocument().createAttributeNS(Constants.NamespaceSpecNS, XMLNS);
+            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);
@@ -315,7 +316,7 @@ public abstract class CanonicalizerBase 
                 }
                 break;
             }
-            while (sibling == null  && parentNode != null) {    		      		      			
+            while (sibling == null && parentNode != null) {    		      		      			
                 writer.write(END_TAG);
                 UtfHelpper.writeByte(((Element)parentNode).getTagName(), writer, cache);        
                 writer.write('>');
@@ -409,7 +410,6 @@ public abstract class CanonicalizerBase 
             case Node.DOCUMENT_FRAGMENT_NODE :
             case Node.DOCUMENT_NODE :
                 ns.outputNodePush();
-                //currentNode = currentNode.getFirstChild();  
                 sibling = currentNode.getFirstChild();
                 break;
 
@@ -609,12 +609,12 @@ public abstract class CanonicalizerBase 
      * @param ns
      */
     protected final void getParentNameSpaces(Element el, NameSpaceSymbTable ns)  {
-        List<Element> parents = new ArrayList<Element>(10);
         Node n1 = el.getParentNode();
         if (n1 == null || Node.ELEMENT_NODE != n1.getNodeType()) {
             return;
         }
-        //Obtain all the parents of the elemnt
+        //Obtain all the parents of the element
+        List<Element> parents = new ArrayList<Element>(10);
         Element parent = (Element) n1;
         while (parent != null) {
             parents.add(parent);

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/params/InclusiveNamespaces.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/params/InclusiveNamespaces.java?rev=1141125&r1=1141124&r2=1141125&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/params/InclusiveNamespaces.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/transforms/params/InclusiveNamespaces.java Wed Jun 29 15:02:12 2011
@@ -20,7 +20,6 @@ package org.apache.xml.security.transfor
 
 import java.util.Set;
 import java.util.SortedSet;
-import java.util.StringTokenizer;
 import java.util.TreeSet;
 
 import org.apache.xml.security.exceptions.XMLSecurityException;
@@ -70,7 +69,6 @@ public class InclusiveNamespaces extends
     public InclusiveNamespaces(Document doc, Set<String> prefixes) {
         super(doc);
 
-        StringBuilder sb = new StringBuilder();
         SortedSet<String> prefixList = null;
         if (prefixes instanceof SortedSet<?>) {
             prefixList = (SortedSet<String>)prefixes;
@@ -78,6 +76,7 @@ public class InclusiveNamespaces extends
             prefixList = new TreeSet<String>(prefixes);
         }
 
+        StringBuilder sb = new StringBuilder();
         for (String prefix : prefixList) {
             if (prefix.equals("xmlns")) {
                 sb.append("#default ");
@@ -135,11 +134,8 @@ public class InclusiveNamespaces extends
             return prefixes;
         }
 
-        StringTokenizer st = new StringTokenizer(inclusiveNamespaces, " \t\r\n");
-
-        while (st.hasMoreTokens()) {
-            String prefix = st.nextToken();
-
+        String[] tokens = inclusiveNamespaces.split("\\s");
+        for (String prefix : tokens) {
             if (prefix.equals("#default")) {
                 prefixes.add("xmlns");
             } else {