KickJava   Java API By Example, From Geeks To Geeks.

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


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.c14n;
18
19
20
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import javax.xml.parsers.DocumentBuilder JavaDoc;
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27
28 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32 import org.xml.sax.InputSource JavaDoc;
33
34
35 /**
36  * Base class which all Caninicalization algorithms extend.
37  *
38  * $todo$ cange JavaDoc
39  * @author Christian Geuer-Pollmann
40  */

41 public abstract class CanonicalizerSpi {
42
43    /**
44     * Method canonicalize
45     *
46     *
47     * @param inputBytes
48     * @return the c14n bytes.
49     *
50     *
51     * @throws CanonicalizationException
52     * @throws java.io.IOException
53     * @throws javax.xml.parsers.ParserConfigurationException
54     * @throws org.xml.sax.SAXException
55     *
56     */

57    public byte[] engineCanonicalize(byte[] inputBytes)
58            throws javax.xml.parsers.ParserConfigurationException JavaDoc,
59                   java.io.IOException JavaDoc, org.xml.sax.SAXException JavaDoc,
60                   CanonicalizationException {
61
62       java.io.ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(inputBytes);
63       InputSource JavaDoc in = new InputSource JavaDoc(bais);
64       DocumentBuilderFactory JavaDoc dfactory = DocumentBuilderFactory.newInstance();
65
66       // needs to validate for ID attribute nomalization
67
dfactory.setNamespaceAware(true);
68
69       DocumentBuilder JavaDoc db = dfactory.newDocumentBuilder();
70
71       /*
72        * for some of the test vectors from the specification,
73        * there has to be a validatin parser for ID attributes, default
74        * attribute values, NMTOKENS, etc.
75        * Unfortunaltely, the test vectors do use different DTDs or
76        * even no DTD. So Xerces 1.3.1 fires many warnings about using
77        * ErrorHandlers.
78        *
79        * Text from the spec:
80        *
81        * The input octet stream MUST contain a well-formed XML document,
82        * but the input need not be validated. However, the attribute
83        * value normalization and entity reference resolution MUST be
84        * performed in accordance with the behaviors of a validating
85        * XML processor. As well, nodes for default attributes (declared
86        * in the ATTLIST with an AttValue but not specified) are created
87        * in each element. Thus, the declarations in the document type
88        * declaration are used to help create the canonical form, even
89        * though the document type declaration is not retained in the
90        * canonical form.
91        *
92        */

93
94       // ErrorHandler eh = new C14NErrorHandler();
95
// db.setErrorHandler(eh);
96
Document JavaDoc document = db.parse(in);
97       byte result[] = this.engineCanonicalizeSubTree(document);
98       return result;
99    }
100
101    /**
102     * Method engineCanonicalizeXPathNodeSet
103     *
104     * @param xpathNodeSet
105     * @return the c14n bytes
106     * @throws CanonicalizationException
107     */

108    public byte[] engineCanonicalizeXPathNodeSet(NodeList JavaDoc xpathNodeSet)
109            throws CanonicalizationException {
110
111       return this
112          .engineCanonicalizeXPathNodeSet(XMLUtils
113             .convertNodelistToSet(xpathNodeSet));
114    }
115    
116    /**
117     * Method engineCanonicalizeXPathNodeSet
118     *
119     * @param xpathNodeSet
120     * @param inclusiveNamespaces
121     * @return the c14n bytes
122     * @throws CanonicalizationException
123     */

124    public byte[] engineCanonicalizeXPathNodeSet(NodeList JavaDoc xpathNodeSet, String JavaDoc inclusiveNamespaces)
125            throws CanonicalizationException {
126
127       return this
128          .engineCanonicalizeXPathNodeSet(XMLUtils
129             .convertNodelistToSet(xpathNodeSet), inclusiveNamespaces);
130    }
131
132    //J-
133
/** Returns the URI of this engine.
134     * @return the URI
135     */

136    public abstract String JavaDoc engineGetURI();
137    
138    /** Returns the URI if include comments
139     * @return true if include.
140     */

141    public abstract boolean engineGetIncludeComments();
142    
143    /**
144     * C14n a nodeset
145     *
146     * @param xpathNodeSet
147     * @return the c14n bytes
148     * @throws CanonicalizationException
149     */

150    public abstract byte[] engineCanonicalizeXPathNodeSet(Set JavaDoc xpathNodeSet)
151       throws CanonicalizationException;
152
153    /**
154     * C14n a nodeset
155     *
156     * @param xpathNodeSet
157     * @param inclusiveNamespaces
158     * @return the c14n bytes
159     * @throws CanonicalizationException
160     */

161    public abstract byte[] engineCanonicalizeXPathNodeSet(Set JavaDoc xpathNodeSet, String JavaDoc inclusiveNamespaces)
162       throws CanonicalizationException;
163
164    /**
165     * C14n a node tree.
166     *
167     * @param rootNode
168     * @return the c14n bytes
169     * @throws CanonicalizationException
170     */

171    public abstract byte[] engineCanonicalizeSubTree(Node JavaDoc rootNode)
172       throws CanonicalizationException;
173
174    /**
175     * C14n a node tree.
176     *
177     * @param rootNode
178     * @param inclusiveNamespaces
179     * @return the c14n bytes
180     * @throws CanonicalizationException
181     */

182    public abstract byte[] engineCanonicalizeSubTree(Node JavaDoc rootNode, String JavaDoc inclusiveNamespaces)
183       throws CanonicalizationException;
184    
185    /**
186     * Sets the writter where the cannocalization ends. ByteArrayOutputStream if
187     * none is setted.
188     * @param os
189     */

190    public abstract void setWriter(OutputStream JavaDoc os);
191    
192    /** Reset the writter after a c14n */
193    protected boolean reset=false;
194    //J+
195
}
196
Popular Tags