From: <bo...@us...> - 2010-05-19 16:18:18
|
Revision: 395 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=395&view=rev Author: bodewig Date: 2010-05-19 16:18:11 +0000 (Wed, 19 May 2010) Log Message: ----------- convert from (I)Source to DOm Document Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java trunk/xmlunit/src/main/net-core/input/DOMSource.cs trunk/xmlunit/src/main/net-core/util/Convert.cs trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/util/ConvertTest.java Added Paths: ----------- 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 10:26:31 UTC (rev 394) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java 2010-05-19 16:18:11 UTC (rev 395) @@ -23,14 +23,19 @@ import java.util.Map; import javax.xml.XMLConstants; import javax.xml.namespace.NamespaceContext; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import net.sf.xmlunit.exceptions.ConfigurationException; import net.sf.xmlunit.exceptions.XMLUnitException; +import org.w3c.dom.Document; +import org.w3c.dom.Node; import org.xml.sax.InputSource; /** @@ -67,6 +72,78 @@ } /** + * Creates a DOM Document from a TraX Source. + * + * <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 + * by the default DocumentBuilderFactory) will be used to read the + * source. This may involve an XSLT identity transform in + * toInputSource.</p> + */ + public static Document toDocument(Source s) { + Document d = tryExtractDocFromDOMSource(s); + return d != null ? d + : toDocument(s, DocumentBuilderFactory.newInstance()); + } + + /** + * Creates a DOM Document from a TraX Source. + * + * <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 + * by given DocumentBuilderFactory) will be used to read the + * source. This may involve an XSLT identity transform in + * toInputSource.</p> + */ + public static Document toDocument(Source s, + DocumentBuilderFactory factory) { + Document d = tryExtractDocFromDOMSource(s); + if (d == null) { + InputSource is = toInputSource(s); + DocumentBuilder b = null; + + // yes, there is a race condition but it is so unlikely to + // happen that I currently don't care enough + boolean oldNsAware = factory.isNamespaceAware(); + try { + if (!oldNsAware) { + factory.setNamespaceAware(true); + } + b = factory.newDocumentBuilder(); + } catch (javax.xml.parsers.ParserConfigurationException e) { + throw new ConfigurationException(e); + } finally { + if (!oldNsAware) { + factory.setNamespaceAware(false); + } + } + + try { + d = b.parse(is); + } catch (org.xml.sax.SAXException e) { + throw new XMLUnitException(e); + } catch (java.io.IOException e) { + throw new XMLUnitException(e); + } + } + return d; + } + + private static Document tryExtractDocFromDOMSource(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 null; + } + + /** * Creates a JAXP NamespaceContext from a Map prefix => Namespace URI. */ public static NamespaceContext Modified: trunk/xmlunit/src/main/net-core/input/DOMSource.cs =================================================================== --- trunk/xmlunit/src/main/net-core/input/DOMSource.cs 2010-05-19 10:26:31 UTC (rev 394) +++ trunk/xmlunit/src/main/net-core/input/DOMSource.cs 2010-05-19 16:18:11 UTC (rev 395) @@ -18,8 +18,19 @@ /// ISource implementation encapsulating a DOM Node. /// </summary> public class DOMSource : AbstractSource { + private readonly XmlNode node; public DOMSource(XmlNode node) : base(new XmlNodeReader(node)) { + this.node = node; } + + /// <summary> + /// The node this source is wrapping + /// </summary> + public XmlNode Node { + get { + return node; + } + } } } Modified: trunk/xmlunit/src/main/net-core/util/Convert.cs =================================================================== --- trunk/xmlunit/src/main/net-core/util/Convert.cs 2010-05-19 10:26:31 UTC (rev 394) +++ trunk/xmlunit/src/main/net-core/util/Convert.cs 2010-05-19 16:18:11 UTC (rev 395) @@ -14,6 +14,7 @@ using System.Collections.Generic; using System.Xml; +using net.sf.xmlunit.input; namespace net.sf.xmlunit.util { /// <summary> @@ -23,6 +24,22 @@ private Convert() { } /// <summary> + /// Creates a DOM Document from an ISource. + /// </summary> + public static XmlDocument ToDocument(ISource s) { + DOMSource ds = s as DOMSource; + if (ds != null) { + XmlDocument doc = ds.Node as XmlDocument; + if (doc != null) { + return doc; + } + } + XmlDocument d = new XmlDocument(); + d.Load(s.Reader); + return d; + } + + /// <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 10:26:31 UTC (rev 394) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/util/ConvertTest.java 2010-05-19 16:18:11 UTC (rev 395) @@ -27,33 +27,60 @@ import org.w3c.dom.Document; import org.xml.sax.InputSource; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; import static org.hamcrest.core.Is.is; public class ConvertTest { - private static void convertAndAssert(Source s) throws Exception { + private static void convertToInputSourceAndAssert(Source s) + throws Exception { DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document d = b.parse(Convert.toInputSource(s)); + documentAsserts(d); + } + + private static void documentAsserts(Document d) { assertThat(d, IsNull.notNullValue()); assertThat(d.getDocumentElement().getTagName(), is("animal")); } @Test public void streamSourceToInputSource() throws Exception { - convertAndAssert(new StreamSource(new File(TestResources.ANIMAL_FILE))); + convertToInputSourceAndAssert(new StreamSource(new File(TestResources.ANIMAL_FILE))); } @Test public void domSourceToInputSource() throws Exception { DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document d = b.parse(new File(TestResources.ANIMAL_FILE)); - convertAndAssert(new DOMSource(d)); + convertToInputSourceAndAssert(new DOMSource(d)); } @Test public void saxSourceToInputSource() throws Exception { InputSource s = new InputSource(new FileInputStream(TestResources.ANIMAL_FILE)); - convertAndAssert(new SAXSource(s)); + convertToInputSourceAndAssert(new SAXSource(s)); } + private static void convertToDocumentAndAssert(Source s) { + documentAsserts(Convert.toDocument(s)); + } + + @Test public void streamSourceToDocument() throws Exception { + convertToDocumentAndAssert(new StreamSource(new File(TestResources.ANIMAL_FILE))); + } + + @Test public void domSourceToDocument() throws Exception { + DocumentBuilder b = + DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document d = b.parse(new File(TestResources.ANIMAL_FILE)); + convertToDocumentAndAssert(new DOMSource(d)); + assertSame(d, Convert.toDocument(new DOMSource(d))); + } + + @Test public void saxSourceToDocument() throws Exception { + InputSource s = new InputSource(new FileInputStream(TestResources.ANIMAL_FILE)); + convertToDocumentAndAssert(new SAXSource(s)); + } + } Added: trunk/xmlunit/src/tests/net-core/util/ConvertTest.cs =================================================================== --- trunk/xmlunit/src/tests/net-core/util/ConvertTest.cs (rev 0) +++ trunk/xmlunit/src/tests/net-core/util/ConvertTest.cs 2010-05-19 16:18:11 UTC (rev 395) @@ -0,0 +1,41 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +using System.Xml; +using NUnit.Framework; +using net.sf.xmlunit.input; + +namespace net.sf.xmlunit.util { + [TestFixture] + public class ConvertTest { + private static void ConvertToDocumentAndAssert(ISource s) { + XmlDocument d = Convert.ToDocument(s); + Assert.IsNotNull(d); + Assert.AreEqual("animal", d.DocumentElement.Name); + } + + [Test] + public void StreamSourceToDocument() { + ConvertToDocumentAndAssert(new StreamSource(TestResources.ANIMAL_FILE)); + } + + [Test] + public void DomSourceToDocument() { + XmlDocument d = new XmlDocument(); + d.Load(TestResources.ANIMAL_FILE); + ConvertToDocumentAndAssert(new DOMSource(d)); + Assert.AreSame(d, Convert.ToDocument(new DOMSource(d))); + } + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/tests/net-core/util/ConvertTest.cs ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |