package com.sun.org.apache.xml.internal.security.signature; import org.w3c.dom.NamedNodeMap; import java.util.Arrays; import org.w3c.dom.Attr; import org.w3c.dom.Element; import org.w3c.dom.ProcessingInstruction; import org.w3c.dom.Comment; import org.w3c.dom.Document; import java.io.IOException; import java.io.StringWriter; import com.sun.org.apache.xml.internal.security.utils.XMLUtils; import com.sun.org.apache.xml.internal.security.c14n.helper.AttrCompare; import java.io.Writer; import org.w3c.dom.Node; import java.util.Set; public class XMLSignatureInputDebugger { private Set xpathNodeSet; private Set inclusiveNamespaces; private Writer writer; static final String HTMLPrefix = "\n\n\nCaninical XML node set\n \n\n\n

Explanation of the output

\n

The following text contains the nodeset of the given Reference before it is canonicalized. There exist four different styles to indicate how a given node is treated.

\n\n

Output

\n
\n";
    static final String HTMLSuffix = "
"; static final String HTMLExcludePrefix = ""; static final String HTMLIncludePrefix = ""; static final String HTMLIncludeOrExcludeSuffix = ""; static final String HTMLIncludedInclusiveNamespacePrefix = ""; static final String HTMLExcludedInclusiveNamespacePrefix = ""; private static final int NODE_BEFORE_DOCUMENT_ELEMENT = -1; private static final int NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT = 0; private static final int NODE_AFTER_DOCUMENT_ELEMENT = 1; static final AttrCompare ATTR_COMPARE; public XMLSignatureInputDebugger(XMLSignatureInput xmlSignatureInput) { if (!xmlSignatureInput.isNodeSet()) { this.xpathNodeSet = null; } else { this.xpathNodeSet = xmlSignatureInput.getInputNodeSet(); } } public XMLSignatureInputDebugger(XMLSignatureInput xmlSignatureInput, Set inclusiveNamespaces) { this(xmlSignatureInput); this.inclusiveNamespaces = inclusiveNamespaces; } public String getHTMLRepresentation() throws XMLSignatureException { if (this.xpathNodeSet == null || this.xpathNodeSet.isEmpty()) { return "\n\n\nCaninical XML node set\n \n\n\n

Explanation of the output

\n

The following text contains the nodeset of the given Reference before it is canonicalized. There exist four different styles to indicate how a given node is treated.

\n
    \n
  • A node which is in the node set is labeled using the INCLUDED style.
  • \n
  • A node which is NOT in the node set is labeled EXCLUDED style.
  • \n
  • A namespace which is in the node set AND in the InclusiveNamespaces PrefixList is labeled using the INCLUDEDINCLUSIVENAMESPACE style.
  • \n
  • A namespace which is in NOT the node set AND in the InclusiveNamespaces PrefixList is labeled using the INCLUDEDINCLUSIVENAMESPACE style.
  • \n
\n

Output

\n
\nno node set, sorry
"; } Document ownerDocument = XMLUtils.getOwnerDocument((Node)this.xpathNodeSet.iterator().next()); try { this.writer = new StringWriter(); this.canonicalizeXPathNodeSet(ownerDocument); this.writer.close(); return this.writer.toString(); } catch (IOException ex) { throw new XMLSignatureException(ex); } finally { this.xpathNodeSet = null; this.writer = null; } } private void canonicalizeXPathNodeSet(Node node) throws XMLSignatureException, IOException { int nodeType = node.getNodeType(); switch (nodeType) { case 2: case 6: case 11: case 12: { throw new XMLSignatureException("empty", new Object[] { "An incorrect node was provided for c14n: " + nodeType }); } case 9: { this.writer.write("\n\n\nCaninical XML node set\n \n\n\n

Explanation of the output

\n

The following text contains the nodeset of the given Reference before it is canonicalized. There exist four different styles to indicate how a given node is treated.

\n
    \n
  • A node which is in the node set is labeled using the INCLUDED style.
  • \n
  • A node which is NOT in the node set is labeled EXCLUDED style.
  • \n
  • A namespace which is in the node set AND in the InclusiveNamespaces PrefixList is labeled using the INCLUDEDINCLUSIVENAMESPACE style.
  • \n
  • A namespace which is in NOT the node set AND in the InclusiveNamespaces PrefixList is labeled using the INCLUDEDINCLUSIVENAMESPACE style.
  • \n
\n

Output

\n
\n");
                for (Node node2 = node.getFirstChild(); node2 != null; node2 = node2.getNextSibling()) {
                    this.canonicalizeXPathNodeSet(node2);
                }
                this.writer.write("
"); break; } case 8: { if (this.xpathNodeSet.contains(node)) { this.writer.write(""); } else { this.writer.write(""); } int positionRelativeToDocumentElement = this.getPositionRelativeToDocumentElement(node); if (positionRelativeToDocumentElement == 1) { this.writer.write("\n"); } this.outputCommentToWriter((Comment)node); if (positionRelativeToDocumentElement == -1) { this.writer.write("\n"); } this.writer.write(""); break; } case 7: { if (this.xpathNodeSet.contains(node)) { this.writer.write(""); } else { this.writer.write(""); } int positionRelativeToDocumentElement2 = this.getPositionRelativeToDocumentElement(node); if (positionRelativeToDocumentElement2 == 1) { this.writer.write("\n"); } this.outputPItoWriter((ProcessingInstruction)node); if (positionRelativeToDocumentElement2 == -1) { this.writer.write("\n"); } this.writer.write(""); break; } case 3: case 4: { if (this.xpathNodeSet.contains(node)) { this.writer.write(""); } else { this.writer.write(""); } this.outputTextToWriter(node.getNodeValue()); for (Node node3 = node.getNextSibling(); node3 != null && (node3.getNodeType() == 3 || node3.getNodeType() == 4); node3 = node3.getNextSibling()) { this.outputTextToWriter(node3.getNodeValue()); } this.writer.write(""); break; } case 1: { Element element = (Element)node; if (this.xpathNodeSet.contains(node)) { this.writer.write(""); } else { this.writer.write(""); } this.writer.write("<"); this.writer.write(element.getTagName()); this.writer.write(""); NamedNodeMap attributes = element.getAttributes(); int length = attributes.getLength(); Attr[] a = new Attr[length]; for (int i = 0; i < length; ++i) { a[i] = (Attr)attributes.item(i); } Arrays.sort(a, XMLSignatureInputDebugger.ATTR_COMPARE); for (int j = 0; j < length; ++j) { Attr attr = (Attr)a[j]; int contains = this.xpathNodeSet.contains(attr); int contains2 = this.inclusiveNamespaces.contains(attr.getName()); if (!(contains == 0)) { if (!(contains2 == 0)) { this.writer.write(""); } else { this.writer.write(""); } } else if (!(contains2 == 0)) { this.writer.write(""); } else { this.writer.write(""); } this.outputAttrToWriter(attr.getNodeName(), attr.getNodeValue()); this.writer.write(""); } if (this.xpathNodeSet.contains(node)) { this.writer.write(""); } else { this.writer.write(""); } this.writer.write(">"); this.writer.write(""); for (Node node4 = node.getFirstChild(); node4 != null; node4 = node4.getNextSibling()) { this.canonicalizeXPathNodeSet(node4); } if (this.xpathNodeSet.contains(node)) { this.writer.write(""); } else { this.writer.write(""); } this.writer.write("</"); this.writer.write(element.getTagName()); this.writer.write(">"); this.writer.write(""); break; } } } private int getPositionRelativeToDocumentElement(Node nextSibling) { if (nextSibling == null) { return 0; } Document ownerDocument = nextSibling.getOwnerDocument(); if (nextSibling.getParentNode() != ownerDocument) { return 0; } Element documentElement = ownerDocument.getDocumentElement(); if (documentElement == null) { return 0; } if (documentElement == nextSibling) { return 0; } while (nextSibling != null) { if (nextSibling == documentElement) { return -1; } nextSibling = nextSibling.getNextSibling(); } return 1; } private void outputAttrToWriter(String str, String s) throws IOException { this.writer.write(" "); this.writer.write(str); this.writer.write("=\""); for (int length = s.length(), i = 0; i < length; ++i) { int char1 = s.charAt(i); switch (char1) { case 38: { this.writer.write("&amp;"); break; } case 60: { this.writer.write("&lt;"); break; } case 34: { this.writer.write("&quot;"); break; } case 9: { this.writer.write("&#x9;"); break; } case 10: { this.writer.write("&#xA;"); break; } case 13: { this.writer.write("&#xD;"); break; } default: { this.writer.write(char1); break; } } } this.writer.write("\""); } private void outputPItoWriter(ProcessingInstruction processingInstruction) throws IOException { if (processingInstruction == null) { return; } this.writer.write("<?"); String target = processingInstruction.getTarget(); for (int length = target.length(), i = 0; i < length; ++i) { int char1 = target.charAt(i); switch (char1) { case 13: { this.writer.write("&#xD;"); break; } case 32: { this.writer.write("·"); break; } case 10: { this.writer.write("¶\n"); break; } default: { this.writer.write(char1); break; } } } String data = processingInstruction.getData(); int length2 = data.length(); if (length2 > 0) { this.writer.write(" "); for (int j = 0; j < length2; ++j) { int char2 = data.charAt(j); switch (char2) { case 13: { this.writer.write("&#xD;"); break; } default: { this.writer.write(char2); break; } } } } this.writer.write("?>"); return; } private void outputCommentToWriter(Comment comment) throws IOException { if (comment == null) { return; } this.writer.write("<!--"); String data = comment.getData(); for (int length = data.length(), i = 0; i < length; ++i) { int char1 = data.charAt(i); switch (char1) { case 13: { this.writer.write("&#xD;"); break; } case 32: { this.writer.write("·"); break; } case 10: { this.writer.write("¶\n"); break; } default: { this.writer.write(char1); break; } } } this.writer.write("-->"); return; } private void outputTextToWriter(String s) throws IOException { if (s == null) { return; } for (int length = s.length(), i = 0; i < length; ++i) { int char1 = s.charAt(i); switch (char1) { case 38: { this.writer.write("&amp;"); break; } case 60: { this.writer.write("&lt;"); break; } case 62: { this.writer.write("&gt;"); break; } case 13: { this.writer.write("&#xD;"); break; } case 32: { this.writer.write("·"); break; } case 10: { this.writer.write("¶\n"); break; } default: { this.writer.write(char1); break; } } } return; } static { ATTR_COMPARE = new AttrCompare(); } }