[Practicalxml-commits] SF.net SVN: practicalxml:[68] trunk/src
Brought to you by:
kdgregory
From: Auto-Generated S. C. M. <pra...@li...> - 2008-12-30 13:50:02
|
Revision: 68 http://practicalxml.svn.sourceforge.net/practicalxml/?rev=68&view=rev Author: kdgregory Date: 2008-12-30 13:49:58 +0000 (Tue, 30 Dec 2008) Log Message: ----------- DomUtil.toList() Modified Paths: -------------- trunk/src/main/java/net/sf/practicalxml/DomUtil.java 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 2008-12-29 02:57:43 UTC (rev 67) +++ trunk/src/main/java/net/sf/practicalxml/DomUtil.java 2008-12-30 13:49:58 UTC (rev 68) @@ -440,6 +440,28 @@ /** + * Creates a paramaterized list from a <code>NodeList</code>, making it + * usable within the Java coding idiom. + * + * @param nodelist The list of nodes to convert. + * @param ofClass The type of the nodes being converted. + * + * @throws ClassCastException if any node in the list is not the expected + * type. + */ + public static <T> List<T> toList(NodeList nodelist, Class<T> ofClass) + { + int size = nodelist.getLength(); + List<T> result = new ArrayList<T>(size); + for (int ii = 0 ; ii < size ; ii++) + { + result.add(ofClass.cast(nodelist.item(ii))); + } + 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 @@ -697,6 +719,6 @@ if (sibling == elem) return elemPos; } - throw new IllegalArgumentException("element not amongs its siblings"); + throw new IllegalArgumentException("element not amongst its siblings"); } } Modified: trunk/src/test/java/net/sf/practicalxml/TestDomUtil.java =================================================================== --- trunk/src/test/java/net/sf/practicalxml/TestDomUtil.java 2008-12-29 02:57:43 UTC (rev 67) +++ trunk/src/test/java/net/sf/practicalxml/TestDomUtil.java 2008-12-30 13:49:58 UTC (rev 68) @@ -309,4 +309,18 @@ // success } } + + + public void testToList() throws Exception + { + Element root = DomUtil.newDocument("foo"); + Element child1 = DomUtil.appendChild(root, "foo"); + DomUtil.setText(root, "blah blah blah"); + Element child2 = DomUtil.appendChild(root, "foo"); + + List<Element> result = DomUtil.toList(root.getElementsByTagName("*"), Element.class); + assertEquals(2, result.size()); + assertSame(child1, result.get(0)); + assertSame(child2, result.get(1)); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |