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