// 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. namespace WixToolset.Core { using System; using System.Collections; using WixToolset.Core.Extensibility; using Wix = WixToolset.Data.Serialize; /// /// The WiX Toolset mutator. /// public class Mutator { private SortedList extensions; private string extensionArgument; /// /// Instantiate a new mutator. /// public Mutator() { this.extensions = new SortedList(); } /// /// Gets or sets the harvester core for the extension. /// /// The harvester core for the extension. public IHarvesterCore Core { get; set; } /// /// Gets or sets the value of the extension argument passed to heat. /// /// The extension argument. public string ExtensionArgument { get { return this.extensionArgument; } set { this.extensionArgument = value; } } /// /// Adds a mutator extension. /// /// The mutator extension to add. public void AddExtension(MutatorExtension mutatorExtension) { this.extensions.Add(mutatorExtension.Sequence, mutatorExtension); } /// /// Mutate a WiX document. /// /// The Wix document element. /// true if mutation was successful public bool Mutate(Wix.Wix wix) { bool encounteredError = false; try { foreach (MutatorExtension mutatorExtension in this.extensions.Values) { if (null == mutatorExtension.Core) { mutatorExtension.Core = this.Core; } mutatorExtension.Mutate(wix); } } finally { encounteredError = this.Core.Messaging.EncounteredError; } // return the Wix document element only if mutation completed successfully return !encounteredError; } /// /// Mutate a WiX document. /// /// The Wix document as a string. /// The mutated Wix document as a string if mutation was successful, else null. public string Mutate(string wixString) { bool encounteredError = false; try { foreach (MutatorExtension mutatorExtension in this.extensions.Values) { if (null == mutatorExtension.Core) { mutatorExtension.Core = this.Core; } wixString = mutatorExtension.Mutate(wixString); if (String.IsNullOrEmpty(wixString) || this.Core.Messaging.EncounteredError) { break; } } } finally { encounteredError = this.Core.Messaging.EncounteredError; } return encounteredError ? null : wixString; } } }