View Javadoc
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.c14n;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.OutputStream;
27  import java.util.Set;
28  
29  import javax.xml.XMLConstants;
30  import javax.xml.parsers.DocumentBuilder;
31  import javax.xml.parsers.DocumentBuilderFactory;
32  
33  import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
34  import org.w3c.dom.Document;
35  import org.w3c.dom.Node;
36  import org.w3c.dom.NodeList;
37  import org.xml.sax.InputSource;
38  
39  /**
40   * Base class which all Canonicalization algorithms extend.
41   *
42   * @author Christian Geuer-Pollmann
43   */
44  public abstract class CanonicalizerSpi {
45  
46      /** Reset the writer after a c14n */
47      protected boolean reset = false;
48  
49      /**
50       * Method canonicalize
51       *
52       * @param inputBytes
53       * @return the c14n bytes.
54       *
55       * @throws CanonicalizationException
56       * @throws java.io.IOException
57       * @throws javax.xml.parsers.ParserConfigurationException
58       * @throws org.xml.sax.SAXException
59       */
60      public byte[] engineCanonicalize(byte[] inputBytes)
61          throws javax.xml.parsers.ParserConfigurationException, java.io.IOException,
62          org.xml.sax.SAXException, CanonicalizationException {
63  
64          java.io.InputStream bais = new ByteArrayInputStream(inputBytes);
65          InputSource in = new InputSource(bais);
66          DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
67          dfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
68  
69          // needs to validate for ID attribute normalization
70          dfactory.setNamespaceAware(true);
71  
72          DocumentBuilder db = dfactory.newDocumentBuilder();
73  
74          Document document = db.parse(in);
75          return this.engineCanonicalizeSubTree(document);
76      }
77  
78      /**
79       * Method engineCanonicalizeXPathNodeSet
80       *
81       * @param xpathNodeSet
82       * @return the c14n bytes
83       * @throws CanonicalizationException
84       */
85      public byte[] engineCanonicalizeXPathNodeSet(NodeList xpathNodeSet)
86          throws CanonicalizationException {
87          return this.engineCanonicalizeXPathNodeSet(
88              XMLUtils.convertNodelistToSet(xpathNodeSet)
89          );
90      }
91  
92      /**
93       * Method engineCanonicalizeXPathNodeSet
94       *
95       * @param xpathNodeSet
96       * @param inclusiveNamespaces
97       * @return the c14n bytes
98       * @throws CanonicalizationException
99       */
100     public byte[] engineCanonicalizeXPathNodeSet(NodeList xpathNodeSet, String inclusiveNamespaces)
101         throws CanonicalizationException {
102         return this.engineCanonicalizeXPathNodeSet(
103             XMLUtils.convertNodelistToSet(xpathNodeSet), inclusiveNamespaces
104         );
105     }
106 
107     /**
108      * Returns the URI of this engine.
109      * @return the URI
110      */
111     public abstract String engineGetURI();
112 
113     /**
114      * Returns true if comments are included
115      * @return true if comments are included
116      */
117     public abstract boolean engineGetIncludeComments();
118 
119     /**
120      * C14n a nodeset
121      *
122      * @param xpathNodeSet
123      * @return the c14n bytes
124      * @throws CanonicalizationException
125      */
126     public abstract byte[] engineCanonicalizeXPathNodeSet(Set<Node> xpathNodeSet)
127         throws CanonicalizationException;
128 
129     /**
130      * C14n a nodeset
131      *
132      * @param xpathNodeSet
133      * @param inclusiveNamespaces
134      * @return the c14n bytes
135      * @throws CanonicalizationException
136      */
137     public abstract byte[] engineCanonicalizeXPathNodeSet(
138         Set<Node> xpathNodeSet, String inclusiveNamespaces
139     ) throws CanonicalizationException;
140 
141     /**
142      * C14n a node tree.
143      *
144      * @param rootNode
145      * @return the c14n bytes
146      * @throws CanonicalizationException
147      */
148     public abstract byte[] engineCanonicalizeSubTree(Node rootNode)
149         throws CanonicalizationException;
150 
151     /**
152      * C14n a node tree.
153      *
154      * @param rootNode
155      * @param inclusiveNamespaces
156      * @return the c14n bytes
157      * @throws CanonicalizationException
158      */
159     public abstract byte[] engineCanonicalizeSubTree(Node rootNode, String inclusiveNamespaces)
160         throws CanonicalizationException;
161 
162     /**
163      * Sets the writer where the canonicalization ends. ByteArrayOutputStream if
164      * none is set.
165      * @param os
166      */
167     public abstract void setWriter(OutputStream os);
168 
169 }