From: <bo...@us...> - 2009-05-11 09:25:21
|
Revision: 313 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=313&view=rev Author: bodewig Date: 2009-05-11 09:25:11 +0000 (Mon, 11 May 2009) Log Message: ----------- most common input scenarios Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java trunk/xmlunit/src/main/net-core/builder/Input.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2009-05-11 08:49:05 UTC (rev 312) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2009-05-11 09:25:11 UTC (rev 313) @@ -1,7 +1,4 @@ /* - Licensed to the XMLUnit developers under one or more - contributor license agreements. See the NOTICE file distributed with - this work for additional information regarding copyright ownership. 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 @@ -16,8 +13,17 @@ */ package net.sf.xmlunit.builder; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.InputStream; +import java.io.Reader; +import java.io.StringReader; +import java.net.URI; +import java.net.URL; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamSource; import org.w3c.dom.Document; public class Input { @@ -40,5 +46,73 @@ public static Builder fromDocument(Document d) { return new DOMBuilder(d); } + + private static class StreamBuilder implements Builder { + private final Source source; + private StreamBuilder(File f) { + source = new StreamSource(f); + } + private StreamBuilder(InputStream s) { + source = new StreamSource(s); + } + private StreamBuilder(Reader r) { + source = new StreamSource(r); + } + public Source build() { + assert source != null; + return source; + } + } + + public static Builder fromFile(File f) { + return new StreamBuilder(f); + } + + public static Builder fromFile(String name) { + return new StreamBuilder(new File(name)); + } + + public static Builder fromStream(InputStream s) { + return new StreamBuilder(s); + } + + public static Builder fromReader(Reader r) { + return new StreamBuilder(r); + } + + public static Builder fromMemory(String s) { + return fromReader(new StringReader(s)); + } + + public static Builder fromMemory(byte[] b) { + return fromStream(new ByteArrayInputStream(b)); + } + + public static Builder fromURL(URL url) throws Exception { + InputStream in = null; + try { + in = url.openStream(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + int read = -1; + byte[] buf = new byte[4096]; + while ((read = in.read(buf)) >= 0) { + if (read > 0) { + baos.write(buf, 0, read); + } + } + return fromMemory(baos.toByteArray()); + } finally { + if (in != null) { + in.close(); + } + } + } + + public static Builder fromURI(URI uri) throws Exception { + return fromURL(uri.toURL()); + } + + public static Builder fromURI(String uri) throws Exception { + return fromURI(new URI(uri)); + } } - \ No newline at end of file Modified: trunk/xmlunit/src/main/net-core/builder/Input.cs =================================================================== --- trunk/xmlunit/src/main/net-core/builder/Input.cs 2009-05-11 08:49:05 UTC (rev 312) +++ trunk/xmlunit/src/main/net-core/builder/Input.cs 2009-05-11 09:25:11 UTC (rev 313) @@ -11,8 +11,9 @@ See the License for the specific language governing permissions and limitations under the License. */ +using System.IO; +using System.Xml; using net.sf.xmlunit.input; -using System.Xml; namespace net.sf.xmlunit.builder { public static class Input { @@ -33,5 +34,50 @@ public static IBuilder FromDocument(XmlDocument d) { return new DOMBuilder(d); } + + internal class StreamBuilder : IBuilder { + private readonly ISource source; + internal StreamBuilder(string s) { + source = new StreamSource(s); + } + internal StreamBuilder(Stream s) { + source = new StreamSource(s); + } + internal StreamBuilder(TextReader r) { + source = new StreamSource(r); + } + public ISource Build() { + return source; + } + } + + public static IBuilder FromFile(string name) { + return new StreamBuilder(name); + } + + public static IBuilder FromStream(Stream s) { + return new StreamBuilder(s); + } + + public static IBuilder FromReader(TextReader r) { + return new StreamBuilder(r); + } + + public static IBuilder FromMemory(string s) { + return FromReader(new StringReader(s)); + } + + public static IBuilder FromMemory(byte[] b) { + return FromStream(new MemoryStream(b)); + } + + public static IBuilder FromURI(string uri) { + return new StreamBuilder(uri); + } + + public static IBuilder FromURI(System.Uri uri) { + return new StreamBuilder(uri.AbsoluteUri); + } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2009-05-11 09:51:00
|
Revision: 314 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=314&view=rev Author: bodewig Date: 2009-05-11 09:50:51 +0000 (Mon, 11 May 2009) Log Message: ----------- input by XSLT transforming another input Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java trunk/xmlunit/src/main/net-core/builder/Input.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2009-05-11 09:25:11 UTC (rev 313) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2009-05-11 09:50:51 UTC (rev 314) @@ -22,6 +22,9 @@ import java.net.URI; import java.net.URL; import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamSource; import org.w3c.dom.Document; @@ -115,4 +118,43 @@ public static Builder fromURI(String uri) throws Exception { return fromURI(new URI(uri)); } + + public static interface TransformationBuilder { + Builder withStylesheet(Source s); + } + + private static class TransformationStep1 implements TransformationBuilder { + private Source sourceDoc; + private TransformationStep1(Source s) { + sourceDoc = s; + } + public Builder withStylesheet(Source s) { + return new TransformationStep2(sourceDoc, s); + } + } + + private static class TransformationStep2 implements Builder { + private Source source; + private Source styleSheet; + private TransformationStep2(Source source, Source styleSheet) { + this.source = source; + this.styleSheet = styleSheet; + } + + public Source build() { + try { + DOMResult r = new DOMResult(); + Transformer t = TransformerFactory.newInstance() + .newTransformer(styleSheet); + t.transform(source, r); + return new DOMSource(r.getNode()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } + + public static TransformationBuilder byTransforming(Source s) { + return new TransformationStep1(s); + } } Modified: trunk/xmlunit/src/main/net-core/builder/Input.cs =================================================================== --- trunk/xmlunit/src/main/net-core/builder/Input.cs 2009-05-11 09:25:11 UTC (rev 313) +++ trunk/xmlunit/src/main/net-core/builder/Input.cs 2009-05-11 09:50:51 UTC (rev 314) @@ -13,6 +13,7 @@ */ using System.IO; using System.Xml; +using System.Xml.Xsl; using net.sf.xmlunit.input; namespace net.sf.xmlunit.builder { @@ -79,5 +80,43 @@ return new StreamBuilder(uri.AbsoluteUri); } + public interface ITransformationBuilder { + IBuilder WithStylesheet(ISource s); + } + + internal class TransformationStep1 : ITransformationBuilder { + private ISource sourceDoc; + internal TransformationStep1(ISource s) { + sourceDoc = s; + } + public IBuilder WithStylesheet(ISource s) { + return new TransformationStep2(sourceDoc, s); + } + } + + internal class TransformationStep2 : IBuilder { + private ISource source; + private ISource styleSheet; + internal TransformationStep2(ISource source, ISource styleSheet) { + this.source = source; + this.styleSheet = styleSheet; + } + + public ISource Build() { + XslCompiledTransform t = new XslCompiledTransform(); + t.Load(styleSheet.Reader); + MemoryStream ms = new MemoryStream(); + using (ms) { + t.Transform(source.Reader, + new XsltArgumentList(), + ms); + } + return FromMemory(ms.ToArray()).Build(); + } + } + + public static ITransformationBuilder ByTransforming(ISource s) { + return new TransformationStep1(s); + } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2009-05-11 10:13:47
|
Revision: 315 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=315&view=rev Author: bodewig Date: 2009-05-11 10:13:41 +0000 (Mon, 11 May 2009) Log Message: ----------- support for XSLT parameters Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java trunk/xmlunit/src/main/net-core/builder/Input.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2009-05-11 09:50:51 UTC (rev 314) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2009-05-11 10:13:41 UTC (rev 315) @@ -21,6 +21,9 @@ import java.io.StringReader; import java.net.URI; import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; @@ -119,33 +122,50 @@ return fromURI(new URI(uri)); } - public static interface TransformationBuilder { - Builder withStylesheet(Source s); + public static interface TransformationBuilder extends Builder { + TransformationBuilder withStylesheet(Source s); + TransformationBuilder withParameter(String name, Object value); + TransformationBuilder withOutputProperty(String name, String value); } - private static class TransformationStep1 implements TransformationBuilder { - private Source sourceDoc; - private TransformationStep1(Source s) { - sourceDoc = s; + private static class Transformation implements TransformationBuilder { + private final Source source; + private Source styleSheet; + private final Properties output = new Properties(); + private final Map<String, Object> params = new HashMap<String, Object>(); + + private Transformation(Source s) { + source = s; } - public Builder withStylesheet(Source s) { - return new TransformationStep2(sourceDoc, s); + public TransformationBuilder withStylesheet(Source s) { + styleSheet = s; + return this; } - } + public TransformationBuilder withOutputProperty(String name, + String value) { + output.setProperty(name, value); + return this; + } - private static class TransformationStep2 implements Builder { - private Source source; - private Source styleSheet; - private TransformationStep2(Source source, Source styleSheet) { - this.source = source; - this.styleSheet = styleSheet; + public TransformationBuilder withParameter(String name, Object value) { + params.put(name, value); + return this; } public Source build() { try { DOMResult r = new DOMResult(); - Transformer t = TransformerFactory.newInstance() - .newTransformer(styleSheet); + TransformerFactory fac = TransformerFactory.newInstance(); + Transformer t = null; + if (styleSheet != null) { + t = fac.newTransformer(styleSheet); + } else { + t = fac.newTransformer(); + } + t.setOutputProperties(output); + for (Map.Entry<String, Object> ent : params.entrySet()) { + t.setParameter(ent.getKey(), ent.getValue()); + } t.transform(source, r); return new DOMSource(r.getNode()); } catch (Exception e) { @@ -155,6 +175,6 @@ } public static TransformationBuilder byTransforming(Source s) { - return new TransformationStep1(s); + return new Transformation(s); } } Modified: trunk/xmlunit/src/main/net-core/builder/Input.cs =================================================================== --- trunk/xmlunit/src/main/net-core/builder/Input.cs 2009-05-11 09:50:51 UTC (rev 314) +++ trunk/xmlunit/src/main/net-core/builder/Input.cs 2009-05-11 10:13:41 UTC (rev 315) @@ -80,35 +80,49 @@ return new StreamBuilder(uri.AbsoluteUri); } - public interface ITransformationBuilder { - IBuilder WithStylesheet(ISource s); + public interface ITransformationBuilder : IBuilder { + ITransformationBuilder WithStylesheet(ISource s); + ITransformationBuilder WithExtensionObject(string namespaceUri, + object extension); + ITransformationBuilder WithParameter(string name, + string namespaceUri, + object parameter); } - internal class TransformationStep1 : ITransformationBuilder { - private ISource sourceDoc; - internal TransformationStep1(ISource s) { - sourceDoc = s; + internal class Transformation : ITransformationBuilder { + private readonly ISource source; + private ISource styleSheet; + private readonly XsltArgumentList args = new XsltArgumentList(); + internal Transformation(ISource s) { + source = s; } - public IBuilder WithStylesheet(ISource s) { - return new TransformationStep2(sourceDoc, s); + public ITransformationBuilder WithStylesheet(ISource s) { + this.styleSheet = styleSheet; + return this; } - } - internal class TransformationStep2 : IBuilder { - private ISource source; - private ISource styleSheet; - internal TransformationStep2(ISource source, ISource styleSheet) { - this.source = source; - this.styleSheet = styleSheet; + public ITransformationBuilder WithExtensionObject(string namespaceUri, + object extension) { + args.AddExtensionObject(namespaceUri, extension); + return this; } + public ITransformationBuilder WithParameter(string name, + string namespaceUri, + object parameter) { + args.AddParam(name, namespaceUri, parameter); + return this; + } + public ISource Build() { XslCompiledTransform t = new XslCompiledTransform(); - t.Load(styleSheet.Reader); + if (styleSheet != null) { + t.Load(styleSheet.Reader); + } MemoryStream ms = new MemoryStream(); using (ms) { t.Transform(source.Reader, - new XsltArgumentList(), + args, ms); } return FromMemory(ms.ToArray()).Build(); @@ -116,7 +130,7 @@ } public static ITransformationBuilder ByTransforming(ISource s) { - return new TransformationStep1(s); + return new Transformation(s); } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2009-05-12 10:56:50
|
Revision: 318 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=318&view=rev Author: bodewig Date: 2009-05-12 10:56:37 +0000 (Tue, 12 May 2009) Log Message: ----------- whitespace Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/ConfigurationException.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitRuntimeException.java trunk/xmlunit/src/main/net-core/ISource.cs trunk/xmlunit/src/main/net-core/builder/Input.cs trunk/xmlunit/src/main/net-core/input/AbstractSource.cs trunk/xmlunit/src/main/net-core/input/DOMSource.cs trunk/xmlunit/src/main/net-core/input/ReaderSource.cs trunk/xmlunit/src/main/net-core/input/StreamSource.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2009-05-12 10:46:49 UTC (rev 317) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2009-05-12 10:56:37 UTC (rev 318) @@ -1,15 +1,15 @@ /* - 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 + 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 + 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. + 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.builder; @@ -132,7 +132,7 @@ public static Builder fromURI(String uri) { try { - return fromURI(new URI(uri)); + return fromURI(new URI(uri)); } catch (java.net.URISyntaxException ex) { throw new IllegalArgumentException("uri " + uri + " is not an URI", ex); Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/ConfigurationException.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/ConfigurationException.java 2009-05-12 10:46:49 UTC (rev 317) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/ConfigurationException.java 2009-05-12 10:56:37 UTC (rev 318) @@ -1,15 +1,15 @@ /* - 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 + 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 + 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. + 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.exceptions; Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitRuntimeException.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitRuntimeException.java 2009-05-12 10:46:49 UTC (rev 317) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/exceptions/XMLUnitRuntimeException.java 2009-05-12 10:56:37 UTC (rev 318) @@ -1,15 +1,15 @@ /* - 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 + 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 + 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. + 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.exceptions; @@ -41,7 +41,7 @@ * * @param message the detail message */ - public XMLUnitRuntimeException(Throwable cause) { + public XMLUnitRuntimeException(Throwable cause) { this(cause != null ? cause.getMessage() : null, cause); } } \ No newline at end of file Modified: trunk/xmlunit/src/main/net-core/ISource.cs =================================================================== --- trunk/xmlunit/src/main/net-core/ISource.cs 2009-05-12 10:46:49 UTC (rev 317) +++ trunk/xmlunit/src/main/net-core/ISource.cs 2009-05-12 10:56:37 UTC (rev 318) @@ -1,15 +1,15 @@ /* - 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 + 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 + 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. + 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; Modified: trunk/xmlunit/src/main/net-core/builder/Input.cs =================================================================== --- trunk/xmlunit/src/main/net-core/builder/Input.cs 2009-05-12 10:46:49 UTC (rev 317) +++ trunk/xmlunit/src/main/net-core/builder/Input.cs 2009-05-12 10:56:37 UTC (rev 318) @@ -1,15 +1,15 @@ /* - 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 + 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 + 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. + 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.IO; using System.Xml; Modified: trunk/xmlunit/src/main/net-core/input/AbstractSource.cs =================================================================== --- trunk/xmlunit/src/main/net-core/input/AbstractSource.cs 2009-05-12 10:46:49 UTC (rev 317) +++ trunk/xmlunit/src/main/net-core/input/AbstractSource.cs 2009-05-12 10:56:37 UTC (rev 318) @@ -1,15 +1,15 @@ /* - 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 + 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 + 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. + 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; Modified: trunk/xmlunit/src/main/net-core/input/DOMSource.cs =================================================================== --- trunk/xmlunit/src/main/net-core/input/DOMSource.cs 2009-05-12 10:46:49 UTC (rev 317) +++ trunk/xmlunit/src/main/net-core/input/DOMSource.cs 2009-05-12 10:56:37 UTC (rev 318) @@ -1,15 +1,15 @@ /* - 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 + 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 + 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. + 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; Modified: trunk/xmlunit/src/main/net-core/input/ReaderSource.cs =================================================================== --- trunk/xmlunit/src/main/net-core/input/ReaderSource.cs 2009-05-12 10:46:49 UTC (rev 317) +++ trunk/xmlunit/src/main/net-core/input/ReaderSource.cs 2009-05-12 10:56:37 UTC (rev 318) @@ -1,15 +1,15 @@ /* - 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 + 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 + 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. + 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; Modified: trunk/xmlunit/src/main/net-core/input/StreamSource.cs =================================================================== --- trunk/xmlunit/src/main/net-core/input/StreamSource.cs 2009-05-12 10:46:49 UTC (rev 317) +++ trunk/xmlunit/src/main/net-core/input/StreamSource.cs 2009-05-12 10:56:37 UTC (rev 318) @@ -1,15 +1,15 @@ /* - 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 + 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 + 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. + 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.IO; using System.Xml; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2010-04-29 06:14:40
|
Revision: 362 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=362&view=rev Author: bodewig Date: 2010-04-29 06:14:31 +0000 (Thu, 29 Apr 2010) Log Message: ----------- New XPath engine for Java 2.0 Modified Paths: -------------- trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/jaxp13/Jaxp13XpathEngine.java trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/jaxp13/XMLUnitNamespaceContext2Jaxp13.java Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java 2010-04-29 06:14:31 UTC (rev 362) @@ -0,0 +1,146 @@ +/* + 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.xpath; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import javax.xml.XMLConstants; +import javax.xml.namespace.NamespaceContext; +import javax.xml.transform.Source; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; +import net.sf.xmlunit.exceptions.ConfigurationException; +import net.sf.xmlunit.exceptions.XMLUnitException; +import net.sf.xmlunit.util.Convert; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class XPathEngine { + private final XPath xpath; + + public XPathEngine(XPathFactory fac) { + try { + xpath = fac.newXPath(); + } catch (Exception e) { + throw new ConfigurationException(e); + } + } + + public XPathEngine() { + this(XPathFactory.newInstance()); + } + + public Iterable<Node> selectNodes(String xPath, Source s) { + try { + NodeList nl = (NodeList) xpath.evaluate(xPath, + Convert.toInputSource(s), + XPathConstants.NODESET); + return new IterableNodeList(nl); + } catch (XPathExpressionException ex) { + throw new XMLUnitException(ex); + } + } + + public String evaluate(String xPath, Source s) { + try { + return xpath.evaluate(xPath, Convert.toInputSource(s)); + } catch (XPathExpressionException ex) { + throw new XMLUnitException(ex); + } + } + + public void setNamespaceContext(Map<String, String> prefix2Uri) { + xpath.setNamespaceContext(new NC(prefix2Uri)); + } + + private static class NC implements NamespaceContext { + private final Map<String, String> prefix2Uri; + + private NC(Map<String, String> prefix2Uri) { + this.prefix2Uri = prefix2Uri; + } + + public String getNamespaceURI(String prefix) { + if (prefix == null) { + throw new IllegalArgumentException("prefix must not be null"); + } + if (XMLConstants.XML_NS_PREFIX.equals(prefix)) { + return XMLConstants.XML_NS_URI; + } + if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) { + return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; + } + String uri = prefix2Uri.get(prefix); + return uri != null ? uri : XMLConstants.NULL_NS_URI; + } + + public String getPrefix(String uri) { + Iterator i = getPrefixes(uri); + return i.hasNext() ? (String) i.next() : null; + } + + public Iterator getPrefixes(String uri) { + if (uri == null) { + throw new IllegalArgumentException("uri must not be null"); + } + Collection<String> c = new HashSet<String>(); + boolean done = false; + if (XMLConstants.XML_NS_URI.equals(uri)) { + c.add(XMLConstants.XML_NS_PREFIX); + done = true; + } + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) { + c.add(XMLConstants.XMLNS_ATTRIBUTE); + done = true; + } + if (!done) { + for (Map.Entry<String, String> entry : prefix2Uri.entrySet()) { + if (uri.equals(entry.getValue())) { + c.add(entry.getKey()); + } + } + } + return c.iterator(); + } + } + + private static class IterableNodeList implements Iterable<Node> { + private final NodeList nl; + private final int length; + private int current = 0; + private IterableNodeList(NodeList nl) { + this.nl = nl; + length = nl.getLength(); + } + public Iterator<Node> iterator() { + return new Iterator<Node>() { + public void remove() { + throw new UnsupportedOperationException(); + } + public Node next() { + return nl.item(current++); + } + public boolean hasNext() { + return current < length; + } + }; + } + } + +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/jaxp13/Jaxp13XpathEngine.java =================================================================== --- trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/jaxp13/Jaxp13XpathEngine.java 2010-04-29 06:13:34 UTC (rev 361) +++ trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/jaxp13/Jaxp13XpathEngine.java 2010-04-29 06:14:31 UTC (rev 362) @@ -42,32 +42,39 @@ import org.custommonkey.xmlunit.exceptions.ConfigurationException; import org.custommonkey.xmlunit.exceptions.XpathException; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathExpressionException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import javax.xml.transform.dom.DOMSource; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; +import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import net.sf.xmlunit.exceptions.XMLUnitException; +import net.sf.xmlunit.xpath.XPathEngine; + /** * XPath engine based on javax.xml.xpath. */ public class Jaxp13XpathEngine implements XpathEngine { - private final XPath xpath; + private final XPathEngine engine; public Jaxp13XpathEngine() throws ConfigurationException { try { - XPathFactory f = null; + XPathEngine e = null; if (XMLUnit.getXPathFactory() != null) { - f = (XPathFactory) Class.forName(XMLUnit.getXPathFactory()) - .newInstance(); + e = new XPathEngine((XPathFactory) Class + .forName(XMLUnit.getXPathFactory()) + .newInstance()); } else { - f = XPathFactory.newInstance(); + e = new XPathEngine(); } - - xpath = f.newXPath(); + engine = e; + } catch (net.sf.xmlunit.exceptions.ConfigurationException ex) { + throw new ConfigurationException(ex.getCause()); } catch (Exception ex) { throw new ConfigurationException(ex); } @@ -84,10 +91,12 @@ public NodeList getMatchingNodes(String select, Document document) throws XpathException { try { - return (NodeList) xpath.evaluate(select, document, - XPathConstants.NODESET); - } catch (XPathExpressionException ex) { - throw new XpathException(ex); + return new NodeListForIterable(engine + .selectNodes(select, + new DOMSource(document)) + ); + } catch (XMLUnitException ex) { + throw new XpathException(ex.getCause()); } } @@ -103,13 +112,34 @@ public String evaluate(String select, Document document) throws XpathException { try { - return xpath.evaluate(select, document); - } catch (XPathExpressionException ex) { - throw new XpathException(ex); + return engine.evaluate(select, new DOMSource(document)); + } catch (XMLUnitException ex) { + throw new XpathException(ex.getCause()); } } public void setNamespaceContext(NamespaceContext ctx) { - xpath.setNamespaceContext(new XMLUnitNamespaceContext2Jaxp13(ctx)); + engine.setNamespaceContext(XMLUnitNamespaceContext2Jaxp13 + .turnIntoMap(ctx)); } + + private static class NodeListForIterable implements NodeList { + private final List<Node> l; + + private NodeListForIterable(Iterable<Node> it) { + ArrayList<Node> a = new ArrayList<Node>(); + for (Node n : it) { + a.add(n); + } + l = Collections.unmodifiableList(a); + } + + public int getLength() { + return l.size(); + } + + public Node item(int idx) { + return l.get(idx); + } + } } Modified: trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/jaxp13/XMLUnitNamespaceContext2Jaxp13.java =================================================================== --- trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/jaxp13/XMLUnitNamespaceContext2Jaxp13.java 2010-04-29 06:13:34 UTC (rev 361) +++ trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/jaxp13/XMLUnitNamespaceContext2Jaxp13.java 2010-04-29 06:14:31 UTC (rev 362) @@ -91,7 +91,7 @@ return i.hasNext() ? (String) i.next() : null; } - private static Map turnIntoMap(NamespaceContext ctx) { + static Map turnIntoMap(NamespaceContext ctx) { HashMap/*<String, String>*/ m = new HashMap(); for (Iterator i = ctx.getPrefixes(); i.hasNext(); ) { String prefix = (String) i.next(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2010-04-29 09:38:26
|
Revision: 367 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=367&view=rev Author: bodewig Date: 2010-04-29 09:38:19 +0000 (Thu, 29 Apr 2010) Log Message: ----------- rename class to leave door open for different implementations (at which point we may want to extract an interface) Modified Paths: -------------- trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/jaxp13/Jaxp13XpathEngine.java Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java Removed Paths: ------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java Copied: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java (from rev 363, trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java) =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java 2010-04-29 09:38:19 UTC (rev 367) @@ -0,0 +1,165 @@ +/* + 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.xpath; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import javax.xml.XMLConstants; +import javax.xml.namespace.NamespaceContext; +import javax.xml.transform.Source; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; +import net.sf.xmlunit.exceptions.ConfigurationException; +import net.sf.xmlunit.exceptions.XMLUnitException; +import net.sf.xmlunit.util.Convert; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * Simplified access to JAXP's XPath API. + */ +public class JAXPXPathEngine { + private final XPath xpath; + + public JAXPXPathEngine(XPathFactory fac) { + try { + xpath = fac.newXPath(); + } catch (Exception e) { + throw new ConfigurationException(e); + } + } + + /** + * Create an XPathEngine that uses JAXP's default XPathFactory + * under the covers. + */ + public JAXPXPathEngine() { + this(XPathFactory.newInstance()); + } + + /** + * Returns a potentially empty collection of Nodes matching an + * XPath expression. + */ + public Iterable<Node> selectNodes(String xPath, Source s) { + try { + NodeList nl = (NodeList) xpath.evaluate(xPath, + Convert.toInputSource(s), + XPathConstants.NODESET); + return new IterableNodeList(nl); + } catch (XPathExpressionException ex) { + throw new XMLUnitException(ex); + } + } + + /** + * Evaluates an XPath expression and stringifies the result. + */ + public String evaluate(String xPath, Source s) { + try { + return xpath.evaluate(xPath, Convert.toInputSource(s)); + } catch (XPathExpressionException ex) { + throw new XMLUnitException(ex); + } + } + + /** + * Establish a namespace context. + * + * @param prefix2Uri maps from prefix to namespace URI. + */ + public void setNamespaceContext(Map<String, String> prefix2Uri) { + xpath.setNamespaceContext(new NC(prefix2Uri)); + } + + private static class NC implements NamespaceContext { + private final Map<String, String> prefix2Uri; + + private NC(Map<String, String> prefix2Uri) { + this.prefix2Uri = prefix2Uri; + } + + public String getNamespaceURI(String prefix) { + if (prefix == null) { + throw new IllegalArgumentException("prefix must not be null"); + } + if (XMLConstants.XML_NS_PREFIX.equals(prefix)) { + return XMLConstants.XML_NS_URI; + } + if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) { + return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; + } + String uri = prefix2Uri.get(prefix); + return uri != null ? uri : XMLConstants.NULL_NS_URI; + } + + public String getPrefix(String uri) { + Iterator i = getPrefixes(uri); + return i.hasNext() ? (String) i.next() : null; + } + + public Iterator getPrefixes(String uri) { + if (uri == null) { + throw new IllegalArgumentException("uri must not be null"); + } + Collection<String> c = new HashSet<String>(); + boolean done = false; + if (XMLConstants.XML_NS_URI.equals(uri)) { + c.add(XMLConstants.XML_NS_PREFIX); + done = true; + } + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) { + c.add(XMLConstants.XMLNS_ATTRIBUTE); + done = true; + } + if (!done) { + for (Map.Entry<String, String> entry : prefix2Uri.entrySet()) { + if (uri.equals(entry.getValue())) { + c.add(entry.getKey()); + } + } + } + return c.iterator(); + } + } + + private static class IterableNodeList implements Iterable<Node> { + private final NodeList nl; + private final int length; + private int current = 0; + private IterableNodeList(NodeList nl) { + this.nl = nl; + length = nl.getLength(); + } + public Iterator<Node> iterator() { + return new Iterator<Node>() { + public void remove() { + throw new UnsupportedOperationException(); + } + public Node next() { + return nl.item(current++); + } + public boolean hasNext() { + return current < length; + } + }; + } + } + +} Deleted: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java 2010-04-29 08:58:05 UTC (rev 366) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java 2010-04-29 09:38:19 UTC (rev 367) @@ -1,165 +0,0 @@ -/* - 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.xpath; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; -import javax.xml.XMLConstants; -import javax.xml.namespace.NamespaceContext; -import javax.xml.transform.Source; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.xpath.XPathFactory; -import net.sf.xmlunit.exceptions.ConfigurationException; -import net.sf.xmlunit.exceptions.XMLUnitException; -import net.sf.xmlunit.util.Convert; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -/** - * Simplified access to JAXP's XPath API. - */ -public class XPathEngine { - private final XPath xpath; - - public XPathEngine(XPathFactory fac) { - try { - xpath = fac.newXPath(); - } catch (Exception e) { - throw new ConfigurationException(e); - } - } - - /** - * Create an XPathEngine that uses JAXP's default XPathFactory - * under the covers. - */ - public XPathEngine() { - this(XPathFactory.newInstance()); - } - - /** - * Returns a potentially empty collection of Nodes matching an - * XPath expression. - */ - public Iterable<Node> selectNodes(String xPath, Source s) { - try { - NodeList nl = (NodeList) xpath.evaluate(xPath, - Convert.toInputSource(s), - XPathConstants.NODESET); - return new IterableNodeList(nl); - } catch (XPathExpressionException ex) { - throw new XMLUnitException(ex); - } - } - - /** - * Evaluates an XPath expression and stringifies the result. - */ - public String evaluate(String xPath, Source s) { - try { - return xpath.evaluate(xPath, Convert.toInputSource(s)); - } catch (XPathExpressionException ex) { - throw new XMLUnitException(ex); - } - } - - /** - * Establish a namespace context. - * - * @param prefix2Uri maps from prefix to namespace URI. - */ - public void setNamespaceContext(Map<String, String> prefix2Uri) { - xpath.setNamespaceContext(new NC(prefix2Uri)); - } - - private static class NC implements NamespaceContext { - private final Map<String, String> prefix2Uri; - - private NC(Map<String, String> prefix2Uri) { - this.prefix2Uri = prefix2Uri; - } - - public String getNamespaceURI(String prefix) { - if (prefix == null) { - throw new IllegalArgumentException("prefix must not be null"); - } - if (XMLConstants.XML_NS_PREFIX.equals(prefix)) { - return XMLConstants.XML_NS_URI; - } - if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) { - return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; - } - String uri = prefix2Uri.get(prefix); - return uri != null ? uri : XMLConstants.NULL_NS_URI; - } - - public String getPrefix(String uri) { - Iterator i = getPrefixes(uri); - return i.hasNext() ? (String) i.next() : null; - } - - public Iterator getPrefixes(String uri) { - if (uri == null) { - throw new IllegalArgumentException("uri must not be null"); - } - Collection<String> c = new HashSet<String>(); - boolean done = false; - if (XMLConstants.XML_NS_URI.equals(uri)) { - c.add(XMLConstants.XML_NS_PREFIX); - done = true; - } - if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) { - c.add(XMLConstants.XMLNS_ATTRIBUTE); - done = true; - } - if (!done) { - for (Map.Entry<String, String> entry : prefix2Uri.entrySet()) { - if (uri.equals(entry.getValue())) { - c.add(entry.getKey()); - } - } - } - return c.iterator(); - } - } - - private static class IterableNodeList implements Iterable<Node> { - private final NodeList nl; - private final int length; - private int current = 0; - private IterableNodeList(NodeList nl) { - this.nl = nl; - length = nl.getLength(); - } - public Iterator<Node> iterator() { - return new Iterator<Node>() { - public void remove() { - throw new UnsupportedOperationException(); - } - public Node next() { - return nl.item(current++); - } - public boolean hasNext() { - return current < length; - } - }; - } - } - -} Modified: trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/jaxp13/Jaxp13XpathEngine.java =================================================================== --- trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/jaxp13/Jaxp13XpathEngine.java 2010-04-29 08:58:05 UTC (rev 366) +++ trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/jaxp13/Jaxp13XpathEngine.java 2010-04-29 09:38:19 UTC (rev 367) @@ -53,24 +53,24 @@ import org.w3c.dom.NodeList; import net.sf.xmlunit.exceptions.XMLUnitException; -import net.sf.xmlunit.xpath.XPathEngine; +import net.sf.xmlunit.xpath.JAXPXPathEngine; /** * XPath engine based on javax.xml.xpath. */ public class Jaxp13XpathEngine implements XpathEngine { - private final XPathEngine engine; + private final JAXPXPathEngine engine; public Jaxp13XpathEngine() throws ConfigurationException { try { - XPathEngine e = null; + JAXPXPathEngine e = null; if (XMLUnit.getXPathFactory() != null) { - e = new XPathEngine((XPathFactory) Class - .forName(XMLUnit.getXPathFactory()) - .newInstance()); + e = new JAXPXPathEngine((XPathFactory) Class + .forName(XMLUnit.getXPathFactory()) + .newInstance()); } else { - e = new XPathEngine(); + e = new JAXPXPathEngine(); } engine = e; } catch (net.sf.xmlunit.exceptions.ConfigurationException ex) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2010-05-03 10:26:29
|
Revision: 376 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=376&view=rev Author: bodewig Date: 2010-05-03 10:26:23 +0000 (Mon, 03 May 2010) Log Message: ----------- extract an interface Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java trunk/xmlunit/src/main/net-core/xpath/XPathEngine.cs Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/IXPathEngine.java trunk/xmlunit/src/main/net-core/xpath/IXPathEngine.cs Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/IXPathEngine.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/IXPathEngine.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/IXPathEngine.java 2010-05-03 10:26:23 UTC (rev 376) @@ -0,0 +1,39 @@ +/* + 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.xpath; + +import java.util.Map; +import javax.xml.transform.Source; +import org.w3c.dom.Node; + +/** + * Interface for XMLUnit's XPath abstraction. + */ +public interface IXPathEngine { + /** + * Returns a potentially empty collection of Nodes matching an + * XPath expression. + */ + Iterable<Node> selectNodes(String xPath, Source s); + /** + * Evaluates an XPath expression and stringifies the result. + */ + String evaluate(String xPath, Source s); + /** + * Establish a namespace context. + * + * @param prefix2Uri maps from prefix to namespace URI. + */ + void setNamespaceContext(Map<String, String> prefix2Uri); +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/IXPathEngine.java ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java 2010-05-03 06:49:13 UTC (rev 375) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java 2010-05-03 10:26:23 UTC (rev 376) @@ -29,7 +29,7 @@ /** * Simplified access to JAXP's XPath API. */ -public class JAXPXPathEngine { +public class JAXPXPathEngine implements IXPathEngine { private final XPath xpath; public JAXPXPathEngine(XPathFactory fac) { @@ -49,8 +49,7 @@ } /** - * Returns a potentially empty collection of Nodes matching an - * XPath expression. + * {@inheritDoc} */ public Iterable<Node> selectNodes(String xPath, Source s) { try { @@ -64,7 +63,7 @@ } /** - * Evaluates an XPath expression and stringifies the result. + * {@inheritDoc} */ public String evaluate(String xPath, Source s) { try { @@ -75,9 +74,7 @@ } /** - * Establish a namespace context. - * - * @param prefix2Uri maps from prefix to namespace URI. + * {@inheritDoc} */ public void setNamespaceContext(Map<String, String> prefix2Uri) { xpath.setNamespaceContext(Convert.toNamespaceContext(prefix2Uri)); Added: trunk/xmlunit/src/main/net-core/xpath/IXPathEngine.cs =================================================================== --- trunk/xmlunit/src/main/net-core/xpath/IXPathEngine.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/xpath/IXPathEngine.cs 2010-05-03 10:26:23 UTC (rev 376) @@ -0,0 +1,38 @@ +/* + 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.Collections.Generic; +using System.Xml; + +namespace net.sf.xmlunit.xpath { + + /// <summary> + /// Interface for XMLUnit's XPath abstraction. + /// </summary> + public interface IXPathEngine { + /// <summary> + /// Returns a potentially empty collection of Nodes matching an + /// XPath expression. + /// </summary> + IEnumerable<XmlNode> SelectNodes(string xPath, ISource s); + /// <summary> + /// Evaluates an XPath expression and stringifies the result. + /// </summary> + string Evaluate(string xPath, ISource s); + /// <summary> + /// Establish a namespace context - maps from prefix to namespace URI. + /// </summary> + IDictionary<string, string> NamespaceContext { set; } + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/main/net-core/xpath/IXPathEngine.cs ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/xmlunit/src/main/net-core/xpath/XPathEngine.cs =================================================================== --- trunk/xmlunit/src/main/net-core/xpath/XPathEngine.cs 2010-05-03 06:49:13 UTC (rev 375) +++ trunk/xmlunit/src/main/net-core/xpath/XPathEngine.cs 2010-05-03 10:26:23 UTC (rev 376) @@ -23,7 +23,7 @@ /// <summary> /// Simplified access to System.Xml.XPath API. /// </summary> - public class XPathEngine { + public class XPathEngine : IXPathEngine { private XmlNamespaceManager nsContext; /// <summary> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2010-05-03 16:08:25
|
Revision: 378 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=378&view=rev Author: bodewig Date: 2010-05-03 16:08:17 +0000 (Mon, 03 May 2010) Log Message: ----------- preliminary interfaces for difference engine Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/Comparison.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonListener.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonResult.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonType.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluator.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ElementSelector.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/IDifferenceEngine.java trunk/xmlunit/src/main/net-core/diff/ trunk/xmlunit/src/main/net-core/diff/Comparison.cs trunk/xmlunit/src/main/net-core/diff/ComparisonListener.cs trunk/xmlunit/src/main/net-core/diff/ComparisonResult.cs trunk/xmlunit/src/main/net-core/diff/ComparisonType.cs trunk/xmlunit/src/main/net-core/diff/DifferenceEvaluator.cs trunk/xmlunit/src/main/net-core/diff/ElementSelector.cs trunk/xmlunit/src/main/net-core/diff/IDifferenceEngine.cs Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/Comparison.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/Comparison.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/Comparison.java 2010-05-03 16:08:17 UTC (rev 378) @@ -0,0 +1,83 @@ +/* + 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.diff; + +import org.w3c.dom.Node; + +/** + * Details of a single comparison XMLUnit has performed. + */ +public class Comparison { + + /** + * The details of a Node that took part in the comparision. + */ + public static class Detail { + private final Node node; + private final String xpath; + private final Object value; + + private Detail(Node n, String x, Object v) { + node = n; + xpath = x; + value = v; + } + + /** + * The actual Node. + */ + public Node getNode() { return node; } + /** + * XPath leading to the Node. + */ + public String getXPath() { return xpath; } + /** + * The value for comparision found at the current node. + */ + public Object getValue() { return value; } + } + + private final Detail control, test; + private final ComparisonType type; + + public Comparison(ComparisonType t, Node controlNode, + String controlXPath, Object controlValue, + Node testNode, String testXPath, Object testValue) { + type = t; + control = new Detail(controlNode, controlXPath, controlValue); + test = new Detail(testNode, testXPath, testValue); + } + + /** + * The kind of comparision performed. + */ + public ComparisonType getType() { + return type; + } + + /** + * Details of the control node. + */ + public Detail getControlNodeDetails() { + return control; + } + + /** + * Details of the test node. + */ + public Detail getTestNodeDetails() { + return test; + } + +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/Comparison.java ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonListener.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonListener.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonListener.java 2010-05-03 16:08:17 UTC (rev 378) @@ -0,0 +1,21 @@ +/* + 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.diff; + +/** + * Is notified of comparisions and their results. + */ +public interface ComparisonListener { + void comparisonPerformed(Comparison comparison, ComparisonResult outcome); +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonListener.java ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonResult.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonResult.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonResult.java 2010-05-03 16:08:17 UTC (rev 378) @@ -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. +*/ +package net.sf.xmlunit.diff; + +/** + * The possible outcomes of a comparision. + */ +public enum ComparisonResult { + /** + * The two nodes are the same for the sake of this comparison. + */ + EQUAL, + /** + * The two nodes are different but similar enough to satisfy a + * weak equality constraint + */ + SIMILAR, + /** + * The two nodes are different. + */ + DIFFERENT, + /** + * The two nodes are different and comparison should stop + * immediately. + * + * <p>Only used as a return type by {@link DifferenceEvaluator} + */ + CRITICAL, +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonResult.java ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonType.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonType.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonType.java 2010-05-03 16:08:17 UTC (rev 378) @@ -0,0 +1,120 @@ +/* + 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.diff; + +/** + * The kinds of comparisons XMLUnit performs. + */ +public enum ComparisonType { + /** + * Do both documents have a DOCTYPE (or neither of each)? + */ + HAS_DOCTYPE_DECLARATION, + /** + * If the documents both have DOCTYPEs, compare the names. + */ + DOCTYPE_NAME, + /** + * If the documents both have DOCTYPEs, compare the PUBLIC + * identifiers. + */ + DOCTYPE_PUBLIC, + /** + * If the documents both have DOCTYPEs, compare the SYSTEM + * identifiers. + */ + DOCTYPE_SYSTEM, + + /** + * Check whether both documents provide the same values for + * xsi:schemaLocation (may even be null). + */ + SCHEMA_LOCATION, + /** + * Check whether both documents provide the same values for + * xsi:noNamspaceSchemaLocation (may even be null). + */ + NO_NAMESPACE_SCHEMA_LOCATION, + + /** + * Compare the node types. + */ + NODE_TYPE, + + /** + * Compare the node's namespace prefixes. + */ + NAMESPACE_PREFIX, + /** + * Compare the node's namespace URIs. + */ + NAMESPACE_URI, + + /** + * Compare content of CDATA sections. + */ + CDATA_VALUE, + /** + * Compare content of comments. + */ + COMMENT_VALUE, + /** + * Compare content of text nodes. + */ + TEXT_VALUE, + /** + * Compare targets of processing instructions. + */ + PROCESSING_INSTRUCTION_TARGET, + /** + * Compare data of processing instructions. + */ + PROCESSING_INSTRUCTION_DATA, + + /** + * Compare element names. + */ + ELEMENT_TAG_NAME, + /** + * Compare explicit/implicit status of attributes. + */ + ATTR_VALUE_EXPLICITLY_SPECIFIED, + /** + * Compare number of attributes. + */ + ELEMENT_NUM_ATTRIBUTES, + /** + * Compare attribute's value. + */ + ATTR_VALUE, + /** + * Compare number of child nodes. + */ + CHILD_NODELIST_LENGTH, + /** + * Compare order of child nodes. + */ + CHILD_NODELIST_SEQUENCE, + + /** + * Search for a child node matching a specific child node of the + * other node. + */ + CHILD_LOOKUP, + /** + * Search for an atribute with a name matching a specific + * attribute of the other node. + */ + ATTR_NAME_LOOKUP, +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonType.java ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluator.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluator.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluator.java 2010-05-03 16:08:17 UTC (rev 378) @@ -0,0 +1,28 @@ +/* + 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.diff; + +/** + * May decide to up- or downgrade the severity of a difference and + * even to stop the comparison completely. + */ +public interface DifferenceEvaluator { + /** + * May alter the outcome of a comparison. + * + * @return the new result of the comparison, should return {@link + * ComaprisonResult#CRITICAL} to stop the comparison completely. + */ + ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome); +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluator.java ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ElementSelector.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ElementSelector.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ElementSelector.java 2010-05-03 16:08:17 UTC (rev 378) @@ -0,0 +1,27 @@ +/* + 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.diff; + +import org.w3c.dom.Element; + +/** + * Strategy for selecting matching elements. + */ +public interface ElementSelector { + /** + * Determine whether the two elements from the control and test + * XML can be compared. + */ + boolean canBeCompared(Element controlElement, Element testElement); +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ElementSelector.java ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/IDifferenceEngine.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/IDifferenceEngine.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/IDifferenceEngine.java 2010-05-03 16:08:17 UTC (rev 378) @@ -0,0 +1,54 @@ +/* + 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.diff; + +import javax.xml.transform.Source; + +/** + * XMLUnit's difference engine. + */ +public interface IDifferenceEngine { + /** + * Registers a listener that is notified of each comparison. + */ + void addComparisonListener(ComparisonListener l); + + /** + * Registers a listener that is notified of each comparison with + * outcome {@link ComparisonResult#EQUAL}. + */ + void addMatchListener(ComparisonListener l); + + /** + * Registers a listener that is notified of each comparison with + * outcome other than {@link ComparisonResult#EQUAL}. + */ + void addDifferenceListener(ComparisonListener l); + + /** + * Sets the strategy for selecting elements to compare. + */ + void setElementSelector(ElementSelector s); + + /** + * Determines whether the comparison should stop after given + * difference has been found. + */ + void setDifferenceEvaluator(DifferenceEvaluator e); + + /** + * Compares two pieces of XML and invokes the registered listeners. + */ + void compare(Source control, Source test); +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/IDifferenceEngine.java ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/net-core/diff/Comparison.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/Comparison.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/diff/Comparison.cs 2010-05-03 16:08:17 UTC (rev 378) @@ -0,0 +1,92 @@ +/* + 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; + +namespace net.sf.xmlunit.diff { + + /// <summary> + /// Details of a single comparison XMLUnit has performed. + /// </summary> + public class Comparison { + + /// <summary> + /// The details of a Node that took part in the comparision. + /// </summary> + public sealed class Detail { + private readonly XmlNode node; + private readonly string xpath; + private readonly object value; + + internal Detail(XmlNode n, string x, object v) { + node = n; + xpath = x; + value = v; + } + + /// <summary> + /// The actual Node. + /// </summary> + public XmlNode Node { get { return node; } } + /// <summary> + /// XPath leading to the Node. + /// </summary> + public string XPath { get { return xpath; } } + /// <summary> + /// The value for comparision found at the current node. + /// </summary> + public object Value { get { return value; } } + } + + private readonly Detail control, test; + private readonly ComparisonType type; + + public Comparison(ComparisonType t, XmlNode controlNode, + string controlXPath, object controlValue, + XmlNode testNode, string testXPath, + object testValue) { + type = t; + control = new Detail(controlNode, controlXPath, controlValue); + test = new Detail(testNode, testXPath, testValue); + } + + /// <summary> + /// The kind of comparision performed. + /// </summary> + public ComparisonType Type { + get { + return type; + } + } + + /// <summary> + /// Details of the control node. + /// </summary> + public Detail ControlNodeDetails { + get { + return control; + } + } + + /// <summary> + /// Details of the test node. + /// </summary> + public Detail TestNodeDetails { + get { + return test; + } + } + + } +} Property changes on: trunk/xmlunit/src/main/net-core/diff/Comparison.cs ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/net-core/diff/ComparisonListener.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/ComparisonListener.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/diff/ComparisonListener.cs 2010-05-03 16:08:17 UTC (rev 378) @@ -0,0 +1,21 @@ +/* + 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. +*/ + +namespace net.sf.xmlunit.diff { + /// <summary> + /// Is notified of comparisions and their results. + /// </summary> + public delegate void ComparisonListener(Comparison comparison, + ComparisonResult outcome); +} Property changes on: trunk/xmlunit/src/main/net-core/diff/ComparisonListener.cs ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/net-core/diff/ComparisonResult.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/ComparisonResult.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/diff/ComparisonResult.cs 2010-05-03 16:08:17 UTC (rev 378) @@ -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. +*/ + +namespace net.sf.xmlunit.diff { + + /// <summary> + /// The possible outcomes of a comparision. + /// </summary> + public enum ComparisonResult { + /// <summary> + /// The two nodes are the same for the sake of this comparison. + /// </summary> + EQUAL, + /// <summary> + /// The two nodes are different but similar enough to satisfy a + /// weak equality constraint + /// </summary> + SIMILAR, + /// <summary> + /// The two nodes are different. + /// </summary> + DIFFERENT, + /// <summary> + /// The two nodes are different and comparison should stop + /// immediately. + /// </summary> + /// <remarks>Only used as a return type by {@link DifferenceEvaluator}</remarks> + CRITICAL, + } +} Property changes on: trunk/xmlunit/src/main/net-core/diff/ComparisonResult.cs ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/net-core/diff/ComparisonType.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/ComparisonType.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/diff/ComparisonType.cs 2010-05-03 16:08:17 UTC (rev 378) @@ -0,0 +1,122 @@ +/* + 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. +*/ + +namespace net.sf.xmlunit.diff { + + /// <summary> + /// The kinds of comparisons XMLUnit performs. + /// </summary> + public enum ComparisonType { + /// <summary> + /// Do both documents have a DOCTYPE (or neither of each)? + /// </summary> + HAS_DOCTYPE_DECLARATION, + /// <summary> + /// If the documents both have DOCTYPEs, compare the names. + /// </summary> + DOCTYPE_NAME, + /// <summary> + /// If the documents both have DOCTYPEs, compare the PUBLIC + /// identifiers. + /// </summary> + DOCTYPE_PUBLIC, + /// <summary> + /// If the documents both have DOCTYPEs, compare the SYSTEM + /// identifiers. + /// </summary> + DOCTYPE_SYSTEM, + + /// <summary> + /// Check whether both documents provide the same values for + /// xsi:schemaLocation (may even be null). + /// </summary> + SCHEMA_LOCATION, + /// <summary> + /// Check whether both documents provide the same values for + /// xsi:noNamspaceSchemaLocation (may even be null). + /// </summary> + NO_NAMESPACE_SCHEMA_LOCATION, + + /// <summary> + /// Compare the node types. + /// </summary> + NODE_TYPE, + + /// <summary> + /// Compare the node's namespace prefixes. + /// </summary> + NAMESPACE_PREFIX, + /// <summary> + /// Compare the node's namespace URIs. + /// </summary> + NAMESPACE_URI, + + /// <summary> + /// Compare content of CDATA sections. + /// </summary> + CDATA_VALUE, + /// <summary> + /// Compare content of comments. + /// </summary> + COMMENT_VALUE, + /// <summary> + /// Compare content of text nodes. + /// </summary> + TEXT_VALUE, + /// <summary> + /// Compare targets of processing instructions. + /// </summary> + PROCESSING_INSTRUCTION_TARGET, + /// <summary> + /// Compare data of processing instructions. + /// </summary> + PROCESSING_INSTRUCTION_DATA, + + /// <summary> + /// Compare element names. + /// </summary> + ELEMENT_TAG_NAME, + /// <summary> + /// Compare explicit/implicit status of attributes. + /// </summary> + ATTR_VALUE_EXPLICITLY_SPECIFIED, + /// <summary> + /// Compare number of attributes. + /// </summary> + ELEMENT_NUM_ATTRIBUTES, + /// <summary> + /// Compare attribute's value. + /// </summary> + ATTR_VALUE, + /// <summary> + /// Compare number of child nodes. + /// </summary> + CHILD_NODELIST_LENGTH, + /// <summary> + /// Compare order of child nodes. + /// </summary> + CHILD_NODELIST_SEQUENCE, + + /// <summary> + /// Search for a child node matching a specific child node of the + /// other node. + /// </summary> + CHILD_LOOKUP, + /// <summary> + /// Search for an atribute with a name matching a specific + /// attribute of the other node. + /// </summary> + ATTR_NAME_LOOKUP, + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/main/net-core/diff/ComparisonType.cs ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/net-core/diff/DifferenceEvaluator.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/DifferenceEvaluator.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/diff/DifferenceEvaluator.cs 2010-05-03 16:08:17 UTC (rev 378) @@ -0,0 +1,25 @@ +/* + 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. +*/ + +namespace net.sf.xmlunit.diff { + + /// <summary> + /// May alter the outcome of a comparison. + /// </summary> + /// <return>the new result of the comparison, should return {@link + /// ComaprisonResult#CRITICAL} to stop the comparison + /// completely.</return> + public delegate ComparisonResult DifferenceEvaluator(Comparison comparison, + ComparisonResult outcome); +} Property changes on: trunk/xmlunit/src/main/net-core/diff/DifferenceEvaluator.cs ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/net-core/diff/ElementSelector.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/ElementSelector.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/diff/ElementSelector.cs 2010-05-03 16:08:17 UTC (rev 378) @@ -0,0 +1,24 @@ +/* + 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; + +namespace net.sf.xmlunit.diff { + + /// <summary> + /// Strategy for selecting matching elements. + /// </summary> + public delegate bool ElementSelector(XmlElement controlElement, + XmlElement testElement); +} Property changes on: trunk/xmlunit/src/main/net-core/diff/ElementSelector.cs ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/net-core/diff/IDifferenceEngine.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/IDifferenceEngine.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/diff/IDifferenceEngine.cs 2010-05-03 16:08:17 UTC (rev 378) @@ -0,0 +1,54 @@ +/* + 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. +*/ + +namespace net.sf.xmlunit.diff { + + /// <summary> + /// XMLUnit's difference engine. + /// </summary> + public interface IDifferenceEngine { + /// <summary> + /// Is notified of each comparison. + /// </summary> + event ComparisonListener ComparisonListener; + + /// <summary> + /// Is notified of each comparison with outcome {@link + /// ComparisonResult#EQUAL}. + /// </summary> + event ComparisonListener MatchListener; + + /// <summary> + /// Is notified of each comparison with + /// outcome other than {@link ComparisonResult#EQUAL}. + /// </summary> + event ComparisonListener DifferenceListener; + + /// <summary> + /// Sets the strategy for selecting elements to compare. + /// </summary> + ElementSelector ElementSelector { set; } + + /// <summary> + /// Determines whether the comparison should stop after given + /// difference has been found. + /// </summary> + DifferenceEvaluator DifferenceEvaluator { set; } + + /// <summary> + /// Compares two pieces of XML and invokes the registered listeners. + /// </summary> + void Compare(ISource control, ISource test); + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/main/net-core/diff/IDifferenceEngine.cs ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2010-05-07 11:45:57
|
Revision: 383 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=383&view=rev Author: bodewig Date: 2010-05-07 11:45:51 +0000 (Fri, 07 May 2010) Log Message: ----------- default difference evaluators Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonType.java trunk/xmlunit/src/main/net-core/diff/ComparisonType.cs Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluators.java trunk/xmlunit/src/main/net-core/diff/DifferenceEvaluators.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonType.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonType.java 2010-05-07 05:48:03 UTC (rev 382) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonType.java 2010-05-07 11:45:51 UTC (rev 383) @@ -29,12 +29,12 @@ * If the documents both have DOCTYPEs, compare the PUBLIC * identifiers. */ - DOCTYPE_PUBLIC, + DOCTYPE_PUBLIC_ID, /** * If the documents both have DOCTYPEs, compare the SYSTEM * identifiers. */ - DOCTYPE_SYSTEM, + DOCTYPE_SYSTEM_ID, /** * Check whether both documents provide the same values for Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluators.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluators.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluators.java 2010-05-07 11:45:51 UTC (rev 383) @@ -0,0 +1,92 @@ +/* + 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.diff; + +import org.w3c.dom.Node; + +/** + * Evaluators used for the base cases. + */ +public final class DifferenceEvaluators { + private DifferenceEvaluators() { } + + private static final Short CDATA = Node.TEXT_NODE; + private static final Short TEXT = Node.CDATA_SECTION_NODE; + + /** + * The "standard" difference evaluator which decides which + * differences make two XML documents really different and which + * still leave them similar. + */ + public static final DifferenceEvaluator Default = + new DifferenceEvaluator() { + public ComparisonResult evaluate(Comparison comparison, + ComparisonResult outcome) { + if (outcome == ComparisonResult.DIFFERENT) { + switch (comparison.getType()) { + case NODE_TYPE: + Short control = (Short) comparison + .getControlNodeDetails().getValue(); + Short test = (Short) comparison + .getTestNodeDetails().getValue(); + if ((control.equals(TEXT) && test.equals(CDATA)) + || + (control.equals(CDATA) && test.equals(TEXT))) { + outcome = ComparisonResult.SIMILAR; + } + break; + case HAS_DOCTYPE_DECLARATION: + case DOCTYPE_SYSTEM_ID: + case SCHEMA_LOCATION: + case NO_NAMESPACE_SCHEMA_LOCATION: + case NAMESPACE_PREFIX: + case ATTR_VALUE_EXPLICITLY_SPECIFIED: + case CHILD_NODELIST_SEQUENCE: + outcome = ComparisonResult.SIMILAR; + break; + } + } + return outcome; + } + }; + + /** + * Makes the comparison stop as soon as the first "real" + * difference is encountered, uses the {@link #Default default} + * evaluator to decide which differences leave the documents + * simlar. + */ + public static final DifferenceEvaluator defaultStopWhenDifferent + = stopWhenDifferent(Default); + + /** + * Makes the comparison stop as soon as the first "real" + * difference is encountered. + * @param nestedEvaluator provides the initial decision whether a + * difference is "real" or still leaves the documents in a similar + * state. + */ + public static DifferenceEvaluator + stopWhenDifferent(final DifferenceEvaluator nestedEvaluator) { + return new DifferenceEvaluator() { + public ComparisonResult evaluate(Comparison comparison, + ComparisonResult outcome) { + ComparisonResult r = nestedEvaluator.evaluate(comparison, + outcome); + return r == ComparisonResult.DIFFERENT + ? ComparisonResult.CRITICAL : r; + } + }; + } +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluators.java ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/xmlunit/src/main/net-core/diff/ComparisonType.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/ComparisonType.cs 2010-05-07 05:48:03 UTC (rev 382) +++ trunk/xmlunit/src/main/net-core/diff/ComparisonType.cs 2010-05-07 11:45:51 UTC (rev 383) @@ -30,12 +30,12 @@ /// If the documents both have DOCTYPEs, compare the PUBLIC /// identifiers. /// </summary> - DOCTYPE_PUBLIC, + DOCTYPE_PUBLIC_ID, /// <summary> /// If the documents both have DOCTYPEs, compare the SYSTEM /// identifiers. /// </summary> - DOCTYPE_SYSTEM, + DOCTYPE_SYSTEM_ID, /// <summary> /// Check whether both documents provide the same values for Added: trunk/xmlunit/src/main/net-core/diff/DifferenceEvaluators.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/DifferenceEvaluators.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/diff/DifferenceEvaluators.cs 2010-05-07 11:45:51 UTC (rev 383) @@ -0,0 +1,91 @@ +/* + 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; + +namespace net.sf.xmlunit.diff { + + /// <summary> + /// Evaluators used for the base cases. + /// </summary> + public sealed class DifferenceEvaluators { + private DifferenceEvaluators() { } + + /// <summary> + /// The "standard" difference evaluator which decides which + /// differences make two XML documents really different and which + /// still leave them similar. + /// </summary> + public static ComparisonResult Default(Comparison comparison, + ComparisonResult outcome) { + if (outcome == ComparisonResult.DIFFERENT) { + switch (comparison.Type) { + case ComparisonType.NODE_TYPE: + XmlNodeType control = + (XmlNodeType) comparison.ControlNodeDetails.Value; + XmlNodeType test = + (XmlNodeType) comparison.TestNodeDetails.Value; + if ((control == XmlNodeType.Text && test == XmlNodeType.CDATA) + || + (control == XmlNodeType.CDATA && test == XmlNodeType.Text) + ) { + outcome = ComparisonResult.SIMILAR; + } + break; + case ComparisonType.HAS_DOCTYPE_DECLARATION: + case ComparisonType.DOCTYPE_SYSTEM_ID: + case ComparisonType.SCHEMA_LOCATION: + case ComparisonType.NO_NAMESPACE_SCHEMA_LOCATION: + case ComparisonType.NAMESPACE_PREFIX: + case ComparisonType.ATTR_VALUE_EXPLICITLY_SPECIFIED: + case ComparisonType.CHILD_NODELIST_SEQUENCE: + outcome = ComparisonResult.SIMILAR; + break; + } + } + return outcome; + } + + private static readonly DifferenceEvaluator defaultStopWhenDifferent + = StopWhenDifferent(Default); + + /// <summary> + /// Makes the comparison stop as soon as the first "real" + /// difference is encountered, uses the {@link #Default default} + /// evaluator to decide which differences leave the documents + /// simlar. + /// </summary> + public static DifferenceEvaluator DefaultStopWhenDifferent { + get { + return defaultStopWhenDifferent; + } + } + + /// <summary> + /// Makes the comparison stop as soon as the first "real" + /// difference is encountered. + /// </summary> + /// <param name="nestedEvaluator">provides the initial + /// decision whether a difference is "real" or still leaves + /// the documents in a similar state.</param> + public static DifferenceEvaluator + StopWhenDifferent(DifferenceEvaluator nestedEvaluator) { + return delegate(Comparison comparison, ComparisonResult outcome) { + ComparisonResult r = nestedEvaluator(comparison, outcome); + return r == ComparisonResult.DIFFERENT + ? ComparisonResult.CRITICAL : r; + }; + } + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/main/net-core/diff/DifferenceEvaluators.cs ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2010-05-17 12:13:20
|
Revision: 385 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=385&view=rev Author: bodewig Date: 2010-05-17 12:12:14 +0000 (Mon, 17 May 2010) Log Message: ----------- a bunch of convenience methods Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java trunk/xmlunit/src/main/net-core/builder/Input.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2010-05-07 16:14:55 UTC (rev 384) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2010-05-17 12:12:14 UTC (rev 385) @@ -28,12 +28,14 @@ import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; +import javax.xml.transform.URIResolver; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; 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; public class Input { @@ -43,7 +45,7 @@ private static class DOMBuilder implements Builder { private final Source source; - private DOMBuilder(Document d) { + private DOMBuilder(Node d) { source = new DOMSource(d); } public Source build() { @@ -56,6 +58,10 @@ return new DOMBuilder(d); } + public static Builder fromNode(Node n) { + return new DOMBuilder(n); + } + private static class StreamBuilder implements Builder { private final Source source; private StreamBuilder(File f) { @@ -140,15 +146,19 @@ } public static interface TransformationBuilder extends Builder { + TransformationBuilder usingFactory(TransformerFactory f); + TransformationBuilder withOutputProperty(String name, String value); + TransformationBuilder withParameter(String name, Object value); + TransformationBuilder withStylesheet(Builder b); TransformationBuilder withStylesheet(Source s); - TransformationBuilder withStylesheet(Builder b); - TransformationBuilder withParameter(String name, Object value); - TransformationBuilder withOutputProperty(String name, String value); + TransformationBuilder withUriResolver(URIResolver r); } private static class Transformation implements TransformationBuilder { private final Source source; private Source styleSheet; + private TransformerFactory factory; + private URIResolver uriResolver; private final Properties output = new Properties(); private final Map<String, Object> params = new HashMap<String, Object>(); @@ -173,16 +183,32 @@ return this; } + public TransformationBuilder usingFactory(TransformerFactory f) { + factory = f; + return this; + } + + public TransformationBuilder withUriResolver(URIResolver r) { + uriResolver = r; + return this; + } + public Source build() { try { DOMResult r = new DOMResult(); - TransformerFactory fac = TransformerFactory.newInstance(); + TransformerFactory fac = factory; + if (fac == null) { + fac = TransformerFactory.newInstance(); + } Transformer t = null; if (styleSheet != null) { t = fac.newTransformer(styleSheet); } else { t = fac.newTransformer(); } + if (uriResolver != null) { + t.setURIResolver(uriResolver); + } t.setOutputProperties(output); for (Map.Entry<String, Object> ent : params.entrySet()) { t.setParameter(ent.getKey(), ent.getValue()); Modified: trunk/xmlunit/src/main/net-core/builder/Input.cs =================================================================== --- trunk/xmlunit/src/main/net-core/builder/Input.cs 2010-05-07 16:14:55 UTC (rev 384) +++ trunk/xmlunit/src/main/net-core/builder/Input.cs 2010-05-17 12:12:14 UTC (rev 385) @@ -25,7 +25,7 @@ internal class DOMBuilder : IBuilder { private readonly ISource source; - internal DOMBuilder(XmlDocument d) { + internal DOMBuilder(XmlNode d) { source = new DOMSource(d); } public ISource Build() { @@ -37,6 +37,10 @@ return new DOMBuilder(d); } + public static IBuilder FromNode(XmlNode n) { + return new DOMBuilder(n); + } + internal class StreamBuilder : IBuilder { private readonly ISource source; internal StreamBuilder(string s) { @@ -82,18 +86,25 @@ } public interface ITransformationBuilder : IBuilder { - ITransformationBuilder WithStylesheet(ISource s); - ITransformationBuilder WithStylesheet(IBuilder b); + ITransformationBuilder WithDocumentFunction(); ITransformationBuilder WithExtensionObject(string namespaceUri, object extension); ITransformationBuilder WithParameter(string name, string namespaceUri, object parameter); + ITransformationBuilder WithScripting(); + ITransformationBuilder WithStylesheet(ISource s); + ITransformationBuilder WithStylesheet(IBuilder b); + ITransformationBuilder WithXmlResolver(XmlResolver r); + ITransformationBuilder WithoutDocumentFunction(); + ITransformationBuilder WithoutScripting(); } internal class Transformation : ITransformationBuilder { private readonly ISource source; private ISource styleSheet; + private XmlResolver xmlResolver = new XmlUrlResolver(); + private readonly XsltSettings settings = new XsltSettings(); private readonly XsltArgumentList args = new XsltArgumentList(); internal Transformation(ISource s) { source = s; @@ -119,11 +130,42 @@ return this; } + public ITransformationBuilder WithXmlResolver(XmlResolver r) { + xmlResolver = r; + return this; + } + + public ITransformationBuilder WithScripting() { + return WithScripting(true); + } + + public ITransformationBuilder WithoutScripting() { + return WithScripting(false); + } + + private ITransformationBuilder WithScripting(bool b) { + settings.EnableScript = b; + return this; + } + + public ITransformationBuilder WithDocumentFunction() { + return WithDocumentFunction(true); + } + + public ITransformationBuilder WithoutDocumentFunction() { + return WithDocumentFunction(false); + } + + private ITransformationBuilder WithDocumentFunction(bool b) { + settings.EnableDocumentFunction = b; + return this; + } + public ISource Build() { try { XslCompiledTransform t = new XslCompiledTransform(); if (styleSheet != null) { - t.Load(styleSheet.Reader); + t.Load(styleSheet.Reader, settings, xmlResolver); } MemoryStream ms = new MemoryStream(); using (ms) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2010-05-19 08:04:32
|
Revision: 392 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=392&view=rev Author: bodewig Date: 2010-05-19 08:04:25 +0000 (Wed, 19 May 2010) Log Message: ----------- documentation Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java trunk/xmlunit/src/main/net-core/ISource.cs trunk/xmlunit/src/main/net-core/builder/Input.cs Added Paths: ----------- trunk/xmlunit/src/main/net-core/builder/AbstractTransformationBuilder.cs trunk/xmlunit/src/main/net-core/builder/ITransformationBuilderBase.cs trunk/xmlunit/src/main/net-core/builder/Transform.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2010-05-19 06:57:47 UTC (rev 391) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/builder/Input.java 2010-05-19 08:04:25 UTC (rev 392) @@ -32,9 +32,15 @@ import org.w3c.dom.Document; import org.w3c.dom.Node; +/** + * Fluent API to create Source instances. + */ public class Input { public static interface Builder { + /** + * build the actual Source instance. + */ Source build(); } @@ -49,10 +55,16 @@ } } + /** + * Build a Source from a DOM Document. + */ public static Builder fromDocument(Document d) { return new DOMBuilder(d); } + /** + * Build a Source from a DOM Node. + */ public static Builder fromNode(Node n) { return new DOMBuilder(n); } @@ -79,30 +91,51 @@ } } + /** + * Build a Source from a file. + */ public static Builder fromFile(File f) { return new StreamBuilder(f); } + /** + * Build a Source from a named file. + */ public static Builder fromFile(String name) { return new StreamBuilder(new File(name)); } + /** + * Build a Source from a stream. + */ public static Builder fromStream(InputStream s) { return new StreamBuilder(s); } + /** + * Build a Source from a reader. + */ public static Builder fromReader(Reader r) { return new StreamBuilder(r); } + /** + * Build a Source from a string. + */ public static Builder fromMemory(String s) { return fromReader(new StringReader(s)); } + /** + * Build a Source from an array of bytes. + */ public static Builder fromMemory(byte[] b) { return fromStream(new ByteArrayInputStream(b)); } + /** + * Build a Source from an URL. + */ public static Builder fromURL(URL url) { try { InputStream in = null; @@ -136,6 +169,10 @@ } } + /** + * Build a Source from an URI. + * @param uri must represent a valid URL + */ public static Builder fromURI(URI uri) { try { return fromURL(uri.toURL()); @@ -145,6 +182,10 @@ } } + /** + * Build a Source from an URI. + * @param uri must represent a valid URL + */ public static Builder fromURI(String uri) { try { return fromURI(new URI(uri)); @@ -158,6 +199,9 @@ TransformationBuilder usingFactory(TransformerFactory f); TransformationBuilder withOutputProperty(String name, String value); TransformationBuilder withParameter(String name, Object value); + /** + * Sets the stylesheet to use. + */ TransformationBuilder withStylesheet(Builder b); TransformationBuilder withStylesheet(Source s); TransformationBuilder withURIResolver(URIResolver r); @@ -202,10 +246,16 @@ } } + /** + * Build a Source by XSLT transforming a different Source. + */ public static TransformationBuilder byTransforming(Source s) { return new Transformation(s); } + /** + * Build a Source by XSLT transforming a different Source. + */ public static TransformationBuilder byTransforming(Builder b) { return byTransforming(b.build()); } Modified: trunk/xmlunit/src/main/net-core/ISource.cs =================================================================== --- trunk/xmlunit/src/main/net-core/ISource.cs 2010-05-19 06:57:47 UTC (rev 391) +++ trunk/xmlunit/src/main/net-core/ISource.cs 2010-05-19 08:04:25 UTC (rev 392) @@ -14,8 +14,18 @@ using System.Xml; namespace net.sf.xmlunit { + /// <summary> + /// Representation of the various ways to provide pieces of XML to + /// XMLUnit. + /// </summary> public interface ISource { + /// <summary> + /// Provides the content. + /// </summary> XmlReader Reader {get;} + /// <summary> + /// Some sort of Base-URI of this ISource. + /// </summary> string SystemId {get; set;} } } Added: trunk/xmlunit/src/main/net-core/builder/AbstractTransformationBuilder.cs =================================================================== --- trunk/xmlunit/src/main/net-core/builder/AbstractTransformationBuilder.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/builder/AbstractTransformationBuilder.cs 2010-05-19 08:04:25 UTC (rev 392) @@ -0,0 +1,72 @@ +/* + 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 net.sf.xmlunit.transform; + +namespace net.sf.xmlunit.builder { + + internal abstract class AbstractTransformationBuilder<T> + : ITransformationBuilderBase<T> + where T : class, ITransformationBuilderBase<T> { + private readonly Transformation t; + + protected AbstractTransformationBuilder(ISource s) { + t = new Transformation(s); + } + public T WithStylesheet(ISource s) { + t.Stylesheet = s; + return this as T; + } + public T WithExtensionObject(string namespaceUri, object extension) { + t.AddExtensionObject(namespaceUri, extension); + return this as T; + } + public T WithParameter(string name, string namespaceUri, + object parameter) { + t.AddParameter(name, namespaceUri, parameter); + return this as T; + } + public T WithXmlResolver(XmlResolver r) { + t.XmlResolver = r; + return this as T; + } + public T WithScripting() { + return WithScripting(true); + } + public T WithoutScripting() { + return WithScripting(false); + } + private T WithScripting(bool b) { + t.EnableScriptBlocks = b; + return this as T; + } + public T WithDocumentFunction() { + return WithDocumentFunction(true); + } + public T WithoutDocumentFunction() { + return WithDocumentFunction(false); + } + private T WithDocumentFunction(bool b) { + t.EnableDocumentFunction = b; + return this as T; + } + + protected Transformation Helper { + get { + return t; + } + } + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/main/net-core/builder/AbstractTransformationBuilder.cs ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/main/net-core/builder/ITransformationBuilderBase.cs =================================================================== --- trunk/xmlunit/src/main/net-core/builder/ITransformationBuilderBase.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/builder/ITransformationBuilderBase.cs 2010-05-19 08:04:25 UTC (rev 392) @@ -0,0 +1,30 @@ +/* + 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; + +namespace net.sf.xmlunit.builder { + + public interface ITransformationBuilderBase<T> + where T : ITransformationBuilderBase<T> { + T WithDocumentFunction(); + T WithExtensionObject(string namespaceUri, object extension); + T WithParameter(string name, string namespaceUri, object parameter); + T WithScripting(); + T WithStylesheet(ISource s); + T WithXmlResolver(XmlResolver r); + T WithoutDocumentFunction(); + T WithoutScripting(); + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/main/net-core/builder/ITransformationBuilderBase.cs ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/xmlunit/src/main/net-core/builder/Input.cs =================================================================== --- trunk/xmlunit/src/main/net-core/builder/Input.cs 2010-05-19 06:57:47 UTC (rev 391) +++ trunk/xmlunit/src/main/net-core/builder/Input.cs 2010-05-19 08:04:25 UTC (rev 392) @@ -18,8 +18,14 @@ using net.sf.xmlunit.input; namespace net.sf.xmlunit.builder { - public static class Input { + /// <summary> + /// Fluent API to create ISource instances. + /// </summary> + public sealed class Input { public interface IBuilder { + /// <summary> + /// build the actual ISource instance. + /// </summary> ISource Build(); } @@ -33,10 +39,16 @@ } } + /// <summary> + /// Build an ISource from a DOM Document. + /// </summary> public static IBuilder FromDocument(XmlDocument d) { return new DOMBuilder(d); } + /// <summary> + /// Build an ISource from a DOM Node. + /// </summary> public static IBuilder FromNode(XmlNode n) { return new DOMBuilder(n); } @@ -62,10 +74,16 @@ } } + /// <summary> + /// Build an ISource from a named file. + /// </summary> public static IBuilder FromFile(string name) { return new StreamBuilder(name); } + /// <summary> + /// Build an ISource from a stream. + /// </summary> public static IBuilder FromStream(Stream s) { StreamBuilder b = new StreamBuilder(s); if (s is FileStream) { @@ -75,6 +93,9 @@ return b; } + /// <summary> + /// Build an ISource from a reader. + /// </summary> public static IBuilder FromReader(TextReader r) { StreamBuilder b = new StreamBuilder(r); StreamReader s = r as StreamReader; @@ -86,18 +107,32 @@ return b; } + /// <summary> + /// Build an ISource from a string. + /// </summary> public static IBuilder FromMemory(string s) { return FromReader(new StringReader(s)); } + /// <summary> + /// Build an ISource from an array of bytes. + /// </summary> public static IBuilder FromMemory(byte[] b) { return FromStream(new MemoryStream(b)); } + /// <summary> + /// Build an ISource from an URI. + /// <param name="uri">must represent a valid URL</param> + /// </summary> public static IBuilder FromURI(string uri) { return new StreamBuilder(uri); } + /// <summary> + /// Build an ISource from an URI. + /// <param name="uri">must represent a valid URL</param> + /// </summary> public static IBuilder FromURI(System.Uri uri) { return new StreamBuilder(uri.AbsoluteUri); } @@ -111,6 +146,9 @@ object parameter); ITransformationBuilder WithScripting(); ITransformationBuilder WithStylesheet(ISource s); + /// <summary> + /// Sets the stylesheet to use. + /// </summary> ITransformationBuilder WithStylesheet(IBuilder b); ITransformationBuilder WithXmlResolver(XmlResolver r); ITransformationBuilder WithoutDocumentFunction(); @@ -182,9 +220,15 @@ } } + /// <summary> + /// Build an ISource by XSLT transforming a different ISource. + /// </summary> public static ITransformationBuilder ByTransforming(ISource s) { return new Transformation(s); } + /// <summary> + /// Build an ISource by XSLT transforming a different ISource. + /// </summary> public static ITransformationBuilder ByTransforming(IBuilder b) { return ByTransforming(b.Build()); } Added: trunk/xmlunit/src/main/net-core/builder/Transform.cs =================================================================== --- trunk/xmlunit/src/main/net-core/builder/Transform.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/builder/Transform.cs 2010-05-19 08:04:25 UTC (rev 392) @@ -0,0 +1,64 @@ +/* + 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.IO; +using System.Xml; + +namespace net.sf.xmlunit.builder { + + public sealed class Transform { + + public interface IBuilder : ITransformationBuilderBase<IBuilder> { + ITransformationResult Build(); + } + public interface ITransformationResult { + void To(Stream s); + void To(TextWriter t); + void To(XmlWriter x); + string ToString(); + XmlDocument ToDocument(); + } + + + internal class TransformationBuilder + : AbstractTransformationBuilder<IBuilder>, IBuilder, + ITransformationResult { + + internal TransformationBuilder(ISource s) : base(s) { + } + public ITransformationResult Build() { + return this; + } + public override string ToString() { + return Helper.TransformToString(); + } + public XmlDocument ToDocument() { + return Helper.TransformToDocument(); + } + public void To(Stream s) { + Helper.TransformTo(s); + } + public void To(TextWriter w) { + Helper.TransformTo(w); + } + public void To(XmlWriter w) { + Helper.TransformTo(w); + } + } + + public static IBuilder Source(ISource s) { + return new TransformationBuilder(s); + } + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/main/net-core/builder/Transform.cs ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2010-08-23 08:41:53
|
Revision: 434 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=434&view=rev Author: bodewig Date: 2010-08-23 08:41:47 +0000 (Mon, 23 Aug 2010) Log Message: ----------- Simplify XpathNodeTracker and document its more obscure corners. This is a preparation to re-implement it in terms of XPathContext later. Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Linqy.java trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/XpathNodeTracker.java Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Linqy.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Linqy.java 2010-08-20 12:56:17 UTC (rev 433) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Linqy.java 2010-08-23 08:41:47 UTC (rev 434) @@ -14,6 +14,7 @@ package net.sf.xmlunit.util; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; public final class Linqy { @@ -27,4 +28,28 @@ } return a; } + + public static <E> Iterable<E> cast(final Iterable i) { + return new Iterable<E>() { + public Iterator<E> iterator() { + return new CastingIterator<E>(i.iterator()); + } + }; + } + + private static class CastingIterator<E> implements Iterator<E> { + private final Iterator i; + private CastingIterator(Iterator i) { + this.i = i; + } + public void remove() { + i.remove(); + } + public E next() { + return (E) i.next(); + } + public boolean hasNext() { + return i.hasNext(); + } + } } \ No newline at end of file Modified: trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/XpathNodeTracker.java =================================================================== --- trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/XpathNodeTracker.java 2010-08-20 12:56:17 UTC (rev 433) +++ trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/XpathNodeTracker.java 2010-08-23 08:41:47 UTC (rev 434) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2007, Jeff Martin, Tim Bacon +Copyright (c) 2001-2010, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -41,15 +41,32 @@ import java.util.List; import java.util.Map; +import net.sf.xmlunit.util.IterableNodeList; +import net.sf.xmlunit.util.Linqy; + import org.w3c.dom.Attr; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** - * Tracks Nodes visited by the DifferenceEngine and - * converts that information into an Xpath-String to supply - * to the NodeDetail of a Difference instance + * Tracks Nodes visited by the DifferenceEngine and converts that + * information into an Xpath-String to supply to the NodeDetail of a + * Difference instance. + * + * <p>The tracker has the concept of a level which corresponds to the + * depth of the tree it has visited. The {@link #indent indent} + * method creates a new level, making the tracker walk prepare to walk + * down the tree, the {@link #outdent outdent} method moves one level + * up and any information about the previous level is discarded.</p> + * + * <p>At each level there may be a current Node - the last one for + * which {@link #visitedNode visitedNode} or {@link #visited visited} + * has been called - and maybe a current attribute at this node - the + * last one for which {@link #visitedAttribute visitedAttribute} or + * {@link #visited visited} has been called. Attributes are assumed + * to be at the same level as the nodes they belong to.</p> + * @see NodeDetail#getXpathLocation() * @see Difference#getControlNodeDetail * @see Difference#getTestNodeDetail @@ -76,6 +93,13 @@ /** * Call before examining child nodes one level of indentation into DOM + * + * <p>Any subsequent call to {@link #visited visited} is assumed + * to belong to nodes one level deeper into the tree than the + * nodes visited before calling {@link #indent indent}.</p> + * + * <p>As a side effect, the current attribute - if any - is + * reset.</p> */ public void indent() { if (currentEntry != null) { @@ -90,7 +114,7 @@ } /** - * Call after processing attributes of an element and turining to + * Call after processing attributes of an element and turning to * compare the child nodes. */ public void clearTrackedAttribute() { @@ -100,7 +124,13 @@ } /** - * Call after examining child nodes, ie before returning back one level of indentation from DOM + * Call after examining child nodes, ie before returning back one + * level of indentation from DOM. + * + * <p>Any subsequent call to {@link #visited visited} is assumed + * to belong to nodes one level closer to the root of the tree + * than the nodes visited before calling {@link #outdent + * outdent}.</p> */ public void outdent() { int last = indentationList.size() - 1; @@ -113,24 +143,21 @@ /** * Call when visiting a node whose xpath location needs tracking - * @param node the Node being visited + * + * <p>Delegates to {@link #visitedAttribute visitedAttribute} for + * attribute nodes, {@link #visitedNode visitedNode} for elements, + * texts, CDATA sections, comments or processing instructions and + * ignores any other type of node.</p> + * + * @param node the Node being visited - must not be null. */ public void visited(Node node) { - String nodeName; switch(node.getNodeType()) { case Node.ATTRIBUTE_NODE: - nodeName = ((Attr)node).getLocalName(); - if (nodeName == null || nodeName.length() == 0) { - nodeName = node.getNodeName(); - } - visitedAttribute(nodeName); + visitedAttribute(getNodeName(node)); break; case Node.ELEMENT_NODE: - nodeName = ((Element)node).getLocalName(); - if (nodeName == null || nodeName.length() == 0) { - nodeName = node.getNodeName(); - } - visitedNode(node, nodeName); + visitedNode(node, getNodeName(node)); break; case Node.COMMENT_NODE: visitedNode(node, XPATH_COMMENT_IDENTIFIER); @@ -148,10 +175,26 @@ } } + /** + * Invoked by {@link #visited visited} when visited is an element, + * text, CDATA section, comment or processing instruction. + * + * @param visited the visited node - Unit tests call this with + * null values, so it is not safe to assume it will never be null. + * It will never be null when the {@link #visited visited} method + * delegates here. + * @param value the local name of the element or an XPath + * identifier matching the type of node. + */ protected void visitedNode(Node visited, String value) { currentEntry.trackNode(visited, value); } + /** + * Invoked by {@link #visited visited} when visiting an attribute node. + * + * @param visited the local name of the attribute. + */ protected void visitedAttribute(String visited) { currentEntry.trackAttribute(visited); } @@ -160,37 +203,38 @@ * Preload the items in a NodeList by visiting each in turn * Required for pieces of test XML whose node children can be visited * out of sequence by a DifferenceEngine comparison + * + * <p>Makes the nodes of this list known as nodes that are + * visitable at the current level and makes the last child node + * the current node as a side effect.</p> + * * @param nodeList the items to preload */ public void preloadNodeList(NodeList nodeList) { - currentEntry.trackNodesAsWellAsValues(true); - int length = nodeList.getLength(); - for (int i=0; i < length; ++i) { - visited(nodeList.item(i)); - } - currentEntry.trackNodesAsWellAsValues(false); + preloadChildren(new IterableNodeList(nodeList)); } /** * Preload the items in a List by visiting each in turn * Required for pieces of test XML whose node children can be visited * out of sequence by a DifferenceEngine comparison + * + * <p>Makes the nodes of this list known as nodes that are + * visitable at the current level and makes the last child node + * the current node as a side effect.</p> + * * @param nodeList the items to preload */ public void preloadChildList(List nodeList) { - currentEntry.trackNodesAsWellAsValues(true); - int length = nodeList.size(); - for (int i=0; i < length; ++i) { - visited((Node) nodeList.get(i)); - } - currentEntry.trackNodesAsWellAsValues(false); + Iterable<Node> nodes = Linqy.cast(nodeList); + preloadChildren(nodes); } /** * @return the last visited node as an xpath-location String */ public String toXpathString() { - StringBuffer buf = new StringBuffer(); + StringBuilder buf = new StringBuilder(); TrackingEntry nextEntry; for (Iterator iter = indentationList.iterator(); iter.hasNext(); ) { nextEntry = (TrackingEntry) iter.next(); @@ -200,53 +244,93 @@ } /** + * extracts the local name of a node. + */ + private static String getNodeName(Node n) { + String nodeName = n.getLocalName(); + if (nodeName == null || nodeName.length() == 0) { + nodeName = n.getNodeName(); + } + return nodeName; + } + + /** + * Preload the nodes by visiting each in turn. + * Required for pieces of test XML whose node children can be visited + * out of sequence by a DifferenceEngine comparison + * + * <p>Makes the nodes of this list known as nodes that are + * visitable at the current level and makes the last child node + * the current node as a side effect.</p> + * + * @param nodeList the items to preload + */ + private void preloadChildren(Iterable<Node> nodeList) { + currentEntry.trackNodesAsWellAsValues(true); + for (Node n : nodeList) { + visited(n); + } + currentEntry.trackNodesAsWellAsValues(false); + } + + /** * Wrapper class around a mutable <code>int</code> value * Avoids creation of many immutable <code>Integer</code> objects */ private static final class Int { private int value; - public Int(int startAt) { + Int(int startAt) { value = startAt; } - public void increment() { + void increment() { ++value; } - public int getValue() { + int getValue() { return value; } - public Integer toInteger() { - return new Integer(value); + Integer toInteger() { + return Integer.valueOf(value); } } /** - * Holds node tracking details - one instance is used for each level of indentation in a DOM - * Provides reference between a String-ified Node value and the xpath index of that value + * Holds node tracking details - one instance is used for each + * level of indentation in a DOM + * + * Provides reference between a String-ified Node value and the + * xpath index of that value */ private static final class TrackingEntry { - private final Map valueMap = new HashMap(); + // how often has the key been seen at this level of tracking + // (including this occurence)? + private final Map<String, Int> valueMap = new HashMap<String, Int>(); + // the current XPath expression for this level and the current + // attribute - if any private String currentValue, currentAttribute; - private Map nodeReferenceMap; + // may be used if children of this level have been preloaded, + // maps the Node instance to number of same-names XPath + // expression that have been seen at this level already. + private Map<Node, Integer> nodeReferenceMap; + + // node references are tracked while preloading child nodes private boolean trackNodeReferences = false; + + // index of current node obtained from nodeReferenceMap - if any. private Integer nodeReferenceLookup = null; - private Int lookup(String value) { - return (Int) valueMap.get(value); - } - /** * Keep a reference to the current visited (non-attribute) node * @param visited the non-attribute node visited * @param value the String-ified value of the non-attribute node visited */ - public void trackNode(Node visited, String value) { + void trackNode(Node visited, String value) { if (nodeReferenceMap == null || trackNodeReferences) { - Int occurrence = lookup(value); + Int occurrence = valueMap.get(value); if (occurrence == null) { occurrence = new Int(1); valueMap.put(value, occurrence); @@ -257,7 +341,7 @@ nodeReferenceMap.put(visited, occurrence.toInteger()); } } else { - nodeReferenceLookup = (Integer) nodeReferenceMap.get(visited); + nodeReferenceLookup = nodeReferenceMap.get(visited); } currentValue = value; clearTrackedAttribute(); @@ -267,29 +351,29 @@ * Keep a reference to the visited attribute at the current visited node * @param value the attribute visited */ - public void trackAttribute(String visited) { + void trackAttribute(String visited) { currentAttribute = visited; } /** * Clear any reference to the current visited attribute */ - public void clearTrackedAttribute() { + void clearTrackedAttribute() { currentAttribute = null; } /** - * Append the details of the current visited node to a StringBuffer - * @param buf the StringBuffer to append to + * Append the details of the current visited node to a StringBuilder + * @param buf the StringBuilder to append to */ - public void appendEntryTo(StringBuffer buf) { + void appendEntryTo(StringBuilder buf) { if (currentValue == null) { return; } buf.append(XPATH_SEPARATOR).append(currentValue); int value = nodeReferenceLookup == null - ? lookup(currentValue).getValue() : nodeReferenceLookup.intValue(); + ? valueMap.get(currentValue).getValue() : nodeReferenceLookup.intValue(); buf.append(XPATH_NODE_INDEX_START).append(value).append(XPATH_NODE_INDEX_END); if (currentAttribute != null) { @@ -298,10 +382,13 @@ } } - public void trackNodesAsWellAsValues(boolean yesNo) { + /** + * whether the indices of subsequently tracked nodes should be tracked. + */ + void trackNodesAsWellAsValues(boolean yesNo) { this.trackNodeReferences = yesNo; if (yesNo) { - nodeReferenceMap = new HashMap(); + nodeReferenceMap = new HashMap<Node, Integer>(); } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2010-08-31 16:17:43
|
Revision: 448 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=448&view=rev Author: bodewig Date: 2010-08-31 16:17:37 +0000 (Tue, 31 Aug 2010) Log Message: ----------- don't recurse into Attr nodes Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.xml Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml 2010-08-31 15:23:42 UTC (rev 447) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml 2010-08-31 16:17:37 UTC (rev 448) @@ -74,19 +74,29 @@ NodeList controlChildren = control.getChildNodes(); NodeList testChildren = test.getChildNodes(); ]]></literal> + if (control.getNodeType() != Node.ATTRIBUTE_NODE) { <compareExpr type="CHILD_NODELIST_LENGTH" controlExpr="controlChildren.getLength()" testExpr="testChildren.getLength()"/> + <literal><![CDATA[ + } +]]></literal> <compareMethod method="nodeTypeSpecificComparison"/> <literal><![CDATA[ - controlContext - .setChildren(Linqy.map(new IterableNodeList(controlChildren), - TO_NODE_INFO)); - testContext - .setChildren(Linqy.map(new IterableNodeList(testChildren), - TO_NODE_INFO)); - return compareNodeLists(controlChildren, controlContext, - testChildren, testContext); + if (control.getNodeType() != Node.ATTRIBUTE_NODE) { + controlContext + .setChildren(Linqy.map(new IterableNodeList(controlChildren), + TO_NODE_INFO)); + testContext + .setChildren(Linqy.map(new IterableNodeList(testChildren), + TO_NODE_INFO)); +]]></literal> + <compareMethodExpr method="compareNodeLists" + controlExpr="controlChildren" + testExpr="testChildren"/> + <literal><![CDATA[ + } + return lastResult; } /** Modified: trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.xml =================================================================== --- trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.xml 2010-08-31 15:23:42 UTC (rev 447) +++ trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.xml 2010-08-31 16:17:37 UTC (rev 448) @@ -63,24 +63,34 @@ <literal><![CDATA[ XmlNodeList controlChildren = control.ChildNodes; XmlNodeList testChildren = test.ChildNodes; + if (control.NodeType != XmlNodeType.Attribute) { ]]></literal> <compareExpr type="CHILD_NODELIST_LENGTH" controlExpr="controlChildren.Count" testExpr="testChildren.Count"/> + <literal><![CDATA[ + } +]]></literal> <compareMethod method="NodeTypeSpecificComparison"/> <literal><![CDATA[ - IEnumerable<XmlNode> cc = Linqy.Cast<XmlNode>(controlChildren); - controlContext - .SetChildren(Linqy.Map<XmlNode, - XPathContext.INodeInfo>(cc, - TO_NODE_INFO)); - IEnumerable<XmlNode> tc = Linqy.Cast<XmlNode>(testChildren); - testContext - .SetChildren(Linqy.Map<XmlNode, - XPathContext.INodeInfo>(tc, - TO_NODE_INFO)); - return CompareNodeLists(controlChildren, controlContext, - testChildren, testContext); + if (control.NodeType != XmlNodeType.Attribute) { + IEnumerable<XmlNode> cc = Linqy.Cast<XmlNode>(controlChildren); + controlContext + .SetChildren(Linqy.Map<XmlNode, + XPathContext.INodeInfo>(cc, + TO_NODE_INFO)); + IEnumerable<XmlNode> tc = Linqy.Cast<XmlNode>(testChildren); + testContext + .SetChildren(Linqy.Map<XmlNode, + XPathContext.INodeInfo>(tc, + TO_NODE_INFO)); +]]></literal> + <compareMethodExpr method="CompareNodeLists" + controlExpr="controlChildren" + testExpr="testChildren"/> + <literal><![CDATA[ + } + return lastResult; } /// <summary> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2010-08-31 16:32:47
|
Revision: 451 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=451&view=rev Author: bodewig Date: 2010-08-31 16:32:41 +0000 (Tue, 31 Aug 2010) Log Message: ----------- cleanup Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/NewDifferenceEngine.java Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml 2010-08-31 16:24:59 UTC (rev 450) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml 2010-08-31 16:32:41 UTC (rev 451) @@ -73,8 +73,8 @@ <literal><![CDATA[ NodeList controlChildren = control.getChildNodes(); NodeList testChildren = test.getChildNodes(); + if (control.getNodeType() != Node.ATTRIBUTE_NODE) { ]]></literal> - if (control.getNodeType() != Node.ATTRIBUTE_NODE) { <compareExpr type="CHILD_NODELIST_LENGTH" controlExpr="controlChildren.getLength()" testExpr="testChildren.getLength()"/> Modified: trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/NewDifferenceEngine.java =================================================================== --- trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/NewDifferenceEngine.java 2010-08-31 16:24:59 UTC (rev 450) +++ trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/NewDifferenceEngine.java 2010-08-31 16:32:41 UTC (rev 451) @@ -52,7 +52,6 @@ import org.w3c.dom.Comment; import org.w3c.dom.Element; import org.w3c.dom.Node; -import org.w3c.dom.Text; /** * Class that has responsibility for comparing Nodes and notifying a This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2010-09-13 08:29:12
|
Revision: 465 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=465&view=rev Author: bodewig Date: 2010-09-13 08:29:05 +0000 (Mon, 13 Sep 2010) Log Message: ----------- don't fire comparison events when just searching for matching nodes Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/AbstractDifferenceEngine.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml trunk/xmlunit/src/main/net-core/diff/AbstractDifferenceEngine.cs trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.xml Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/AbstractDifferenceEngine.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/AbstractDifferenceEngine.java 2010-09-12 06:12:20 UTC (rev 464) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/AbstractDifferenceEngine.java 2010-09-13 08:29:05 UTC (rev 465) @@ -82,16 +82,23 @@ * listeners and returns the outcome. */ protected final ComparisonResult compare(Comparison comp) { + ComparisonResult altered = compareDontFire(comp); + listeners.fireComparisonPerformed(comp, altered); + return altered; + } + + /** + * Compares the detail values for object equality, lets the + * difference evaluator evaluate the result + */ + protected final ComparisonResult compareDontFire(Comparison comp) { Object controlValue = comp.getControlDetails().getValue(); Object testValue = comp.getTestDetails().getValue(); boolean equal = controlValue == null ? testValue == null : controlValue.equals(testValue); ComparisonResult initial = equal ? ComparisonResult.EQUAL : ComparisonResult.DIFFERENT; - ComparisonResult altered = - getDifferenceEvaluator().evaluate(comp, initial); - listeners.fireComparisonPerformed(comp, altered); - return altered; + return getDifferenceEvaluator().evaluate(comp, initial); } protected static String getXPath(XPathContext ctx) { Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml 2010-09-12 06:12:20 UTC (rev 464) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml 2010-09-13 08:29:05 UTC (rev 465) @@ -481,9 +481,9 @@ .canBeCompared((Element) n1, (Element) n2); } ComparisonResult r = - compare(new Comparison(ComparisonType.NODE_TYPE, - n1, null, n1.getNodeType(), - n2, null, n2.getNodeType())); + compareDontFire(new Comparison(ComparisonType.NODE_TYPE, + n1, null, n1.getNodeType(), + n2, null, n2.getNodeType())); return r != ComparisonResult.CRITICAL; } @@ -515,4 +515,4 @@ } }; ]]></literal> -</class> \ No newline at end of file +</class> Modified: trunk/xmlunit/src/main/net-core/diff/AbstractDifferenceEngine.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/AbstractDifferenceEngine.cs 2010-09-12 06:12:20 UTC (rev 464) +++ trunk/xmlunit/src/main/net-core/diff/AbstractDifferenceEngine.cs 2010-09-13 08:29:05 UTC (rev 465) @@ -68,15 +68,23 @@ /// listeners and returns the outcome. /// </summary> protected internal ComparisonResult Compare(Comparison comp) { + ComparisonResult altered = CompareDontFire(comp); + FireComparisonPerformed(comp, altered); + return altered; + } + + /// <summary> + /// Compares the detail values for object equality, lets the + /// difference evaluator evaluate the result. + /// </summary> + protected internal ComparisonResult CompareDontFire(Comparison comp) { object controlValue = comp.ControlDetails.Value; object testValue = comp.TestDetails.Value; bool equal = controlValue == null ? testValue == null : controlValue.Equals(testValue); ComparisonResult initial = equal ? ComparisonResult.EQUAL : ComparisonResult.DIFFERENT; - ComparisonResult altered = DifferenceEvaluator(comp, initial); - FireComparisonPerformed(comp, altered); - return altered; + return DifferenceEvaluator(comp, initial); } private void FireComparisonPerformed(Comparison comp, Modified: trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.xml =================================================================== --- trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.xml 2010-09-12 06:12:20 UTC (rev 464) +++ trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.xml 2010-09-13 08:29:05 UTC (rev 465) @@ -512,9 +512,9 @@ return ElementSelector(n1 as XmlElement, n2 as XmlElement); } ComparisonResult r = - Compare(new Comparison(ComparisonType.NODE_TYPE, - n1, null, n1.NodeType, - n2, null, n2.NodeType)); + CompareDontFire(new Comparison(ComparisonType.NODE_TYPE, + n1, null, n1.NodeType, + n2, null, n2.NodeType)); return r != ComparisonResult.CRITICAL; } @@ -536,4 +536,4 @@ && n.NodeType != XmlNodeType.XmlDeclaration; } ]]></literal> -</class> \ No newline at end of file +</class> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2010-09-14 10:25:59
|
Revision: 478 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=478&view=rev Author: bodewig Date: 2010-09-14 10:25:52 +0000 (Tue, 14 Sep 2010) Log Message: ----------- don't need compareDontFire anymore Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/AbstractDifferenceEngine.java trunk/xmlunit/src/main/net-core/diff/AbstractDifferenceEngine.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/AbstractDifferenceEngine.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/AbstractDifferenceEngine.java 2010-09-14 10:23:36 UTC (rev 477) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/AbstractDifferenceEngine.java 2010-09-14 10:25:52 UTC (rev 478) @@ -82,23 +82,16 @@ * listeners and returns the outcome. */ protected final ComparisonResult compare(Comparison comp) { - ComparisonResult altered = compareDontFire(comp); - listeners.fireComparisonPerformed(comp, altered); - return altered; - } - - /** - * Compares the detail values for object equality, lets the - * difference evaluator evaluate the result - */ - protected final ComparisonResult compareDontFire(Comparison comp) { Object controlValue = comp.getControlDetails().getValue(); Object testValue = comp.getTestDetails().getValue(); boolean equal = controlValue == null ? testValue == null : controlValue.equals(testValue); ComparisonResult initial = equal ? ComparisonResult.EQUAL : ComparisonResult.DIFFERENT; - return getDifferenceEvaluator().evaluate(comp, initial); + ComparisonResult altered = + getDifferenceEvaluator().evaluate(comp, initial); + listeners.fireComparisonPerformed(comp, altered); + return altered; } protected static String getXPath(XPathContext ctx) { Modified: trunk/xmlunit/src/main/net-core/diff/AbstractDifferenceEngine.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/AbstractDifferenceEngine.cs 2010-09-14 10:23:36 UTC (rev 477) +++ trunk/xmlunit/src/main/net-core/diff/AbstractDifferenceEngine.cs 2010-09-14 10:25:52 UTC (rev 478) @@ -68,23 +68,15 @@ /// listeners and returns the outcome. /// </summary> protected internal ComparisonResult Compare(Comparison comp) { - ComparisonResult altered = CompareDontFire(comp); - FireComparisonPerformed(comp, altered); - return altered; - } - - /// <summary> - /// Compares the detail values for object equality, lets the - /// difference evaluator evaluate the result. - /// </summary> - protected internal ComparisonResult CompareDontFire(Comparison comp) { object controlValue = comp.ControlDetails.Value; object testValue = comp.TestDetails.Value; bool equal = controlValue == null ? testValue == null : controlValue.Equals(testValue); ComparisonResult initial = equal ? ComparisonResult.EQUAL : ComparisonResult.DIFFERENT; - return DifferenceEvaluator(comp, initial); + ComparisonResult altered = DifferenceEvaluator(comp, initial); + FireComparisonPerformed(comp, altered); + return altered; } private void FireComparisonPerformed(Comparison comp, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2010-09-24 09:47:34
|
Revision: 482 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=482&view=rev Author: bodewig Date: 2010-09-24 09:47:28 +0000 (Fri, 24 Sep 2010) Log Message: ----------- docs Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Linqy.java trunk/xmlunit/src/main/net-core/util/Linqy.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Linqy.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Linqy.java 2010-09-17 11:50:31 UTC (rev 481) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Linqy.java 2010-09-24 09:47:28 UTC (rev 482) @@ -18,6 +18,9 @@ import java.util.List; import java.util.NoSuchElementException; +/** + * A couple of (functional) sequence processing constructs. + */ public final class Linqy { /** * Turns the iterable into a list. @@ -30,6 +33,9 @@ return a; } + /** + * Turns an iterable into its type-safe cousin. + */ public static <E> Iterable<E> cast(final Iterable i) { return map(i, new Mapper<Object, E>() { public E map(Object o) { @@ -38,6 +44,9 @@ }); } + /** + * An iterable containing a single element. + */ public static <E> Iterable<E> singleton(final E single) { return new Iterable<E>() { public Iterator<E> iterator() { @@ -46,6 +55,10 @@ }; } + /** + * Create a new iterable by applying a mapper function to each + * element of a given sequence. + */ public static <F, T> Iterable<T> map(final Iterable<F> from, final Mapper<? super F, T> mapper) { return new Iterable<T>() { @@ -55,10 +68,17 @@ }; } + /** + * A function mapping from one type to another. + */ public interface Mapper<F, T> { T map(F from); } + /** + * Exclude all elements from an iterable that don't match a given + * predicate. + */ public static <T> Iterable<T> filter(final Iterable<T> sequence, final Predicate<? super T> filter) { return new Iterable<T>() { @@ -68,10 +88,16 @@ }; } + /** + * A function that tests an object for a property. + */ public interface Predicate<T> { boolean matches(T toTest); } + /** + * Count the number of elements in a sequence. + */ public static int count(Iterable seq) { int c = 0; Iterator it = seq.iterator(); Modified: trunk/xmlunit/src/main/net-core/util/Linqy.cs =================================================================== --- trunk/xmlunit/src/main/net-core/util/Linqy.cs 2010-09-17 11:50:31 UTC (rev 481) +++ trunk/xmlunit/src/main/net-core/util/Linqy.cs 2010-09-24 09:47:28 UTC (rev 482) @@ -17,21 +17,30 @@ namespace net.sf.xmlunit.util { /// <summary> - /// Conversion methods. + /// A couple of (functional) sequence processing constructs. /// </summary> public sealed class Linqy { + + /// <summary> + /// Turns an enumerable into its type-safe cousin. + /// </summary> public static IEnumerable<T> Cast<T>(IEnumerable i) { foreach (T t in i) { yield return t; } } + /// <summary> + /// An enumerable containing a single element. + /// </summary> public static IEnumerable<T> Singleton<T>(T t) { yield return t; } - public delegate T Mapper<F, T>(F from); - + /// <summary> + /// Create a new enumerable by applying a mapper function to + /// each element of a given sequence. + /// </summary> public static IEnumerable<T> Map<F, T>(IEnumerable<F> from, Mapper<F, T> mapper) { foreach (F f in from) { @@ -39,8 +48,15 @@ } } - public delegate bool Predicate<T>(T toTest); + /// <summary> + /// A function mapping from one type to another. + /// </summary> + public delegate T Mapper<F, T>(F from); + /// <summary> + /// Exclude all elements from an enumerable that don't match a + /// given predicate. + /// </summary> public static IEnumerable<T> Filter<T>(IEnumerable<T> sequence, Predicate<T> filter) { foreach (T t in sequence) { @@ -50,6 +66,14 @@ } } + /// <summary> + /// A function that tests an object for a property. + /// </summary> + public delegate bool Predicate<T>(T toTest); + + /// <summary> + /// Count the number of elements in a sequence. + /// </summary> public static int Count(IEnumerable e) { int c = 0; foreach (object o in e) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2010-09-24 09:54:59
|
Revision: 483 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=483&view=rev Author: bodewig Date: 2010-09-24 09:54:53 +0000 (Fri, 24 Sep 2010) Log Message: ----------- extract predicate from linqy Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Linqy.java trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/NewDifferenceEngine.java trunk/xmlunit/src/main/net-core/util/Linqy.cs Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Predicate.java trunk/xmlunit/src/main/net-core/util/Predicate.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml 2010-09-24 09:47:28 UTC (rev 482) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml 2010-09-24 09:54:53 UTC (rev 483) @@ -30,6 +30,7 @@ <import reference="net.sf.xmlunit.util.IterableNodeList"/> <import reference="net.sf.xmlunit.util.Linqy"/> <import reference="net.sf.xmlunit.util.Nodes"/> + <import reference="net.sf.xmlunit.util.Predicate"/> <import reference="org.w3c.dom.Attr"/> <import reference="org.w3c.dom.CharacterData"/> <import reference="org.w3c.dom.Document"/> @@ -473,8 +474,8 @@ } }; - private static final Linqy.Predicate<Node> INTERESTING_NODES = - new Linqy.Predicate<Node>() { + private static final Predicate<Node> INTERESTING_NODES = + new Predicate<Node>() { public boolean matches(Node n) { return n.getNodeType() != Node.DOCUMENT_TYPE_NODE; } Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Linqy.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Linqy.java 2010-09-24 09:47:28 UTC (rev 482) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Linqy.java 2010-09-24 09:54:53 UTC (rev 483) @@ -89,13 +89,6 @@ } /** - * A function that tests an object for a property. - */ - public interface Predicate<T> { - boolean matches(T toTest); - } - - /** * Count the number of elements in a sequence. */ public static int count(Iterable seq) { Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Predicate.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Predicate.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Predicate.java 2010-09-24 09:54:53 UTC (rev 483) @@ -0,0 +1,21 @@ +/* + 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.util; + +/** + * A function that tests an object for a property. + */ +public interface Predicate<T> { + boolean matches(T toTest); +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Predicate.java ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/NewDifferenceEngine.java =================================================================== --- trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/NewDifferenceEngine.java 2010-09-24 09:47:28 UTC (rev 482) +++ trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/NewDifferenceEngine.java 2010-09-24 09:54:53 UTC (rev 483) @@ -60,6 +60,7 @@ import net.sf.xmlunit.input.WhitespaceNormalizedSource; import net.sf.xmlunit.input.WhitespaceStrippedSource; import net.sf.xmlunit.util.Linqy; +import net.sf.xmlunit.util.Predicate; import org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier; import org.w3c.dom.CDATASection; @@ -531,7 +532,7 @@ result.add(new Entry(n, map.get(n))); } else { Iterable<Node> unmatchedTestElements = - Linqy.filter(testNodes, new Linqy.Predicate<Node>() { + Linqy.filter(testNodes, new Predicate<Node>() { public boolean matches(Node t) { return !map.containsValue(t); } Modified: trunk/xmlunit/src/main/net-core/util/Linqy.cs =================================================================== --- trunk/xmlunit/src/main/net-core/util/Linqy.cs 2010-09-24 09:47:28 UTC (rev 482) +++ trunk/xmlunit/src/main/net-core/util/Linqy.cs 2010-09-24 09:54:53 UTC (rev 483) @@ -67,11 +67,6 @@ } /// <summary> - /// A function that tests an object for a property. - /// </summary> - public delegate bool Predicate<T>(T toTest); - - /// <summary> /// Count the number of elements in a sequence. /// </summary> public static int Count(IEnumerable e) { Added: trunk/xmlunit/src/main/net-core/util/Predicate.cs =================================================================== --- trunk/xmlunit/src/main/net-core/util/Predicate.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/util/Predicate.cs 2010-09-24 09:54:53 UTC (rev 483) @@ -0,0 +1,20 @@ +/* + 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. +*/ + +namespace net.sf.xmlunit.util { + /// <summary> + /// A function that tests an object for a property. + /// </summary> + public delegate bool Predicate<T>(T toTest); +} \ No newline at end of file Property changes on: trunk/xmlunit/src/main/net-core/util/Predicate.cs ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2010-09-24 10:52:29
|
Revision: 485 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=485&view=rev Author: bodewig Date: 2010-09-24 10:52:22 +0000 (Fri, 24 Sep 2010) Log Message: ----------- exception handling Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java 2010-09-24 10:42:48 UTC (rev 484) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java 2010-09-24 10:52:22 UTC (rev 485) @@ -23,6 +23,7 @@ import javax.xml.XMLConstants; import javax.xml.namespace.QName; import javax.xml.transform.Source; +import net.sf.xmlunit.exceptions.XMLUnitException; import net.sf.xmlunit.util.Convert; import net.sf.xmlunit.util.IterableNodeList; import net.sf.xmlunit.util.Linqy; @@ -37,6 +38,7 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.ProcessingInstruction; + /** * Difference engine based on DOM. */ @@ -49,8 +51,13 @@ if (test == null) { throw new IllegalArgumentException("test must not be null"); } - compareNodes(Convert.toNode(control), new XPathContext(), - Convert.toNode(test), new XPathContext()); + try { + compareNodes(Convert.toNode(control), new XPathContext(), + Convert.toNode(test), new XPathContext()); + } catch (Exception ex) { + throw new XMLUnitException("Caught exception during comparison", + ex); + } } /** Modified: trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.cs 2010-09-24 10:42:48 UTC (rev 484) +++ trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.cs 2010-09-24 10:52:22 UTC (rev 485) @@ -16,6 +16,7 @@ using System.Collections.Generic; using System.Xml; using System.Xml.Schema; +using net.sf.xmlunit.exceptions; using net.sf.xmlunit.util; namespace net.sf.xmlunit.diff{ @@ -34,11 +35,15 @@ if (test == null) { throw new ArgumentNullException("test"); } - - CompareNodes(net.sf.xmlunit.util.Convert.ToNode(control), - new XPathContext(), - net.sf.xmlunit.util.Convert.ToNode(test), - new XPathContext()); + try { + CompareNodes(net.sf.xmlunit.util.Convert.ToNode(control), + new XPathContext(), + net.sf.xmlunit.util.Convert.ToNode(test), + new XPathContext()); + } catch (Exception ex) { + throw new XMLUnitException("Caught exception during comparison", + ex); + } } /// <summary> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2013-04-14 18:21:38
|
Revision: 528 http://sourceforge.net/p/xmlunit/code/528 Author: bodewig Date: 2013-04-14 18:21:34 +0000 (Sun, 14 Apr 2013) Log Message: ----------- caching of (partial) XPaths to speed up comparisions, submitted by David Rees - fixes #61 Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java trunk/xmlunit/src/main/net-core/diff/XPathContext.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java 2013-04-14 16:29:49 UTC (rev 527) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java 2013-04-14 18:21:34 UTC (rev 528) @@ -16,11 +16,15 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; + import javax.xml.namespace.QName; + import net.sf.xmlunit.util.Nodes; + import org.w3c.dom.Node; public class XPathContext { @@ -131,13 +135,22 @@ } public String getXPath() { - StringBuilder sb = new StringBuilder(); - for (Level l : path) { - sb.append(SEP).append(l.expression); + String xpath = getXPath(path.descendingIterator()); + return xpath.replace(SEP + SEP, SEP); + } + + public String getXPath(Iterator<Level> dIterator) { + if (!dIterator.hasNext()) { + return ""; } - return sb.toString().replace(SEP + SEP, SEP); + Level l = dIterator.next(); + if (null == l.xpath) { + l.xpath = getXPath(dIterator) + SEP + l.expression; + } + return l.xpath; } + private String getName(QName name) { String ns = name.getNamespaceURI(); String p = null; @@ -164,6 +177,7 @@ private final String expression; private List<Level> children = new ArrayList<Level>(); private Map<QName, Level> attributes = new HashMap<QName, Level>(); + private String xpath; private Level(String expression) { this.expression = expression; } Modified: trunk/xmlunit/src/main/net-core/diff/XPathContext.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/XPathContext.cs 2013-04-14 16:29:49 UTC (rev 527) +++ trunk/xmlunit/src/main/net-core/diff/XPathContext.cs 2013-04-14 18:21:34 UTC (rev 528) @@ -128,14 +128,23 @@ public string XPath { get { - StringBuilder sb = new StringBuilder(); - foreach (Level l in path) { - sb.AppendFormat(SEP + "{0}", l.Expression); - } - return sb.Replace(SEP + SEP, SEP).ToString(); + String xpath = EnsureXPathsAreSetOnLevels(path.Last); + return xpath.Replace(SEP + SEP, SEP); } } + private static String EnsureXPathsAreSetOnLevels(LinkedListNode<Level> l) { + if (l == null) { + return string.Empty; + } + Level level = l.Value; + if (null == level.XPath) { + level.XPath = EnsureXPathsAreSetOnLevels(l.Previous) + + SEP + level.Expression; + } + return level.XPath; + } + private string GetName(XmlQualifiedName name) { string ns = name.Namespace; string p = null; @@ -159,10 +168,15 @@ } internal class Level { + private string xpath; internal readonly string Expression; internal readonly IList<Level> Children = new List<Level>(); internal readonly IDictionary<XmlQualifiedName, Level> Attributes = new Dictionary<XmlQualifiedName, Level>(); + internal string XPath { + get { return xpath; } + set { xpath = value; } + } internal Level(string expression) { this.Expression = expression; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2013-04-14 18:30:32
|
Revision: 529 http://sourceforge.net/p/xmlunit/code/529 Author: bodewig Date: 2013-04-14 18:30:29 +0000 (Sun, 14 Apr 2013) Log Message: ----------- cosmetic changes Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java trunk/xmlunit/src/main/net-core/diff/XPathContext.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java 2013-04-14 18:21:34 UTC (rev 528) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java 2013-04-14 18:30:29 UTC (rev 529) @@ -139,7 +139,7 @@ return xpath.replace(SEP + SEP, SEP); } - public String getXPath(Iterator<Level> dIterator) { + private String getXPath(Iterator<Level> dIterator) { if (!dIterator.hasNext()) { return ""; } Modified: trunk/xmlunit/src/main/net-core/diff/XPathContext.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/XPathContext.cs 2013-04-14 18:21:34 UTC (rev 528) +++ trunk/xmlunit/src/main/net-core/diff/XPathContext.cs 2013-04-14 18:30:29 UTC (rev 529) @@ -128,18 +128,18 @@ public string XPath { get { - String xpath = EnsureXPathsAreSetOnLevels(path.Last); + String xpath = GetXPath(path.Last); return xpath.Replace(SEP + SEP, SEP); } } - private static String EnsureXPathsAreSetOnLevels(LinkedListNode<Level> l) { + private static string GetXPath(LinkedListNode<Level> l) { if (l == null) { return string.Empty; } Level level = l.Value; if (null == level.XPath) { - level.XPath = EnsureXPathsAreSetOnLevels(l.Previous) + level.XPath = GetXPath(l.Previous) + SEP + level.Expression; } return level.XPath; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2013-04-14 18:52:22
|
Revision: 530 http://sourceforge.net/p/xmlunit/code/530 Author: bodewig Date: 2013-04-14 18:52:18 +0000 (Sun, 14 Apr 2013) Log Message: ----------- replace in XPath was only needed because the very first level has a path of / - avoid it Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java trunk/xmlunit/src/main/net-core/diff/XPathContext.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java 2013-04-14 18:30:29 UTC (rev 529) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java 2013-04-14 18:52:18 UTC (rev 530) @@ -135,17 +135,20 @@ } public String getXPath() { - String xpath = getXPath(path.descendingIterator()); - return xpath.replace(SEP + SEP, SEP); + return getXPath(path.descendingIterator()); } private String getXPath(Iterator<Level> dIterator) { if (!dIterator.hasNext()) { - return ""; + return EMPTY; } Level l = dIterator.next(); if (null == l.xpath) { - l.xpath = getXPath(dIterator) + SEP + l.expression; + String previous = getXPath(dIterator); + if (!SEP.equals(previous)) { + previous += SEP; + } + l.xpath = previous + l.expression; } return l.xpath; } Modified: trunk/xmlunit/src/main/net-core/diff/XPathContext.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/XPathContext.cs 2013-04-14 18:30:29 UTC (rev 529) +++ trunk/xmlunit/src/main/net-core/diff/XPathContext.cs 2013-04-14 18:52:18 UTC (rev 530) @@ -40,7 +40,7 @@ } else { this.uri2Prefix = new Dictionary<string, string>(uri2Prefix); } - path.AddLast(new Level("")); + path.AddLast(new Level(string.Empty)); } public void NavigateToChild(int index) { @@ -128,8 +128,7 @@ public string XPath { get { - String xpath = GetXPath(path.Last); - return xpath.Replace(SEP + SEP, SEP); + return GetXPath(path.Last); } } @@ -139,8 +138,11 @@ } Level level = l.Value; if (null == level.XPath) { - level.XPath = GetXPath(l.Previous) - + SEP + level.Expression; + string previous = GetXPath(l.Previous); + if (previous != SEP) { + previous += SEP; + } + level.XPath = previous + level.Expression; } return level.XPath; } @@ -151,7 +153,7 @@ if (ns != null) { uri2Prefix.TryGetValue(ns, out p); } - return (p == null ? "" : p + ":") + name.Name; + return (p == null ? string.Empty : p + ":") + name.Name; } /// <summary> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2013-05-01 03:55:03
|
Revision: 537 http://sourceforge.net/p/xmlunit/code/537 Author: bodewig Date: 2013-05-01 03:55:01 +0000 (Wed, 01 May 2013) Log Message: ----------- .NET 2.0 compatibility Modified Paths: -------------- trunk/xmlunit/src/main/net-constraints/ValidationConstraints.cs trunk/xmlunit/src/main/net-core/util/Linqy.cs Modified: trunk/xmlunit/src/main/net-constraints/ValidationConstraints.cs =================================================================== --- trunk/xmlunit/src/main/net-constraints/ValidationConstraints.cs 2013-04-27 15:08:57 UTC (rev 536) +++ trunk/xmlunit/src/main/net-constraints/ValidationConstraints.cs 2013-05-01 03:55:01 UTC (rev 537) @@ -57,8 +57,10 @@ } private string GrabSystemIds() { - return string.Join("\n", Linqy.Map<ISource, - string>(validator.SchemaSources, GrabSystemId)); + return string.Join("\n", Linqy.ToArray(Linqy.Map<ISource, + string>(validator + .SchemaSources, + GrabSystemId))); } private string GrabActual() { @@ -72,8 +74,9 @@ } private string GrabProblems() { - return string.Join(", ", Linqy.Map<ValidationProblem, - string>(result.Problems, ProblemToString)); + return string.Join(", ", Linqy.ToArray(Linqy.Map<ValidationProblem, + string>(result.Problems, + ProblemToString))); } private string ProblemToString(ValidationProblem problem) { Modified: trunk/xmlunit/src/main/net-core/util/Linqy.cs =================================================================== --- trunk/xmlunit/src/main/net-core/util/Linqy.cs 2013-04-27 15:08:57 UTC (rev 536) +++ trunk/xmlunit/src/main/net-core/util/Linqy.cs 2013-05-01 03:55:01 UTC (rev 537) @@ -76,5 +76,12 @@ } return c; } + + /// <summary> + /// Collects the elements of a sequence into an array. + /// </summary> + public static T[] ToArray<T>(IEnumerable<T> e) { + return new List<T>(e).ToArray(); + } } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2014-11-28 19:20:50
|
Revision: 549 http://sourceforge.net/p/xmlunit/code/549 Author: bodewig Date: 2014-11-28 19:20:42 +0000 (Fri, 28 Nov 2014) Log Message: ----------- Require .NET 3.5, use LINQ to Object Modified Paths: -------------- trunk/xmlunit/src/main/net-constraints/ValidationConstraints.cs trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.cs trunk/xmlunit/src/main/net-core/util/Linqy.cs Modified: trunk/xmlunit/src/main/net-constraints/ValidationConstraints.cs =================================================================== --- trunk/xmlunit/src/main/net-constraints/ValidationConstraints.cs 2014-11-24 13:36:39 UTC (rev 548) +++ trunk/xmlunit/src/main/net-constraints/ValidationConstraints.cs 2014-11-28 19:20:42 UTC (rev 549) @@ -12,8 +12,9 @@ limitations under the License. */ +using System.Linq; +using System.Text; using NUnit.Framework.Constraints; -using net.sf.xmlunit.util; using net.sf.xmlunit.validation; namespace net.sf.xmlunit.constraints { @@ -57,10 +58,10 @@ } private string GrabSystemIds() { - return string.Join("\n", Linqy.ToArray(Linqy.Map<ISource, - string>(validator - .SchemaSources, - GrabSystemId))); + return validator.SchemaSources.Select(GrabSystemId) + .Aggregate(new StringBuilder(), + (sb, systemId) => sb.AppendLine(systemId), + sb => sb.Remove(sb.Length - 1, 1).ToString()); } private string GrabActual() { @@ -74,9 +75,10 @@ } private string GrabProblems() { - return string.Join(", ", Linqy.ToArray(Linqy.Map<ValidationProblem, - string>(result.Problems, - ProblemToString))); + return result.Problems + .Aggregate(new StringBuilder(), + (sb, p) => sb.AppendFormat("{0}, ", p), + sb => sb.Remove(sb.Length - 2, 2).ToString()); } private string ProblemToString(ValidationProblem problem) { Modified: trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.cs 2014-11-24 13:36:39 UTC (rev 548) +++ trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.cs 2014-11-28 19:20:42 UTC (rev 549) @@ -14,6 +14,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Xml; using System.Xml.Schema; using net.sf.xmlunit.exceptions; @@ -91,19 +92,17 @@ } IEnumerable<XmlNode> controlChildren = - Linqy.Filter(Linqy.Cast<XmlNode>(control.ChildNodes), - INTERESTING_NODES); + control.ChildNodes.Cast<XmlNode>().Where(INTERESTING_NODES); IEnumerable<XmlNode> testChildren = - Linqy.Filter(Linqy.Cast<XmlNode>(test.ChildNodes), - INTERESTING_NODES); + test.ChildNodes.Cast<XmlNode>().Where(INTERESTING_NODES); if (control.NodeType != XmlNodeType.Attribute) { lastResult = Compare(new Comparison(ComparisonType.CHILD_NODELIST_LENGTH, control, GetXPath(controlContext), - Linqy.Count(controlChildren), + controlChildren.Count(), test, GetXPath(testContext), - Linqy.Count(testChildren))); + testChildren.Count())); if (lastResult == ComparisonResult.CRITICAL) { return lastResult; } @@ -117,13 +116,9 @@ if (control.NodeType != XmlNodeType.Attribute) { controlContext - .SetChildren(Linqy.Map<XmlNode, - XPathContext.INodeInfo>(controlChildren, - TO_NODE_INFO)); + .SetChildren(controlChildren.Select(TO_NODE_INFO)); testContext - .SetChildren(Linqy.Map<XmlNode, - XPathContext.INodeInfo>(testChildren, - TO_NODE_INFO)); + .SetChildren(testChildren.Select(TO_NODE_INFO)); lastResult = CompareNodeLists(controlChildren, controlContext, testChildren, testContext); @@ -346,16 +341,12 @@ Attributes controlAttributes = SplitAttributes(control.Attributes); controlContext - .AddAttributes(Linqy.Map<XmlAttribute, - XmlQualifiedName>(controlAttributes - .RemainingAttributes, - Nodes.GetQName)); + .AddAttributes(controlAttributes.RemainingAttributes + .Select(Nodes.GetQName)); Attributes testAttributes = SplitAttributes(test.Attributes); testContext - .AddAttributes(Linqy.Map<XmlAttribute, - XmlQualifiedName>(testAttributes - .RemainingAttributes, - Nodes.GetQName)); + .AddAttributes(testAttributes.RemainingAttributes + .Select(Nodes.GetQName)); IDictionary<XmlAttribute, object> foundTestAttributes = new Dictionary<XmlAttribute, object>(); Modified: trunk/xmlunit/src/main/net-core/util/Linqy.cs =================================================================== --- trunk/xmlunit/src/main/net-core/util/Linqy.cs 2014-11-24 13:36:39 UTC (rev 548) +++ trunk/xmlunit/src/main/net-core/util/Linqy.cs 2014-11-28 19:20:42 UTC (rev 549) @@ -12,76 +12,20 @@ limitations under the License. */ -using System.Collections; using System.Collections.Generic; namespace net.sf.xmlunit.util { /// <summary> - /// A couple of (functional) sequence processing constructs. + /// Sequence processing constructs not present in System.Linq. /// </summary> public sealed class Linqy { /// <summary> - /// Turns an enumerable into its type-safe cousin. - /// </summary> - public static IEnumerable<T> Cast<T>(IEnumerable i) { - foreach (T t in i) { - yield return t; - } - } - - /// <summary> /// An enumerable containing a single element. /// </summary> public static IEnumerable<T> Singleton<T>(T t) { yield return t; } - /// <summary> - /// Create a new enumerable by applying a mapper function to - /// each element of a given sequence. - /// </summary> - public static IEnumerable<T> Map<F, T>(IEnumerable<F> from, - Mapper<F, T> mapper) { - foreach (F f in from) { - yield return mapper(f); - } - } - - /// <summary> - /// A function mapping from one type to another. - /// </summary> - public delegate T Mapper<F, T>(F from); - - /// <summary> - /// Exclude all elements from an enumerable that don't match a - /// given predicate. - /// </summary> - public static IEnumerable<T> Filter<T>(IEnumerable<T> sequence, - Predicate<T> filter) { - foreach (T t in sequence) { - if (filter(t)) { - yield return t; - } - } - } - - /// <summary> - /// Count the number of elements in a sequence. - /// </summary> - public static int Count(IEnumerable e) { - int c = 0; - foreach (object o in e) { - c++; - } - return c; - } - - /// <summary> - /// Collects the elements of a sequence into an array. - /// </summary> - public static T[] ToArray<T>(IEnumerable<T> e) { - return new List<T>(e).ToArray(); - } } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2014-11-28 20:49:35
|
Revision: 550 http://sourceforge.net/p/xmlunit/code/550 Author: bodewig Date: 2014-11-28 20:49:33 +0000 (Fri, 28 Nov 2014) Log Message: ----------- DOMNodeInfo should be visibly immutable Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java trunk/xmlunit/src/main/net-core/diff/XPathContext.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java 2014-11-28 19:20:42 UTC (rev 549) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/XPathContext.java 2014-11-28 20:49:33 UTC (rev 550) @@ -192,8 +192,8 @@ } public static final class DOMNodeInfo implements NodeInfo { - private QName name; - private short type; + private final QName name; + private final short type; public DOMNodeInfo(Node n) { name = Nodes.getQName(n); type = n.getNodeType(); Modified: trunk/xmlunit/src/main/net-core/diff/XPathContext.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/XPathContext.cs 2014-11-28 19:20:42 UTC (rev 549) +++ trunk/xmlunit/src/main/net-core/diff/XPathContext.cs 2014-11-28 20:49:33 UTC (rev 550) @@ -190,8 +190,8 @@ } public class DOMNodeInfo : INodeInfo { - private XmlQualifiedName name; - private XmlNodeType type; + private readonly XmlQualifiedName name; + private readonly XmlNodeType type; public DOMNodeInfo(XmlNode n) { name = Nodes.GetQName(n); type = n.NodeType; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |