1 2 18 package com.sun.org.apache.xml.internal.security.transforms.implementations; 19 20 21 22 import java.io.BufferedInputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 26 import javax.xml.parsers.DocumentBuilderFactory ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 29 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; 30 import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException; 31 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; 32 import com.sun.org.apache.xml.internal.security.transforms.TransformSpi; 33 import com.sun.org.apache.xml.internal.security.transforms.TransformationException; 34 import com.sun.org.apache.xml.internal.security.transforms.Transforms; 35 import com.sun.org.apache.xml.internal.security.utils.Base64; 36 import org.w3c.dom.Document ; 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.Node ; 39 import org.w3c.dom.Text ; 40 import org.xml.sax.SAXException ; 41 42 43 69 public class TransformBase64Decode extends TransformSpi { 70 71 72 public static final String implementedTransformURI = 73 Transforms.TRANSFORM_BASE64_DECODE; 74 75 80 protected String engineGetURI() { 81 return TransformBase64Decode.implementedTransformURI; 82 } 83 84 94 protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input) 95 throws IOException , CanonicalizationException, 96 TransformationException { 97 return enginePerformTransform(input,null); 98 } 99 protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, 100 OutputStream os) 101 throws IOException , CanonicalizationException, 102 TransformationException { 103 try { 104 if (input.isElement()) { 105 Node el=input.getSubNode(); 106 if (input.getSubNode().getNodeType()==Node.TEXT_NODE) { 107 el=el.getParentNode(); 108 } 109 StringBuffer sb=new StringBuffer (); 110 traverseElement((Element )el,sb); 111 if (os==null) { 112 byte[] decodedBytes = Base64.decode(sb.toString()); 113 return new XMLSignatureInput(decodedBytes); 114 } 115 Base64.decode(sb.toString().getBytes(),os); 116 XMLSignatureInput output=new XMLSignatureInput((byte[])null); 117 output.setOutputStream(os); 118 return output; 119 120 } 121 if (input.isOctetStream() || input.isNodeSet()) { 122 123 124 if (os==null) { 125 byte[] base64Bytes = input.getBytes(); 126 byte[] decodedBytes = Base64.decode(base64Bytes); 127 return new XMLSignatureInput(decodedBytes); 128 } 129 if (input.isByteArray() || input.isNodeSet()) { 130 Base64.decode(input.getBytes(),os); 131 } else { 132 Base64.decode(new BufferedInputStream (input.getOctetStreamReal()) 133 ,os); 134 } 135 XMLSignatureInput output=new XMLSignatureInput((byte[])null); 136 output.setOutputStream(os); 137 return output; 138 139 140 } 141 142 try { 143 Document doc = 146 DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( 147 input.getOctetStream()); 148 149 Element rootNode = doc.getDocumentElement(); 150 StringBuffer sb = new StringBuffer (); 151 traverseElement(rootNode,sb); 152 byte[] decodedBytes = Base64.decode(sb.toString()); 153 154 return new XMLSignatureInput(decodedBytes); 155 } catch (ParserConfigurationException e) { 156 throw new TransformationException("c14n.Canonicalizer.Exception",e); 157 } catch (SAXException e) { 158 throw new TransformationException("SAX exception", e); 159 } 160 } catch (Base64DecodingException e) { 161 throw new TransformationException("Base64Decoding", e); 162 } 163 } 164 165 void traverseElement(org.w3c.dom.Element node,StringBuffer sb) { 166 Node sibling=node.getFirstChild(); 167 while (sibling!=null) { 168 switch (sibling.getNodeType()) { 169 case Node.ELEMENT_NODE: 170 traverseElement((Element )sibling,sb); 171 break; 172 case Node.TEXT_NODE: 173 sb.append(((Text )sibling).getData()); 174 } 175 sibling=sibling.getNextSibling(); 176 } 177 } 178 } 179 | Popular Tags |