Skip to content

Commit

Permalink
Merge pull request #2589 from deqenq/gh-2524
Browse files Browse the repository at this point in the history
GH2524: Added support for XSL arguments in XmlTransform
  • Loading branch information
nils-a authored Jan 30, 2022
2 parents 93d4bf7 + ebec9c0 commit d454dc2
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 21 deletions.
39 changes: 39 additions & 0 deletions src/Cake.Common.Tests/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions src/Cake.Common.Tests/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1433,4 +1433,72 @@ Global
EndGlobalSection
EndGlobal</value>
</data>
<data name="XmlTransformationWithArguments_Xsl" xml:space="preserve">
<value>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
&lt;xsl:output method="html" encoding="utf-8"/&gt;
&lt;xsl:param name="BackgroundColor"&gt;&lt;/xsl:param&gt;
&lt;xsl:param name="Color"&gt;&lt;/xsl:param&gt;

&lt;xsl:template match="/"&gt;
&lt;html&gt;
&lt;body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE"&gt;
&lt;xsl:for-each select="breakfast_menu/food"&gt;
&lt;div style="background-color:{$BackgroundColor};color:{$Color};padding:4px"&gt;
&lt;span style="font-weight:bold"&gt;
&lt;xsl:value-of select="name" /&gt;
-
&lt;/span&gt;
&lt;xsl:value-of select="price" /&gt;
&lt;/div&gt;
&lt;div style="margin-left:20px;margin-bottom:1em;font-size:10pt"&gt;
&lt;p&gt;
&lt;xsl:value-of select="description" /&gt;
&lt;span style="font-style:italic"&gt;
(
&lt;xsl:value-of select="calories" /&gt;
calories per serving)
&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/xsl:for-each&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;</value>
</data>
<data name="XmlTransformationWithArgumentsAndNamespace_Xsl" xml:space="preserve">
<value>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:input="http://example.com" exclude-result-prefixes="input" version="1.0"&gt;
&lt;xsl:output method="html" encoding="utf-8"/&gt;
&lt;xsl:param name="input:BackgroundColor"&gt;&lt;/xsl:param&gt;
&lt;xsl:param name="input:Color"&gt;&lt;/xsl:param&gt;

&lt;xsl:template match="/"&gt;
&lt;html&gt;
&lt;body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE"&gt;
&lt;xsl:for-each select="breakfast_menu/food"&gt;
&lt;div style="background-color:{$input:BackgroundColor};color:{$input:Color};padding:4px"&gt;
&lt;span style="font-weight:bold"&gt;
&lt;xsl:value-of select="name" /&gt;
-
&lt;/span&gt;
&lt;xsl:value-of select="price" /&gt;
&lt;/div&gt;
&lt;div style="margin-left:20px;margin-bottom:1em;font-size:10pt"&gt;
&lt;p&gt;
&lt;xsl:value-of select="description" /&gt;
&lt;span style="font-style:italic"&gt;
(
&lt;xsl:value-of select="calories" /&gt;
calories per serving)
&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/xsl:for-each&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;</value>
</data>
</root>
71 changes: 68 additions & 3 deletions src/Cake.Common.Tests/Unit/XML/XmlTransformationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Xsl;
using Cake.Common.Tests.Fixtures;
using Cake.Common.Tests.Properties;
using Cake.Common.Xml;
Expand Down Expand Up @@ -88,7 +89,7 @@ public void Should_Throw_If_Xml_Not_Exists()
var result = Record.Exception(() => fixture.Transform());

// Then
AssertEx.IsExceptionWithMessage<FileNotFoundException>(result, "XML File not found.");
AssertEx.IsExceptionWithMessage<FileNotFoundException>(result, "XML file not found.");
}

[Fact]
Expand All @@ -104,7 +105,7 @@ public void Should_Throw_If_Xsl_Not_Exists()
var result = Record.Exception(() => fixture.Transform());

// Then
AssertEx.IsExceptionWithMessage<FileNotFoundException>(result, "Xsl File not found.");
AssertEx.IsExceptionWithMessage<FileNotFoundException>(result, "XSL file not found.");
}

[Fact]
Expand Down Expand Up @@ -209,7 +210,7 @@ public void Should_Throw_If_Xsl_Was_Null()
}

[Fact]
public void Should_Throw_If_String_Settings_Was_Null()
public void Should_Throw_If_Xml_Transformation_Settings_Was_Null()
{
// Given
var xml = Resources.XmlTransformation_Xml;
Expand All @@ -221,6 +222,70 @@ public void Should_Throw_If_String_Settings_Was_Null()
// Then
AssertEx.IsArgumentNullException(result, "settings");
}

