0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * Copyright 1999-2004 The Apache Software Foundation.
0N/A *
0N/A * Licensed under the Apache License, Version 2.0 (the "License");
0N/A * you may not use this file except in compliance with the License.
0N/A * You may obtain a copy of the License at
0N/A *
0N/A * http://www.apache.org/licenses/LICENSE-2.0
0N/A *
0N/A * Unless required by applicable law or agreed to in writing, software
0N/A * distributed under the License is distributed on an "AS IS" BASIS,
0N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0N/A * See the License for the specific language governing permissions and
0N/A * limitations under the License.
0N/A *
0N/A */
0N/Apackage com.sun.org.apache.xml.internal.security.c14n;
0N/A
0N/A
0N/A
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.OutputStream;
0N/Aimport java.util.Set;
0N/A
6159N/Aimport javax.xml.XMLConstants;
0N/Aimport javax.xml.parsers.DocumentBuilder;
0N/Aimport javax.xml.parsers.DocumentBuilderFactory;
6159N/Aimport javax.xml.xpath.XPath;
0N/A
0N/Aimport com.sun.org.apache.xml.internal.security.utils.XMLUtils;
0N/Aimport org.w3c.dom.Document;
0N/Aimport org.w3c.dom.Node;
0N/Aimport org.w3c.dom.NodeList;
0N/Aimport org.xml.sax.InputSource;
0N/A
0N/A
0N/A/**
0N/A * Base class which all Caninicalization algorithms extend.
0N/A *
0N/A * $todo$ cange JavaDoc
0N/A * @author Christian Geuer-Pollmann
0N/A */
0N/Apublic abstract class CanonicalizerSpi {
0N/A
0N/A /**
0N/A * Method canonicalize
0N/A *
0N/A *
0N/A * @param inputBytes
0N/A * @return the c14n bytes.
0N/A *
0N/A *
0N/A * @throws CanonicalizationException
0N/A * @throws java.io.IOException
0N/A * @throws javax.xml.parsers.ParserConfigurationException
0N/A * @throws org.xml.sax.SAXException
0N/A *
0N/A */
0N/A public byte[] engineCanonicalize(byte[] inputBytes)
0N/A throws javax.xml.parsers.ParserConfigurationException,
0N/A java.io.IOException, org.xml.sax.SAXException,
0N/A CanonicalizationException {
0N/A
0N/A java.io.ByteArrayInputStream bais = new ByteArrayInputStream(inputBytes);
0N/A InputSource in = new InputSource(bais);
0N/A DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
6159N/A dfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
0N/A
0N/A // needs to validate for ID attribute nomalization
0N/A dfactory.setNamespaceAware(true);
0N/A
0N/A DocumentBuilder db = dfactory.newDocumentBuilder();
0N/A
0N/A /*
0N/A * for some of the test vectors from the specification,
0N/A * there has to be a validatin parser for ID attributes, default
0N/A * attribute values, NMTOKENS, etc.
0N/A * Unfortunaltely, the test vectors do use different DTDs or
0N/A * even no DTD. So Xerces 1.3.1 fires many warnings about using
0N/A * ErrorHandlers.
0N/A *
0N/A * Text from the spec:
0N/A *
0N/A * The input octet stream MUST contain a well-formed XML document,
0N/A * but the input need not be validated. However, the attribute
0N/A * value normalization and entity reference resolution MUST be
0N/A * performed in accordance with the behaviors of a validating
0N/A * XML processor. As well, nodes for default attributes (declared
0N/A * in the ATTLIST with an AttValue but not specified) are created
0N/A * in each element. Thus, the declarations in the document type
0N/A * declaration are used to help create the canonical form, even
0N/A * though the document type declaration is not retained in the
0N/A * canonical form.
0N/A *
0N/A */
0N/A
0N/A // ErrorHandler eh = new C14NErrorHandler();
0N/A // db.setErrorHandler(eh);
0N/A Document document = db.parse(in);
0N/A byte result[] = this.engineCanonicalizeSubTree(document);
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Method engineCanonicalizeXPathNodeSet
0N/A *
0N/A * @param xpathNodeSet
0N/A * @return the c14n bytes
0N/A * @throws CanonicalizationException
0N/A */
0N/A public byte[] engineCanonicalizeXPathNodeSet(NodeList xpathNodeSet)
0N/A throws CanonicalizationException {
0N/A
0N/A return this
0N/A .engineCanonicalizeXPathNodeSet(XMLUtils
0N/A .convertNodelistToSet(xpathNodeSet));
0N/A }
0N/A
0N/A /**
0N/A * Method engineCanonicalizeXPathNodeSet
0N/A *
0N/A * @param xpathNodeSet
0N/A * @param inclusiveNamespaces
0N/A * @return the c14n bytes
0N/A * @throws CanonicalizationException
0N/A */
0N/A public byte[] engineCanonicalizeXPathNodeSet(NodeList xpathNodeSet, String inclusiveNamespaces)
0N/A throws CanonicalizationException {
0N/A
0N/A return this
0N/A .engineCanonicalizeXPathNodeSet(XMLUtils
0N/A .convertNodelistToSet(xpathNodeSet), inclusiveNamespaces);
0N/A }
0N/A
0N/A //J-
0N/A /** Returns the URI of this engine.
0N/A * @return the URI
0N/A */
0N/A public abstract String engineGetURI();
0N/A
0N/A /** Returns the URI if include comments
0N/A * @return true if include.
0N/A */
0N/A public abstract boolean engineGetIncludeComments();
0N/A
0N/A /**
0N/A * C14n a nodeset
0N/A *
0N/A * @param xpathNodeSet
0N/A * @return the c14n bytes
0N/A * @throws CanonicalizationException
0N/A */
6159N/A public abstract byte[] engineCanonicalizeXPathNodeSet(Set<Node> xpathNodeSet)
0N/A throws CanonicalizationException;
0N/A
0N/A /**
0N/A * C14n a nodeset
0N/A *
0N/A * @param xpathNodeSet
0N/A * @param inclusiveNamespaces
0N/A * @return the c14n bytes
0N/A * @throws CanonicalizationException
0N/A */
6159N/A public abstract byte[] engineCanonicalizeXPathNodeSet(Set<Node> xpathNodeSet, String inclusiveNamespaces)
0N/A throws CanonicalizationException;
0N/A
0N/A /**
0N/A * C14n a node tree.
0N/A *
0N/A * @param rootNode
0N/A * @return the c14n bytes
0N/A * @throws CanonicalizationException
0N/A */
0N/A public abstract byte[] engineCanonicalizeSubTree(Node rootNode)
0N/A throws CanonicalizationException;
0N/A
0N/A /**
0N/A * C14n a node tree.
0N/A *
0N/A * @param rootNode
0N/A * @param inclusiveNamespaces
0N/A * @return the c14n bytes
0N/A * @throws CanonicalizationException
0N/A */
0N/A public abstract byte[] engineCanonicalizeSubTree(Node rootNode, String inclusiveNamespaces)
0N/A throws CanonicalizationException;
0N/A
0N/A /**
0N/A * Sets the writter where the cannocalization ends. ByteArrayOutputStream if
0N/A * none is setted.
0N/A * @param os
0N/A */
0N/A public abstract void setWriter(OutputStream os);
0N/A
0N/A /** Reset the writter after a c14n */
0N/A protected boolean reset=false;
0N/A //J+
0N/A}