[Practicalxml-commits] SF.net SVN: practicalxml:[134] trunk/src
Brought to you by:
kdgregory
From: Auto-Generated S. C. M. <pra...@li...> - 2009-09-16 21:29:23
|
Revision: 134 http://practicalxml.svn.sourceforge.net/practicalxml/?rev=134&view=rev Author: kdgregory Date: 2009-09-16 21:29:12 +0000 (Wed, 16 Sep 2009) Log Message: ----------- DomUtil: getChildren(), getChild(), trimTextRecursive(), and removeEmptyTextRecursive() now take any Node (so you can pass Document as well as Element) 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-16 20:46:30 UTC (rev 133) +++ trunk/src/main/java/net/sf/practicalxml/DomUtil.java 2009-09-16 21:29:12 UTC (rev 134) @@ -29,6 +29,7 @@ import org.w3c.dom.Text; import net.sf.practicalxml.internal.StringUtils; +import net.sf.practicalxml.util.NodeListIterator; import net.sf.practicalxml.xpath.NamespaceResolver; @@ -193,10 +194,11 @@ /** - * Returns all <code>Element</code> children of the passed element, in - * document order. + * Returns all <code>Element</code> children of the passed node, in + * document order. Will accept any node type, although only <code>Document + * </code> and <code>Element</code> make sense. */ - public static List<Element> getChildren(Element parent) + public static List<Element> getChildren(Node parent) { return filter(parent.getChildNodes(), Element.class); } @@ -209,7 +211,7 @@ * 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 lclName) + public static List<Element> getChildren(Node parent, String lclName) { List<Element> ret = getChildren(parent); Iterator<Element> itx = ret.iterator(); @@ -231,7 +233,7 @@ * 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) + public static List<Element> getChildren(Node parent, String nsUri, String lclName) { List<Element> ret = getChildren(parent); Iterator<Element> itx = ret.iterator(); @@ -249,7 +251,7 @@ * Returns the first child element with the given <em>localname</em>, * null if there are no such nodes. */ - public static Element getChild(Element parent, String lclName) + public static Element getChild(Node parent, String lclName) { List<Element> children = getChildren(parent, lclName); return (children.size() > 0) ? children.get(0) : null; @@ -260,7 +262,7 @@ * Returns the first child element with the given namespace and * local name, null if there are no such elements. */ - public static Element getChild(Element parent, String nsUri, String lclName) + public static Element getChild(Node parent, String nsUri, String lclName) { List<Element> children = getChildren(parent, nsUri, lclName); return (children.size() > 0) ? children.get(0) : null; @@ -346,12 +348,12 @@ * Removes leading and trailing whitespace from all descendent text * nodes. Will remove text nodes that trim to an empty string. */ - public static void trimTextRecursive(Element elem) + public static void trimTextRecursive(Node node) { - NodeList children = elem.getChildNodes(); - for (int ii = children.getLength() - 1 ; ii >= 0 ; ii--) + Iterator<Node> itx = new NodeListIterator(node.getChildNodes()); + while (itx.hasNext()) { - Node child = children.item(ii); + Node child = itx.next(); switch (child.getNodeType()) { case Node.ELEMENT_NODE : @@ -361,7 +363,7 @@ case Node.TEXT_NODE : String value = StringUtils.trimToEmpty(((Text)child).getData()); if (StringUtils.isEmpty(value)) - elem.removeChild(child); + itx.remove(); else ((Text)child).setData(value); break; @@ -378,12 +380,12 @@ * could be removed by by the parser if you had a DTD that specified * element-only content. */ - public static void removeEmptyTextRecursive(Element elem) + public static void removeEmptyTextRecursive(Node node) { - NodeList children = elem.getChildNodes(); - for (int ii = children.getLength() - 1 ; ii >= 0 ; ii--) + Iterator<Node> itx = new NodeListIterator(node.getChildNodes()); + while (itx.hasNext()) { - Node child = children.item(ii); + Node child = itx.next(); switch (child.getNodeType()) { case Node.ELEMENT_NODE : @@ -392,7 +394,7 @@ case Node.CDATA_SECTION_NODE : case Node.TEXT_NODE : if (StringUtils.isBlank(child.getNodeValue())) - elem.removeChild(child); + itx.remove(); break; default : // do nothing Modified: trunk/src/site/changes.xml =================================================================== --- trunk/src/site/changes.xml 2009-09-16 20:46:30 UTC (rev 133) +++ trunk/src/site/changes.xml 2009-09-16 21:29:12 UTC (rev 134) @@ -11,11 +11,16 @@ initial context </action> <action dev='kdgregory' type='add'> - DomUtil: Add filter(NodeList) + DomUtil: Add filter() </action> <action dev='kdgregory' type='update'> DomUtil: appendText() now returns the created node </action> + <action dev='kdgregory' type='update'> + DomUtil: getChildren(), getChild(), trimTextRecursive(), + and removeEmptyTextRecursive() now take any Node (so you + can pass Document as well as Element) + </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-16 20:46:30 UTC (rev 133) +++ trunk/src/test/java/net/sf/practicalxml/TestDomUtil.java 2009-09-16 21:29:12 UTC (rev 134) @@ -162,7 +162,7 @@ } - public void testGetChildren() throws Exception + public void testGetChildrenOfElement() throws Exception { Element root = DomUtil.newDocument("foo"); DomUtil.appendText(root, "bar"); @@ -197,6 +197,62 @@ } + + public void testGetChildrenOfDocument() throws Exception + { + Element root1 = DomUtil.newDocument("foo"); + Document dom1 = root1.getOwnerDocument(); + + assertSame(root1, DomUtil.getChild(dom1, "foo")); + assertNull(DomUtil.getChild(dom1, "bar")); + + List<Element> rslt1a = DomUtil.getChildren(dom1); + assertEquals(1, rslt1a.size()); + assertSame(root1, rslt1a.get(0)); + + List<Element> rslt1b = DomUtil.getChildren(dom1, "foo"); + assertEquals(1, rslt1b.size()); + assertSame(root1, rslt1b.get(0)); + + List<Element> rslt1c = DomUtil.getChildren(dom1, "bar"); + assertEquals(0, rslt1c.size()); + + // and now with namespaces + + Element root2 = DomUtil.newDocument("urn:bar", "foo"); + Document dom2 = root2.getOwnerDocument(); + + assertSame(root2, DomUtil.getChild(dom2, "urn:bar", "foo")); + assertNull(DomUtil.getChild(dom2, "urn:bar", "bar")); + + List<Element> rslt2a = DomUtil.getChildren(dom2); + assertEquals(1, rslt2a.size()); + assertSame(root2, rslt2a.get(0)); + + List<Element> rslt2b = DomUtil.getChildren(dom2, "urn:bar", "foo"); + assertEquals(1, rslt2b.size()); + assertSame(root2, rslt2b.get(0)); + + List<Element> rslt2c = DomUtil.getChildren(dom2, "urn:bar", "bar"); + assertEquals(0, rslt2c.size()); + + List<Element> rslt2d = DomUtil.getChildren(dom2, "bar"); + assertEquals(0, rslt2d.size()); + } + + + // ensure that we don't throw if we pass something dumb + public void testGetChildrenOfComment() throws Exception + { + Element root = DomUtil.newDocument("foo"); + Comment comment = root.getOwnerDocument().createComment("blah blah blah"); + root.appendChild(comment); + + List<Element> result = DomUtil.getChildren(comment); + assertEquals(0, result.size()); + } + + public void testAppendText() throws Exception { Element root = DomUtil.newDocument("root"); @@ -284,7 +340,7 @@ assertEquals(TEXT1, child1.getTextContent()); assertEquals(TEXT2_WS, child2.getTextContent()); - DomUtil.trimTextRecursive(root); + DomUtil.trimTextRecursive(root.getOwnerDocument()); assertEquals(TEXT1, child1.getTextContent()); assertEquals("", child2.getTextContent()); assertEquals(0, child2.getChildNodes().getLength()); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |