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

Canonicalizer

public class Canonicalizer extends Object
author
Christian Geuer-Pollmann

Fields Summary
public static final String
ENCODING
The output encoding of canonicalized data
public static final String
XPATH_C14N_WITH_COMMENTS_SINGLE_NODE
XPath Expresion for selecting every node and continuos comments joined in only one node
public static final String
ALGO_ID_C14N_OMIT_COMMENTS
The URL defined in XML-SEC Rec for inclusive c14n without comments.
public static final String
ALGO_ID_C14N_WITH_COMMENTS
The URL defined in XML-SEC Rec for inclusive c14n with comments.
public static final String
ALGO_ID_C14N_EXCL_OMIT_COMMENTS
The URL defined in XML-SEC Rec for exclusive c14n without comments.
public static final String
ALGO_ID_C14N_EXCL_WITH_COMMENTS
The URL defined in XML-SEC Rec for exclusive c14n with comments.
static boolean
_alreadyInitialized
static Map
_canonicalizerHash
protected CanonicalizerSpi
canonicalizerSpi
Constructors Summary
private Canonicalizer(String algorithmURI)
Constructor Canonicalizer

param
algorithmURI
throws
InvalidCanonicalizerException


      try {
         Class implementingClass = getImplementingClass(algorithmURI);

         this.canonicalizerSpi =
            (CanonicalizerSpi) implementingClass.newInstance();
         this.canonicalizerSpi.reset=true;
      } catch (Exception e) {
         Object exArgs[] = { algorithmURI };

         throw new InvalidCanonicalizerException(
            "signature.Canonicalizer.UnknownCanonicalizer", exArgs);
      }
   
Methods Summary
public byte[]canonicalize(byte[] inputBytes)
This method tries to canonicalize the given bytes. It's possible to even canonicalize non-wellformed sequences if they are well-formed after being wrapped with a >a<...>/a<.

param
inputBytes
return
the result of the conicalization.
throws
CanonicalizationException
throws
java.io.IOException
throws
javax.xml.parsers.ParserConfigurationException
throws
org.xml.sax.SAXException


      ByteArrayInputStream bais = new ByteArrayInputStream(inputBytes);
      InputSource in = new InputSource(bais);
      DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();

      dfactory.setNamespaceAware(true);

      // needs to validate for ID attribute nomalization
      dfactory.setValidating(true);

      DocumentBuilder db = dfactory.newDocumentBuilder();

      /*
       * for some of the test vectors from the specification,
       * there has to be a validatin parser for ID attributes, default
       * attribute values, NMTOKENS, etc.
       * Unfortunaltely, the test vectors do use different DTDs or
       * even no DTD. So Xerces 1.3.1 fires many warnings about using
       * ErrorHandlers.
       *
       * Text from the spec:
       *
       * The input octet stream MUST contain a well-formed XML document,
       * but the input need not be validated. However, the attribute
       * value normalization and entity reference resolution MUST be
       * performed in accordance with the behaviors of a validating
       * XML processor. As well, nodes for default attributes (declared
       * in the ATTLIST with an AttValue but not specified) are created
       * in each element. Thus, the declarations in the document type
       * declaration are used to help create the canonical form, even
       * though the document type declaration is not retained in the
       * canonical form.
       *
       */
      db.setErrorHandler(new com.sun.org.apache.xml.internal.security.utils
         .IgnoreAllErrorHandler());

      Document document = db.parse(in);
      byte result[] = this.canonicalizeSubtree(document);

      return result;
   
public byte[]canonicalizeSubtree(org.w3c.dom.Node node)
Canonicalizes the subtree rooted by node.

param
node The node to canicalize
return
the result of the c14n.
throws
CanonicalizationException

      return this.canonicalizerSpi.engineCanonicalizeSubTree(node);
   
public byte[]canonicalizeSubtree(org.w3c.dom.Node node, java.lang.String inclusiveNamespaces)
Canonicalizes the subtree rooted by node.

param
node
param
inclusiveNamespaces
return
the result of the c14n.
throws
CanonicalizationException

      return this.canonicalizerSpi.engineCanonicalizeSubTree(node,
              inclusiveNamespaces);
   
