From: <bo...@us...> - 2010-05-19 06:56:55
|
Revision: 390 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=390&view=rev Author: bodewig Date: 2010-05-19 06:56:49 +0000 (Wed, 19 May 2010) Log Message: ----------- extract XSLT logic into something reusable Modified Paths: -------------- trunk/xmlunit/src/main/net-core/builder/Input.cs trunk/xmlunit/src/tests/net-core/TestResources.cs Added Paths: ----------- trunk/xmlunit/src/main/net-core/transform/ trunk/xmlunit/src/main/net-core/transform/Transformation.cs trunk/xmlunit/src/tests/net-core/transform/ trunk/xmlunit/src/tests/net-core/transform/TransformationTest.cs Modified: trunk/xmlunit/src/main/net-core/builder/Input.cs =================================================================== --- trunk/xmlunit/src/main/net-core/builder/Input.cs 2010-05-19 06:56:18 UTC (rev 389) +++ trunk/xmlunit/src/main/net-core/builder/Input.cs 2010-05-19 06:56:49 UTC (rev 390) @@ -14,7 +14,6 @@ using System; using System.IO; using System.Xml; -using System.Xml.Xsl; using net.sf.xmlunit.exceptions; using net.sf.xmlunit.input; @@ -119,16 +118,12 @@ } 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(); + private readonly net.sf.xmlunit.transform.Transformation t; internal Transformation(ISource s) { - source = s; + t = new net.sf.xmlunit.transform.Transformation(s); } public ITransformationBuilder WithStylesheet(ISource s) { - this.styleSheet = s; + t.Stylesheet = s; return this; } public ITransformationBuilder WithStylesheet(IBuilder b) { @@ -137,19 +132,19 @@ public ITransformationBuilder WithExtensionObject(string namespaceUri, object extension) { - args.AddExtensionObject(namespaceUri, extension); + t.AddExtensionObject(namespaceUri, extension); return this; } public ITransformationBuilder WithParameter(string name, string namespaceUri, object parameter) { - args.AddParam(name, namespaceUri, parameter); + t.AddParameter(name, namespaceUri, parameter); return this; } public ITransformationBuilder WithXmlResolver(XmlResolver r) { - xmlResolver = r; + t.XmlResolver = r; return this; } @@ -162,7 +157,7 @@ } private ITransformationBuilder WithScripting(bool b) { - settings.EnableScript = b; + t.EnableScriptBlocks = b; return this; } @@ -175,25 +170,14 @@ } private ITransformationBuilder WithDocumentFunction(bool b) { - settings.EnableDocumentFunction = b; + t.EnableDocumentFunction = b; return this; } public ISource Build() { - try { - XslCompiledTransform t = new XslCompiledTransform(); - if (styleSheet != null) { - t.Load(styleSheet.Reader, settings, xmlResolver); - } - MemoryStream ms = new MemoryStream(); - using (ms) { - t.Transform(source.Reader, - args, - ms); - } + using (MemoryStream ms = new MemoryStream()) { + t.TransformTo(ms); return FromMemory(ms.ToArray()).Build(); - } catch (System.Exception ex) { - throw new XMLUnitException(ex); } } } Added: trunk/xmlunit/src/main/net-core/transform/Transformation.cs =================================================================== --- trunk/xmlunit/src/main/net-core/transform/Transformation.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/transform/Transformation.cs 2010-05-19 06:56:49 UTC (rev 390) @@ -0,0 +1,225 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +using System; +using System.IO; +using System.Xml; +using System.Xml.Xsl; +using net.sf.xmlunit.exceptions; + +namespace net.sf.xmlunit.transform { + + /// <summary> + /// Provides a convenience layer over System.Xml.Xsl. + /// </summary> + /// <remarks> + /// Apart from ArgumentExceptions if you try to pass in null + /// values only the Transform methods will ever throw exceptions + /// and these will be XMLUnit's exceptions. + /// + /// Each invocation of a Transform method will use a fresh + /// XslCompiledTransform instance, the Transform methods are + /// thread-safe. + /// </remarks> + public sealed class Transformation { + private ISource source; + private ISource styleSheet; + private XmlResolver xmlResolver = null; + private readonly XsltSettings settings = new XsltSettings(); + private readonly XsltArgumentList args = new XsltArgumentList(); + + public Transformation() { + } + /// <param name="s">the source to transform - must not be null.</param> + public Transformation(ISource s) { + Source = s; + } + /// <summary> + /// Set the source document to transform - must not be null. + /// </summary> + public ISource Source { + set { + if (value == null) { + throw new ArgumentNullException(); + } + source = value; + } + } + /// <summary> + /// Set the stylesheet to use - may be null in which case an + /// identity transformation will be performed. + /// </summary> + /// <param name="s">the stylesheet to use</param> + public ISource Stylesheet { + set { + styleSheet = value; + } + } + /// <summary> + /// Add a named extension object. + /// </summary> + public void AddExtensionObject(string namespaceUri, object extension) { + if (namespaceUri == null) { + throw new ArgumentNullException("namespaceUri"); + } + args.AddExtensionObject(namespaceUri, extension); + } + /// <summary> + /// Clears all extension objects and parameters. + /// </summary> + public void Clear() { + args.Clear(); + } + /// <summary> + /// Add a named parameter. + /// </summary> + public void AddParameter(string name, string nsUri, object parameter) { + if (name == null) { + throw new ArgumentNullException("name"); + } + if (nsUri == null) { + throw new ArgumentNullException("nsUri"); + } + args.AddParam(name, nsUri, parameter); + } + /// <summary> + /// Set the resolver to use for document() and xsl:include/import + /// </summary> + /// <remarks>may be null in which case an empty XmlUrlResolver + /// will be used.</remarks> + public XmlResolver XmlResolver { + set { + xmlResolver = value; + } + } + /// <summary> + /// Whether the document() function will be allowed. + /// </summary> + public bool EnableDocumentFunction { + set { + settings.EnableDocumentFunction = value; + } + } + /// <summary> + /// Whether embedded script blocks will be allowed. + /// </summary> + public bool EnableScriptBlocks { + set { + settings.EnableScript = value; + } + } + + /// <summary> + /// Perform the transformation. + /// </summary> + public void TransformTo(Stream stream) { + if (stream == null) { + throw new ArgumentNullException("stream"); + } + Transform(TransformToStream(stream)); + } + + /// <summary> + /// Perform the transformation. + /// </summary> + public void TransformTo(TextWriter writer) { + if (writer == null) { + throw new ArgumentNullException("writer"); + } + Transform(TransformToTextWriter(writer)); + } + + /// <summary> + /// Perform the transformation. + /// </summary> + public void TransformTo(XmlWriter writer) { + if (writer == null) { + throw new ArgumentNullException("writer"); + } + Transform(TransformToXmlWriter(writer)); + } + + /// <summary> + /// Perform the transformation. + /// </summary> + private void Transform(Transformer transformer) { + if (source == null) { + throw new ArgumentNullException("source"); + } + try { + XslCompiledTransform t = new XslCompiledTransform(); + if (styleSheet != null) { + t.Load(styleSheet.Reader, settings, xmlResolver); + } + transformer(t, source.Reader, args); + } catch (System.Exception ex) { + throw new XMLUnitException(ex); + } + } + + /// <summary> + /// Convenience method that returns the result of the + /// transformation as a String. + /// </summary> + public string TransformToString() { + StringWriter sw = new StringWriter(); + TransformTo(sw); + return sw.ToString(); + } + + /// <summary> + /// Convenience method that returns the result of the + /// transformation as a Document. + /// </summary> + public XmlDocument TransformToDocument() { + using (MemoryStream ms = new MemoryStream()) { + TransformTo(ms); + ms.Flush(); + ms.Seek(0, SeekOrigin.Begin); + + XmlDocument doc = new XmlDocument(); + doc.Load(ms); + return doc; + } + } + + private delegate void Transformer(XslCompiledTransform t, + XmlReader r, + XsltArgumentList args); + + private static Transformer TransformToStream(Stream stream) { + return delegate(XslCompiledTransform t, XmlReader r, + XsltArgumentList args) { + t.Transform(r, args, stream); + }; + } + + private static Transformer TransformToTextWriter(TextWriter tw) { + return delegate(XslCompiledTransform t, XmlReader r, + XsltArgumentList args) { + t.Transform(r, args, tw); + }; + } + + private static Transformer TransformToXmlWriter(XmlWriter xw) { + return delegate(XslCompiledTransform t, XmlReader r, + XsltArgumentList args) { + t.Transform(r, args, xw); + }; + } + + } + +} + Property changes on: trunk/xmlunit/src/main/net-core/transform/Transformation.cs ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/xmlunit/src/tests/net-core/TestResources.cs =================================================================== --- trunk/xmlunit/src/tests/net-core/TestResources.cs 2010-05-19 06:56:18 UTC (rev 389) +++ trunk/xmlunit/src/tests/net-core/TestResources.cs 2010-05-19 06:56:49 UTC (rev 390) @@ -17,6 +17,9 @@ public const string ANIMAL_FILE = "../../../src/tests/resources/test1.xml"; public const string BLAME_FILE = "../../../src/tests/resources/test.blame.html"; + public const string ANIMAL_XSL = "../../../src/tests/resources/animal.xsl"; + public const string DOG_FILE = "../../../src/tests/resources/testAnimal.xml"; + private TestResources() { } } } Added: trunk/xmlunit/src/tests/net-core/transform/TransformationTest.cs =================================================================== --- trunk/xmlunit/src/tests/net-core/transform/TransformationTest.cs (rev 0) +++ trunk/xmlunit/src/tests/net-core/transform/TransformationTest.cs 2010-05-19 06:56:49 UTC (rev 390) @@ -0,0 +1,40 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +using System.Xml; +using net.sf.xmlunit.builder; +using NUnit.Framework; + +namespace net.sf.xmlunit.transform { + [TestFixture] + public class TransformationTest { + private Transformation t; + + [SetUp] public void CreateTransformation() { + t = new Transformation(Input.FromFile(TestResources.DOG_FILE) + .Build()); + t.Stylesheet = Input.FromFile(TestResources.ANIMAL_XSL).Build(); + } + + [Test] public void TransformAnimalToString() { + Assert.AreEqual("<?xml version=\"1.0\" encoding=\"utf-16\"?><dog />", + t.TransformToString()); + } + + [Test] public void TransformAnimalToDocument() { + XmlDocument doc = t.TransformToDocument(); + Assert.AreEqual("dog", doc.DocumentElement.Name); + } + } +} Property changes on: trunk/xmlunit/src/tests/net-core/transform/TransformationTest.cs ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |