FileDocCategorySizeDatePackage
TransformBase64Decode.javaAPI DocJava SE 6 API6759Tue Jun 10 00:23:04 BST 2008com.sun.org.apache.xml.internal.security.transforms.implementations

TransformBase64Decode

public class TransformBase64Decode extends TransformSpi
Implements the http://www.w3.org/2000/09/xmldsig#base64 decoding transform.

The normative specification for base64 decoding transforms is [MIME]. The base64 Transform element has no content. The input is decoded by the algorithms. This transform is useful if an application needs to sign the raw data associated with the encoded content of an element.

This transform requires an octet stream for input. If an XPath node-set (or sufficiently functional alternative) is given as input, then it is converted to an octet stream by performing operations logically equivalent to 1) applying an XPath transform with expression self::text(), then 2) taking the string-value of the node-set. Thus, if an XML element is identified by a barename XPointer in the Reference URI, and its content consists solely of base64 encoded character data, then this transform automatically strips away the start and end tags of the identified element and any of its descendant elements as well as any descendant comments and processing instructions. The output of this transform is an octet stream.

author
Christian Geuer-Pollmann
see
com.sun.org.apache.xml.internal.security.utils.Base64

Fields Summary
public static final String
implementedTransformURI
Field implementedTransformURI
Constructors Summary
Methods Summary
protected java.lang.StringengineGetURI()
Method engineGetURI

inheritDoc


          
      
      return TransformBase64Decode.implementedTransformURI;
   
protected com.sun.org.apache.xml.internal.security.signature.XMLSignatureInputenginePerformTransform(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput input)
Method enginePerformTransform

param
input
return
{@link XMLSignatureInput} as the result of transformation
inheritDoc
throws
CanonicalizationException
throws
IOException
throws
TransformationException

   	return enginePerformTransform(input,null);
   
protected com.sun.org.apache.xml.internal.security.signature.XMLSignatureInputenginePerformTransform(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput input, java.io.OutputStream os)

	 try {
      if (input.isElement()) {
         Node el=input.getSubNode();
         if (input.getSubNode().getNodeType()==Node.TEXT_NODE) {         	
            el=el.getParentNode();
         }
         StringBuffer sb=new StringBuffer();
         traverseElement((Element)el,sb);
         if (os==null) {
         	byte[] decodedBytes = Base64.decode(sb.toString());            
         	return new XMLSignatureInput(decodedBytes);
         } 
         	Base64.decode(sb.toString().getBytes(),os);
            XMLSignatureInput output=new XMLSignatureInput((byte[])null);
            output.setOutputStream(os);
            return output;
         
      }
      if (input.isOctetStream() || input.isNodeSet()) {
                    
        
        if (os==null) {
            byte[] base64Bytes = input.getBytes();
            byte[] decodedBytes = Base64.decode(base64Bytes);            
            return new XMLSignatureInput(decodedBytes);
         } 
        if (input.isByteArray() || input.isNodeSet()) {
               Base64.decode(input.getBytes(),os);
        } else {
            Base64.decode(new BufferedInputStream(input.getOctetStreamReal())
                    ,os);
        }
            XMLSignatureInput output=new XMLSignatureInput((byte[])null);
            output.setOutputStream(os);
            return output;
         
        
      } 
       
	 try {
            //Exceptional case there is current not text case testing this(Before it was a
	 	    //a common case).
            Document doc =
               DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
                  input.getOctetStream());
                  
            Element rootNode = doc.getDocumentElement();
            StringBuffer sb = new StringBuffer();
            traverseElement(rootNode,sb);
            byte[] decodedBytes = Base64.decode(sb.toString());
			
            return new XMLSignatureInput(decodedBytes);
		  } catch (ParserConfigurationException e) {
			  throw new TransformationException("c14n.Canonicalizer.Exception",e);
		  } catch (SAXException e) {
			  throw new TransformationException("SAX exception", e);
		  }      
	} catch (Base64DecodingException e) {
        throw new TransformationException("Base64Decoding", e);
	}
   
voidtraverseElement(org.w3c.dom.Element node, java.lang.StringBuffer sb)

   	    Node sibling=node.getFirstChild();
        while (sibling!=null) {
        	switch (sibling.getNodeType()) {
        		case Node.ELEMENT_NODE:
                    traverseElement((Element)sibling,sb);
                    break;
               case Node.TEXT_NODE:
               	    sb.append(((Text)sibling).getData());
            }
            sibling=sibling.getNextSibling();
        }