| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.Harvesters |
| 4 | { |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using System.Xml; |
| 8 | using System.Xml.Xsl; |
| 9 | using WixToolset.Harvesters.Data; |
| 10 | using WixToolset.Harvesters.Extensibility; |
| 11 | |
| 12 | public sealed class UtilTransformMutator : BaseMutatorExtension |
| 13 | { |
| 14 | private string transform; |
| 15 | private int transformSequence; |
| 16 | |
| 17 | /// <summary> |
| 18 | /// Instantiate a new UtilTransformMutator. |
| 19 | /// </summary> |
| 20 | /// <param name="transform">Path to the XSL transform file.</param> |
| 21 | /// <param name="transformSequence">Order in which the transform should be applied, |
| 22 | /// relative to other transforms.</param> |
| 23 | public UtilTransformMutator(string transform, int transformSequence) |
| 24 | { |
| 25 | this.transform = transform; |
| 26 | this.transformSequence = transformSequence; |
| 27 | } |
| 28 | |
| 29 | /// <summary> |
| 30 | /// Gets the sequence of the extension. |
| 31 | /// </summary> |
| 32 | /// <value>The sequence of the extension.</value> |
| 33 | public override int Sequence |
| 34 | { |
| 35 | get { return 3000 + this.transformSequence; } |
| 36 | } |
| 37 | |
| 38 | /// <summary> |
| 39 | /// Mutate a WiX document as a string. |
| 40 | /// </summary> |
| 41 | /// <param name="wixString">The Wix document element as a string.</param> |
| 42 | /// <returns>The mutated Wix document as a string.</returns> |
| 43 | public override string Mutate(string wixString) |
| 44 | { |
| 45 | try |
| 46 | { |
| 47 | XslCompiledTransform xslt = new XslCompiledTransform(); |
| 48 | xslt.Load(this.transform, XsltSettings.TrustedXslt, new XmlUrlResolver()); |
| 49 | |
| 50 | using (XmlTextReader xmlReader = new XmlTextReader(new StringReader(wixString))) |
| 51 | { |
| 52 | using (StringWriter stringWriter = new StringWriter()) |
| 53 | { |
| 54 | XmlWriterSettings xmlSettings = new XmlWriterSettings(); |
| 55 | xmlSettings.Indent = true; |
| 56 | xmlSettings.IndentChars = " "; |
| 57 | xmlSettings.OmitXmlDeclaration = true; |
| 58 | |
| 59 | using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, xmlSettings)) |
| 60 | { |
| 61 | xslt.Transform(xmlReader, xmlWriter); |
| 62 | } |
| 63 | |
| 64 | wixString = stringWriter.ToString(); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | catch (Exception ex) |
| 69 | { |
| 70 | this.Core.Messaging.Write(HarvesterErrors.ErrorTransformingHarvestedWiX(this.transform, ex.Message)); |
| 71 | return null; |
| 72 | } |
| 73 | |
| 74 | return wixString; |
| 75 | } |
| 76 | } |
| 77 | } |