KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > c14n > implementations > Canonicalizer20010315Excl


1
2 /*
3  * Copyright 1999-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6  * use this file except in compliance with the License. You may obtain a copy of
7  * 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, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  *
17  */

18 package com.sun.org.apache.xml.internal.security.c14n.implementations;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.SortedSet JavaDoc;
23 import java.util.TreeSet JavaDoc;
24
25 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
26 import com.sun.org.apache.xml.internal.security.c14n.helper.C14nHelper;
27 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
28 import com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces;
29 import com.sun.org.apache.xml.internal.security.utils.Constants;
30 import org.w3c.dom.Attr JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.NamedNodeMap JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 /**
35  * Implements &quot; <A
36  * HREF="http://www.w3.org/TR/2002/REC-xml-exc-c14n-20020718/">Exclusive XML
37  * Canonicalization, Version 1.0 </A>&quot; <BR />
38  * Credits: During restructuring of the Canonicalizer framework, Ren??
39  * Kollmorgen from Software AG submitted an implementation of ExclC14n which
40  * fitted into the old architecture and which based heavily on my old (and slow)
41  * implementation of "Canonical XML". A big "thank you" to Ren?? for this.
42  * <BR />
43  * <i>THIS </i> implementation is a complete rewrite of the algorithm.
44  *
45  * @author Christian Geuer-Pollmann <geuerp@apache.org>
46  * @version $Revision: 1.21 $
47  * @see <a HREF="http://www.w3.org/TR/2002/REC-xml-exc-c14n-20020718/ Exclusive#">
48  * XML Canonicalization, Version 1.0</a>
49  */

50 public abstract class Canonicalizer20010315Excl extends CanonicalizerBase {
51     /**
52       * This Set contains the names (Strings like "xmlns" or "xmlns:foo") of
53       * the inclusive namespaces.
54       */

55     TreeSet JavaDoc _inclusiveNSSet = null;
56     static final String JavaDoc XMLNS_URI=Constants.NamespaceSpecNS;
57     final SortedSet JavaDoc result = new TreeSet JavaDoc(COMPARE);
58     /**
59      * Constructor Canonicalizer20010315Excl
60      *
61      * @param includeComments
62      */

63     public Canonicalizer20010315Excl(boolean includeComments) {
64         super(includeComments);
65     }
66
67     /**
68      * Method engineCanonicalizeSubTree
69      * @inheritDoc
70      * @param rootNode
71      *
72      * @throws CanonicalizationException
73      */

74     public byte[] engineCanonicalizeSubTree(Node JavaDoc rootNode)
75             throws CanonicalizationException {
76         return this.engineCanonicalizeSubTree(rootNode, "",null);
77     }
78     /**
79      * Method engineCanonicalizeSubTree
80      * @inheritDoc
81      * @param rootNode
82      * @param inclusiveNamespaces
83      *
84      * @throws CanonicalizationException
85      */

86     public byte[] engineCanonicalizeSubTree(Node JavaDoc rootNode,
87             String JavaDoc inclusiveNamespaces) throws CanonicalizationException {
88         return this.engineCanonicalizeSubTree(rootNode, inclusiveNamespaces,null);
89     }
90     /**
91      * Method engineCanonicalizeSubTree
92      * @param rootNode
93      * @param inclusiveNamespaces
94      * @param excl A element to exclude from the c14n process.
95      * @return the rootNode c14n.
96      * @throws CanonicalizationException
97      */

98     public byte[] engineCanonicalizeSubTree(Node JavaDoc rootNode,
99             String JavaDoc inclusiveNamespaces,Node JavaDoc excl) throws CanonicalizationException {
100             this._inclusiveNSSet = (TreeSet JavaDoc)InclusiveNamespaces
101                     .prefixStr2Set(inclusiveNamespaces);
102             return super.engineCanonicalizeSubTree(rootNode,excl);
103     }
104     /**
105      *
106      * @param rootNode
107      * @param inclusiveNamespaces
108      * @return the rootNode c14n.
109      * @throws CanonicalizationException
110      */

111     public byte[] engineCanonicalize(XMLSignatureInput rootNode,
112             String JavaDoc inclusiveNamespaces) throws CanonicalizationException {
113             this._inclusiveNSSet = (TreeSet JavaDoc)InclusiveNamespaces
114                     .prefixStr2Set(inclusiveNamespaces);
115             return super.engineCanonicalize(rootNode);
116     }
117  
118     /**
119      * Method handleAttributesSubtree
120      * @inheritDoc
121      * @param E
122      * @throws CanonicalizationException
123      */

124     Iterator JavaDoc handleAttributesSubtree(Element JavaDoc E,NameSpaceSymbTable ns)
125             throws CanonicalizationException {
126         // System.out.println("During the traversal, I encountered " +
127
// XMLUtils.getXPath(E));
128
// result will contain the attrs which have to be outputted
129
SortedSet JavaDoc result = this.result;
130         result.clear();
131         NamedNodeMap JavaDoc attrs=null;
132         
133         int attrsLength = 0;
134         if (E.hasAttributes()) {
135             attrs = E.getAttributes();
136             attrsLength = attrs.getLength();
137         }
138         //The prefix visibly utilized(in the attribute or in the name) in the element
139
SortedSet JavaDoc visiblyUtilized =(SortedSet JavaDoc) _inclusiveNSSet.clone();
140                     
141         for (int i = 0; i < attrsLength; i++) {
142             Attr JavaDoc N = (Attr JavaDoc) attrs.item(i);
143             String JavaDoc NName=N.getLocalName();
144             String JavaDoc NNodeValue=N.getNodeValue();
145                         
146             if (!XMLNS_URI.equals(N.getNamespaceURI())) {
147                 //Not a namespace definition.
148
//The Element is output element, add his prefix(if used) to visibyUtilized
149
String JavaDoc prefix = N.getPrefix();
150                 if ( (prefix != null) && (!prefix.equals(XML) && !prefix.equals(XMLNS)) ) {
151                         visiblyUtilized.add(prefix);
152                 }
153                 //Add to the result.
154
result.add(N);
155                 continue;
156             }
157     
158             if (ns.addMapping(NName, NNodeValue,N)) {
159                 //New definition check if it is relative.
160
if (C14nHelper.namespaceIsRelative(NNodeValue)) {
161                     Object JavaDoc exArgs[] = {E.getTagName(), NName,
162                             N.getNodeValue()};
163                     throw new CanonicalizationException(
164                             "c14n.Canonicalizer.RelativeNamespace", exArgs);
165                 }
166             }
167         }
168                             
169         if (E.getNamespaceURI() != null) {
170             String JavaDoc prefix = E.getPrefix();
171             if ((prefix == null) || (prefix.length() == 0)) {
172                 visiblyUtilized.add(XMLNS);
173             } else {
174                 visiblyUtilized.add(prefix);
175             }
176         } else {
177             visiblyUtilized.add(XMLNS);
178         }
179                                     
180         //This can be optimezed by I don't have time
181
Iterator JavaDoc it=visiblyUtilized.iterator();
182         while (it.hasNext()) {
183             String JavaDoc s=(String JavaDoc)it.next();
184             Attr JavaDoc key=ns.getMapping(s);
185             if (key==null) {
186                 continue;
187             }
188             result.add(key);
189         }
190         
191         return result.iterator();
192     }
193
194     /**
195      * Method engineCanonicalizeXPathNodeSet
196      * @inheritDoc
197      * @param xpathNodeSet
198      * @param inclusiveNamespaces
199      * @throws CanonicalizationException
200      */

201     public byte[] engineCanonicalizeXPathNodeSet(Set JavaDoc xpathNodeSet,
202             String JavaDoc inclusiveNamespaces) throws CanonicalizationException {
203         
204         
205             this._inclusiveNSSet = (TreeSet JavaDoc)InclusiveNamespaces
206                     .prefixStr2Set(inclusiveNamespaces);
207             return super.engineCanonicalizeXPathNodeSet(xpathNodeSet);
208         
209     }
210     
211     /** @inheritDoc */
212     public byte[] engineCanonicalizeXPathNodeSet(Set JavaDoc xpathNodeSet
213             ) throws CanonicalizationException {
214         return engineCanonicalizeXPathNodeSet(xpathNodeSet,"");
215     }
216             
217     /**
218      * @inheritDoc
219      * @param E
220      * @throws CanonicalizationException
221      */

222     final Iterator JavaDoc handleAttributes(Element JavaDoc E, NameSpaceSymbTable ns)
223             throws CanonicalizationException {
224         // result will contain the attrs which have to be outputted
225
SortedSet JavaDoc result = this.result;
226         result.clear();
227         NamedNodeMap JavaDoc attrs = null;
228         int attrsLength = 0;
229         if (E.hasAttributes()) {
230             attrs = E.getAttributes();
231             attrsLength = attrs.getLength();
232         }
233         //The prefix visibly utilized(in the attribute or in the name) in the element
234
Set JavaDoc visiblyUtilized =null;
235         //It's the output selected.
236
boolean isOutputElement = isVisible(E);
237         if (isOutputElement) {
238             visiblyUtilized = (Set JavaDoc) this._inclusiveNSSet.clone();
239         }
240         
241         for (int i = 0; i < attrsLength; i++) {
242             Attr JavaDoc N = (Attr JavaDoc) attrs.item(i);
243             String JavaDoc NName=N.getLocalName();
244             String JavaDoc NNodeValue=N.getNodeValue();
245             if ( !isVisible(N) ) {
246                 //The node is not in the nodeset(if there is a nodeset)
247
continue;
248             }
249                         
250             if (!XMLNS_URI.equals(N.getNamespaceURI())) {
251                 //Not a namespace definition.
252
if (isOutputElement) {
253                     //The Element is output element, add his prefix(if used) to visibyUtilized
254
String JavaDoc prefix = N.getPrefix();
255                     if ((prefix != null) && (!prefix.equals(XML) && !prefix.equals(XMLNS)) ){
256                             visiblyUtilized.add(prefix);
257                     }
258                     //Add to the result.
259
result.add(N);
260                 }
261                 continue;
262             }
263                         
264             
265             if (ns.addMapping(NName, NNodeValue,N)) {
266                 //New definiton check if it is relative
267
if (C14nHelper.namespaceIsRelative(NNodeValue)) {
268                     Object JavaDoc exArgs[] = {E.getTagName(), NName,
269                             N.getNodeValue()};
270                     throw new CanonicalizationException(
271                             "c14n.Canonicalizer.RelativeNamespace", exArgs);
272                 }
273             }
274         }
275
276         if (isOutputElement) {
277            //The element is visible, handle the xmlns definition
278
Attr JavaDoc xmlns = E.getAttributeNodeNS(XMLNS_URI, XMLNS);
279            if ((xmlns!=null) && (!isVisible(xmlns))) {
280               //There is a definition but the xmlns is not selected by the xpath.
281
//then xmlns=""
282
ns.addMapping(XMLNS,"",nullNode);
283             }
284
285             if (E.getNamespaceURI() != null) {
286                 String JavaDoc prefix = E.getPrefix();
287                 if ((prefix == null) || (prefix.length() == 0)) {
288                     visiblyUtilized.add(XMLNS);
289                 } else {
290                     visiblyUtilized.add( prefix);
291                 }
292             } else {
293                 visiblyUtilized.add(XMLNS);
294             }
295             //This can be optimezed by I don't have time
296
//visiblyUtilized.addAll(this._inclusiveNSSet);
297
Iterator JavaDoc it=visiblyUtilized.iterator();
298             while (it.hasNext()) {
299                 String JavaDoc s=(String JavaDoc)it.next();
300                 Attr JavaDoc key=ns.getMapping(s);
301                 if (key==null) {
302                     continue;
303                 }
304                 result.add(key);
305             }
306         } else /*if (_circunvented)*/ {
307             Iterator JavaDoc it=this._inclusiveNSSet.iterator();
308             while (it.hasNext()) {
309                 String JavaDoc s=(String JavaDoc)it.next();
310                 Attr JavaDoc key=ns.getMappingWithoutRendered(s);
311                 if (key==null) {
312                     continue;
313                 }
314                 result.add(key);
315             }
316         }
317
318         return result.iterator();
319     }
320 }
321
322
Popular Tags