diff options
| author | Rob Mensching <rob@firegiant.com> | 2017-12-19 12:24:16 -0800 |
|---|---|---|
| committer | Rob Mensching <rob@firegiant.com> | 2017-12-19 12:24:16 -0800 |
| commit | f680e915f065026efd0301a76fd524f87b8c5f06 (patch) | |
| tree | 63b6a8d7b799b10d77e133209041a809f4f3d144 /src/WixToolset.Data/Message.cs | |
| parent | 77dc8cd1acd5332aa0cb077c7e87d0678756969b (diff) | |
| download | wix-f680e915f065026efd0301a76fd524f87b8c5f06.tar.gz wix-f680e915f065026efd0301a76fd524f87b8c5f06.tar.bz2 wix-f680e915f065026efd0301a76fd524f87b8c5f06.zip | |
Simplify message handling
Diffstat (limited to 'src/WixToolset.Data/Message.cs')
| -rw-r--r-- | src/WixToolset.Data/Message.cs | 110 |
1 files changed, 110 insertions, 0 deletions
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 @@ | |||
| 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.Data | ||
| 4 | { | ||
| 5 | using System; | ||
| 6 | using System.Resources; | ||
| 7 | |||
| 8 | /// <summary> | ||
| 9 | /// Event args for message events. | ||
| 10 | /// </summary> | ||
| 11 | public class Message | ||
| 12 | { | ||
| 13 | /// <summary> | ||
| 14 | /// Creates a new Message using a format string. | ||
| 15 | /// </summary> | ||
| 16 | /// <param name="sourceLineNumbers">Source line numbers for the message.</param> | ||
| 17 | /// <param name="level">Message level.</param> | ||
| 18 | /// <param name="id">Id for the message.</param> | ||
| 19 | /// <param name="format">Format .</param> | ||
| 20 | /// <param name="messageArgs">Arguments for the format string.</param> | ||
| 21 | public Message(SourceLineNumber sourceLineNumbers, MessageLevel level, int id, string format, params object[] messageArgs) | ||
| 22 | { | ||
| 23 | this.SourceLineNumbers = sourceLineNumbers; | ||
| 24 | this.Level = level; | ||
| 25 | this.Id = id; | ||
| 26 | this.ResourceNameOrFormat = format; | ||
| 27 | this.MessageArgs = messageArgs; | ||
| 28 | } | ||
| 29 | |||
| 30 | /// <summary> | ||
| 31 | /// Creates a new Message using a format string from a resource manager. | ||
| 32 | /// </summary> | ||
| 33 | /// <param name="sourceLineNumbers">Source line numbers for the message.</param> | ||
| 34 | /// <param name="level">Message level.</param> | ||
| 35 | /// <param name="id">Id for the message.</param> | ||
| 36 | /// <param name="resourceManager">Resource manager.</param> | ||
| 37 | /// <param name="resourceName">Name of the resource.</param> | ||
| 38 | /// <param name="messageArgs">Arguments for the format string.</param> | ||
| 39 | public Message(SourceLineNumber sourceLineNumbers, MessageLevel level, int id, ResourceManager resourceManager, string resourceName, params object[] messageArgs) | ||
| 40 | { | ||
| 41 | this.SourceLineNumbers = sourceLineNumbers; | ||
| 42 | this.Level = level; | ||
| 43 | this.Id = id; | ||
| 44 | this.ResourceManager = resourceManager; | ||
| 45 | this.ResourceNameOrFormat = resourceName; | ||
| 46 | this.MessageArgs = messageArgs; | ||
| 47 | } | ||
| 48 | |||
| 49 | /// <summary> | ||
| 50 | /// Gets the source line numbers. | ||
| 51 | /// </summary> | ||
| 52 | /// <value>The source line numbers.</value> | ||
| 53 | public SourceLineNumber SourceLineNumbers { get; } | ||
| 54 | |||
| 55 | /// <summary> | ||
| 56 | /// Gets the Id for the message. | ||
| 57 | /// </summary> | ||
| 58 | /// <value>The Id for the message.</value> | ||
| 59 | public int Id { get; } | ||
| 60 | |||
| 61 | /// <summary> | ||
| 62 | /// Gets the resource manager for this event args. | ||
| 63 | /// </summary> | ||
| 64 | /// <value>The resource manager for this event args.</value> | ||
| 65 | public ResourceManager ResourceManager { get; } | ||
| 66 | |||
| 67 | /// <summary> | ||
| 68 | /// Gets the name of the resource or format string if no resource manager was provided. | ||
| 69 | /// </summary> | ||
| 70 | /// <value>The name of the resource or format string.</value> | ||
| 71 | public string ResourceNameOrFormat { get; } | ||
| 72 | |||
| 73 | /// <summary> | ||
| 74 | /// Gets or sets the <see cref="MessageLevel"/> for the message. | ||
| 75 | /// </summary> | ||
| 76 | /// <value>The <see cref="MessageLevel"/> for the message.</value> | ||
| 77 | public MessageLevel Level { get; private set; } | ||
| 78 | |||
| 79 | /// <summary> | ||
| 80 | /// Gets the arguments for the format string. | ||
| 81 | /// </summary> | ||
| 82 | /// <value>The arguments for the format string.</value> | ||
| 83 | public object[] MessageArgs { get; } | ||
| 84 | |||
| 85 | /// <summary> | ||
| 86 | /// Makes a warning an error. | ||
| 87 | /// </summary> | ||
| 88 | public void ElevateWarningToError() | ||
| 89 | { | ||
| 90 | if (this.Level != MessageLevel.Warning) | ||
| 91 | { | ||
| 92 | throw new InvalidOperationException($"Cannot elevate {this.Level.ToString()} Message to an Error. Only Warning Messages can be elevated to an Error."); | ||
| 93 | } | ||
| 94 | |||
| 95 | this.Level = MessageLevel.Error; | ||
| 96 | } | ||
| 97 | |||
| 98 | public override string ToString() | ||
| 99 | { | ||
| 100 | if (this.ResourceManager == null) | ||
| 101 | { | ||
| 102 | return String.Format(this.ResourceNameOrFormat, this.MessageArgs); | ||
| 103 | } | ||
| 104 | else | ||
| 105 | { | ||
| 106 | return String.Format(this.ResourceManager.GetString(this.ResourceNameOrFormat), this.MessageArgs); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||
