FileDocCategorySizeDatePackage
XMLSignatureInputDebugger.javaAPI DocJava SE 6 API17877Tue Jun 10 00:23:04 BST 2008com.sun.org.apache.xml.internal.security.signature

XMLSignatureInputDebugger

public class XMLSignatureInputDebugger extends Object
Class XMLSignatureInputDebugger
author
$Author: raul $
version
$Revision: 1.3 $

Fields Summary
private Set
_xpathNodeSet
Field _xmlSignatureInput
private Set
_inclusiveNamespaces
private Document
_doc
Field _doc
private Writer
_writer
Field _writer
static final String
HTMLPrefix
The HTML Prefix*
static final String
HTMLSuffix
HTML Suffix *
static final String
HTMLExcludePrefix
static final String
HTMLExcludeSuffix
static final String
HTMLIncludePrefix
static final String
HTMLIncludeSuffix
static final String
HTMLIncludedInclusiveNamespacePrefix
static final String
HTMLIncludedInclusiveNamespaceSuffix
static final String
HTMLExcludedInclusiveNamespacePrefix
static final String
HTMLExcludedInclusiveNamespaceSuffix
private static final int
NODE_BEFORE_DOCUMENT_ELEMENT
private static final int
NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT
private static final int
NODE_AFTER_DOCUMENT_ELEMENT
static final AttrCompare
ATTR_COMPARE
Constructors Summary
private XMLSignatureInputDebugger()


	// J+
	  
		// do nothing
	
public XMLSignatureInputDebugger(XMLSignatureInput xmlSignatureInput)
Constructor XMLSignatureInputDebugger

param
xmlSignatureInput the signatur to pretty print


		if (!xmlSignatureInput.isNodeSet()) {
			this._xpathNodeSet = null;
		} else {
			this._xpathNodeSet = xmlSignatureInput._inputNodeSet;
		}
	
public XMLSignatureInputDebugger(XMLSignatureInput xmlSignatureInput, Set inclusiveNamespace)
Constructor XMLSignatureInputDebugger

param
xmlSignatureInput the signatur to pretty print
param
inclusiveNamespace


		this(xmlSignatureInput);

		this._inclusiveNamespaces = inclusiveNamespace;
	
Methods Summary
private voidcanonicalizeXPathNodeSet(org.w3c.dom.Node currentNode)
Method canonicalizeXPathNodeSet

param
currentNode
throws
XMLSignatureException
throws
IOException


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

		case Node.DOCUMENT_TYPE_NODE:
		default:
			break;

		case Node.ENTITY_NODE:
		case Node.NOTATION_NODE:
		case Node.DOCUMENT_FRAGMENT_NODE:
		case Node.ATTRIBUTE_NODE:
			throw new XMLSignatureException("empty");
		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");
			}

			if (this._xpathNodeSet.contains(currentNode)) {
				this._writer.write(HTMLIncludeSuffix);
			} else {
				this._writer.write(HTMLExcludeSuffix);
			}
			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");
			}

			if (this._xpathNodeSet.contains(currentNode)) {
				this._writer.write(HTMLIncludeSuffix);
			} else {
				this._writer.write(HTMLExcludeSuffix);
			}
			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());
			}

			if (this._xpathNodeSet.contains(currentNode)) {
				this._writer.write(HTMLIncludeSuffix);
			} else {
				this._writer.write(HTMLExcludeSuffix);
			}
			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());

			if (this._xpathNodeSet.contains(currentNode)) {
				this._writer.write(HTMLIncludeSuffix);
			} else {
				this._writer.write(HTMLExcludeSuffix);
			}

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

			for (int i = 0; i < attrsLength; i++) {
				attrs2[i] = 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());

				if (included) {
					if (inclusive) {

						// included and inclusive
						this._writer
								.write(HTMLIncludedInclusiveNamespaceSuffix);
					} else {

						// included and not inclusive
						this._writer.write(HTMLIncludeSuffix);
					}
				} else {
					if (inclusive) {

						// excluded and inclusive
						this._writer
								.write(HTMLExcludedInclusiveNamespaceSuffix);
					} else {

						// excluded and not inclusive
						this._writer.write(HTMLExcludeSuffix);
					}
				}
			}

			if (this._xpathNodeSet.contains(currentNode)) {
				this._writer.write(HTMLIncludePrefix);
			} else {
				this._writer.write(HTMLExcludePrefix);
			}

			this._writer.write(">");

			if (this._xpathNodeSet.contains(currentNode)) {
				this._writer.write(HTMLIncludeSuffix);
			} else {
				this._writer.write(HTMLExcludeSuffix);
			}

			// 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("</");
			this._writer.write(currentElement.getTagName());
			this._writer.write(">");

			if (this._xpathNodeSet.contains(currentNode)) {
				this._writer.write(HTMLIncludeSuffix);
			} else {
				this._writer.write(HTMLExcludeSuffix);
			}
			break;
		}
	
public java.lang.StringgetHTMLRepresentation()
Method getHTMLRepresentation

return
The HTML Representation.
throws
XMLSignatureException


		if ((this._xpathNodeSet == null) || (this._xpathNodeSet.size() == 0)) {
			return HTMLPrefix + "<blink>no node set, sorry</blink>"
					+ HTMLSuffix;
		}

		{

			// get only a single node as anchor to fetch the owner document
			Node n = (Node) this._xpathNodeSet.iterator().next();

			this._doc = XMLUtils.getOwnerDocument(n);
		}

		try {
			this._writer = new StringWriter();

			this.canonicalizeXPathNodeSet(this._doc);
			this._writer.close();

			return this._writer.toString();
		} catch (IOException ex) {
			throw new XMLSignatureException("empty", ex);
		} finally {
			this._xpathNodeSet = null;
			this._doc = null;
			this._writer = null;
		}
	
private intgetPositionRelativeToDocumentElement(org.w3c.dom.Node currentNode)
Checks whether a Comment or ProcessingInstruction is before or after the document element. This is needed for prepending or appending "\n"s.

param
currentNode comment or pi to check
return
NODE_BEFORE_DOCUMENT_ELEMENT, NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT or NODE_AFTER_DOCUMENT_ELEMENT
see
#NODE_BEFORE_DOCUMENT_ELEMENT
see
#NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT
see
#NODE_AFTER_DOCUMENT_ELEMENT


		if (currentNode == null) {
			return NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
		}

		Document doc = currentNode.getOwnerDocument();

		if (currentNode.getParentNode() != doc) {
			return NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
		}

		Element documentElement = doc.getDocumentElement();

		if (documentElement == null) {
			return NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
		}

		if (documentElement == currentNode) {
			return NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
		}

		for (Node x = currentNode; x != null; x = x.getNextSibling()) {
			if (x == documentElement) {
				return NODE_BEFORE_DOCUMENT_ELEMENT;
			}
		}

		return NODE_AFTER_DOCUMENT_ELEMENT;
	
private voidoutputAttrToWriter(java.lang.String name, java.lang.String value)
Normalizes an {@link Attr}ibute value The string value of the node is modified by replacing
  • all ampersands (&) with &amp;
  • all open angle brackets (<) with &lt;
  • all quotation mark characters with &quot;
  • and the whitespace characters #x9, #xA, and #xD, with character references. The character references are written in uppercase hexadecimal with no leading zeroes (for example, #xD is represented by the character reference &#xD;)

param
name
param
value
throws
IOException


		this._writer.write(" ");
		this._writer.write(name);
		this._writer.write("=\"");

		int length = value.length();

		for (int i = 0; i < length; i++) {
			char c = value.charAt(i);

			switch (c) {

			case '&":
				this._writer.write("&amp;");
				break;

			case '<":
				this._writer.write("&lt;");
				break;

			case '"":
				this._writer.write("&quot;");
				break;

			case 0x09: // '\t'
				this._writer.write("&#x9;");
				break;

			case 0x0A: // '\n'
				this._writer.write("&#xA;");
				break;

			case 0x0D: // '\r'
				this._writer.write("&#xD;");
				break;

			default:
				this._writer.write(c);
				break;
			}
		}

		this._writer.write("\"");
	
private voidoutputCommentToWriter(org.w3c.dom.Comment currentComment)
Method outputCommentToWriter

param
currentComment
throws
IOException


		if (currentComment == null) {
			return;
		}

		this._writer.write("<!--");

		String data = currentComment.getData();
		int length = data.length();

		for (int i = 0; i < length; i++) {
			char c = data.charAt(i);

			switch (c) {

			case 0x0D:
				this._writer.write("&#xD;");
				break;

			case ' ":
				this._writer.write("·");
				break;

			case '\n":
				this._writer.write("¶\n");
				break;

			default:
				this._writer.write(c);
				break;
			}
		}

		this._writer.write("-->");
	
private voidoutputPItoWriter(org.w3c.dom.ProcessingInstruction currentPI)
Normalizes a {@link org.w3c.dom.Comment} value

param
currentPI
throws
IOException


		if (currentPI == null) {
			return;
		}

		this._writer.write("<?");

		String target = currentPI.getTarget();
		int length = target.length();

		for (int i = 0; i < length; i++) {
			char c = target.charAt(i);

			switch (c) {

			case 0x0D:
				this._writer.write("&#xD;");
				break;

			case ' ":
				this._writer.write("·");
				break;

			case '\n":
				this._writer.write("¶\n");
				break;

			default:
				this._writer.write(c);
				break;
			}
		}

		String data = currentPI.getData();

		length = data.length();

		if ((data != null) && (length > 0)) {
			this._writer.write(" ");

			for (int i = 0; i < length; i++) {
				char c = data.charAt(i);

				switch (c) {

				case 0x0D:
					this._writer.write("&#xD;");
					break;

				default:
					this._writer.write(c);
					break;
				}
			}
		}

		this._writer.write("?>");
	
private voidoutputTextToWriter(java.lang.String text)
Method outputTextToWriter

param
text
throws
IOException


		if (text == null) {
			return;
		}

		int length = text.length();

		for (int i = 0; i < length; i++) {
			char c = text.charAt(i);

			switch (c) {

			case '&":
				this._writer.write("&amp;");
				break;

			case '<":
				this._writer.write("&lt;");
				break;

			case '>":
				this._writer.write("&gt;");
				break;

			case 0xD:
				this._writer.write("&#xD;");
				break;

			case ' ":
				this._writer.write("·");
				break;

			case '\n":
				this._writer.write("¶\n");
				break;

			default:
				this._writer.write(c);
				break;
			}
		}