Add autoconf checks for platforms without IPv6
[mono.git] / mcs / class / monodoc / Monodoc / providers / addins-provider.cs
1 // addins-provider.cs
2 //
3 // A provider to display Mono.Addins extension models
4 //
5 // Author:
6 //   Lluis Sanchez Gual <lluis@novell.com>
7 //
8 // Copyright (c) 2007 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
27 //
28 //
29
30 using System;
31 using System.Linq;
32 using System.Diagnostics;
33 using System.Text;
34 using System.IO;
35 using System.Xml;
36 using System.Collections.Generic;
37
38 namespace Monodoc.Providers
39 {
40         public class AddinsProvider : Provider
41         {
42                 string file;
43                 
44                 public AddinsProvider (string xmlModelFile)
45                 {
46                         file = xmlModelFile;
47                         
48                         if (!File.Exists (file))
49                                 throw new FileNotFoundException (String.Format ("The file `{0}' does not exist", file));
50                 }
51
52                 public override void PopulateTree (Tree tree)
53                 {
54                         string fileId = Path.GetFileNameWithoutExtension (file);
55                         using (var f = File.OpenRead (file))
56                                 tree.HelpSource.Storage.Store (fileId, f);
57
58                         XmlDocument doc = new XmlDocument ();
59                         doc.Load (file);
60                         
61                         foreach (XmlElement addin in doc.SelectNodes ("Addins/Addin")) {
62
63                                 string addinId = addin.GetAttribute ("fullId");
64                                 Node newNode = tree.RootNode.CreateNode (addin.GetAttribute ("name"), "addin:" + fileId + "#" + addinId);
65
66                                 foreach (XmlElement node in addin.SelectNodes ("ExtensionPoint")) {
67                                         string target = "extension-point:" + fileId + "#" + addinId + "#" + node.GetAttribute ("path");
68                                         Node newExt = newNode.CreateNode (node.GetAttribute ("name"), target);
69                         
70                                         foreach (XmlElement en in node.SelectNodes ("ExtensionNode")) {
71                                                 string nid = en.GetAttribute ("id");
72                                                 string nname = en.GetAttribute ("name");
73                                                 newExt.CreateNode (nname, "extension-node:" + fileId + "#" + addinId + "#" + nid);
74                                         }
75                                 }
76                         }
77                 }
78
79                 public override void CloseTree (HelpSource hs, Tree tree)
80                 {
81                 }
82         }
83
84         public class AddinsHelpSource : HelpSource
85         {
86                 public AddinsHelpSource (string base_file, bool create) : base (base_file, create) 
87                 {
88                 }
89                 
90                 internal protected const string AddinPrefix = "addin:";
91                 internal protected const string ExtensionPrefix = "extension-point:";
92                 internal protected const string ExtensionNodePrefix = "extension-node:";
93
94                 public override bool CanHandleUrl (string url)
95                 {
96                         return url.StartsWith (AddinPrefix, StringComparison.OrdinalIgnoreCase)
97                                 || url.StartsWith (ExtensionPrefix, StringComparison.OrdinalIgnoreCase)
98                                 || url.StartsWith (ExtensionNodePrefix, StringComparison.OrdinalIgnoreCase);
99                 }
100
101                 protected override string UriPrefix {
102                         get {
103                                 return AddinPrefix;
104                         }
105                 }
106                 
107                 public override DocumentType GetDocumentTypeForId (string id)
108                 {
109                         return DocumentType.AddinXml;
110                 }
111
112                 public override string GetInternalIdForUrl (string url, out Node node, out Dictionary<string, string> context)
113                 {
114                         var id = base.GetInternalIdForUrl (url, out node, out context);
115                         var idParts = id.Split ('#');
116                         context = new Dictionary<string, string> ();
117                         context["FileID"] = idParts[0];
118                         context["AddinID"] = idParts[1];
119                         context["NodeID"] = idParts[2];
120
121                         return idParts[0];
122                 }
123
124                 public override Node MatchNode (string url)
125                 {
126                         var prefix = new[] { AddinPrefix, ExtensionPrefix, ExtensionNodePrefix }.First (p => url.StartsWith (p, StringComparison.OrdinalIgnoreCase));
127                         return base.MatchNode (prefix != null ? url.Substring (prefix.Length) : url);
128                 }
129         }
130 }