[Practicalxml-commits] SF.net SVN: practicalxml:[61] trunk/src
Brought to you by:
kdgregory
|
From: Auto-Generated S. C. M. <pra...@li...> - 2008-12-28 00:46:56
|
Revision: 61
http://practicalxml.svn.sourceforge.net/practicalxml/?rev=61&view=rev
Author: kdgregory
Date: 2008-12-28 00:46:35 +0000 (Sun, 28 Dec 2008)
Log Message:
-----------
OutputUtil: add elementToString(), treeToString()
Modified Paths:
--------------
trunk/src/main/java/net/sf/practicalxml/OutputUtil.java
trunk/src/test/java/net/sf/practicalxml/TestOutputUtil.java
Modified: trunk/src/main/java/net/sf/practicalxml/OutputUtil.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/OutputUtil.java 2008-12-24 00:31:26 UTC (rev 60)
+++ trunk/src/main/java/net/sf/practicalxml/OutputUtil.java 2008-12-28 00:46:35 UTC (rev 61)
@@ -14,6 +14,7 @@
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
+import org.w3c.dom.Element;
/**
@@ -25,6 +26,31 @@
public class OutputUtil
{
/**
+ * A simple <code>toString()</code> for an element, using the format
+ * "<code>{<i>NSURI</i>}<i>LOCALNAME</i></code>"; if the element has no
+ * namespace, the brackets remain but are empty.
+ */
+ public static String elementToString(Element elem)
+ {
+ return appendElementString(new StringBuilder(256), elem).toString();
+ }
+
+
+ /**
+ * Debug dump of the tree rooted at the specified element. Each line holds
+ * one element,
+ *
+ *
+ * @param elem
+ * @param indent
+ */
+ public static String treeToString(Element elem, int indent)
+ {
+ return appendTreeString(new StringBuilder(1024), elem, indent, 0).toString();
+ }
+
+
+ /**
* Writes a DOM document to a simple string format, without a prologue or
* whitespace between elements.
* <p>
@@ -206,6 +232,41 @@
//----------------------------------------------------------------------------
/**
+ * The actual implementation of {@link #elementToString}, which appends
+ * the string format to a passed buffer. Returns the buffer as a
+ * convenience.
+ */
+ private static StringBuilder appendElementString(StringBuilder buf, Element elem)
+ {
+ String namespaceURI = elem.getNamespaceURI();
+ String localName = DomUtil.getLocalName(elem);
+
+ return buf.append("{")
+ .append((namespaceURI != null) ? namespaceURI : "")
+ .append("}")
+ .append(localName);
+ }
+
+ /**
+ * Actual implementation of <code>dumpTree</code>, using a passed buffer
+ * so that we're not doing lots of string concats
+ */
+ private static StringBuilder appendTreeString(StringBuilder buf, Element elem, int indent, int curIndent)
+ {
+ if (buf.length() > 0)
+ buf.append("\n");
+ for (int ii = 0 ; ii < curIndent ; ii++)
+ buf.append(" ");
+ appendElementString(buf, elem);
+ for (Element child : DomUtil.getChildren(elem))
+ {
+ appendTreeString(buf, child, indent, curIndent + indent);
+ }
+ return buf;
+ }
+
+
+ /**
* Flushes an <code>OutputStream</code>, wrapping exceptions.
*/
private static void flushStream(OutputStream stream)
Modified: trunk/src/test/java/net/sf/practicalxml/TestOutputUtil.java
===================================================================
--- trunk/src/test/java/net/sf/practicalxml/TestOutputUtil.java 2008-12-24 00:31:26 UTC (rev 60)
+++ trunk/src/test/java/net/sf/practicalxml/TestOutputUtil.java 2008-12-28 00:46:35 UTC (rev 61)
@@ -31,10 +31,30 @@
//----------------------------------------------------------------------------
-// Test Cases -- we're looking for overall structure, assume that the output
-// transform will do the right thing with the details
+// Test Cases -- in most of these tests we look for overall structure, assume
+// that the output transform will do the right thing
//----------------------------------------------------------------------------
+ public void testElementToString() throws Exception
+ {
+ Element root = DomUtil.newDocument("foo");
+ Element child1 = DomUtil.appendChild(root, "argle", "bargle");
+
+ assertEquals("{}foo", OutputUtil.elementToString(root));
+ assertEquals("{argle}bargle", OutputUtil.elementToString(child1));
+ }
+
+
+ public void testTreeToString() throws Exception
+ {
+ Element root = DomUtil.newDocument("foo");
+ Element child1 = DomUtil.appendChild(root, "argle", "bargle");
+
+ assertEquals("{}foo\n {argle}bargle", OutputUtil.treeToString(root, 2));
+ assertEquals("{argle}bargle", OutputUtil.treeToString(child1, 2));
+ }
+
+
public void testCompactStringSingleElement() throws Exception
{
Element root = DomUtil.newDocument(EL_ROOT);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|