From: <bo...@us...> - 2010-08-31 10:15:07
|
Revision: 443 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=443&view=rev Author: bodewig Date: 2010-08-31 10:15:01 +0000 (Tue, 31 Aug 2010) Log Message: ----------- a source that is obtained by stripping comments from a different source Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/input/ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/input/CommentLessSource.java trunk/xmlunit/src/main/net-core/input/CommentLessSource.cs trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/input/ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/input/CommentLessSourceTest.java trunk/xmlunit/src/tests/net-core/input/ trunk/xmlunit/src/tests/net-core/input/CommentLessSourceTest.cs Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/input/CommentLessSource.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/input/CommentLessSource.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/input/CommentLessSource.java 2010-08-31 10:15:01 UTC (rev 443) @@ -0,0 +1,48 @@ +/* + 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. +*/ +package net.sf.xmlunit.input; + +import javax.xml.transform.Source; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamSource; + +import net.sf.xmlunit.transform.Transformation; + +/** + * A source that is obtained from a different source by stripping all + * comments. + */ +public final class CommentLessSource extends DOMSource { + + public CommentLessSource(Source originalSource) { + super(); + if (originalSource == null) { + throw new IllegalArgumentException("source must not be null"); + } + Transformation t = new Transformation(originalSource); + t.setStylesheet(getStylesheet()); + setNode(t.transformToDocument()); + } + + private static final String STYLE = + "<stylesheet xmlns=\"http://www.w3.org/1999/XSL/Transform\">" + + "<template match=\"node()[not(self::comment())]|@*\"><copy>" + + "<apply-templates select=\"node()[not(self::comment())]|@*\"/>" + + "</copy></template>" + + "</stylesheet>"; + + private static Source getStylesheet() { + return new StreamSource(new java.io.StringReader(STYLE)); + } +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/input/CommentLessSource.java ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/net-core/input/CommentLessSource.cs =================================================================== --- trunk/xmlunit/src/main/net-core/input/CommentLessSource.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/input/CommentLessSource.cs 2010-08-31 10:15:01 UTC (rev 443) @@ -0,0 +1,67 @@ +/* + 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; +using System.Xml; +using net.sf.xmlunit.transform; + +namespace net.sf.xmlunit.input { + + /// <summary> + /// ISource implementation that is obtained from a different + /// source by stripping all comments. + /// </summary> + public sealed class CommentLessSource : ISource { + private readonly XmlReader reader; + private string systemId; + + public CommentLessSource(ISource originalSource) { + if (originalSource == null) { + throw new ArgumentNullException(); + } + systemId = originalSource.SystemId; + + Transformation t = new Transformation(originalSource); + t.Stylesheet = Stylesheet; + reader = new XmlNodeReader(t.TransformToDocument()); + } + + public XmlReader Reader { + get { + return reader; + } + } + public string SystemId { + get { + return systemId; + } + set { + systemId = value; + } + } + + private const string STYLE = + "<stylesheet xmlns=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" + + "<template match=\"node()[not(self::comment())]|@*\"><copy>" + + "<apply-templates select=\"node()[not(self::comment())]|@*\"/>" + + "</copy></template>" + + "</stylesheet>"; + + private static ISource Stylesheet { + get { + return new StreamSource(new System.IO.StringReader(STYLE)); + } + } + } +} Property changes on: trunk/xmlunit/src/main/net-core/input/CommentLessSource.cs ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/input/CommentLessSourceTest.java =================================================================== --- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/input/CommentLessSourceTest.java (rev 0) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/input/CommentLessSourceTest.java 2010-08-31 10:15:01 UTC (rev 443) @@ -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. +*/ +package net.sf.xmlunit.input; + +import java.io.StringReader; +import javax.xml.transform.stream.StreamSource; +import net.sf.xmlunit.util.Convert; +import org.junit.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import static org.junit.Assert.*; + +public class CommentLessSourceTest { + + @Test public void stripCommentsAtDifferentLevels() { + StreamSource s = + new StreamSource(new StringReader("<?xml version='1.0'?>" + + "<!-- comment 1 -->" + + "<foo>" + + "<!-- comment 2 -->" + + "</foo>")); + CommentLessSource cls = new CommentLessSource(s); + Document d = Convert.toDocument(cls); + assertEquals(1, d.getChildNodes().getLength()); + assertTrue(d.getChildNodes().item(0) instanceof Element); + assertEquals(0, d.getChildNodes().item(0).getChildNodes().getLength()); + } + +} Property changes on: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/input/CommentLessSourceTest.java ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/tests/net-core/input/CommentLessSourceTest.cs =================================================================== --- trunk/xmlunit/src/tests/net-core/input/CommentLessSourceTest.cs (rev 0) +++ trunk/xmlunit/src/tests/net-core/input/CommentLessSourceTest.cs 2010-08-31 10:15:01 UTC (rev 443) @@ -0,0 +1,40 @@ +/* + 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 NUnit.Framework; +using System.IO; +using System.Xml; + +namespace net.sf.xmlunit.input { + [TestFixture] + public class CommentLessSourceTest { + + [Test] + public void StripCommentsAtDifferentLevels() { + StreamSource s = + new StreamSource(new StringReader("<?xml version='1.0'?>" + + "<!-- comment 1 -->" + + "<foo>" + + "<!-- comment 2 -->" + + "</foo>")); + CommentLessSource cls = new CommentLessSource(s); + XmlDocument d = net.sf.xmlunit.util.Convert.ToDocument(cls); + Assert.AreEqual(2, d.ChildNodes.Count); + Assert.IsTrue(d.ChildNodes[0] is XmlDeclaration); + Assert.IsTrue(d.ChildNodes[1] is XmlElement); + Assert.AreEqual(0, d.ChildNodes[1].ChildNodes.Count); + } + } +} + Property changes on: trunk/xmlunit/src/tests/net-core/input/CommentLessSourceTest.cs ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |