1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /**
6  * Licensed to the Apache Software Foundation (ASF) under one
7  * or more contributor license agreements. See the NOTICE file
8  * distributed with this work for additional information
9  * regarding copyright ownership. The ASF licenses this file
10  * to you under the Apache License, Version 2.0 (the
11  * "License"); you may not use this file except in compliance
12  * with the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing,
17  * software distributed under the License is distributed on an
18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19  * KIND, either express or implied. See the License for the
20  * specific language governing permissions and limitations
21  * under the License.
22  */
23 package com.sun.org.apache.xml.internal.security.transforms.implementations;
24 
25 import java.io.OutputStream;
26 
27 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
28 import com.sun.org.apache.xml.internal.security.c14n.implementations.Canonicalizer20010315ExclWithComments;
29 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
30 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
31 import com.sun.org.apache.xml.internal.security.transforms.Transform;
32 import com.sun.org.apache.xml.internal.security.transforms.TransformSpi;
33 import com.sun.org.apache.xml.internal.security.transforms.Transforms;
34 import com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces;
35 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
36 import org.w3c.dom.Element;
37 
38 /**
39  * Implements the {@code http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments}
40  * transform.
41  *
42  */
43 public class TransformC14NExclusiveWithComments extends TransformSpi {
44 
45     /** Field implementedTransformURI */
46     public static final String implementedTransformURI =
47         Transforms.TRANSFORM_C14N_EXCL_WITH_COMMENTS;
48 
49     /**
50      * Method engineGetURI
51      *{@inheritDoc}
52      *
53      */
engineGetURI()54     protected String engineGetURI() {
55         return implementedTransformURI;
56     }
57 
enginePerformTransform( XMLSignatureInput input, OutputStream os, Transform transformObject )58     protected XMLSignatureInput enginePerformTransform(
59         XMLSignatureInput input, OutputStream os, Transform transformObject
60     ) throws CanonicalizationException {
61         try {
62             String inclusiveNamespaces = null;
63 
64             if (transformObject.length(
65                 InclusiveNamespaces.ExclusiveCanonicalizationNamespace,
66                 InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES) == 1
67             ) {
68                 Element inclusiveElement =
69                     XMLUtils.selectNode(
70                         transformObject.getElement().getFirstChild(),
71                         InclusiveNamespaces.ExclusiveCanonicalizationNamespace,
72                         InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES,
73                         0
74                     );
75 
76                 inclusiveNamespaces =
77                     new InclusiveNamespaces(
78                         inclusiveElement, transformObject.getBaseURI()
79                     ).getInclusiveNamespaces();
80             }
81 
82             Canonicalizer20010315ExclWithComments c14n =
83                 new Canonicalizer20010315ExclWithComments();
84             c14n.setSecureValidation(secureValidation);
85             if (os != null) {
86                 c14n.setWriter(os);
87             }
88             byte[] result = c14n.engineCanonicalize(input, inclusiveNamespaces);
89             XMLSignatureInput output = new XMLSignatureInput(result);
90             output.setSecureValidation(secureValidation);
91 
92             return output;
93         } catch (XMLSecurityException ex) {
94             throw new CanonicalizationException(ex);
95         }
96     }
97 }
98