KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > transforms > implementations > TransformBase64Decode


1
2 /*
3  * Copyright 1999-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package com.sun.org.apache.xml.internal.security.transforms.implementations;
19
20
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.Node JavaDoc;
39 import org.w3c.dom.Text JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41
42
43 /**
44  * Implements the <CODE>http://www.w3.org/2000/09/xmldsig#base64</CODE> decoding
45  * transform.
46  *
47  * <p>The normative specification for base64 decoding transforms is
48  * <A HREF="http://www.w3.org/TR/2001/CR-xmldsig-core-20010419/#ref-MIME">[MIME]</A>.
49  * The base64 Transform element has no content. The input
50  * is decoded by the algorithms. This transform is useful if an
51  * application needs to sign the raw data associated with the encoded
52  * content of an element. </p>
53  *
54  * <p>This transform requires an octet stream for input.
55  * If an XPath node-set (or sufficiently functional alternative) is
56  * given as input, then it is converted to an octet stream by
57  * performing operations logically equivalent to 1) applying an XPath
58  * transform with expression self::text(), then 2) taking the string-value
59  * of the node-set. Thus, if an XML element is identified by a barename
60  * XPointer in the Reference URI, and its content consists solely of base64
61  * encoded character data, then this transform automatically strips away the
62  * start and end tags of the identified element and any of its descendant
63  * elements as well as any descendant comments and processing instructions.
64  * The output of this transform is an octet stream.</p>
65  *
66  * @author Christian Geuer-Pollmann
67  * @see com.sun.org.apache.xml.internal.security.utils.Base64
68  */

69 public class TransformBase64Decode extends TransformSpi {
70
71    /** Field implementedTransformURI */
72    public static final String JavaDoc implementedTransformURI =
73       Transforms.TRANSFORM_BASE64_DECODE;
74
75    /**
76     * Method engineGetURI
77     *
78     * @inheritDoc
79     */

80    protected String JavaDoc engineGetURI() {
81       return TransformBase64Decode.implementedTransformURI;
82    }
83
84    /**
85     * Method enginePerformTransform
86     *
87     * @param input
88     * @return {@link XMLSignatureInput} as the result of transformation
89     * @inheritDoc
90     * @throws CanonicalizationException
91     * @throws IOException
92     * @throws TransformationException
93     */

94    protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input)
95            throws IOException JavaDoc, CanonicalizationException,
96                   TransformationException {
97     return enginePerformTransform(input,null);
98    }
99     protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input,
100             OutputStream JavaDoc os)
101     throws IOException JavaDoc, CanonicalizationException,
102            TransformationException {
103      try {
104       if (input.isElement()) {
105          Node JavaDoc el=input.getSubNode();
106          if (input.getSubNode().getNodeType()==Node.TEXT_NODE) {
107             el=el.getParentNode();
108          }
109          StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
110          traverseElement((Element JavaDoc)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 JavaDoc(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             //Exceptional case there is current not text case testing this(Before it was a
144
//a common case).
145
Document JavaDoc doc =
146                DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
147                   input.getOctetStream());
148                   
149             Element JavaDoc rootNode = doc.getDocumentElement();
150             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
151             traverseElement(rootNode,sb);
152             byte[] decodedBytes = Base64.decode(sb.toString());
153             
154             return new XMLSignatureInput(decodedBytes);
155           } catch (ParserConfigurationException JavaDoc e) {
156               throw new TransformationException("c14n.Canonicalizer.Exception",e);
157           } catch (SAXException JavaDoc 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 JavaDoc node,StringBuffer JavaDoc sb) {
166         Node JavaDoc sibling=node.getFirstChild();
167         while (sibling!=null) {
168             switch (sibling.getNodeType()) {
169                 case Node.ELEMENT_NODE:
170                     traverseElement((Element JavaDoc)sibling,sb);
171                     break;
172                case Node.TEXT_NODE:
173                     sb.append(((Text JavaDoc)sibling).getData());
174             }
175             sibling=sibling.getNextSibling();
176         }
177    }
178 }
179
Popular Tags