From f680e915f065026efd0301a76fd524f87b8c5f06 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Tue, 19 Dec 2017 12:24:16 -0800 Subject: Simplify message handling --- src/WixToolset.Data/Message.cs | 110 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/WixToolset.Data/Message.cs (limited to 'src/WixToolset.Data/Message.cs') diff --git a/src/WixToolset.Data/Message.cs b/src/WixToolset.Data/Message.cs new file mode 100644 index 00000000..6cd4105c --- /dev/null +++ b/src/WixToolset.Data/Message.cs @@ -0,0 +1,110 @@ +// 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.Data +{ + using System; + using System.Resources; + + /// + /// Event args for message events. + /// + public class Message + { + /// + /// Creates a new Message using a format string. + /// + /// Source line numbers for the message. + /// Message level. + /// Id for the message. + /// Format . + /// Arguments for the format string. + public Message(SourceLineNumber sourceLineNumbers, MessageLevel level, int id, string format, params object[] messageArgs) + { + this.SourceLineNumbers = sourceLineNumbers; + this.Level = level; + this.Id = id; + this.ResourceNameOrFormat = format; + this.MessageArgs = messageArgs; + } + + /// + /// Creates a new Message using a format string from a resource manager. + /// + /// Source line numbers for the message. + /// Message level. + /// Id for the message. + /// Resource manager. + /// Name of the resource. + /// Arguments for the format string. + public Message(SourceLineNumber sourceLineNumbers, MessageLevel level, int id, ResourceManager resourceManager, string resourceName, params object[] messageArgs) + { + this.SourceLineNumbers = sourceLineNumbers; + this.Level = level; + this.Id = id; + this.ResourceManager = resourceManager; + this.ResourceNameOrFormat = resourceName; + this.MessageArgs = messageArgs; + } + + /// + /// Gets the source line numbers. + /// + /// The source line numbers. + public SourceLineNumber SourceLineNumbers { get; } + + /// + /// Gets the Id for the message. + /// + /// The Id for the message. + public int Id { get; } + + /// + /// Gets the resource manager for this event args. + /// + /// The resource manager for this event args. + public ResourceManager ResourceManager { get; } + + /// + /// Gets the name of the resource or format string if no resource manager was provided. + /// + /// The name of the resource or format string. + public string ResourceNameOrFormat { get; } + + /// + /// Gets or sets the for the message. + /// + /// The for the message. + public MessageLevel Level { get; private set; } + + /// + /// Gets the arguments for the format string. + /// + /// The arguments for the format string. + public object[] MessageArgs { get; } + + /// + /// Makes a warning an error. + /// + public void ElevateWarningToError() + { + if (this.Level != MessageLevel.Warning) + { + throw new InvalidOperationException($"Cannot elevate {this.Level.ToString()} Message to an Error. Only Warning Messages can be elevated to an Error."); + } + + this.Level = MessageLevel.Error; + } + + public override string ToString() + { + if (this.ResourceManager == null) + { + return String.Format(this.ResourceNameOrFormat, this.MessageArgs); + } + else + { + return String.Format(this.ResourceManager.GetString(this.ResourceNameOrFormat), this.MessageArgs); + } + } + } +} -- cgit v1.2.3-55-g6feb