Add autoconf checks for platforms without IPv6
[mono.git] / mcs / class / monodoc / Monodoc / 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 Monodoc.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                                                 using (var file = File.OpenRead (page.Value))
57                                                         storage.Store (name.Value, file);
58                                 }
59                         }
60                 }
61
62                 public override void CloseTree (HelpSource hs, Tree tree)
63                 {
64                 }
65         }
66
67         public class ManHelpSource : HelpSource
68         {
69                 const string ManPrefix = "man:";
70                 Dictionary<string, Node> nodesMap;
71
72                 public ManHelpSource (string base_file, bool create) : base (base_file, create)
73                 {
74                         nodesMap = Tree.RootNode.ChildNodes.ToDictionary (n => n.Element);
75                 }
76
77                 // Since man always has a flat tree and rather small amount of item
78                 // we store them in a dictionary
79                 public override Node MatchNode (string url)
80                 {
81                         Node result;
82                         return nodesMap.TryGetValue (url, out result) ? result : null;
83                 }
84
85                 public override DocumentType GetDocumentTypeForId (string id)
86                 {
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 }