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. |