forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableView.cs
More file actions
284 lines (250 loc) · 12.5 KB
/
Copy pathTableView.cs
File metadata and controls
284 lines (250 loc) · 12.5 KB
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
278
279
280
281
282
283
284
//
// Copyright (C) Microsoft. All rights reserved.
//
namespace Microsoft.PowerShell.Commands
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Internal;
using Microsoft.PowerShell.Commands.Internal.Format;
using System.IO;
internal class TableView
{
private MshExpressionFactory _expressionFactory;
private TypeInfoDataBase _typeInfoDatabase;
private FormatErrorManager _errorManager;
internal void Initialize(MshExpressionFactory expressionFactory,
TypeInfoDataBase db)
{
_expressionFactory = expressionFactory;
_typeInfoDatabase = db;
// Initialize Format Error Manager.
FormatErrorPolicy formatErrorPolicy = new FormatErrorPolicy();
formatErrorPolicy.ShowErrorsAsMessages = _typeInfoDatabase.defaultSettingsSection.formatErrorPolicy.ShowErrorsAsMessages;
formatErrorPolicy.ShowErrorsInFormattedOutput = _typeInfoDatabase.defaultSettingsSection.formatErrorPolicy.ShowErrorsInFormattedOutput;
_errorManager = new FormatErrorManager(formatErrorPolicy);
}
internal HeaderInfo GenerateHeaderInfo(PSObject input, TableControlBody tableBody, OutGridViewCommand parentCmdlet)
{
HeaderInfo headerInfo = new HeaderInfo();
// This verification is needed because the database returns "LastWriteTime" value for file system objects
// as strings and it is used to detect this situation and use the actual field value.
bool fileSystemObject = typeof(FileSystemInfo).IsInstanceOfType(input.BaseObject);
if (tableBody != null) // If the tableBody is null, the TableControlBody info was not put into the database.
{
// Generate HeaderInfo from the type information database.
List<TableRowItemDefinition> activeRowItemDefinitionList = GetActiveTableRowDefinition(tableBody, input);
int col = 0;
foreach (TableRowItemDefinition rowItem in activeRowItemDefinitionList)
{
ColumnInfo columnInfo = null;
string displayName = null;
TableColumnHeaderDefinition colHeader = null;
// Retrieve a matching TableColumnHeaderDefinition
if (col < tableBody.header.columnHeaderDefinitionList.Count)
colHeader = tableBody.header.columnHeaderDefinitionList[col];
if (colHeader != null && colHeader.label != null)
{
displayName = _typeInfoDatabase.displayResourceManagerCache.GetTextTokenString(colHeader.label);
}
FormatToken token = null;
if (rowItem.formatTokenList.Count > 0)
{
token = rowItem.formatTokenList[0];
}
if (token != null)
{
FieldPropertyToken fpt = token as FieldPropertyToken;
if (fpt != null)
{
if (displayName == null)
{
// Database does not provide a label(DisplayName) for the current property, use the expression value instead.
displayName = fpt.expression.expressionValue;
}
if (fpt.expression.isScriptBlock)
{
MshExpression ex = _expressionFactory.CreateFromExpressionToken(fpt.expression);
// Using the displayName as a propertyName for a stale PSObject.
const string LastWriteTimePropertyName = "LastWriteTime";
// For FileSystem objects "LastWriteTime" property value should be used although the database indicates that a script should be executed to get the value.
if (fileSystemObject && displayName.Equals(LastWriteTimePropertyName, StringComparison.OrdinalIgnoreCase))
{
columnInfo = new OriginalColumnInfo(displayName, displayName, LastWriteTimePropertyName, parentCmdlet);
}
else
{
columnInfo = new ExpressionColumnInfo(displayName, displayName, ex);
}
}
else
{
columnInfo = new OriginalColumnInfo(fpt.expression.expressionValue, displayName, fpt.expression.expressionValue, parentCmdlet);
}
}
else
{
TextToken tt = token as TextToken;
if (tt != null)
{
displayName = _typeInfoDatabase.displayResourceManagerCache.GetTextTokenString(tt);
columnInfo = new OriginalColumnInfo(tt.text, displayName, tt.text, parentCmdlet);
}
}
}
if (columnInfo != null)
{
headerInfo.AddColumn(columnInfo);
}
col++;
}
}
return headerInfo;
}
internal HeaderInfo GenerateHeaderInfo(PSObject input, OutGridViewCommand parentCmdlet)
{
HeaderInfo headerInfo = new HeaderInfo();
List<MshResolvedExpressionParameterAssociation> activeAssociationList;
// Get properties from the default property set of the object
activeAssociationList = AssociationManager.ExpandDefaultPropertySet(input, _expressionFactory);
if (activeAssociationList.Count > 0)
{
// we got a valid set of properties from the default property set..add computername for
// remoteobjects (if available)
if (PSObjectHelper.ShouldShowComputerNameProperty(input))
{
activeAssociationList.Add(new MshResolvedExpressionParameterAssociation(null,
new MshExpression(RemotingConstants.ComputerNameNoteProperty)));
}
}
else
{
// We failed to get anything from the default property set
activeAssociationList = AssociationManager.ExpandAll(input);
if (activeAssociationList.Count > 0)
{
// Remove PSComputerName and PSShowComputerName from the display as needed.
AssociationManager.HandleComputerNameProperties(input, activeAssociationList);
FilterActiveAssociationList(activeAssociationList);
}
else
{
// We were unable to retrieve any properties, so we leave an empty list
activeAssociationList = new List<MshResolvedExpressionParameterAssociation>();
}
}
for (int k = 0; k < activeAssociationList.Count; k++)
{
string propertyName = null;
MshResolvedExpressionParameterAssociation association = activeAssociationList[k];
// set the label of the column
if (association.OriginatingParameter != null)
{
object key = association.OriginatingParameter.GetEntry(FormatParameterDefinitionKeys.LabelEntryKey);
if (key != AutomationNull.Value)
propertyName = (string)key;
}
if (propertyName == null)
{
propertyName = association.ResolvedExpression.ToString();
}
ColumnInfo columnInfo = new OriginalColumnInfo(propertyName, propertyName, propertyName, parentCmdlet);
headerInfo.AddColumn(columnInfo);
}
return headerInfo;
}
/// <summary>
/// Method to filter resolved expressions as per table view needs.
/// For v1.0, table view supports only 10 properties.
///
/// This method filters and updates "activeAssociationList" instance property.
/// </summary>
/// <returns>None.</returns>
/// <remarks>This method updates "activeAssociationList" instance property.</remarks>
private void FilterActiveAssociationList(List<MshResolvedExpressionParameterAssociation> activeAssociationList)
{
// we got a valid set of properties from the default property set
// make sure we do not have too many properties
// NOTE: this is an arbitrary number, chosen to be a sensitive default
int nMax = 256;
if (activeAssociationList.Count > nMax)
{
List<MshResolvedExpressionParameterAssociation> tmp = new List<MshResolvedExpressionParameterAssociation>(activeAssociationList);
activeAssociationList.Clear();
for (int k = 0; k < nMax; k++)
activeAssociationList.Add(tmp[k]);
}
return;
}
private List<TableRowItemDefinition> GetActiveTableRowDefinition(TableControlBody tableBody, PSObject so)
{
if (tableBody.optionalDefinitionList.Count == 0)
{
// we do not have any override, use default
return tableBody.defaultDefinition.rowItemDefinitionList;
}
// see if we have an override that matches
TableRowDefinition matchingRowDefinition = null;
var typeNames = so.InternalTypeNames;
TypeMatch match = new TypeMatch(_expressionFactory, _typeInfoDatabase, typeNames);
foreach (TableRowDefinition x in tableBody.optionalDefinitionList)
{
if (match.PerfectMatch(new TypeMatchItem(x, x.appliesTo)))
{
matchingRowDefinition = x;
break;
}
}
if (matchingRowDefinition == null)
{
matchingRowDefinition = match.BestMatch as TableRowDefinition;
}
if (matchingRowDefinition == null)
{
Collection<string> typesWithoutPrefix = Deserializer.MaskDeserializationPrefix(typeNames);
if (null != typesWithoutPrefix)
{
match = new TypeMatch(_expressionFactory, _typeInfoDatabase, typesWithoutPrefix);
foreach (TableRowDefinition x in tableBody.optionalDefinitionList)
{
if (match.PerfectMatch(new TypeMatchItem(x, x.appliesTo)))
{
matchingRowDefinition = x;
break;
}
}
if (matchingRowDefinition == null)
{
matchingRowDefinition = match.BestMatch as TableRowDefinition;
}
}
}
if (matchingRowDefinition == null)
{
// no matching override, use default
return tableBody.defaultDefinition.rowItemDefinitionList;
}
// we have an override, we need to compute the merge of the active cells
List<TableRowItemDefinition> activeRowItemDefinitionList = new List<TableRowItemDefinition>();
int col = 0;
foreach (TableRowItemDefinition rowItem in matchingRowDefinition.rowItemDefinitionList)
{
// Check if the row is an override or not
if (rowItem.formatTokenList.Count == 0)
{
// It's a place holder, use the default
activeRowItemDefinitionList.Add(tableBody.defaultDefinition.rowItemDefinitionList[col]);
}
else
{
// Use the override
activeRowItemDefinitionList.Add(rowItem);
}
col++;
}
return activeRowItemDefinitionList;
}
}
}