File size: 9,954 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Management.Automation.Host;
using System.Management.Automation.Internal;
using System.Runtime.Serialization;
using System.Security.Permissions;
using Microsoft.PowerShell.Commands.Internal.Format;
namespace System.Management.Automation.Runspaces
{
/// <summary>
/// This exception is used by Formattable constructor to indicate errors
/// occurred during construction time.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "FormatTable")]
public class FormatTableLoadException : RuntimeException
{
private readonly Collection<string> _errors;
#region Constructors
/// <summary>
/// This is the default constructor.
/// </summary>
public FormatTableLoadException()
: base()
{
SetDefaultErrorRecord();
}
/// <summary>
/// This constructor takes a localized error message.
/// </summary>
/// <param name="message">
/// A localized error message.
/// </param>
public FormatTableLoadException(string message)
: base(message)
{
SetDefaultErrorRecord();
}
/// <summary>
/// This constructor takes a localized message and an inner exception.
/// </summary>
/// <param name="message">
/// Localized error message.
/// </param>
/// <param name="innerException">
/// Inner exception.
/// </param>
public FormatTableLoadException(string message, Exception innerException)
: base(message, innerException)
{
SetDefaultErrorRecord();
}
/// <summary>
/// This constructor takes a collection of errors occurred during construction
/// time.
/// </summary>
/// <param name="loadErrors">
/// The errors that occurred.
/// </param>
internal FormatTableLoadException(ConcurrentBag<string> loadErrors)
: base(StringUtil.Format(FormatAndOutXmlLoadingStrings.FormatTableLoadErrors))
{
_errors = new Collection<string>(loadErrors.ToArray());
SetDefaultErrorRecord();
}
/// <summary>
/// This constructor is required by serialization.
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
[Obsolete("Legacy serialization support is deprecated since .NET 8", DiagnosticId = "SYSLIB0051")]
protected FormatTableLoadException(SerializationInfo info, StreamingContext context)
{
throw new NotSupportedException();
}
#endregion Constructors
/// <summary>
/// Set the default ErrorRecord.
/// </summary>
protected void SetDefaultErrorRecord()
{
SetErrorCategory(ErrorCategory.InvalidData);
SetErrorId(typeof(FormatTableLoadException).FullName);
}
/// <summary>
/// The specific Formattable load errors.
/// </summary>
public Collection<string> Errors
{
get
{
return _errors;
}
}
}
/// <summary>
/// A class that keeps the information from format.ps1xml files in a cache table.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "FormatTable")]
public sealed class FormatTable
{
#region Private Data
private readonly TypeInfoDataBaseManager _formatDBMgr;
#endregion
#region Constructor
/// <summary>
/// Default Constructor.
/// </summary>
internal FormatTable()
{
_formatDBMgr = new TypeInfoDataBaseManager();
}
/// <summary>
/// Constructor that creates a FormatTable from a set of format files.
/// </summary>
/// <param name="formatFiles">
/// Format files to load for format information.
/// </param>
/// <exception cref="ArgumentException">
/// 1. Path {0} is not fully qualified. Specify a fully qualified type file path.
/// </exception>
/// <exception cref="FormatTableLoadException">
/// 1. There were errors loading Formattable. Look in the Errors property to
/// get detailed error messages.
/// </exception>
public FormatTable(IEnumerable<string> formatFiles) : this(formatFiles, null, null)
{
}
/// <summary>
/// Append the formatData to the list of formatting configurations, and update the
/// entire formatting database.
/// </summary>
/// <param name="formatData">
/// The formatData is of type 'ExtendedTypeDefinition'. It defines the View configuration
/// including TableControl, ListControl, and WideControl.
/// </param>
/// <exception cref="FormatTableLoadException">
/// 1. There were errors loading Formattable. Look in the Errors property to
/// get detailed error messages.
/// </exception>
public void AppendFormatData(IEnumerable<ExtendedTypeDefinition> formatData)
{
if (formatData == null)
throw PSTraceSource.NewArgumentNullException(nameof(formatData));
_formatDBMgr.AddFormatData(formatData, false);
}
/// <summary>
/// Prepend the formatData to the list of formatting configurations, and update the
/// entire formatting database.
/// </summary>
/// <param name="formatData">
/// The formatData is of type 'ExtendedTypeDefinition'. It defines the View configuration
/// including TableControl, ListControl, and WideControl.
/// </param>
/// <exception cref="FormatTableLoadException">
/// 1. There were errors loading Formattable. Look in the Errors property to
/// get detailed error messages.
/// </exception>
public void PrependFormatData(IEnumerable<ExtendedTypeDefinition> formatData)
{
if (formatData == null)
throw PSTraceSource.NewArgumentNullException(nameof(formatData));
_formatDBMgr.AddFormatData(formatData, true);
}
/// <summary>
/// Constructor that creates a FormatTable from a set of format files.
/// </summary>
/// <param name="formatFiles">
/// Format files to load for format information.
/// </param>
/// <param name="authorizationManager">
/// Authorization manager to perform signature checks before reading ps1xml files (or null of no checks are needed)
/// </param>
/// <param name="host">
/// Host passed to <paramref name="authorizationManager"/>. Can be null if no interactive questions should be asked.
/// </param>
/// <exception cref="ArgumentException">
/// 1. Path {0} is not fully qualified. Specify a fully qualified type file path.
/// </exception>
/// <exception cref="FormatTableLoadException">
/// 1. There were errors loading Formattable. Look in the Errors property to
/// get detailed error messages.
/// </exception>
internal FormatTable(IEnumerable<string> formatFiles, AuthorizationManager authorizationManager, PSHost host)
{
if (formatFiles == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(formatFiles));
}
_formatDBMgr = new TypeInfoDataBaseManager(formatFiles, true, authorizationManager, host);
}
#endregion
#region Internal Methods / Properties
internal TypeInfoDataBaseManager FormatDBManager
{
get { return _formatDBMgr; }
}
/// <summary>
/// Adds the <paramref name="formatFile"/> to the current FormatTable's file list.
/// The FormatTable will not reflect the change until Update is called.
/// </summary>
/// <param name="formatFile"></param>
/// <param name="shouldPrepend">
/// if true, <paramref name="formatFile"/> is prepended to the current FormatTable's file list.
/// if false, it will be appended.
/// </param>
internal void Add(string formatFile, bool shouldPrepend)
{
_formatDBMgr.Add(formatFile, shouldPrepend);
}
/// <summary>
/// Removes the <paramref name="formatFile"/> from the current FormatTable's file list.
/// The FormatTable will not reflect the change until Update is called.
/// </summary>
/// <param name="formatFile"></param>
internal void Remove(string formatFile)
{
_formatDBMgr.Remove(formatFile);
}
#endregion
#region static methods
/// <summary>
/// Returns a format table instance with all default
/// format files loaded.
/// </summary>
/// <returns></returns>
public static FormatTable LoadDefaultFormatFiles()
{
string psHome = Utils.DefaultPowerShellAppBase;
List<string> defaultFormatFiles = new List<string>();
if (!string.IsNullOrEmpty(psHome))
{
defaultFormatFiles.AddRange(Platform.FormatFileNames.Select(file => Path.Combine(psHome, file)));
}
return new FormatTable(defaultFormatFiles);
}
#endregion static methods
}
}
|