KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 package com.sun.org.apache.xml.internal.security.transforms.implementations;
18
19
20
21 import com.sun.org.apache.xml.internal.security.signature.NodeFilter;
22 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
23 import com.sun.org.apache.xml.internal.security.transforms.TransformSpi;
24 import com.sun.org.apache.xml.internal.security.transforms.TransformationException;
25 import com.sun.org.apache.xml.internal.security.transforms.Transforms;
26 import com.sun.org.apache.xml.internal.security.utils.Constants;
27 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30
31
32 /**
33  * Implements the <CODE>http://www.w3.org/2000/09/xmldsig#enveloped-signature</CODE>
34  * transform.
35  *
36  * @author Christian Geuer-Pollmann
37  */

38 public class TransformEnvelopedSignature extends TransformSpi {
39
40    /** Field implementedTransformURI */
41    public static final String JavaDoc implementedTransformURI =
42       Transforms.TRANSFORM_ENVELOPED_SIGNATURE;
43
44    /**
45     * Method engineGetURI
46     *
47     * @inheritDoc
48     */

49    protected String JavaDoc engineGetURI() {
50       return implementedTransformURI;
51    }
52
53    /**
54     * @inheritDoc
55     */

56    protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input)
57            throws TransformationException {
58
59
60
61          /**
62           * If the actual input is an octet stream, then the application MUST
63           * convert the octet stream to an XPath node-set suitable for use by
64           * Canonical XML with Comments. (A subsequent application of the
65           * REQUIRED Canonical XML algorithm would strip away these comments.)
66           *
67           * ...
68           *
69           * The evaluation of this expression includes all of the document's nodes
70           * (including comments) in the node-set representing the octet stream.
71           */

72
73          /*
74          if (input.isOctetStream()) {
75             input.setNodesetXPath(Canonicalizer.XPATH_C14N_WITH_COMMENTS);
76          }
77          */

78          
79          Element JavaDoc transformElement = this._transformObject.getElement();
80          Node JavaDoc signatureElement = transformElement;
81          
82
83          signatureElement = searchSignatureElement(signatureElement);
84             input.setExcludeNode(signatureElement);
85             input.addNodeFilter(new EnvelopedNodeFilter(signatureElement));
86             return input;
87          
88          //
89

90       
91    }
92
93    /**
94     * @param signatureElement
95     * @return the node that is the signature
96     * @throws TransformationException
97     */

98     private static Node JavaDoc searchSignatureElement(Node JavaDoc signatureElement) throws TransformationException {
99         boolean found=false;
100         
101         while (true) {
102             if ((signatureElement == null)
103                 || (signatureElement.getNodeType() == Node.DOCUMENT_NODE)) {
104                 break;
105             }
106             Element JavaDoc el=(Element JavaDoc)signatureElement;
107             if (el.getNamespaceURI().equals(Constants.SignatureSpecNS)
108                     &&
109                    el.getLocalName().equals(Constants._TAG_SIGNATURE)) {
110                 found = true;
111                 break;
112             }
113
114             signatureElement = signatureElement.getParentNode();
115         }
116
117         if (!found) {
118           throw new TransformationException(
119            "envelopedSignatureTransformNotInSignatureElement");
120         }
121         return signatureElement;
122     }
123     class EnvelopedNodeFilter implements NodeFilter {
124         Node JavaDoc exclude;
125         EnvelopedNodeFilter(Node JavaDoc n) {
126             exclude=n;
127         }
128         /**
129          * @see com.sun.org.apache.xml.internal.security.signature.NodeFilter#isNodeInclude(org.w3c.dom.Node)
130          */

131         public boolean isNodeInclude(Node JavaDoc n) {
132             // TODO Optimize me.
133
return !XMLUtils.isDescendantOrSelf(exclude,n);
134         }
135         
136     }
137 }
138
Popular Tags