private void canonicalizeXPathNodeSet()

in src/main/java/org/apache/xml/security/signature/XMLSignatureInputDebugger.java [175:357]


    private void canonicalizeXPathNodeSet(Node currentNode)
        throws XMLSignatureException, IOException {

        int currentNodeType = currentNode.getNodeType();
        switch (currentNodeType) {


        case Node.ENTITY_NODE:
        case Node.NOTATION_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
        case Node.ATTRIBUTE_NODE:
            throw new XMLSignatureException("empty", new Object[]{"An incorrect node was provided for c14n: " + currentNodeType});
        case Node.DOCUMENT_NODE:
            this.writer.write(HTMLPrefix);

            for (Node currentChild = currentNode.getFirstChild();
                currentChild != null; currentChild = currentChild.getNextSibling()) {
                this.canonicalizeXPathNodeSet(currentChild);
            }

            this.writer.write(HTMLSuffix);
            break;

        case Node.COMMENT_NODE:
            if (this.xpathNodeSet.contains(currentNode)) {
                this.writer.write(HTMLIncludePrefix);
            } else {
                this.writer.write(HTMLExcludePrefix);
            }

            int position = getPositionRelativeToDocumentElement(currentNode);

            if (position == NODE_AFTER_DOCUMENT_ELEMENT) {
                this.writer.write("\n");
            }

            this.outputCommentToWriter((Comment) currentNode);

            if (position == NODE_BEFORE_DOCUMENT_ELEMENT) {
                this.writer.write("\n");
            }

            this.writer.write(HTMLIncludeOrExcludeSuffix);
            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            if (this.xpathNodeSet.contains(currentNode)) {
                this.writer.write(HTMLIncludePrefix);
            } else {
                this.writer.write(HTMLExcludePrefix);
            }

            position = getPositionRelativeToDocumentElement(currentNode);

            if (position == NODE_AFTER_DOCUMENT_ELEMENT) {
                this.writer.write("\n");
            }

            this.outputPItoWriter((ProcessingInstruction) currentNode);

            if (position == NODE_BEFORE_DOCUMENT_ELEMENT) {
                this.writer.write("\n");
            }

            this.writer.write(HTMLIncludeOrExcludeSuffix);
            break;

        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
            if (this.xpathNodeSet.contains(currentNode)) {
                this.writer.write(HTMLIncludePrefix);
            } else {
                this.writer.write(HTMLExcludePrefix);
            }

            outputTextToWriter(currentNode.getNodeValue());

            for (Node nextSibling = currentNode.getNextSibling();
                nextSibling != null
                && (nextSibling.getNodeType() == Node.TEXT_NODE
                    || nextSibling.getNodeType() == Node.CDATA_SECTION_NODE);
                nextSibling = nextSibling.getNextSibling()) {
                /*
                 * The XPath data model allows to select only the first of a
                 * sequence of mixed text and CDATA nodes. But we must output
                 * them all, so we must search:
                 *
                 * @see http://nagoya.apache.org/bugzilla/show_bug.cgi?id=6329
                 */
                this.outputTextToWriter(nextSibling.getNodeValue());
            }

            this.writer.write(HTMLIncludeOrExcludeSuffix);
            break;

        case Node.ELEMENT_NODE:
            Element currentElement = (Element) currentNode;

            if (this.xpathNodeSet.contains(currentNode)) {
                this.writer.write(HTMLIncludePrefix);
            } else {
                this.writer.write(HTMLExcludePrefix);
            }

            this.writer.write("<");
            this.writer.write(currentElement.getTagName());

            this.writer.write(HTMLIncludeOrExcludeSuffix);

            // we output all Attrs which are available
            NamedNodeMap attrs = currentElement.getAttributes();
            int attrsLength = attrs.getLength();
            Attr attrs2[] = new Attr[attrsLength];

            for (int i = 0; i < attrsLength; i++) {
                attrs2[i] = (Attr)attrs.item(i);
            }

            Arrays.sort(attrs2, ATTR_COMPARE);
            Object[] attrs3 = attrs2;

            for (int i = 0; i < attrsLength; i++) {
                Attr a = (Attr) attrs3[i];
                boolean included = this.xpathNodeSet.contains(a);
                boolean inclusive = this.inclusiveNamespaces.contains(a.getName());

                if (included) {
                    if (inclusive) {
                        // included and inclusive
                        this.writer.write(HTMLIncludedInclusiveNamespacePrefix);
                    } else {
                        // included and not inclusive
                        this.writer.write(HTMLIncludePrefix);
                    }
                } else {
                    if (inclusive) {
                        // excluded and inclusive
                        this.writer.write(HTMLExcludedInclusiveNamespacePrefix);
                    } else {
                        // excluded and not inclusive
                        this.writer.write(HTMLExcludePrefix);
                    }
                }

                this.outputAttrToWriter(a.getNodeName(), a.getNodeValue());
                this.writer.write(HTMLIncludeOrExcludeSuffix);
            }

            if (this.xpathNodeSet.contains(currentNode)) {
                this.writer.write(HTMLIncludePrefix);
            } else {
                this.writer.write(HTMLExcludePrefix);
            }

            this.writer.write("&gt;");

            this.writer.write(HTMLIncludeOrExcludeSuffix);

            // traversal
            for (Node currentChild = currentNode.getFirstChild();
                currentChild != null;
                currentChild = currentChild.getNextSibling()) {
                this.canonicalizeXPathNodeSet(currentChild);
            }

            if (this.xpathNodeSet.contains(currentNode)) {
                this.writer.write(HTMLIncludePrefix);
            } else {
                this.writer.write(HTMLExcludePrefix);
            }

            this.writer.write("&lt;/");
            this.writer.write(currentElement.getTagName());
            this.writer.write("&gt;");

            this.writer.write(HTMLIncludeOrExcludeSuffix);
            break;

        case Node.DOCUMENT_TYPE_NODE:
        default:
            break;
        }
    }