From: <bo...@us...> - 2010-05-20 11:20:44
|
Revision: 396 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=396&view=rev Author: bodewig Date: 2010-05-20 11:20:38 +0000 (Thu, 20 May 2010) Log Message: ----------- convert a(n) (I)Source to a DOM Node Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java trunk/xmlunit/src/main/net-core/util/Convert.cs trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/util/ConvertTest.java trunk/xmlunit/src/tests/net-core/util/ConvertTest.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java 2010-05-19 16:18:11 UTC (rev 395) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java 2010-05-20 11:20:38 UTC (rev 396) @@ -76,7 +76,7 @@ * * <p>If the source is a {@link DOMSource} holding a Document * Node, this one will be returned. Otherwise {@link - * toInputSource} and a namespace aware DocumentBuilder (created + * #toInputSource} and a namespace aware DocumentBuilder (created * by the default DocumentBuilderFactory) will be used to read the * source. This may involve an XSLT identity transform in * toInputSource.</p> @@ -92,7 +92,7 @@ * * <p>If the source is a {@link DOMSource} holding a Document * Node, this one will be returned. Otherwise {@link - * toInputSource} and a namespace aware DocumentBuilder (created + * #toInputSource} and a namespace aware DocumentBuilder (created * by given DocumentBuilderFactory) will be used to read the * source. This may involve an XSLT identity transform in * toInputSource.</p> @@ -132,13 +132,42 @@ } private static Document tryExtractDocFromDOMSource(Source s) { + Node n = tryExtractNodeFromDOMSource(s); + if (n != null && n instanceof Document) { + @SuppressWarnings("unchecked") Document d = (Document) n; + return d; + } + return null; + } + + /** + * Creates a DOM Node from a TraX Source. + * + * <p>If the source is a {@link DOMSource} its Node will be + * returned, otherwise this delegates to {@link #toDocument}.</p> + */ + public static Node toNode(Source s) { + Node n = tryExtractNodeFromDOMSource(s); + return n != null ? n + : toDocument(s, DocumentBuilderFactory.newInstance()); + } + + /** + * Creates a DOM Node from a TraX Source. + * + * <p>If the source is a {@link DOMSource} its Node will be + * returned, otherwise this delegates to {@link #toDocument}.</p> + */ + public static Node toNode(Source s, + DocumentBuilderFactory factory) { + Node n = tryExtractNodeFromDOMSource(s); + return n != null ? n : toDocument(s, factory); + } + + private static Node tryExtractNodeFromDOMSource(Source s) { if (s instanceof DOMSource) { @SuppressWarnings("unchecked") DOMSource ds = (DOMSource) s; - Node n = ds.getNode(); - if (n instanceof Document) { - @SuppressWarnings("unchecked") Document d = (Document) n; - return d; - } + return ds.getNode(); } return null; } Modified: trunk/xmlunit/src/main/net-core/util/Convert.cs =================================================================== --- trunk/xmlunit/src/main/net-core/util/Convert.cs 2010-05-19 16:18:11 UTC (rev 395) +++ trunk/xmlunit/src/main/net-core/util/Convert.cs 2010-05-20 11:20:38 UTC (rev 396) @@ -40,6 +40,18 @@ } /// <summary> + /// Creates a DOM Node from an ISource. + /// </summary> + /// <remarks> + /// Unless the source is a DOMSource this will return the same + /// result as ToDocument. + /// </remarks> + public static XmlNode ToNode(ISource s) { + DOMSource ds = s as DOMSource; + return ds != null ? ds.Node : ToDocument(s); + } + + /// <summary> /// Creates a namespace resolver from a Map prefix => /// Namespace URI. /// </summary> Modified: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/util/ConvertTest.java =================================================================== --- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/util/ConvertTest.java 2010-05-19 16:18:11 UTC (rev 395) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/util/ConvertTest.java 2010-05-20 11:20:38 UTC (rev 396) @@ -25,6 +25,7 @@ import org.hamcrest.core.IsNull; import org.junit.Test; import org.w3c.dom.Document; +import org.w3c.dom.Node; import org.xml.sax.InputSource; import static org.junit.Assert.assertSame; @@ -83,4 +84,27 @@ convertToDocumentAndAssert(new SAXSource(s)); } + private static void convertToNodeAndAssert(Source s) { + Node n = Convert.toNode(s); + Document d = n instanceof Document ? (Document) n : n.getOwnerDocument(); + documentAsserts(d); + } + + @Test public void streamSourceToNode() throws Exception { + convertToNodeAndAssert(new StreamSource(new File(TestResources.ANIMAL_FILE))); + } + + @Test public void domSourceToNode() throws Exception { + DocumentBuilder b = + DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document d = b.parse(new File(TestResources.ANIMAL_FILE)); + convertToNodeAndAssert(new DOMSource(d)); + assertSame(d, Convert.toNode(new DOMSource(d))); + } + + @Test public void saxSourceToNode() throws Exception { + InputSource s = new InputSource(new FileInputStream(TestResources.ANIMAL_FILE)); + convertToNodeAndAssert(new SAXSource(s)); + } + } Modified: trunk/xmlunit/src/tests/net-core/util/ConvertTest.cs =================================================================== --- trunk/xmlunit/src/tests/net-core/util/ConvertTest.cs 2010-05-19 16:18:11 UTC (rev 395) +++ trunk/xmlunit/src/tests/net-core/util/ConvertTest.cs 2010-05-20 11:20:38 UTC (rev 396) @@ -20,7 +20,10 @@ [TestFixture] public class ConvertTest { private static void ConvertToDocumentAndAssert(ISource s) { - XmlDocument d = Convert.ToDocument(s); + DocumentAsserts(Convert.ToDocument(s)); + } + + private static void DocumentAsserts(XmlDocument d) { Assert.IsNotNull(d); Assert.AreEqual("animal", d.DocumentElement.Name); } @@ -37,5 +40,24 @@ ConvertToDocumentAndAssert(new DOMSource(d)); Assert.AreSame(d, Convert.ToDocument(new DOMSource(d))); } + + private static void ConvertToNodeAndAssert(ISource s) { + XmlNode n = Convert.ToNode(s); + DocumentAsserts(n is XmlDocument + ? n as XmlDocument : n.OwnerDocument); + } + + [Test] + public void StreamSourceToNode() { + ConvertToNodeAndAssert(new StreamSource(TestResources.ANIMAL_FILE)); + } + + [Test] + public void DomSourceToNode() { + XmlDocument d = new XmlDocument(); + d.Load(TestResources.ANIMAL_FILE); + ConvertToNodeAndAssert(new DOMSource(d)); + Assert.AreSame(d, Convert.ToNode(new DOMSource(d))); + } } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |