forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlossaryHelpProvider.cs
More file actions
268 lines (232 loc) · 9.29 KB
/
Copy pathGlossaryHelpProvider.cs
File metadata and controls
268 lines (232 loc) · 9.29 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
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System.IO;
using System.Collections;
using System.Collections.ObjectModel;
using System.Xml;
using System.Reflection;
namespace System.Management.Automation
{
/// <summary>
/// Class GlossaryHelpProvider implement the help provider for glossary's.
/// </summary>
///
/// <remarks>
/// Glossary Help information are stored in 'glossary.xml' files. These files are
/// located in the Monad / CustomShell Path as well as in the Application Base
/// of PSSnapIns
/// </remarks>
internal class GlossaryHelpProvider : HelpProviderWithFullCache
{
/// <summary>
/// Constructor for GlossaryHelpProvider
/// </summary>
internal GlossaryHelpProvider(HelpSystem helpSystem) : base(helpSystem)
{
this.HasCustomMatch = true;
}
#region Common Properties
/// <summary>
/// Name of this provider
/// </summary>
/// <value>Name of this provider</value>
internal override string Name
{
get
{
return "Glossary Help Provider";
}
}
/// <summary>
/// Help category for this provider, which is a constant: HelpCategory.Command.
/// </summary>
/// <value>Help category for this provider</value>
internal override HelpCategory HelpCategory
{
get
{
return HelpCategory.Glossary;
}
}
#endregion
#region Load cache
/// <summary>
/// This is for implementing CustomMatch algorithm to be used in
/// HelpProviderWithCache for matching a help target with keys in
/// help cache.
///
/// For each glossary entry, it can contain multiple terms. The
/// key stored in help cache is a concatenation of keys. For example,
/// if there are two terms "foo" and "bar", the key to be used in
/// help cache will be "foo, bar".
///
/// Because of this mangling, key "foo, bar" should match both
/// "foo" and "bar".
///
/// </summary>
/// <param name="target">target to search</param>
/// <param name="key">key used in cache table</param>
/// <returns></returns>
protected sealed override bool CustomMatch(string target, string key)
{
if (String.IsNullOrEmpty(target) || String.IsNullOrEmpty(key))
return false;
string[] terms = key.Split(Utils.Separators.Comma);
for (int i = 0; i < terms.Length; i++)
{
string term = terms[i].Trim();
if (term.Equals(target, StringComparison.OrdinalIgnoreCase))
return true;
}
return false;
}
/// <summary>
/// Load cache for glossary help's
/// </summary>
internal sealed override void LoadCache()
{
Collection<String> files = MUIFileSearcher.SearchFiles("*.glossary.xml", GetSearchPaths());
if (files == null)
return;
foreach (string file in files)
{
if (!_helpFiles.ContainsKey(file))
{
LoadHelpFile(file);
// Add this file into _helpFiles hashtable to prevent it to be loaded again.
_helpFiles[file] = 0;
}
}
}
/// <summary>
/// Load help file for HelpInfo objects. The HelpInfo objects will be
/// put into help cache.
/// </summary>
/// <remarks>
/// 1. Needs to pay special attention about error handling in this function.
/// Common errors include: file not found and invalid xml. None of these error
/// should cause help search to stop.
/// </remarks>
/// <param name="helpFile"></param>
private void LoadHelpFile(string helpFile)
{
if (String.IsNullOrEmpty(helpFile))
{
return;
}
XmlDocument doc;
try
{
doc = InternalDeserializer.LoadUnsafeXmlDocument(
new FileInfo(helpFile),
false, /* ignore whitespace, comments, etc. */
null); /* default maxCharactersInDocument */
}
catch (IOException ioException)
{
ErrorRecord errorRecord = new ErrorRecord(ioException, "HelpFileLoadFailure", ErrorCategory.OpenError, null);
errorRecord.ErrorDetails = new ErrorDetails(typeof(GlossaryHelpProvider).GetTypeInfo().Assembly, "HelpErrors", "HelpFileLoadFailure", helpFile, ioException.Message);
this.HelpSystem.LastErrors.Add(errorRecord);
return;
}
catch (System.Security.SecurityException securityException)
{
ErrorRecord errorRecord = new ErrorRecord(securityException, "HelpFileNotAccessible", ErrorCategory.OpenError, null);
errorRecord.ErrorDetails = new ErrorDetails(typeof(GlossaryHelpProvider).GetTypeInfo().Assembly, "HelpErrors", "HelpFileNotAccessible", helpFile, securityException.Message);
this.HelpSystem.LastErrors.Add(errorRecord);
return;
}
catch (XmlException xmlException)
{
ErrorRecord errorRecord = new ErrorRecord(xmlException, "HelpFileNotValid", ErrorCategory.SyntaxError, null);
errorRecord.ErrorDetails = new ErrorDetails(typeof(GlossaryHelpProvider).GetTypeInfo().Assembly, "HelpErrors", "HelpFileNotValid", helpFile, xmlException.Message);
this.HelpSystem.LastErrors.Add(errorRecord);
return;
}
XmlNode helpItemsNode = null;
if (doc.HasChildNodes)
{
for (int i = 0; i < doc.ChildNodes.Count; i++)
{
XmlNode node = doc.ChildNodes[i];
if (node.NodeType == XmlNodeType.Element && String.Compare(node.Name, "glossary", StringComparison.OrdinalIgnoreCase) == 0)
{
helpItemsNode = node;
break;
}
}
}
if (helpItemsNode == null)
return;
using (this.HelpSystem.Trace(helpFile))
{
if (helpItemsNode.HasChildNodes)
{
for (int i = 0; i < helpItemsNode.ChildNodes.Count; i++)
{
XmlNode node = helpItemsNode.ChildNodes[i];
if (node.NodeType == XmlNodeType.Element && String.Compare(node.Name, "glossaryEntry", StringComparison.OrdinalIgnoreCase) == 0)
{
HelpInfo helpInfo = null;
helpInfo = GlossaryHelpInfo.Load(node);
if (helpInfo != null)
{
this.HelpSystem.TraceErrors(helpInfo.Errors);
AddCache(helpInfo.Name, helpInfo);
}
continue;
}
if (node.NodeType == XmlNodeType.Element && String.Compare(node.Name, "glossaryDiv", StringComparison.OrdinalIgnoreCase) == 0)
{
LoadGlossaryDiv(node);
}
}
}
}
}
/// <summary>
///
/// </summary>
/// <param name="xmlNode"></param>
private void LoadGlossaryDiv(XmlNode xmlNode)
{
if (xmlNode == null)
return;
for (int i = 0; i < xmlNode.ChildNodes.Count; i++)
{
XmlNode node = xmlNode.ChildNodes[i];
if (node.NodeType == XmlNodeType.Element && String.Compare(node.Name, "glossaryEntry", StringComparison.OrdinalIgnoreCase) == 0)
{
HelpInfo helpInfo = null;
helpInfo = GlossaryHelpInfo.Load(node);
if (helpInfo != null)
{
this.HelpSystem.TraceErrors(helpInfo.Errors);
AddCache(helpInfo.Name, helpInfo);
}
}
}
}
#endregion
# region Help Provider Interface
/// <summary>
/// This will reset the help cache. Normally this corresponds to a
/// help culture change.
/// </summary>
internal override void Reset()
{
base.Reset();
_helpFiles.Clear();
}
#endregion
#region Private Data
/// <summary>
/// This is a hashtable to track which help files are loaded already.
///
/// This will avoid one help file getting loaded again and again.
/// </summary>
private readonly Hashtable _helpFiles = new Hashtable();
#endregion
}
}