[Fact]
public void Should_Not_Throw_If_Xsl_Argument_List_Was_Null()
{
// Given
var xml = Resources.XmlTransformation_Xml;
var xsl = Resources.XmlTransformation_Xsl;
XmlTransformationSettings xmlTransformationSettings = new XmlTransformationSettings
{
XsltArgumentList = null
};

// When
var result = Record.Exception(() => XmlTransformation.Transform(xsl, xml, xmlTransformationSettings));

// Then
Assert.Null(result);
}

[Fact]
public void Should_Transform_Xml_String_And_Xsl_String_WithArguments_To_Result_String()
{
// Given
var xml = Resources.XmlTransformation_Xml;
var xsl = Resources.XmlTransformationWithArguments_Xsl;
var htm = Resources.XmlTransformation_Htm_NoXmlDeclaration;
XmlTransformationSettings xmlTransformationSettings = new XmlTransformationSettings
{
OmitXmlDeclaration = true,
Encoding = new UTF8Encoding(false),
XsltArgumentList = new XsltArgumentList()
};
xmlTransformationSettings.XsltArgumentList.AddParam("BackgroundColor", string.Empty, "teal");
xmlTransformationSettings.XsltArgumentList.AddParam("Color", string.Empty, "white");

// When
var result = XmlTransformation.Transform(xsl, xml, xmlTransformationSettings);

// Then
Assert.Equal(htm, result, ignoreLineEndingDifferences: true);
}

