-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDocParser.cs
More file actions
367 lines (313 loc) · 12.4 KB
/
DocParser.cs
File metadata and controls
367 lines (313 loc) · 12.4 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
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
using System.Text.RegularExpressions;
using System.Xml;
namespace HtmlDocGenerator;
public class DocParser
{
Regex _regexHttp = new Regex("(\\b(http|ftp|https):(\\/\\/|\\\\\\\\)[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?|\\bwww\\.[^\\s])");
Dictionary<char, DocObjectType> _typeKeys = new Dictionary<char, DocObjectType>()
{
['T'] = DocObjectType.ObjectType,
['E'] = DocObjectType.Event,
['P'] = DocObjectType.Property,
['F'] = DocObjectType.Field,
['M'] = DocObjectType.Method,
['!'] = DocObjectType.Invalid
};
/// <summary>
/// Parses a summary xml file and adds it's information to the provided <see cref="DocContext"/>.
/// </summary>
/// <param name="context">The <see cref="DocContext"/> to which the parsed XML will be added.</param>
/// <param name="xmlPath">The path of the XML summary file to be parsed.</param>
/// <param name="assemblyPath">A custom path to the assembly file from which the xml summary file was generated.</param>
public void LoadXml(DocContext context, string xmlPath, string assemblyPath = null)
{
FileInfo info = new FileInfo(xmlPath);
if (!info.Exists)
{
Console.WriteLine($"The specified file does not exist: {info.FullName}");
return;
}
using (FileStream stream = new FileStream(info.FullName, FileMode.Open, FileAccess.Read))
{
using (XmlReader reader = XmlReader.Create(stream))
{
reader.MoveToContent();
XmlDocument xml = new XmlDocument();
xml.Load(reader);
XmlNode docRoot = xml["doc"];
if (docRoot == null) {
context.Error($"The root 'doc' node was not found. Unable to continue.");
return;
}
//LoadXmlAssembly(context, docNode, info, assemblyPath);
XmlNode xAssembly = docRoot["assembly"];
if (xAssembly != null)
{
if (!context.Assemblies.TryGetValue(xAssembly.InnerText, out DocAssembly da))
{
da = new DocAssembly();
da.Name = xAssembly.InnerText;
da.FilePath = assemblyPath;
if (string.IsNullOrWhiteSpace(da.FilePath))
da.FilePath = $"{info.Directory}\\{da.Name}.dll";
if (!da.Load(context))
return;
context.Assemblies.Add(da.Name, da);
}
if(da.XmlRoot == null)
{
// Run through the public types in the assembly.
da.XmlRoot = docRoot;
context.Log($"Retrieving type list for '{da.Name}'");
Type[] aTypes = da.Assembly.GetExportedTypes();
context.Log($"Retrieved {aTypes.Length} public types for '{da.Name}'");
foreach (Type t in aTypes)
{
DocObject obj = context.CreateObject(t);
obj.UnderlyingType = t;
}
}
}
}
}
}
public void Parse(DocContext context, string destPath)
{
// Parse assembly XML members.
foreach (DocAssembly a in context.Assemblies.Values)
{
XmlNode xMembers = a.XmlRoot["members"];
if (xMembers != null)
{
foreach (XmlNode node in xMembers.ChildNodes)
{
if (node.Name != "member")
continue;
ParseMember(context, node);
}
}
}
}
private void ParseMember(DocContext context, XmlNode memberNode)
{
XmlAttribute attName = memberNode.Attributes["name"];
if (attName == null)
{
context.Error($"No 'name' attribute found for member. Skipping");
return;
}
DocElement el = ParseXmlName(context, attName.Value, out DocObjectType mType, out string mName);
if (el != null)
{
foreach (XmlNode sumNode in memberNode)
{
switch (sumNode.Name)
{
case "summary":
el.Summary = ParseSummary(context, sumNode.InnerXml);
break;
case "remarks":
el.Remark = ParseSummary(context, sumNode.InnerXml);
break;
case "param":
if (el is DocMethodMember method)
{
XmlAttribute attParamName = sumNode.Attributes["name"];
if (attParamName != null)
{
if (method.ParametersByName.TryGetValue(attParamName.Value, out DocParameter dp))
dp.Summary = ParseSummary(context, sumNode.InnerXml);
}
}
break;
}
}
}
}
private DocElement ParseXmlName(DocContext context, string typeName, out DocObjectType mType, out string name)
{
typeName = typeName.Replace("<", "<").Replace(">", ">");
string ns = "";
string nsPrev = "";
mType = DocObjectType.Unknown;
name = "";
char startsWith = typeName[0];
if (!_typeKeys.TryGetValue(startsWith, out mType))
{
context.Error($"Invalid type-name prefix '{startsWith}' for '{typeName}'");
return null;
}
typeName = typeName.Substring(2); // Get typeName without the [type]: prefix
if (mType == DocObjectType.Invalid)
{
context.Error($"Invalid type '{typeName}' detected. Cannot parse XML name. Starts with '{startsWith}' operator");
name = typeName;
return null;
}
string objectName = "";
string memberName = "";
string prev = "";
string cur = "";
List<Type> methodParams = new List<Type>();
for (int i = 0; i < typeName.Length; i++)
{
char c = typeName[i];
switch (c)
{
case '.':
// Ignore namespace separator if member name is already defined
// because these will be parameter namespace separators.
if (memberName.Length == 0)
{
nsPrev = ns;
ns += ns.Length > 0 ? $".{cur}" : cur;
prev = cur;
cur = "";
}
else
{
cur += ".";
}
break;
case ',':
Type t = Type.GetType(cur);
methodParams.Add(t);
prev = cur;
cur = "";
break;
case '#':
objectName = prev;
ns = nsPrev;
break;
case '(':
memberName = cur;
prev = cur;
cur = "";
break;
case ')':
prev = cur;
cur = "";
break;
default:
cur += c;
break;
}
}
if (memberName.Length == 0)
memberName = cur;
DocElement el = null;
if (mType != DocObjectType.ObjectType)
{
if (objectName.Length == 0)
objectName = prev;
ns = nsPrev;
DocObject parentObj = context.GetObject(ns, objectName);
if (parentObj != null)
el = parentObj.GetMember<DocMethodMember>(memberName, methodParams?.ToArray());
}
else
{
el = context.GetObject(ns, memberName);
}
return el;
}
private string ParseSummary(DocContext context, string xmlText, XmlDocument xmlDoc = null)
{
xmlText = xmlText.Trim();
xmlDoc = xmlDoc ?? new XmlDocument();
int nodeStart = 0;
bool tagStarted = false;
bool inOpenNode = false; // True if a <tag> has been parsed without a closing </tag>.
char cPrev = '\0';
string summary = "";
for(int i = 0; i < xmlText.Length; i++)
{
char c = xmlText[i];
switch (c)
{
case '<':
if (!tagStarted)
{
if (inOpenNode)
{
// Check if we're starting a closing </tag>.
if (i < xmlText.Length - 1 && xmlText[i + 1] != '/')
{
int closeIndex = xmlText.IndexOf('>', i, xmlText.Length - i);
if (closeIndex > -1)
{
string nXml = xmlText.Substring(i, xmlText.Length - closeIndex);
string nText = ParseSummary(context, nXml, xmlDoc);
xmlText = xmlText.Replace(nXml, nText);
summary += nText;
}
}
}
else
{
nodeStart = i;
tagStarted = true;
}
}
break;
case '>':
if (tagStarted)
{
// Consider a "/>" inline tag terminator.
if (inOpenNode || cPrev == '/')
{
int len = (i - nodeStart) + 1;
string nXml = xmlText.Substring(nodeStart, len);
string nText = ParseSummaryXml(context, nXml, xmlDoc);
xmlText = xmlText.Remove(nodeStart, len).Insert(nodeStart, nText);
summary += nText;
i = nodeStart + (nText.Length-1);
c = xmlText[i]; // Recheck current char.
inOpenNode = false;
}
tagStarted = false;
}
break;
default:
if (!inOpenNode && !tagStarted)
summary += c;
break;
}
cPrev = c;
}
// Add anchor tags to URLs
Match mUrl = _regexHttp.Match(summary);
while (mUrl.Success)
{
string anchor = $"<a class=\"url\" target=\"_blank\" href=\"{mUrl.Value}\">{mUrl.Value}</a>";
summary = summary.Replace(mUrl.Value, anchor);
mUrl = _regexHttp.Match(summary, mUrl.Index + anchor.Length);
}
return summary;
}
private string ParseSummaryXml(DocContext context, string xml, XmlDocument xmlDoc)
{
xmlDoc.LoadXml(xml);
XmlNode subNode = xmlDoc.FirstChild;
string summary = "";
switch (subNode.Name)
{
case "see":
XmlAttribute attCRef = subNode.Attributes["cref"];
if (attCRef != null)
{
DocElement refObj = ParseXmlName(context, attCRef.Value, out DocObjectType mType, out string mName);
if (refObj != null) {
summary = $"<a class=\"{context.Css.Target}\" data-target=\"{refObj.Namespace}.{refObj.Name}\">{refObj.Name}</a>";
}
else if (mType == DocObjectType.Invalid)
summary = $"<b class=\"{context.Css.Invalid}\" title=\"Invalid object name\">{mName}</b>";
}
break;
case "para":
summary = "<br/><br/>";
break;
}
return summary;
}
}