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