-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEditorBase.cs
880 lines (823 loc) · 29.1 KB
/
EditorBase.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using WDCMLSDK;
using WDCMLSDKDerived;
namespace WDCMLSDKBase
{
/// <summary>
/// Abstract base class for an xml document editor, whether the document is WDCML, xtoc, or other.
/// Provides common element/attribute query and update services, maintains a dirty flag,
/// and checks out and saves the document on request. The SDK skeleton project extends EditorBase
/// with a class called Editor. Editor is the class you add app-specific services to. Augment
/// EditorBase only with common facilities.
/// </summary>
internal abstract class EditorBase
{
/// <summary>
/// The namespace to use if this is a wdcml document.
/// </summary>
protected XNamespace xNamespace = "http://microsoft.com/wdcml";
/// <summary>
/// The underlying FileInfo object representing the document on disk.
/// </summary>
public FileInfo FileInfo = null;
/// <summary>
/// The XDocument object representing the xml document.
/// </summary>
protected XDocument xDocument = null;
/// <summary>
/// Represents whether the document is dirty (has unsaved changes) or not. Calling
/// <see cref="EditorBase.CheckOutAndSaveChangesIfDirty"/> only has an effect if
/// <see cref="EditorBase.IsDirty"/> is true. The <see cref="EditorBase"/> should
/// manage this value itself, but if necessary you can force the desired behavior by setting this field.
/// </summary>
public bool IsDirty = false;
/// <summary>
/// Constructs a new EditorBase.
/// </summary>
/// <param name="fileInfo">The file to edit.</param>
/// <param name="xNamespace">The default xmlns, or null to use the wdcml namespace.</param>
public EditorBase(FileInfo fileInfo, XNamespace xNamespace = null)
{
this.FileInfo = fileInfo;
if (xNamespace != null) this.xNamespace = xNamespace;
try
{
this.xDocument = XDocument.Load(this.FileInfo.FullName, LoadOptions.PreserveWhitespace);
}
catch (Exception ex)
{
ProgramBase.ConsoleWrite(fileInfo.FullName + " is invalid.", ConsoleWriteStyle.Error);
ProgramBase.ConsoleWrite(ex.Message, ConsoleWriteStyle.Error);
throw new WDCMLSDKException();
}
}
public bool IsValid { get { return this.xDocument != null; } }
// Methods that don't modify.
/// <summary>
/// Gets the single unique descendant with the specified name. Throws if the name is not unique.
/// </summary>
/// <param name="name">The element's name (without namespace).</param>
/// <param name="container">The scope to search in. Uses the entire document by default but you can pass another XElement to limit the seach to that element's descendants.</param>
/// <returns>The element, or null if the element does not exist.</returns>
public XElement GetUniqueDescendant(string name, XContainer container = null)
{
if (container == null) container = this.xDocument;
List<XElement> elements = this.GetDescendants(name, container);
if (elements == null || elements.Count == 0) return null;
if (elements.Count == 1)
{
return elements[0];
}
else
{
ProgramBase.ConsoleWrite("You called GetUniqueDescendant(\"" + name + "\"), but \"" + name + "\" is not unique.", ConsoleWriteStyle.Error);
throw new WDCMLSDKException();
}
}
/// <summary>
/// Gets the first descendant with the specified name.
/// </summary>
/// <param name="name">The element's name (without namespace).</param>
/// <param name="container">The scope to search in. Uses the entire document by default but you can pass another XElement to limit the seach to that element's descendants.</param>
/// <returns>The element, or null if the element does not exist.</returns>
public XElement GetFirstDescendant(string name, XContainer container = null)
{
if (container == null) container = this.xDocument;
List<XElement> elements = this.GetDescendants(name, container);
if (elements != null && elements.Count > 0)
{
return elements[0];
}
else
{
return null;
}
}
/// <summary>
/// Gets all descendants, or all descendants with the specified name if one is specified.
/// </summary>
/// <param name="name">The element's name (without namespace) or null to return all descendants.</param>
/// <param name="container">The scope to search in. Uses the entire document by default but you can pass another XElement to limit the seach to that element's descendants.</param>
/// <returns>The elements, or null if the element does not exist.</returns>
public List<XElement> GetDescendants(string name = null, XContainer container = null)
{
if (container == null) container = this.xDocument;
if (name == null)
{
return container.Descendants().ToList();
}
else
{
return container.Descendants(this.xNamespace + name).ToList();
}
}
/// <summary>
/// Gets metadata@id as a string.
/// </summary>
/// <returns>The value of the id attribute if found, otherwise null.</returns>
public string GetMetadataAtId()
{
XElement metadata = this.GetUniqueDescendant("metadata");
if (metadata != null)
{
XAttribute id = metadata.Attribute("id");
if (id != null) return id.Value;
}
return null;
}
/// <summary>
/// Gets metadata@type as a string.
/// </summary>
/// <returns>The value of the type attribute if found, otherwise null.</returns>
public string GetMetadataAtTypeAsString()
{
XElement metadata = this.GetUniqueDescendant("metadata");
if (metadata != null)
{
XAttribute type = metadata.Attribute("type");
if (type != null) return type.Value;
}
return null;
}
/// <summary>
/// Gets metadata@type as an enum value.
/// </summary>
/// <returns>An enum value representing the value of the type attribute.</returns>
public TopicType GetMetadataAtTypeAsEnum()
{
XElement metadata = this.GetUniqueDescendant("metadata");
if (metadata != null)
{
XAttribute xAttribute = metadata.Attribute("type");
if (xAttribute != null)
{
if (xAttribute.Value == "attachedmember_winrt")
{
return TopicType.AttachedPropertyWinRT;
}
else if (xAttribute.Value == "attribute")
{
return TopicType.AttributeWinRT; // change the name a little so we're consistent.
}
else if (xAttribute.Value == "class_winrt")
{
return TopicType.ClassWinRT;
}
else if (xAttribute.Value == "delegate")
{
return TopicType.DelegateWinRT; // change the name a little so we're consistent.
}
else if (xAttribute.Value == "enum_winrt")
{
return TopicType.EnumWinRT;
}
else if (xAttribute.Value == "event_winrt")
{
return TopicType.EventWinRT;
}
else if (xAttribute.Value == "function")
{
return TopicType.MethodWinRT; // change the name so we're consistent. Some WinRT DX topics are like this.
}
//else if (xAttribute.Value == "iface")
//{
// return TopicType.InterfaceWinRT; // change the name so we're consistent. Some WinRT topics are like this (e.g. see w_net_backgrxfer).
//}
else if (xAttribute.Value == "interface_winrt")
{
return TopicType.InterfaceWinRT;
}
//else if (xAttribute.Value == "method")
//{
// return TopicType.MethodWinRT; // change the name so we're consistent. Some WinRT topics are like this (e.g. see w_net_backgrxfer).
//}
else if (xAttribute.Value == "method_overload")
{
return TopicType.NotYetKnown; // don't process these.
}
else if (xAttribute.Value == "method_overload_winrt")
{
return TopicType.NotYetKnown; // don't process these.
}
else if (xAttribute.Value == "method_winrt")
{
return TopicType.MethodWinRT;
}
if (xAttribute.Value == "namespace")
{
return TopicType.NamespaceWinRT; // change the name a little so we're consistent.
}
else if (xAttribute.Value == "nodepage")
{
return TopicType.NotYetKnown; // don't process these.
}
else if (xAttribute.Value == "ovw")
{
return TopicType.NotYetKnown; // don't process these.
}
else if (xAttribute.Value == "property_winrt")
{
return TopicType.PropertyWinRT;
}
else if (xAttribute.Value == "refpage")
{
return TopicType.NotYetKnown; // don't process these.
}
else if (xAttribute.Value == "startpage")
{
return TopicType.NotYetKnown; // don't process these.
}
//else if (xAttribute.Value == "struct")
//{
// return TopicType.StructWinRT; // change the name a little so we're consistent. Some WinRT DX topics are like this.
//}
else if (xAttribute.Value == "struct_winrt")
{
return TopicType.StructWinRT;
}
else
{
return TopicType.NotYetKnown;
}
}
}
return TopicType.NotYetKnown;
}
/// <summary>
/// Gets metadata@title as a string.
/// </summary>
/// <returns>The value of the title attribute if found, otherwise null.</returns>
public string GetMetadataAtTitle()
{
XElement metadata = this.GetUniqueDescendant("metadata");
if (metadata != null)
{
XElement title = this.GetUniqueDescendant("title", metadata);
if (title != null) return title.Value;
}
return null;
}
public string GetMetadataAtMsdnId()
{
XElement metadata = this.GetUniqueDescendant("metadata");
if (metadata != null)
{
XAttribute xAttribute = metadata.Attribute("msdnID");
if (xAttribute != null)
{
return xAttribute.Value;
}
}
return null;
}
/// <summary>
/// Gets metadata@title as nodes.
/// </summary>
/// <returns>The nodes of the title attribute if found, otherwise null.</returns>
public IEnumerable<XNode> GetMetadataAtTitleAsNodes()
{
XElement metadata = this.GetUniqueDescendant("metadata");
if (metadata != null)
{
XElement title = this.GetUniqueDescendant("title", metadata);
if (title != null) return title.Nodes();
}
return null;
}
public string GetSyntaxName()
{
XElement syntax = this.GetUniqueDescendant("syntax");
if (syntax != null)
{
XElement name = this.GetFirstDescendant("name", syntax);
if (name != null) return name.Value;
}
return null;
}
public string GetMetadataAtIntellisenseIdString()
{
XElement metadata = this.GetUniqueDescendant("metadata");
if (metadata != null)
{
XAttribute intellisenseIdStringAttribute = metadata.Attribute("intellisense_id_string");
if (intellisenseIdStringAttribute != null)
{
return intellisenseIdStringAttribute.Value;
}
}
return null;
}
/// <summary>
/// Gets the type name extracted from metadata@intellisense_id_string as a string.
/// </summary>
/// <returns>The type name extracted from the intellisense_id_string attribute if found, otherwise null.</returns>
public string GetTypeNameFromMetadataAtIntellisenseIdString()
{
string intellisenseIdString = this.GetMetadataAtIntellisenseIdString();
if (intellisenseIdString != null)
{
string typeName = intellisenseIdString;
int startIndexOfTypeName = typeName.LastIndexOf('.') + 1;
int lengthOfTypeName = typeName.Length - startIndexOfTypeName;
typeName = typeName.Substring(startIndexOfTypeName, lengthOfTypeName);
return typeName;
}
return null;
}
public string GetMethodWinRTParametersFromParams()
{
string parmsList = string.Empty;
XElement paramsEl = this.GetUniqueDescendant("params");
if (paramsEl != null)
{
foreach (XElement param in this.GetDescendants("param", paramsEl))
{
if (parmsList.Length == 0) parmsList = "(";
XElement datatype = this.GetUniqueDescendant("datatype", param);
if (datatype == null) continue;
XElement xref = this.GetUniqueDescendant("xref", datatype);
if (xref == null) continue;
if (parmsList.Length > 1) parmsList += ", ";
parmsList += xref.Value;
}
if (parmsList.Length > 0) parmsList += ")";
}
return parmsList;
}
/// <summary>
/// Gets applicationplatform@name as a string.
/// </summary>
/// <returns>The value of the name attribute if found, otherwise null.</returns>
public string GetApplicationPlatformAtName()
{
XElement applicationPlatform = this.GetUniqueDescendant("ApplicationPlatform");
if (applicationPlatform != null)
{
XAttribute name = applicationPlatform.Attribute("name");
if (name != null) return name.Value;
}
return null;
}
/// <summary>
/// Gets applicationplatform@friendlyname as a string.
/// </summary>
/// <returns>The value of the friendlyname attribute if found, otherwise null.</returns>
public string GetApplicationPlatformAtFriendlyName()
{
XElement applicationPlatform = this.GetUniqueDescendant("ApplicationPlatform");
if (applicationPlatform != null)
{
XAttribute friendlyName = applicationPlatform.Attribute("friendlyName");
if (friendlyName != null) return friendlyName.Value;
}
return null;
}
/// <summary>
/// Gets applicationplatform@version as a string.
/// </summary>
/// <returns>The value of the friendlyname attribute if found, otherwise null.</returns>
public string GetApplicationPlatformAtVersion()
{
XElement applicationPlatform = this.GetUniqueDescendant("ApplicationPlatform");
if (applicationPlatform != null)
{
XAttribute version = applicationPlatform.Attribute("version");
if (version != null) return version.Value;
}
return null;
}
/// <summary>
/// Gets the xref element inside either applies@class or applies@iface as a string.
/// </summary>
/// <returns>The xref element if found, otherwise null.</returns>
public string GetAppliesClassOrIfaceXrefAtRid()
{
XElement applies = this.GetUniqueDescendant("applies");
if (applies != null)
{
XElement classEl = this.GetUniqueDescendant("class", applies);
if (classEl != null)
{
return this.GetAtRidForUniqueXrefDescendant(classEl);
}
else
{
XElement iface = this.GetUniqueDescendant("iface", applies);
if (iface != null)
{
return this.GetAtRidForUniqueXrefDescendant(iface);
}
}
}
return null;
}
/// <summary>
/// Gets @rid for the unique xref descendant of the specified element.
/// </summary>
/// <param name="xElement">The element whose xref descendant to find.</param>
/// <returns>The value of the rid attribute if found, otherwise null.</returns>
public string GetAtRidForUniqueXrefDescendant(XElement xElement)
{
XElement xref = this.GetUniqueDescendant("xref", xElement);
if (xref != null)
{
XAttribute rid = xref.Attribute("rid");
if (rid != null) return rid.Value;
}
return null;
}
/// <summary>
/// Gets xref elements whose @hlink contains the specified substring.
/// </summary>
/// <param name="substring">The substring to search for</param>
/// <param name="caseSensitive">True if the comparison should be case-sensitive, otherwise false.</param>
/// <param name="container">The scope to search in. Uses the entire document by default but you can pass another XElement to limit the seach to that element's descendants.</param>
/// <returns>A list of matching xref elements.</returns>
public List<XElement> GetXrefsWhereAtHlinkContains(string substring, bool caseSensitive = false, XContainer container = null)
{
if (container == null) container = this.xDocument;
if (!caseSensitive) substring = substring.ToLower();
List<XElement> xrefsWhereAtHlinkContains = new List<XElement>();
foreach (XElement eachXref in this.GetDescendants("xref", container))
{
XAttribute hlink = eachXref.Attribute("hlink");
if (hlink != null)
{
string hlinkValue = hlink.Value;
if (!caseSensitive) hlinkValue = hlinkValue.ToLower();
if (hlinkValue.Contains(substring)) xrefsWhereAtHlinkContains.Add(eachXref);
}
}
return xrefsWhereAtHlinkContains;
}
public List<XElement> GetXrefsForRid(string ridString, bool caseSensitive = false, XContainer container = null)
{
if (container == null) container = this.xDocument;
if (!caseSensitive) ridString = ridString.ToLower();
List<XElement> xrefsForRid = new List<XElement>();
foreach (XElement eachXref in this.GetDescendants("xref", container))
{
XAttribute ridAttribute = eachXref.Attribute("rid");
if (ridAttribute != null)
{
string ridValue = ridAttribute.Value;
if (!caseSensitive) ridValue = ridValue.ToLower();
if (ridValue == ridString) xrefsForRid.Add(eachXref);
}
}
return xrefsForRid;
}
/// <summary>
/// Factory method that creates an Editor for the xtoc file that is inside, and
/// that has the same name as, the specified project folder. Throws if not exactly
/// one xtoc file is found whose name matches the folder name.
/// </summary>
/// <param name="projectDirectoryInfo">The folder containing the project.</param>
/// <returns>An Editor object for the xtoc file.</returns>
public static Editor GetEditorForXtoc(DirectoryInfo projectDirectoryInfo)
{
List<FileInfo> xtocFiles = projectDirectoryInfo.GetFiles(projectDirectoryInfo.Name + ".xtoc").ToList();
if (xtocFiles.Count != 1)
{
ProgramBase.ConsoleWrite(string.Format("Project folder {0} does not contain {1}", projectDirectoryInfo.Name, projectDirectoryInfo.Name + ".xtoc"), ConsoleWriteStyle.Error);
throw new WDCMLSDKException();
}
return new Editor(xtocFiles[0], string.Empty);
}
/// <summary>
/// Factory method that creates an Editor for each topic file inside the specified project folder.
/// </summary>
/// <param name="projectDirectoryInfo">A DirectoryInfo representing the project folder.</param>
/// <returns>A list of Editor objects for the topics.</returns>
public static List<Editor> GetEditorsForTopicsInProject(DirectoryInfo projectDirectoryInfo)
{
List<FileInfo> fileInfos = EditorBase.GetFileInfosForTopicsInProject(projectDirectoryInfo);
List<Editor> editors = new List<Editor>();
foreach (FileInfo eachFileInfo in fileInfos)
{
//try
{
editors.Add(new Editor(eachFileInfo));
}
//catch (WDCMLSDKException){}
}
return editors;
}
/// <summary>
/// Factory method that creates a FileInfo for each topic file inside the specified project folder.
/// </summary>
/// <param name="projectDirectoryInfo">A DirectoryInfo representing the project folder.</param>
/// <returns>A list of FileInfo objects for the topics.</returns>
public static List<FileInfo> GetFileInfosForTopicsInProject(DirectoryInfo projectDirectoryInfo)
{
return EditorBase.GetFileInfosForTopicsInXtoc(projectDirectoryInfo, EditorBase.GetEditorForXtoc(projectDirectoryInfo));
}
private static List<FileInfo> GetFileInfosForTopicsInXtoc(DirectoryInfo projectDirectoryInfo, Editor xtocEditor)
{
List<FileInfo> topicFileInfos = new List<FileInfo>();
List<XElement> nodes = xtocEditor.GetDescendants("node");
foreach (XElement eachNode in nodes)
{
XAttribute topicUrlAttribute = eachNode.Attribute("topicURL");
if (topicUrlAttribute != null && EditorBase.IsTopicPublishedToMsdn(eachNode))
{
//string topicFilename = topicUrlAttribute.Value.Substring(topicUrlAttribute.Value.IndexOf('/') + 1);
FileInfo fileInfo = new FileInfo(Path.Combine(projectDirectoryInfo.FullName, topicUrlAttribute.Value));
if (fileInfo != null)
{
topicFileInfos.Add(fileInfo);
}
}
}
List<XElement> includes = xtocEditor.GetDescendants("include");
foreach (XElement eachInclude in includes)
{
XAttribute urlAttribute = eachInclude.Attribute("url");
if (urlAttribute != null && EditorBase.IsTopicPublishedToMsdn(eachInclude))
{
//string topicFilename = urlAttribute.Value.Substring(urlAttribute.Value.IndexOf('/') + 1);
FileInfo fileInfo = new FileInfo(Path.Combine(projectDirectoryInfo.FullName, urlAttribute.Value));
if (fileInfo != null)
{
Editor includedXtocEditor = new Editor(fileInfo, string.Empty);
topicFileInfos.AddRange(EditorBase.GetFileInfosForTopicsInXtoc(projectDirectoryInfo, includedXtocEditor));
}
}
}
return topicFileInfos;
}
/// <summary>
/// Determines whether a topic is present in the xtoc and published to msdn. Call this
/// method on an Editor that represents an xtoc file.
/// </summary>
/// <param name="topicUrl">The url (or filename) of the topic in the form of an xtoc node@topicURL value.</param>
/// <returns>True if the topic is present in the xtoc and published to msdn, otherwise false.</returns>
public bool IsTopicUrlInXtocAndPublishedToMsdn(string topicUrl)
{
List<XElement> nodes = this.GetDescendants("node");
foreach (XElement eachNode in nodes)
{
XAttribute topicUrlAttribute = eachNode.Attribute("topicURL");
if (topicUrlAttribute != null && topicUrlAttribute.Value == topicUrl)
{
return EditorBase.IsTopicPublishedToMsdn(eachNode);
}
}
return false;
}
/// <summary>
/// Determines whether a section with the specified id exists.
/// </summary>
/// <param name="sectionId">The section id to search for.</param>
/// <returns>True if a section with the specified id exists, otherwise false.</returns>
public bool DoesSectionWithThisIdExist(string sectionId)
{
foreach (XElement section in this.GetDescendants("section"))
{
XAttribute idAttribute = section.Attribute("id");
if (idAttribute != null && idAttribute.Value == sectionId) return true;
}
return false;
}
/// <summary>
/// Determines whether a topic is published to msdn based on an xtoc node element.
/// </summary>
/// <param name="node">A node element from an xtoc that represents a topic.</param>
/// <returns>True if the topic is published to msdn (there is no msdn filter), otherwise false.</returns>
private static bool IsTopicPublishedToMsdn(XElement node)
{
return node.Attribute("filter_msdn") == null;
}
/// <summary>
/// Gets a list of interfaces impemented by the type topic represented by the Editor.
/// </summary>
/// <returns>A list of interface names as strings.</returns>
public List<string> GetInterfacesImplemented()
{
List<string> interfacesImplemented = null;
XElement inheritance = this.GetUniqueDescendant("inheritance");
if (inheritance != null)
{
List<XElement> ancestors = this.GetDescendants("ancestor", inheritance);
foreach (XElement eachAncestor in ancestors)
{
XAttribute access_level = eachAncestor.Attribute("access_level");
if (access_level != null && access_level.Value == "private")
{
XElement xref = this.GetUniqueDescendant("xref", eachAncestor);
if (xref != null)
{
XAttribute targtype = xref.Attribute("targtype");
if (targtype.Value == "interface_winrt")
{
if (interfacesImplemented == null) interfacesImplemented = new List<string>();
interfacesImplemented.Add(xref.Value);
}
}
}
}
}
return interfacesImplemented;
}
// Methods that modify. Set this.IsDirty to true only you modify the document directly, not
// if you call a method that already does so.
/// <summary>
/// Constructs a new element with the specified name, optionally specified content, and optionally specified parent element.
/// If the parent element exists in the document represented by the Editor then the Editor marks itself dirty.
/// </summary>
/// <param name="name">The name to give the new element (without namespace).</param>
/// <param name="content">Optional content to put inside the new element.</param>
/// <param name="parentTheNewElementToThisElement">Optional element to parent the new element to (the parent can be, but doesn't have to be, inside the document represented by the Editor).</param>
/// <returns>The new XElement.</returns>
public XElement NewXElement(string name, object content = null, XElement parentTheNewElementToThisElement = null)
{
XElement xElement = new XElement(this.xNamespace + name, content);
if (parentTheNewElementToThisElement != null)
{
parentTheNewElementToThisElement.Add(xElement);
}
// If the parent element is in the document then dirty the document, otherwise don't.
if (parentTheNewElementToThisElement != null && this.GetDescendants().Contains(parentTheNewElementToThisElement)) this.IsDirty = true;
return xElement;
}
/// <summary>
/// Sets the specified attribute on the specified element to the specified value. If the element exists
/// in the document represented by the Editor then the Editor marks itself dirty.
/// </summary>
/// <param name="xElement"></param>
/// <param name="attributeName"></param>
/// <param name="value"></param>
public void SetAttributeValue(XElement xElement, string attributeName, string value)
{
if (xElement != null)
{
xElement.SetAttributeValue(attributeName, value);
// If the parent element is in the document then dirty the document, otherwise don't.
if (this.GetDescendants().Contains(xElement)) this.IsDirty = true;
}
}
/// <summary>
/// Sets metadata@title to a specified string value.
/// </summary>
/// <param name="titleAsString">The title to set.</param>
public void SetMetadataAtTitle(string titleAsString)
{
XElement metadata = this.GetUniqueDescendant("metadata");
if (metadata != null)
{
XElement title = this.GetUniqueDescendant("title", metadata);
if (title != null)
{
title.Value = titleAsString;
this.IsDirty = true;
}
}
}
public List<string> GetLibraryFilenames()
{
var libraryFilenames = new List<string>();
XElement content = this.GetUniqueDescendant("content");
if (content != null)
{
XElement info = this.GetUniqueDescendant("info", content);
if (info != null)
{
List<XElement> libraries = this.GetDescendants("library", info);
if (libraries != null)
{
foreach (var library in libraries)
{
XElement filename = this.GetUniqueDescendant("filename", library);
if (filename != null)
{
libraryFilenames.Add(filename.Value);
}
}
}
}
}
return libraryFilenames;
}
public string GetMetadataAtBeta()
{
XElement metadata = this.GetUniqueDescendant("metadata");
if (metadata != null)
{
XAttribute beta = metadata.Attribute("beta");
if (beta != null)
{
return beta.Value;
}
}
return "0";
}
public void SetMetadataAtBeta(string betaAttributeValue)
{
XElement metadata = this.GetUniqueDescendant("metadata");
if (metadata != null)
{
XAttribute beta = metadata.Attribute("beta");
if (beta != null)
{
beta.Value = betaAttributeValue;
}
else
{
metadata.Add(new XAttribute("beta", betaAttributeValue));
}
}
this.IsDirty = true;
}
/// <summary>
/// Sets node@text (the xtoc title) to a specified string value for the specified node@topicURL value.
/// Call this method on an Editor that represents an xtoc file.
/// </summary>
/// <param name="topicUrl">The node@topicURL identifying the topic whose xtoc title you want to set.</param>
/// <param name="text">The xtoc title to set.</param>
public void SetXtocNodeAtTextForTopicUrl(string topicUrl, string text)
{
List<XElement> nodes = this.GetDescendants("node");
foreach (XElement eachNode in nodes)
{
XAttribute topicUrlAttribute = eachNode.Attribute("topicURL");
if (topicUrlAttribute != null && topicUrlAttribute.Value == topicUrl)
{
eachNode.SetAttributeValue("text", text);
this.IsDirty = true;
return;
}
}
}
/// <summary>
/// If one or both of the device_families and api_contracts elements is missing, create an empty one.
/// </summary>
public void EnsureAtLeastEmptyDeviceFamiliesAndApiContractsElements()
{
XElement addAfterMe = this.GetUniqueDescendant("max_os");
if (addAfterMe == null)
{
addAfterMe = this.GetUniqueDescendant("min_os");
if (addAfterMe == null)
{
addAfterMe = this.GetUniqueDescendant("info");
}
}
if (addAfterMe != null)
{
XElement device_families = this.GetUniqueDescendant("device_families");
if (device_families == null)
{
device_families = this.NewXElement("device_families");
addAfterMe.AddAfterSelf(device_families);
this.IsDirty = true;
}
XElement api_contracts = this.GetUniqueDescendant("api_contracts");
if (api_contracts == null)
{
device_families.AddAfterSelf(this.NewXElement("api_contracts"));
this.IsDirty = true;
}
}
}
/// <summary>
/// Delete every section element from the document.
/// </summary>
public void DeleteAllSections()
{
List<XElement> sections = this.GetDescendants("section");
foreach (XElement eachSection in sections)
{
eachSection.Remove();
this.IsDirty = true;
}
}
/// <summary>
/// If the document is dirty then check it out (using sd edit) and save it to disk.
/// Log success or failure.
/// </summary>
public void CheckOutAndSaveChangesIfDirty()
{
if (!this.IsDirty) return;
string fileName = this.FileInfo.FullName;
try
{
if (!ProgramBase.DryRun)
{
Interaction.Shell(string.Format("sd edit {0}", fileName), AppWinStyle.Hide, true);
}
this.xDocument.Save(fileName);
ProgramBase.FilesSavedLog.Add(fileName);
}
catch (System.Exception ex)
{
ProgramBase.FileSaveErrorsLog.Add(string.Format("{0}", ex.Message));
}
this.IsDirty = false;
}
}
}