Merge pull request #496 from nicolas-raoul/unit-test-for-issue2907
[mono.git] / mcs / tools / monkeydoc / Monkeydoc / providers / man-provider.cs
1 //
2 // A provider to display man pages
3 //
4 // Authors:
5 //   Johannes Roith <johannes@roith.de>
6 //   Jonathan Pryor <jpryor@novell.com>
7 //
8 // (C) 2008 Novell, Inc.
9
10 using System;
11 using System.IO;
12 using System.Text;
13 using System.Xml;
14 using System.Linq;
15 using System.Collections.Generic;
16
17 namespace MonkeyDoc.Providers
18 {
19         public class ManProvider : Provider
20         {
21                 string[] tocFiles;
22         
23                 public ManProvider (string[] handbookTocFiles)
24                 {
25                         tocFiles = handbookTocFiles;
26
27                         // huh...
28                         if (!File.Exists (tocFiles[0]))
29                                 throw new FileNotFoundException (String.Format ("The table of contents, `{0}' does not exist", tocFiles[0]));
30                 }
31
32                 public override void PopulateTree (Tree tree)
33                 {
34                         foreach(string TocFile in tocFiles) {
35                                 XmlDocument doc = new XmlDocument();
36                                 doc.Load (TocFile);
37
38                                 XmlNodeList nodeList = doc.GetElementsByTagName("manpage");
39                                 Node nodeToAddChildrenTo = tree.RootNode;
40                                 var storage = nodeToAddChildrenTo.Tree.HelpSource.Storage;
41
42                                 foreach (XmlNode node in nodeList) {
43
44                                         XmlAttribute name = node.Attributes["name"];
45                                         XmlAttribute page = node.Attributes["page"];
46
47                                         if (name == null || page == null) continue;
48
49                                         if (!File.Exists (page.Value))
50                                                 continue;
51
52                                         string target = "man:" + name.Value;
53                                         nodeToAddChildrenTo.CreateNode (name.Value, target);
54
55                                         if (File.Exists (page.Value))
56                                                 storage.Store (name.Value, File.OpenRead (page.Value));
57                                 }
58                         }
59                 }
60
61                 public override void CloseTree (HelpSource hs, Tree tree)
62                 {
63                 }
64         }
65
66         public class ManHelpSource : HelpSource
67         {
68                 const string ManPrefix = "man:";
69                 Dictionary<string, Node> nodesMap;
70
71                 public ManHelpSource (string base_file, bool create) : base (base_file, create)
72                 {
73                         nodesMap = Tree.RootNode.Nodes.ToDictionary (n => n.Element);
74                 }
75
76                 // Since man always has a flat tree and rather small amount of item
77                 // we store them in a dictionary
78                 public override Node MatchNode (string url)
79                 {
80                         Node result;
81                         return nodesMap.TryGetValue (url, out result) ? result : null;
82                 }
83
84                 public override DocumentType GetDocumentTypeForId (string id, out Dictionary<string, string> extraParams)
85                 {
86                         extraParams = null;
87                         return id == "root:" ? DocumentType.TocXml : DocumentType.Man;
88                 }
89
90                 public override bool IsGeneratedContent (string id)
91                 {
92                         return id == "root:";
93                 }
94         
95                 public override string GetText (string url)
96                 {
97                         return TreeDumper.ExportToTocXml (Tree.RootNode, "Mono Documentation Library", "Available man pages:");
98                 }
99
100                 protected override string UriPrefix {
101                         get {
102                                 return ManPrefix;
103                         }
104                 }
105         }
106 }