-
Notifications
You must be signed in to change notification settings - Fork 365
/
TopicFile.cs
289 lines (252 loc) · 11.1 KB
/
TopicFile.cs
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
//===============================================================================================================
// System : Sandcastle Help File Builder Utilities
// File : TopicFile.cs
// Author : Eric Woodruff (Eric@EWoodruff.us)
// Updated : 04/14/2021
// Note : Copyright 2008-2021, Eric Woodruff, All rights reserved
//
// This file contains a class representing a conceptual content topic file.
//
// This code is published under the Microsoft Public License (Ms-PL). A copy of the license should be
// distributed with the code and can be found at the project website: https://GitHub.com/EWSoftware/SHFB. This
// notice, the author's name, and all copyright notices must remain intact in all applications, documentation,
// and source files.
//
// Date Who Comments
// ==============================================================================================================
// 08/07/2008 EFW Created the code
// 01/02/2013 EFW Added method to get referenced namespaces
// 05/08/2015 EFW Removed support for raw HTML files
//===============================================================================================================
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using Sandcastle.Core;
namespace SandcastleBuilder.Utils.ConceptualContent
{
/// <summary>
/// This class represents a conceptual content topic file
/// </summary>
public class TopicFile
{
#region Private data members
//=====================================================================
private ContentFile contentFile;
private DocumentType docType;
private string id, errorMessage;
private int revision;
private bool contentParsed;
#endregion
#region Properties
//=====================================================================
/// <summary>
/// This is used to get or set the file build item
/// </summary>
public ContentFile ContentFile
{
get => contentFile;
set
{
contentFile = value;
contentParsed = false;
docType = DocumentType.None;
}
}
/// <summary>
/// Get the name of the file without the path
/// </summary>
public string Name => contentFile.Filename;
/// <summary>
/// Get the full path to the file
/// </summary>
public string FullPath => contentFile.FullPath;
/// <summary>
/// This is used to get the unique ID of the topic
/// </summary>
public string Id
{
get
{
this.ParseContent(false);
return id;
}
}
/// <summary>
/// This is used to get the topic's revision number
/// </summary>
public int RevisionNumber
{
get
{
this.ParseContent(false);
return revision;
}
}
/// <summary>
/// This read-only property is used to get the document type
/// </summary>
public DocumentType DocumentType
{
get
{
this.ParseContent(false);
return docType;
}
}
/// <summary>
/// This read-only property is used to return the error message if <see cref="DocumentType" /> returns
/// <c>Invalid</c>.
/// </summary>
public string ErrorMessage => errorMessage;
#endregion
#region Constructor
//=====================================================================
/// <summary>
/// Constructor
/// </summary>
/// <param name="contentFile">The content file from the project</param>
/// <exception cref="ArgumentNullException">This is thrown if the content file is null</exception>
public TopicFile(ContentFile contentFile)
{
this.contentFile = contentFile ?? throw new ArgumentNullException(nameof(contentFile));
revision = 1;
}
#endregion
#region Parsing methods
//=====================================================================
/// <summary>
/// This will parse the file content and extract the document type, unique ID, and revision number
/// </summary>
/// <param name="reparse">If false and the file has already been parsed, the method just returns. If
/// true, the file is reparsed to refresh the information.</param>
public void ParseContent(bool reparse)
{
if(!reparse && contentParsed)
return;
contentParsed = false;
docType = DocumentType.None;
id = errorMessage = null;
revision = 1;
if(!File.Exists(contentFile.FullPath))
{
docType = DocumentType.NotFound;
return;
}
try
{
var settings = new XmlReaderSettings
{
CloseInput = true,
IgnoreComments = true,
IgnoreProcessingInstructions = true,
IgnoreWhitespace = true
};
using(var xr = XmlReader.Create(contentFile.FullPath, settings))
{
xr.MoveToContent();
while(!xr.EOF)
{
if(xr.NodeType != XmlNodeType.Element)
xr.Read();
else
{
switch(xr.Name)
{
case "topic":
// If a <topic> element is found, parse the ID
// and revision number from it.
string attrValue = xr.GetAttribute("id");
// The ID is required
if(attrValue != null && attrValue.Trim().Length != 0)
id = attrValue;
else
throw new XmlException("<topic> element " +
"is missing the 'id' attribute");
// This is optional
attrValue = xr.GetAttribute("revisionNumber");
if(attrValue != null && Int32.TryParse(attrValue, out int rev))
revision = rev;
xr.Read();
break;
case "developerConceptualDocument":
case "developerErrorMessageDocument":
case "developerGlossaryDocument":
case "developerHowToDocument":
case "developerOrientationDocument":
case "codeEntityDocument":
case "developerReferenceWithSyntaxDocument":
case "developerReferenceWithoutSyntaxDocument":
case "developerSampleDocument":
case "developerSDKTechnologyOverviewArchitectureDocument":
case "developerSDKTechnologyOverviewCodeDirectoryDocument":
case "developerSDKTechnologyOverviewOrientationDocument":
case "developerSDKTechnologyOverviewScenariosDocument":
case "developerSDKTechnologyOverviewTechnologySummaryDocument":
case "developerTroubleshootingDocument":
case "developerUIReferenceDocument":
case "developerWalkthroughDocument":
case "developerWhitePaperDocument":
case "developerXmlReference":
docType = (DocumentType)Enum.Parse(typeof(DocumentType), xr.Name, true);
xr.Read();
break;
default: // Ignore it
xr.Skip();
break;
}
}
}
}
}
catch(Exception ex)
{
docType = DocumentType.Invalid;
errorMessage = ex.Message;
}
finally
{
contentParsed = true;
}
}
/// <summary>
/// This is used to get an enumerable list of unique namespaces referenced in the topic
/// </summary>
/// <param name="validNamespaces">An enumerable list of valid framework namespaces</param>
/// <returns>An enumerable list of unique namespaces in the topic</returns>
public IEnumerable<string> GetReferencedNamespaces(IEnumerable<string> validNamespaces)
{
HashSet<string> seenNamespaces = new HashSet<string>();
string ns;
// Find all code entity references
var entityRefs = ComponentUtilities.XmlStreamAxis(contentFile.FullPath, "codeEntityReference").Select(el => el.Value);
foreach(var refName in entityRefs)
if(refName.Length > 2 && refName.IndexOfAny(new[] { '.', '(' }) != -1)
{
ns = refName.Trim();
// Strip off member name?
if(!ns.StartsWith("R:", StringComparison.OrdinalIgnoreCase) &&
!ns.StartsWith("G:", StringComparison.OrdinalIgnoreCase) &&
!ns.StartsWith("N:", StringComparison.OrdinalIgnoreCase) &&
!ns.StartsWith("T:", StringComparison.OrdinalIgnoreCase))
{
if(ns.IndexOf('(') != -1)
ns = ns.Substring(0, ns.IndexOf('('));
if(ns.IndexOf('.') != -1)
ns = ns.Substring(0, ns.LastIndexOf('.'));
}
if(ns.IndexOf('.') != -1)
ns = ns.Substring(2, ns.LastIndexOf('.') - 2);
else
ns = ns.Substring(2);
if(validNamespaces.Contains(ns) && !seenNamespaces.Contains(ns))
{
seenNamespaces.Add(ns);
yield return ns;
}
}
}
#endregion
}
}