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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
// ------------------------------------------------------------------------------------------------
// <copyright file="InheritDocumentationComponent.cs" company="Microsoft">
// Copyright © Microsoft Corporation.
// This source file is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
// </copyright>
// <summary>Contains code that indexes XML comments files for <inheritdoc /> tags, reflection files
// for API information and produces a new XML comments file containing the inherited documentation
// for use by Sandcastle.
// </summary>
// ------------------------------------------------------------------------------------------------
namespace Microsoft.Ddue.Tools
{
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Configuration;
using System.Globalization;
using Microsoft.Ddue.Tools.CommandLine;
/// <summary>
/// InheritDocumentationComponent class.
/// </summary>
public class InheritDocumentationComponent : CopyComponent
{
#region private members
/// <summary>
/// XPathExpression for API name.
/// </summary>
private static XPathExpression apiNameExpression = XPathExpression.Compile("string(apidata/@name)");
/// <summary>
/// XPathExpression for API group.
/// </summary>
private static XPathExpression apiGroupExpression = XPathExpression.Compile("string(apidata/@group)");
/// <summary>
/// XPathExpression for API subgroup.
/// </summary>
private static XPathExpression apiSubgroupExpression = XPathExpression.Compile("string(apidata/@subgroup)");
/// <summary>
/// XPathExpression for API ancestors.
/// </summary>
private static XPathExpression typeExpression = XPathExpression.Compile("family/ancestors/type/@api");
/// <summary>
/// XPathExpression for API type interface implementations.
/// </summary>
private static XPathExpression interfaceImplementationExpression = XPathExpression.Compile("implements/type/@api");
/// <summary>
/// XPathExpression for API containers.
/// </summary>
private static XPathExpression containerTypeExpression = XPathExpression.Compile("string(containers/type/@api)");
/// <summary>
/// XPathExpression for override members.
/// </summary>
private static XPathExpression overrideMemberExpression = XPathExpression.Compile("overrides/member/@api");
/// <summary>
/// XPathExpression for API member interface implementaions.
/// </summary>
private static XPathExpression interfaceImplementationMemberExpression = XPathExpression.Compile("implements/member/@api");
/// <summary>
/// XPathExpression for <inheritdoc /> nodes.
/// </summary>
private static XPathExpression inheritDocExpression = XPathExpression.Compile("//inheritdoc");
/// <summary>
/// XPathExpression that looks for example, filterpriority, preliminary, remarks, returns, summary, threadsafety and value nodes.
/// </summary>
private static XPathExpression tagsExpression = XPathExpression.Compile("example|filterpriority|preliminary|remarks|returns|summary|threadsafety|value");
/// <summary>
/// XPathExpression for source nodes.
/// </summary>
private static XPathExpression sourceExpression;
/// <summary>
/// Document to be parsed.
/// </summary>
private XmlDocument sourceDocument;
/// <summary>
/// A cache for comment files.
/// </summary>
private IndexedDocumentCache index;
/// <summary>
/// A cache for reflection files.
/// </summary>
private IndexedDocumentCache reflectionIndex;
#endregion
#region constructor
/// <summary>
/// Creates an instance of InheritDocumentationComponent class.
/// </summary>
/// <param name="configuration">Configuration section to be parsed.</param>
/// <param name="data">A dictionary object with string as key and object as value.</param>
public InheritDocumentationComponent(XPathNavigator configuration, Dictionary<string, object> data)
: base(configuration, data)
{
// get the copy commands
XPathNodeIterator copy_nodes = configuration.Select("copy");
foreach (XPathNavigator copy_node in copy_nodes)
{
// get the comments info
string source_name = copy_node.GetAttribute("name", string.Empty);
if (String.IsNullOrEmpty(source_name))
{
throw new ConfigurationErrorsException("Each copy command must specify an index to copy from.");
}
// get the reflection info
string reflection_name = copy_node.GetAttribute("use", String.Empty);
if (String.IsNullOrEmpty(reflection_name))
{
throw new ConfigurationErrorsException("Each copy command must specify an index to get reflection information from.");
}
this.index = (IndexedDocumentCache)data[source_name];
this.reflectionIndex = (IndexedDocumentCache)data[reflection_name];
}
}
#endregion
#region methods
/// <summary>
/// Deletes the specified node and logs the message.
/// </summary>
/// <param name="inheritDocNodeNavigator">navigator for inheritdoc node</param>
/// <param name="key">Id of the topic specified</param>
public static void DeleteNode(XPathNavigator inheritDocNodeNavigator, string key)
{
ConsoleApplication.WriteMessage(LogLevel.Info, string.Format(CultureInfo.InvariantCulture, "Comments are not found for topic:{0}", key));
inheritDocNodeNavigator.DeleteSelf();
}
/// <summary>
/// Implement inheritDocumentation.
/// </summary>
/// <param name="document">document to be parsed</param>
/// <param name="key">Id pf the topic specified</param>
public override void Apply(XmlDocument document, string key)
{
// default selection filter set not to inherit <overloads>.
sourceExpression = XPathExpression.Compile("*[not(local-name()='overloads')]");
this.sourceDocument = document;
this.InheritDocumentation(key);
}
/// <summary>
/// Inherit the documentation.
/// </summary>
/// <param name="key">Id of the topic specified</param>
public void InheritDocumentation(string key)
{
foreach (XPathNavigator inheritDocNodeNavigator in this.sourceDocument.CreateNavigator().Select(inheritDocExpression))
{
inheritDocNodeNavigator.MoveToParent();
XPathNodeIterator iterator = (XPathNodeIterator) inheritDocNodeNavigator.CreateNavigator().Evaluate(tagsExpression);
// do not inherit the comments if the tags specified in tagsExpression are already present.
if (iterator.Count != 0)
{
inheritDocNodeNavigator.MoveTo(this.sourceDocument.CreateNavigator().SelectSingleNode(inheritDocExpression));
inheritDocNodeNavigator.DeleteSelf();
continue;
}
inheritDocNodeNavigator.MoveTo(this.sourceDocument.CreateNavigator().SelectSingleNode(inheritDocExpression));
// Inherit from the specified API [id=cref].
string cref = inheritDocNodeNavigator.GetAttribute("cref", string.Empty);
if (!string.IsNullOrEmpty(cref))
{
XPathNavigator contentNodeNavigator = this.index.GetContent(cref);
// if no comments were found for the specified api, delete the <inheritdoc /> node,
// otherwise update the <inheritdoc /> node with the comments from the specified api.
if (contentNodeNavigator == null)
{
DeleteNode(inheritDocNodeNavigator, cref);
}
else
{
this.UpdateNode(inheritDocNodeNavigator, contentNodeNavigator);
if (this.sourceDocument.CreateNavigator().Select(inheritDocExpression).Count != 0)
{
this.InheritDocumentation(cref);
}
}
}
else
{
XPathNavigator reflectionNodeNavigator = this.reflectionIndex.GetContent(key);
// no reflection information was found for the api, so delete <inheritdoc /> node.
if (reflectionNodeNavigator == null)
{
DeleteNode(inheritDocNodeNavigator, key);
continue;
}
string group = (string)reflectionNodeNavigator.Evaluate(apiGroupExpression);
string subgroup = (string)reflectionNodeNavigator.Evaluate(apiSubgroupExpression);
if (group == "type")
{
// Inherit from base types
XPathNodeIterator typeNodeIterator = (XPathNodeIterator)reflectionNodeNavigator.Evaluate(typeExpression);
this.GetComments(typeNodeIterator, inheritDocNodeNavigator);
// no <inheritdoc /> nodes were found, so continue with next iteration. Otherwise inherit from interface implementation types.
if (this.sourceDocument.CreateNavigator().Select(inheritDocExpression).Count == 0)
{
continue;
}
// Inherit from interface implementation types
XPathNodeIterator interfaceNodeIterator = (XPathNodeIterator)reflectionNodeNavigator.Evaluate(interfaceImplementationExpression);
this.GetComments(interfaceNodeIterator, inheritDocNodeNavigator);
}
else if (group == "member")
{
// constructors do not have override member information in reflection files, so search all the base types for a matching signature.
if (subgroup == "constructor")
{
string name = (string)reflectionNodeNavigator.Evaluate(apiNameExpression);
string typeApi = (string) reflectionNodeNavigator.Evaluate(containerTypeExpression);
// no container type api was found, so delete <inheritdoc /> node.
if (string.IsNullOrEmpty(typeApi))
{
DeleteNode(inheritDocNodeNavigator, key);
continue;
}
reflectionNodeNavigator = this.reflectionIndex.GetContent(typeApi);
// no reflection information for container type api was found, so delete <inheritdoc /> node.
if (reflectionNodeNavigator == null)
{
DeleteNode(inheritDocNodeNavigator, key);
continue;
}
XPathNodeIterator containerIterator = reflectionNodeNavigator.Select(typeExpression);
foreach (XPathNavigator containerNavigator in containerIterator)
{
string constructorId = string.Format(CultureInfo.InvariantCulture, "M:{0}.{1}", containerNavigator.Value.Substring(2), name.Replace('.', '#'));
XPathNavigator contentNodeNavigator = this.index.GetContent(constructorId);
if (contentNodeNavigator == null)
{
continue;
}
this.UpdateNode(inheritDocNodeNavigator, contentNodeNavigator);
if (this.sourceDocument.CreateNavigator().Select(inheritDocExpression).Count == 0)
{
break;
}
else
{
inheritDocNodeNavigator.MoveTo(this.sourceDocument.CreateNavigator().SelectSingleNode(inheritDocExpression));
}
}
}
else
{
// Inherit from override members.
XPathNodeIterator memberNodeIterator = (XPathNodeIterator)reflectionNodeNavigator.Evaluate(overrideMemberExpression);
this.GetComments(memberNodeIterator, inheritDocNodeNavigator);
if (this.sourceDocument.CreateNavigator().Select(inheritDocExpression).Count == 0)
{
continue;
}
// Inherit from interface implementations members.
XPathNodeIterator interfaceNodeIterator = (XPathNodeIterator)reflectionNodeNavigator.Evaluate(interfaceImplementationMemberExpression);
this.GetComments(interfaceNodeIterator, inheritDocNodeNavigator);
}
}
// no comments were found, so delete <iheritdoc /> node.
if (this.sourceDocument.CreateNavigator().Select(inheritDocExpression).Count != 0)
{
DeleteNode(inheritDocNodeNavigator, key);
}
}
}
}
/// <summary>
/// Updates the node replacing inheritdoc node with comments found.
/// </summary>
/// <param name="inheritDocNodeNavigator">Navigator for inheritdoc node</param>
/// <param name="contentNodeNavigator">Navigator for content</param>
public void UpdateNode(XPathNavigator inheritDocNodeNavigator, XPathNavigator contentNodeNavigator)
{
// retrieve the selection filter if specified.
string selectValue = inheritDocNodeNavigator.GetAttribute("select", string.Empty);
if (!string.IsNullOrEmpty(selectValue))
{
sourceExpression = XPathExpression.Compile(selectValue);
}
inheritDocNodeNavigator.MoveToParent();
if (inheritDocNodeNavigator.LocalName != "comments" && inheritDocNodeNavigator.LocalName != "element")
{
sourceExpression = XPathExpression.Compile(inheritDocNodeNavigator.LocalName);
}
else
{
inheritDocNodeNavigator.MoveTo(this.sourceDocument.CreateNavigator().SelectSingleNode(inheritDocExpression));
}
XPathNodeIterator sources = (XPathNodeIterator) contentNodeNavigator.CreateNavigator().Evaluate(sourceExpression);
inheritDocNodeNavigator.DeleteSelf();
// append the source nodes to the target node
foreach (XPathNavigator source in sources)
{
inheritDocNodeNavigator.AppendChild(source);
}
}
/// <summary>
/// Gets the comments for inheritdoc node.
/// </summary>
/// <param name="iterator">Iterator for API information</param>
/// <param name="inheritDocNodeNavigator">Navigator for inheritdoc node</param>
public void GetComments(XPathNodeIterator iterator, XPathNavigator inheritDocNodeNavigator)
{
foreach (XPathNavigator navigator in iterator)
{
XPathNavigator contentNodeNavigator = this.index.GetContent(navigator.Value);
if (contentNodeNavigator == null)
{
continue;
}
this.UpdateNode(inheritDocNodeNavigator, contentNodeNavigator);
if (this.sourceDocument.CreateNavigator().Select(inheritDocExpression).Count == 0)
{
break;
}
else
{
inheritDocNodeNavigator.MoveTo(this.sourceDocument.CreateNavigator().SelectSingleNode(inheritDocExpression));
}
}
}
#endregion
}
}
|