| 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 | /// Changes the message into an error message. |
| 87 | /// </summary> |
| 88 | public void ElevateToError() |
| 89 | { |
| 90 | this.Level = MessageLevel.Error; |
| 91 | } |
| 92 | |
| 93 | public override string ToString() |
| 94 | { |
| 95 | if (this.ResourceManager == null) |
| 96 | { |
| 97 | return String.Format(this.ResourceNameOrFormat, this.MessageArgs); |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | return String.Format(this.ResourceManager.GetString(this.ResourceNameOrFormat), this.MessageArgs); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |