FileDocCategorySizeDatePackage
Canonicalizer20010315Excl.javaAPI DocJava SE 6 API10424Tue Jun 10 00:23:00 BST 2008com.sun.org.apache.xml.internal.security.c14n.implementations

Canonicalizer20010315Excl

public abstract class Canonicalizer20010315Excl extends CanonicalizerBase
Implements " Exclusive XML Canonicalization, Version 1.0 "
Credits: During restructuring of the Canonicalizer framework, Ren?? Kollmorgen from Software AG submitted an implementation of ExclC14n which fitted into the old architecture and which based heavily on my old (and slow) implementation of "Canonical XML". A big "thank you" to Ren?? for this.
THIS implementation is a complete rewrite of the algorithm.
author
Christian Geuer-Pollmann
version
$Revision: 1.21 $
see
XML Canonicalization, Version 1.0

Fields Summary
TreeSet
_inclusiveNSSet
This Set contains the names (Strings like "xmlns" or "xmlns:foo") of the inclusive namespaces.
static final String
XMLNS_URI
final SortedSet
result
Constructors Summary
public Canonicalizer20010315Excl(boolean includeComments)
Constructor Canonicalizer20010315Excl

param
includeComments

	     	 
	   
		super(includeComments);
	
Methods Summary
public byte[]engineCanonicalize(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput rootNode, java.lang.String inclusiveNamespaces)

param
rootNode
param
inclusiveNamespaces
return
the rootNode c14n.
throws
CanonicalizationException

			this._inclusiveNSSet = (TreeSet)InclusiveNamespaces
					.prefixStr2Set(inclusiveNamespaces);			
			return super.engineCanonicalize(rootNode);
	
public byte[]engineCanonicalizeSubTree(org.w3c.dom.Node rootNode)
Method engineCanonicalizeSubTree

inheritDoc
param
rootNode
throws
CanonicalizationException

		return this.engineCanonicalizeSubTree(rootNode, "",null);
	
public byte[]engineCanonicalizeSubTree(org.w3c.dom.Node rootNode, java.lang.String inclusiveNamespaces)
Method engineCanonicalizeSubTree

inheritDoc
param
rootNode
param
inclusiveNamespaces
throws
CanonicalizationException

		return this.engineCanonicalizeSubTree(rootNode, inclusiveNamespaces,null);
	
public byte[]engineCanonicalizeSubTree(org.w3c.dom.Node rootNode, java.lang.String inclusiveNamespaces, org.w3c.dom.Node excl)
Method engineCanonicalizeSubTree

param
rootNode
param
inclusiveNamespaces
param
excl A element to exclude from the c14n process.
return
the rootNode c14n.
throws
CanonicalizationException

			this._inclusiveNSSet = (TreeSet)InclusiveNamespaces
					.prefixStr2Set(inclusiveNamespaces);			
			return super.engineCanonicalizeSubTree(rootNode,excl);
	
public byte[]engineCanonicalizeXPathNodeSet(java.util.Set xpathNodeSet, java.lang.String inclusiveNamespaces)
Method engineCanonicalizeXPathNodeSet

inheritDoc
param
xpathNodeSet
param
inclusiveNamespaces
throws
CanonicalizationException

		
		
			this._inclusiveNSSet = (TreeSet)InclusiveNamespaces
					.prefixStr2Set(inclusiveNamespaces);
			return super.engineCanonicalizeXPathNodeSet(xpathNodeSet);
		
	
public byte[]engineCanonicalizeXPathNodeSet(java.util.Set xpathNodeSet)

inheritDoc

        return engineCanonicalizeXPathNodeSet(xpathNodeSet,"");
    
final java.util.IteratorhandleAttributes(org.w3c.dom.Element E, com.sun.org.apache.xml.internal.security.c14n.implementations.NameSpaceSymbTable ns)

inheritDoc
param
E
throws
CanonicalizationException

		// result will contain the attrs which have to be outputted
		SortedSet result = this.result;       
	    result.clear();
		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 visiblyUtilized =null;
		//It's the output selected.
		boolean isOutputElement = isVisible(E);			
		if (isOutputElement) {
			visiblyUtilized =  (Set) this._inclusiveNSSet.clone();
		}
		
		for (int i = 0; i < attrsLength; i++) {
			Attr N = (Attr) attrs.item(i);
			String NName=N.getLocalName();
			String NNodeValue=N.getNodeValue();
			if ( !isVisible(N) )  {
				//The node is not in the nodeset(if there is a nodeset)
				continue;
			}			
						
			if (!XMLNS_URI.equals(N.getNamespaceURI())) {
				//Not a namespace definition.
				if (isOutputElement) {
					//The Element is output element, add his 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;
			}
						
			
			if (ns.addMapping(NName, NNodeValue,N)) {
                //New definiton check if it is relative
                if (C14nHelper.namespaceIsRelative(NNodeValue)) {
                    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);
           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);
				}
			} else {
				visiblyUtilized.add(XMLNS);
			}									
			//This can be optimezed by I don't have time
			//visiblyUtilized.addAll(this._inclusiveNSSet);
			Iterator it=visiblyUtilized.iterator();
			while (it.hasNext()) {
				String s=(String)it.next();										
				Attr key=ns.getMapping(s);
				if (key==null) {
					continue;
				}
				result.add(key);
			}
		} else /*if (_circunvented)*/ {			
			Iterator it=this._inclusiveNSSet.iterator();
			while (it.hasNext()) {
				String s=(String)it.next();				
				Attr key=ns.getMappingWithoutRendered(s);
				if (key==null) {
					continue;
				}
				result.add(key);								
			}
		}

		return result.iterator(); 
	
java.util.IteratorhandleAttributesSubtree(org.w3c.dom.Element E, com.sun.org.apache.xml.internal.security.c14n.implementations.NameSpaceSymbTable ns)
Method handleAttributesSubtree

inheritDoc
param
E
throws
CanonicalizationException

		// System.out.println("During the traversal, I encountered " +
		// XMLUtils.getXPath(E));
		// result will contain the attrs which have to be outputted
		SortedSet result = this.result;       
	    result.clear();
		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
		SortedSet visiblyUtilized =(SortedSet) _inclusiveNSSet.clone();
					
		for (int i = 0; i < attrsLength; i++) {
			Attr N = (Attr) attrs.item(i);
			String NName=N.getLocalName();
			String NNodeValue=N.getNodeValue();
						
			if (!XMLNS_URI.equals(N.getNamespaceURI())) {
				//Not a namespace definition.
				//The Element is output element, add his 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;
			}
	
			if (ns.addMapping(NName, NNodeValue,N)) {
				//New definition check if it is relative.
                if (C14nHelper.namespaceIsRelative(NNodeValue)) {
                    Object exArgs[] = {E.getTagName(), NName,
                            N.getNodeValue()};
                    throw new CanonicalizationException(
                            "c14n.Canonicalizer.RelativeNamespace", exArgs);
                }
            }
		}		
							
		if (E.getNamespaceURI() != null) {
			String prefix = E.getPrefix();
			if ((prefix == null) || (prefix.length() == 0)) {
				visiblyUtilized.add(XMLNS);
			} else {
				visiblyUtilized.add(prefix);
			}
		} else {
			visiblyUtilized.add(XMLNS);
		}
									
		//This can be optimezed by I don't have time
		Iterator it=visiblyUtilized.iterator();
		while (it.hasNext()) {
			String s=(String)it.next();									
			Attr key=ns.getMapping(s);
			if (key==null) {
				continue;
			}
			result.add(key);
		}
		
		return result.iterator();