public byte[]canonicalizeXPathNodeSet(org.w3c.dom.NodeList xpathNodeSet)
Canonicalizes an XPath node set. The xpathNodeSet is treated as a list of XPath nodes, not as a list of subtrees.

param
xpathNodeSet
return
the result of the c14n.
throws
CanonicalizationException

      return this.canonicalizerSpi.engineCanonicalizeXPathNodeSet(xpathNodeSet);
   
public byte[]canonicalizeXPathNodeSet(org.w3c.dom.NodeList xpathNodeSet, java.lang.String inclusiveNamespaces)
Canonicalizes an XPath node set. The xpathNodeSet is treated as a list of XPath nodes, not as a list of subtrees.

param
xpathNodeSet
param
inclusiveNamespaces
return
the result of the c14n.
throws
CanonicalizationException

      return this.canonicalizerSpi.engineCanonicalizeXPathNodeSet(xpathNodeSet,
              inclusiveNamespaces);
   
public byte[]canonicalizeXPathNodeSet(java.util.Set xpathNodeSet)
Canonicalizes an XPath node set.

param
xpathNodeSet
return
the result of the c14n.
throws
CanonicalizationException

       return this.canonicalizerSpi.engineCanonicalizeXPathNodeSet(xpathNodeSet);
   
public byte[]canonicalizeXPathNodeSet(java.util.Set xpathNodeSet, java.lang.String inclusiveNamespaces)
Canonicalizes an XPath node set.

param
xpathNodeSet
param
inclusiveNamespaces
return
the result of the c14n.
throws
CanonicalizationException

       return this.canonicalizerSpi.engineCanonicalizeXPathNodeSet(xpathNodeSet,
              inclusiveNamespaces);
   
public java.lang.StringgetImplementingCanonicalizerClass()
Returns the name of the implementing {@link CanonicalizerSpi} class

return
the name of the implementing {@link CanonicalizerSpi} class

      return this.canonicalizerSpi.getClass().getName();
   
private static java.lang.ClassgetImplementingClass(java.lang.String URI)
Method getImplementingClass

param
URI
return
the name of the class that implements the give URI

      return (Class) _canonicalizerHash.get(URI);         
   
public booleangetIncludeComments()
Method getIncludeComments

return
true if the c14n respect the comments.

      return this.canonicalizerSpi.engineGetIncludeComments();
   
public static final com.sun.org.apache.xml.internal.security.c14n.CanonicalizergetInstance(java.lang.String algorithmURI)
Method getInstance

param
algorithmURI
return
a Conicicalizer instance ready for the job
throws
InvalidCanonicalizerException


      Canonicalizer c14nizer = new Canonicalizer(algorithmURI);

      return c14nizer;
   
public final java.lang.StringgetURI()
Method getURI

return
the URI defined for this c14n instance.

      return this.canonicalizerSpi.engineGetURI();
   
public static voidinit()
Method init

   //J+

         
       

      if (!Canonicalizer._alreadyInitialized) {
         Canonicalizer._canonicalizerHash = new HashMap(10);
         Canonicalizer._alreadyInitialized = true;
      }
   
public voidnotReset()
Set the canonicalizator behaviour to not reset.

   	    this.canonicalizerSpi.reset=false;
   
public static voidregister(java.lang.String algorithmURI, java.lang.String implementingClass)
Method register

param
algorithmURI
param
implementingClass
throws
AlgorithmAlreadyRegisteredException


      // check whether URI is already registered
      Class registeredClass = getImplementingClass(algorithmURI);

      if (registeredClass != null)  {
         Object exArgs[] = { algorithmURI, registeredClass };

         throw new AlgorithmAlreadyRegisteredException(
            "algorithm.alreadyRegistered", exArgs);
      }

      try {
		_canonicalizerHash.put(algorithmURI, Class.forName(implementingClass));
	} catch (ClassNotFoundException e) {
		throw new RuntimeException("c14n class not found");
	}
   
public voidsetWriter(java.io.OutputStream os)
Sets the writter where the cannocalization ends. ByteArrayOutputStream if none is setted.

param
os

   	    this.canonicalizerSpi.setWriter(os);