[Practicalxml-commits] SF.net SVN: practicalxml:[133] trunk/src
Brought to you by:
kdgregory
|
From: Auto-Generated S. C. M. <pra...@li...> - 2009-09-16 20:46:44
|
Revision: 133
http://practicalxml.svn.sourceforge.net/practicalxml/?rev=133&view=rev
Author: kdgregory
Date: 2009-09-16 20:46:30 +0000 (Wed, 16 Sep 2009)
Log Message:
-----------
DomUtil:
- added filter(), use it internally
- appendText() now returns the created node
Modified Paths:
--------------
trunk/src/main/java/net/sf/practicalxml/DomUtil.java
trunk/src/site/changes.xml
trunk/src/test/java/net/sf/practicalxml/TestDomUtil.java
Modified: trunk/src/main/java/net/sf/practicalxml/DomUtil.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/DomUtil.java 2009-09-15 18:37:09 UTC (rev 132)
+++ trunk/src/main/java/net/sf/practicalxml/DomUtil.java 2009-09-16 20:46:30 UTC (rev 133)
@@ -15,6 +15,7 @@
package net.sf.practicalxml;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
@@ -197,17 +198,7 @@
*/
public static List<Element> getChildren(Element parent)
{
- List<Element> ret = new ArrayList<Element>();
- NodeList children = parent.getChildNodes();
- for (int ii = 0 ; ii < children.getLength() ; ii++)
- {
- Node child = children.item(ii);
- if (child instanceof Element)
- {
- ret.add((Element)child);
- }
- }
- return ret;
+ return filter(parent.getChildNodes(), Element.class);
}
@@ -216,45 +207,39 @@
* <em>localname</em>, ignoring namespace.
* <p>
* Returns the children in document order. Returns an empty list if
- * there are no children matching the specified name.
+ * there are no children matching the specified namespace/name.
*/
public static List<Element> getChildren(Element parent, String lclName)
{
- List<Element> ret = new ArrayList<Element>();
- NodeList children = parent.getChildNodes();
- for (int ii = 0 ; ii < children.getLength() ; ii++)
+ List<Element> ret = getChildren(parent);
+ Iterator<Element> itx = ret.iterator();
+ while (itx.hasNext())
{
- Node child = children.item(ii);
- if ((child instanceof Element)
- && (lclName.equals(getLocalName((Element)child))))
- {
- ret.add((Element)child);
- }
+ Element child = itx.next();
+ if (!lclName.equals(getLocalName((Element)child)))
+ itx.remove();
}
return ret;
}
/**
- * Returns the children of the passed element that have the given
- * namespace and <em>localname</em> (ignoring prefix). Namespace may
- * be <code>null</code>, in which case the child element must not
- * have a namespace.
+ * Returns the children of the passed element that have the given namespace
+ * and localname (ignoring prefix). Namespace may be <code>null</code>, in
+ * which case the child element must not have a namespace.
* <p>
* Returns the children in document order. Returns an empty list if
* there are no children matching the specified namespace/name.
*/
public static List<Element> getChildren(Element parent, String nsUri, String lclName)
{
- List<Element> ret = new ArrayList<Element>();
- NodeList children = parent.getChildNodes();
- for (int ii = 0 ; ii < children.getLength() ; ii++)
+ List<Element> ret = getChildren(parent);
+ Iterator<Element> itx = ret.iterator();
+ while (itx.hasNext())
{
- Node child = children.item(ii);
- if ((child instanceof Element) && isNamed((Element)child, nsUri, lclName))
- {
- ret.add((Element)child);
- }
+ Element child = itx.next();
+ if (!isNamed(child, nsUri, lclName))
+ itx.remove();
}
return ret;
}
@@ -283,13 +268,13 @@
/**
- * Returns text that is an immediate child of this node. This is
- * unlike <code>Node.getTextContent()</code>, which returns all
- * descendent text for the element.
+ * Returns the concatenation of all text and CDATA nodes that are immediate
+ * children of the passed node. If there are no text/CDATA nodes, returns
+ * <code>null</code>.
* <p>
- * Concatenates all text and CDATA nodes, does not trim whitespace.
- * If there are no text nodes, returns <code>null</code>; if all
- * text nodes contain empty strings, returns an empty string.
+ * This method differs from <code>Node.getTextContent()</code> in two ways:
+ * the latter concatenates all descendent text nodes, and will return an
+ * empty string (rather than <code>null</code>) if there are none.
*/
public static String getText(Element elem)
{
@@ -309,7 +294,6 @@
break;
default :
// do nothing
-
}
}
@@ -321,10 +305,11 @@
* Appends the specified text as a new text node on the specified
* element.
*/
- public static void appendText(Element elem, String text)
+ public static Text appendText(Element elem, String text)
{
Text child = elem.getOwnerDocument().createTextNode(text);
elem.appendChild(child);
+ return child;
}
@@ -453,7 +438,7 @@
/**
- * Creates a paramaterized list from a <code>NodeList</code>, making it
+ * Creates a parameterized list from a <code>NodeList</code>, making it
* usable within the Java coding idiom.
*
* @param nodelist The list of nodes to convert.
@@ -475,6 +460,26 @@
/**
+ * Extracts all nodes of a given type from the passed NodeList, creating
+ * a Java list of the nodes in document order.
+ *
+ * @param list The source list, which may contain any node type.
+ * @param klass The desired node type to extract from this list.
+ */
+ public static <T> List<T> filter(NodeList list, Class<T> klass)
+ {
+ ArrayList<T> result = new ArrayList<T>();
+ for (int ii = 0 ; ii < list.getLength() ; ii++)
+ {
+ Node node = list.item(ii);
+ if (klass.isInstance(node))
+ result.add(klass.cast(node));
+ }
+ return result;
+ }
+
+
+ /**
* Returns the path from the root of the document to the specified
* element, consisting of each node's qualified name, separated by
* slashes. Accepts an arbitrary number of attribute names, and
Modified: trunk/src/site/changes.xml
===================================================================
--- trunk/src/site/changes.xml 2009-09-15 18:37:09 UTC (rev 132)
+++ trunk/src/site/changes.xml 2009-09-16 20:46:30 UTC (rev 133)
@@ -5,11 +5,17 @@
<body>
<release version="1.0.5" date="2009-09-15"
- description="DomAsserts">
+ description="DomAsserts, DomUtil">
<action dev='kdgregory' type='update'>
DomAsserts: XPath assertions now take any node as
initial context
</action>
+ <action dev='kdgregory' type='add'>
+ DomUtil: Add filter(NodeList)
+ </action>
+ <action dev='kdgregory' type='update'>
+ DomUtil: appendText() now returns the created node
+ </action>
</release>
<release version="1.0.4" date="2009-09-10"
Modified: trunk/src/test/java/net/sf/practicalxml/TestDomUtil.java
===================================================================
--- trunk/src/test/java/net/sf/practicalxml/TestDomUtil.java 2009-09-15 18:37:09 UTC (rev 132)
+++ trunk/src/test/java/net/sf/practicalxml/TestDomUtil.java 2009-09-16 20:46:30 UTC (rev 133)
@@ -16,8 +16,11 @@
import java.util.List;
+import org.w3c.dom.CDATASection;
+import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
@@ -107,33 +110,6 @@
}
- public void testGetSetAppendText() throws Exception
- {
- String t1 = "argle";
- String t2 = "bargle";
- String t3 = "wargle";
-
- Element root = DomUtil.newDocument("foo");
- assertNull(DomUtil.getText(root));
-
- DomUtil.setText(root, t1);
- assertEquals(t1, DomUtil.getText(root));
-
- DomUtil.appendText(root, t2);
- assertEquals(t1 + t2, DomUtil.getText(root));
-
- DomUtil.setText(root, t3);
- assertEquals(t3, DomUtil.getText(root));
-
- Element child = DomUtil.appendChildInheritNamespace(root, "bar");
- assertNull(DomUtil.getText(child));
-
- DomUtil.appendText(child, t1);
- assertEquals(t1, DomUtil.getText(child));
- assertEquals(t3, DomUtil.getText(root));
- }
-
-
public void testGetLocalName() throws Exception
{
Element root = DomUtil.newDocument("foo");
@@ -221,39 +197,70 @@
}
+ public void testAppendText() throws Exception
+ {
+ Element root = DomUtil.newDocument("root");
+ Text text = DomUtil.appendText(root, "blah blah");
+ assertSame(text, root.getChildNodes().item(0));
+ }
+
+
public void testGetText() throws Exception
{
+ String t1 = "argle";
+ String t2 = "bargle";
+ String t3 = "wargle";
+
Element root = DomUtil.newDocument("foo");
- Text rootText1 = root.getOwnerDocument().createTextNode("argle");
- root.appendChild(rootText1);
- Element child = root.getOwnerDocument().createElement("bargle");
- root.appendChild(child);
- Text rootText2 = root.getOwnerDocument().createTextNode("wargle");
- root.appendChild(rootText2);
- Text childText = root.getOwnerDocument().createTextNode("zippy");
- child.appendChild(childText);
+ assertNull(DomUtil.getText(root));
- assertEquals("arglewargle", DomUtil.getText(root));
+ DomUtil.appendText(root, t1);
+ DomUtil.appendText(root, t2);
+ assertEquals(t1 + t2, DomUtil.getText(root));
+
+ Element child = DomUtil.appendChild(root, "bar");
+ DomUtil.appendText(child, t3);
+ assertEquals(t1 + t2 + t3, root.getTextContent());
+ assertEquals(t1 + t2, DomUtil.getText(root));
+ assertEquals(t3, DomUtil.getText(child));
}
+ // this test is just here for coverage
+ public void testGetSetTextWithCData() throws Exception
+ {
+ String t1 = "argle";
+ String t2 = "bargle";
+
+ Element root = DomUtil.newDocument("root");
+ Document dom = root.getOwnerDocument();
+ CDATASection cdata = dom.createCDATASection(t1);
+ root.appendChild(cdata);
+
+ assertEquals(t1, DomUtil.getText(root));
+ assertEquals(t1, root.getTextContent());
+
+ DomUtil.setText(root, t2);
+ assertEquals(t2, DomUtil.getText(root));
+ assertEquals(t2, root.getTextContent());
+ }
+
+
public void testSetText() throws Exception
{
+ String t1 = "argle";
+ String t2 = "bargle";
+ String t3 = "wargle";
+
Element root = DomUtil.newDocument("foo");
- Text rootText1 = root.getOwnerDocument().createTextNode("argle");
- root.appendChild(rootText1);
- Element child = root.getOwnerDocument().createElement("bargle");
- root.appendChild(child);
- Text rootText2 = root.getOwnerDocument().createTextNode("wargle");
- root.appendChild(rootText2);
- Text childText = root.getOwnerDocument().createTextNode("zippy");
- child.appendChild(childText);
+ Element child = DomUtil.appendChild(root, "bar");
+ DomUtil.appendText(root, t1);
+ DomUtil.appendText(child, t2);
- DomUtil.setText(root, "bar");
- assertEquals(2, root.getChildNodes().getLength());
- assertSame(child, root.getChildNodes().item(0));
- assertEquals("bar", root.getChildNodes().item(1).getTextContent());
- assertEquals("zippy", child.getTextContent());
+ DomUtil.setText(root, t3);
+ assertEquals(t3, DomUtil.getText(root));
+ assertEquals(t2, DomUtil.getText(child));
+ assertEquals(t2 + t3, root.getTextContent());
}
@@ -337,4 +344,40 @@
assertSame(child1, result.get(0));
assertSame(child2, result.get(1));
}
+
+
+ public void testFilterNodeList() throws Exception
+ {
+ Element root = DomUtil.newDocument("foo");
+ Document dom = root.getOwnerDocument();
+ Text child1 = dom.createTextNode("argle");
+ root.appendChild(child1);
+ Element child2 = dom.createElement("bar");
+ root.appendChild(child2);
+ Comment child3 = dom.createComment("blah blah blah");
+ root.appendChild(child3);
+ Text child4 = dom.createTextNode("wargle");
+ root.appendChild(child4);
+ Element child5 = dom.createElement("baz");
+ root.appendChild(child5);
+ NodeList list = root.getChildNodes();
+
+ List<Element> elems = DomUtil.filter(list, Element.class);
+ assertEquals(2, elems.size());
+ assertSame(child2, elems.get(0));
+ assertSame(child5, elems.get(1));
+
+ List<Text> texts = DomUtil.filter(list, Text.class);
+ assertEquals(2, texts.size());
+ assertSame(child1, texts.get(0));
+ assertSame(child4, texts.get(1));
+
+ List<Comment> comments = DomUtil.filter(list, Comment.class);
+ assertEquals(1, comments.size());
+ assertSame(child3, comments.get(0));
+
+ List<Document> docs = DomUtil.filter(list, Document.class);
+ assertEquals(0, docs.size());
+ }
+
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|