forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFaqHelpInfo.cs
More file actions
221 lines (183 loc) · 6.71 KB
/
FaqHelpInfo.cs
File metadata and controls
221 lines (183 loc) · 6.71 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
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System.Collections;
using System.Xml;
using System.Diagnostics.CodeAnalysis; // for fxcop
namespace System.Management.Automation
{
/// <summary>
///
/// Class FaqHelpInfo keeps track of help information to be returned by
/// faq help provider.
///
/// </summary>
internal class FaqHelpInfo : HelpInfo
{
/// <summary>
/// Constructor for FaqHelpInfo
/// </summary>
/// <remarks>
/// This constructor is can be called only from constructors of derived class
/// for FaqHelpInfo. The only way to to create a FaqHelpInfo is through
/// static function
/// Load(XmlNode node)
/// where some sanity check is done.
/// </remarks>
[SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors", Justification = "This is internal code and is verified.")]
protected FaqHelpInfo(XmlNode xmlNode)
{
MamlNode mamlNode = new MamlNode(xmlNode);
_fullHelpObject = mamlNode.PSObject;
this.Errors = mamlNode.Errors;
_fullHelpObject.TypeNames.Clear();
_fullHelpObject.TypeNames.Add(string.Format(Globalization.CultureInfo.InvariantCulture,
"FaqHelpInfo#{0}", Name));
_fullHelpObject.TypeNames.Add("FaqHelpInfo");
_fullHelpObject.TypeNames.Add("HelpInfo");
}
#region Basic Help Properties / Methods
/// <summary>
/// Name of faq.
/// </summary>
/// <value>Name of faq</value>
internal override string Name
{
get
{
if (_fullHelpObject == null)
return "";
if (_fullHelpObject.Properties["Title"] == null)
return "";
if (_fullHelpObject.Properties["Title"].Value == null)
return "";
string name = _fullHelpObject.Properties["Title"].Value.ToString();
if (name == null)
return "";
return name.Trim();
}
}
/// <summary>
/// Synopsis for this faq help.
/// </summary>
/// <value>Synopsis for this faq help</value>
internal override string Synopsis
{
get
{
if (_fullHelpObject == null)
return "";
if (_fullHelpObject.Properties["question"] == null)
return "";
if (_fullHelpObject.Properties["question"].Value == null)
return "";
string synopsis = _fullHelpObject.Properties["question"].Value.ToString();
if (synopsis == null)
return "";
return synopsis.Trim();
}
}
/// <summary>
/// Help category for this faq help, which is constantly HelpCategory.FAQ.
/// </summary>
/// <value>Help category for this faq help</value>
internal override HelpCategory HelpCategory
{
get
{
return HelpCategory.FAQ;
}
}
private PSObject _fullHelpObject;
/// <summary>
/// Full help object for this help item.
/// </summary>
/// <value>Full help object for this help item.</value>
internal override PSObject FullHelp
{
get
{
return _fullHelpObject;
}
}
/// <summary>
/// Returns true if help content in help info matches the
/// pattern contained in <paramref name="pattern"/>.
/// The underlying code will usually run pattern.IsMatch() on
/// content it wants to search.
/// FAQ help info looks for pattern in Synopsis and
/// Answers
/// </summary>
/// <param name="pattern"></param>
/// <returns></returns>
internal override bool MatchPatternInContent(WildcardPattern pattern)
{
Diagnostics.Assert(null != pattern, "pattern cannot be null");
string synopsis = Synopsis;
string answers = Answers;
if (null == synopsis)
{
synopsis = string.Empty;
}
if (null == Answers)
{
answers = string.Empty;
}
return pattern.IsMatch(synopsis) || pattern.IsMatch(answers);
}
#endregion
#region Private Methods / Properties
/// <summary>
/// Answers string of this FAQ help info.
/// </summary>
private string Answers
{
get
{
if (this.FullHelp == null)
return "";
if (this.FullHelp.Properties["answer"] == null ||
this.FullHelp.Properties["answer"].Value == null)
{
return "";
}
IList answerItems = FullHelp.Properties["answer"].Value as IList;
if (answerItems == null || answerItems.Count == 0)
{
return "";
}
Text.StringBuilder result = new Text.StringBuilder();
foreach (object answerItem in answerItems)
{
PSObject answerObject = PSObject.AsPSObject(answerItem);
if ((null == answerObject) ||
(null == answerObject.Properties["Text"]) ||
(null == answerObject.Properties["Text"].Value))
{
continue;
}
string text = answerObject.Properties["Text"].Value.ToString();
result.Append(text);
result.Append(Environment.NewLine);
}
return result.ToString().Trim();
}
}
#endregion
#region Load
/// <summary>
/// Create a FaqHelpInfo object from an XmlNode.
/// </summary>
/// <param name="xmlNode">xmlNode that contains help info</param>
/// <returns>FaqHelpInfo object created</returns>
internal static FaqHelpInfo Load(XmlNode xmlNode)
{
FaqHelpInfo faqHelpInfo = new FaqHelpInfo(xmlNode);
if (String.IsNullOrEmpty(faqHelpInfo.Name))
return null;
faqHelpInfo.AddCommonHelpProperties();
return faqHelpInfo;
}
#endregion
}
}