Debian Codegraph @

external/api-doc-tools/mdoc/Mono.Documentation/Framework/FrameworkIndexHelper.cs

raw
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.IO;
5 using System.Xml;
6 using System.Xml.Linq;
7
8 namespace Mono.Documentation.Framework
9 {
10 public static class FrameworkIndexHelper
11 {
12 public static Dictionary<string, FrameworkNamespaceModel> CreateFrameworkIndex(string path, string frameworkName)
13 {
14 string frameworkFilePath = GetFrameworkFilePath(path, frameworkName);
15 if (!File.Exists(frameworkFilePath))
16 {
17 throw new ArgumentException($"Can't find framework file: {frameworkFilePath}");
18 }
19 using (XmlReader xmlReader = XmlReader.Create(frameworkFilePath))
20 {
21 return ReadFrameworkIndex(xmlReader);
22 }
23 }
24
25 public static Dictionary<string, FrameworkNamespaceModel> ReadFrameworkIndex(XmlReader xmlReader)
26 {
27 var dict = new Dictionary<string, FrameworkNamespaceModel>();
28
29 xmlReader.ReadToFollowing("Framework");
30 xmlReader.ReadToDescendant("Namespace");
31
32 while (xmlReader.NodeType != XmlNodeType.EndElement)
33 {
34 XNode node = XNode.ReadFrom(xmlReader);
35 XElement element = node as XElement;
36
37 if (element == null) continue;
38 var ns = new FrameworkNamespaceModel(node);
39
40 dict.Add(ns.Name, ns);
41 }
42
43 return dict;
44 }
45
46 private static string GetFrameworkFilePath(string path, string frameworkName)
47 {
48 string docsRoot = Path.GetDirectoryName(path) ?? "";
49 string frameworksIndexPath = Path.Combine(docsRoot, Consts.FrameworksIndexFolderName);
50 foreach (string frameworkIndexFilePath in Directory.EnumerateFiles(frameworksIndexPath))
51 {
52 using (XmlReader xmlReader = XmlReader.Create(frameworkIndexFilePath))
53 {
54 bool isFrameworkNodeFound = xmlReader.ReadToFollowing("Framework");
55 if (!isFrameworkNodeFound)
56 {
57 throw new InvalidOperationException($"Invalid framework file format in {frameworkIndexFilePath}");
58 }
59
60 var frameworkNameInFile = xmlReader.GetAttribute("Name");
61 if (frameworkNameInFile == frameworkName)
62 {
63 return frameworkIndexFilePath;
64 }
65 }
66 }
67
68 throw new InvalidEnumArgumentException($"Can't find file for frameworkName = {frameworkName}");
69 }
70 }
71 }