[Fact]
public void Should_Transform_Xml_String_And_Xsl_String_WithArgumentsAndNamespace_To_Result_String()
{
// Given
var xml = Resources.XmlTransformation_Xml;
var xsl = Resources.XmlTransformationWithArgumentsAndNamespace_Xsl;
var htm = Resources.XmlTransformation_Htm_NoXmlDeclaration;
XmlTransformationSettings xmlTransformationSettings = new XmlTransformationSettings
{
OmitXmlDeclaration = true,
Encoding = new UTF8Encoding(false),
XsltArgumentList = new XsltArgumentList()
};
xmlTransformationSettings.XsltArgumentList.AddParam("BackgroundColor", "http://example.com", "teal");
xmlTransformationSettings.XsltArgumentList.AddParam("Color", "http://example.com", "white");

// When
var result = XmlTransformation.Transform(xsl, xml, xmlTransformationSettings);

// Then
Assert.Equal(htm, result, ignoreLineEndingDifferences: true);
}
}
}
}
4 changes: 2 additions & 2 deletions src/Cake.Common/Polyfill/XmlTransformationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace Cake.Common.Polyfill
{
internal static class XmlTransformationHelper
{
public static void Transform(XmlReader xsl, XmlReader xml, XmlWriter result)
public static void Transform(XmlReader xsl, XsltArgumentList arguments, XmlReader xml, XmlWriter result)
{
var xslTransform = new XslCompiledTransform();
xslTransform.Load(xsl);
xslTransform.Transform(xml, result);
xslTransform.Transform(xml, arguments, result);
}
}
}
34 changes: 18 additions & 16 deletions src/Cake.Common/Xml/XmlTransformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using Cake.Common.Polyfill;
using Cake.Core;
using Cake.Core.IO;
Expand Down Expand Up @@ -39,7 +40,7 @@ public static string Transform(string xsl, string xml)
/// </summary>
/// <param name="xsl">XML style sheet.</param>
/// <param name="xml">XML data.</param>
/// <param name="settings">Settings for result file xml transformation.</param>
/// <param name="settings">Settings for result file XML transformation.</param>
/// <returns>Transformed XML string.</returns>
public static string Transform(string xsl, string xml, XmlTransformationSettings settings)
{
Expand All @@ -64,7 +65,7 @@ public static string Transform(string xsl, string xml, XmlTransformationSettings
{
using (var result = new MemoryStream())
{
Transform(xslReader, xmlReader, result, settings.XmlWriterSettings);
Transform(xslReader, settings.XsltArgumentList, xmlReader, result, settings.XmlWriterSettings);
result.Position = 0;
return settings.Encoding.GetString(result.ToArray());
}
Expand All @@ -75,8 +76,8 @@ public static string Transform(string xsl, string xml, XmlTransformationSettings
/// Performs XML XSL transformation.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="xslPath">Path to xml style sheet.</param>
/// <param name="xmlPath">Path xml data.</param>
/// <param name="xslPath">Path to XML style sheet.</param>
/// <param name="xmlPath">Path XML data.</param>
/// <param name="resultPath">Transformation result path.</param>
public static void Transform(IFileSystem fileSystem, FilePath xslPath, FilePath xmlPath, FilePath resultPath)
{
Expand All @@ -88,10 +89,10 @@ public static void Transform(IFileSystem fileSystem, FilePath xslPath, FilePath
/// Performs XML XSL transformation.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="xslPath">Path to xml style sheet.</param>
/// <param name="xmlPath">Path xml data.</param>
/// <param name="xslPath">Path to XML style sheet.</param>
/// <param name="xmlPath">Path XML data.</param>
/// <param name="resultPath">Transformation result path.</param>
/// <param name="settings">Settings for result file xml transformation.</param>
/// <param name="settings">Settings for result file XML transformation.</param>
public static void Transform(IFileSystem fileSystem, FilePath xslPath, FilePath xmlPath, FilePath resultPath, XmlTransformationSettings settings)
{
if (fileSystem == null)
Expand Down Expand Up @@ -126,12 +127,12 @@ public static void Transform(IFileSystem fileSystem, FilePath xslPath, FilePath

if (!xslFile.Exists)
{
throw new FileNotFoundException("Xsl File not found.", xslFile.Path.FullPath);
throw new FileNotFoundException("XSL file not found.", xslFile.Path.FullPath);
}

if (!xmlFile.Exists)
{
throw new FileNotFoundException("XML File not found.", xmlFile.Path.FullPath);
throw new FileNotFoundException("XML file not found.", xmlFile.Path.FullPath);
}

if (!settings.Overwrite && resultFile.Exists)
Expand All @@ -149,19 +150,19 @@ public static void Transform(IFileSystem fileSystem, FilePath xslPath, FilePath
xmlReader = XmlReader.Create(xmlStream);

var resultWriter = XmlWriter.Create(resultStream, settings.XmlWriterSettings);

Transform(xslReader, xmlReader, resultWriter);
Transform(xslReader, settings.XsltArgumentList, xmlReader, resultWriter);
}
}

/// <summary>
/// Performs XML XSL transformation.
/// </summary>
/// <param name="xsl">XML style sheet.</param>
/// <param name="arguments">XSLT argument list.</param>
/// <param name="xml">XML data.</param>
/// <param name="result">Transformation result.</param>
/// <param name="settings">Optional settings for result file xml writer.</param>
private static void Transform(TextReader xsl, TextReader xml, Stream result, XmlWriterSettings settings = null)
/// <param name="settings">Optional settings for result file XML writer.</param>
private static void Transform(TextReader xsl, XsltArgumentList arguments, TextReader xml, Stream result, XmlWriterSettings settings = null)
{
if (xsl == null)
{
Expand All @@ -186,16 +187,17 @@ private static void Transform(TextReader xsl, TextReader xml, Stream result, Xml
var xslXmlReader = XmlReader.Create(xsl);
var xmlXmlReader = XmlReader.Create(xml);
var resultXmlTextWriter = XmlWriter.Create(result, settings);
Transform(xslXmlReader, xmlXmlReader, resultXmlTextWriter);
Transform(xslXmlReader, arguments, xmlXmlReader, resultXmlTextWriter);
}

/// <summary>
/// Performs XML XSL transformation.
/// </summary>
/// <param name="xsl">XML style sheet.</param>
/// <param name="arguments">XSLT argument list.</param>
/// <param name="xml">XML data.</param>
/// <param name="result">Transformation result.</param>
private static void Transform(XmlReader xsl, XmlReader xml, XmlWriter result)
private static void Transform(XmlReader xsl, XsltArgumentList arguments, XmlReader xml, XmlWriter result)
{
if (xsl == null)
{
Expand All @@ -212,7 +214,7 @@ private static void Transform(XmlReader xsl, XmlReader xml, XmlWriter result)
throw new ArgumentNullException(nameof(result), "Null result supplied.");
}

XmlTransformationHelper.Transform(xsl, xml, result);
XmlTransformationHelper.Transform(xsl, arguments, xml, result);
}
}
}
6 changes: 6 additions & 0 deletions src/Cake.Common/Xml/XmlTransformationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Text;
using System.Xml;
using System.Xml.Xsl;
using Cake.Common.Polyfill;

namespace Cake.Common.Xml
Expand Down Expand Up @@ -128,6 +129,11 @@ public bool WriteEndDocumentOnClose
set { XmlWriterSettings.WriteEndDocumentOnClose = value; }
}

/// <summary>
/// Gets or sets an argument list for XSL transformation.
/// </summary>
public XsltArgumentList XsltArgumentList { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="XmlTransformationSettings"/> class.
/// </summary>
Expand Down

0 comments on commit d454dc2

Please sign in to comment.