30f3ff47754147adeb5425a04c0a1c7d578a2b60
[mono.git] / mcs / class / monodoc / Monodoc / providers / ecmauncompiled-provider.cs
1 using System;
2 using System.Linq;
3 using System.IO;
4 using System.Text;
5 using System.Xml;
6 using System.Xml.Linq;
7 using System.Collections.Generic;
8
9 using Lucene.Net.Index;
10 using Lucene.Net.Documents;
11
12 using Monodoc.Ecma;
13 using Monodoc.Storage;
14 using Mono.Utilities;
15
16 namespace Monodoc.Providers
17 {
18         public class EcmaUncompiledHelpSource : EcmaHelpSource
19         {
20                 readonly DirectoryInfo basedir;
21                 readonly string basedoc;
22
23                 public readonly string BasePath;
24
25                 public new string Name {
26                         get;
27                         private set;
28                 }
29         
30                 /* base_file: the directory containing the index.xml file, usually in Mono land .../Documentation/en
31                  * markName: if true, we encase the node caption with [] to clearly mark it's from an uncompiled source
32                  */
33                 public EcmaUncompiledHelpSource (string base_file, bool markName = true) : base ()
34                 {
35                         basedir = new DirectoryInfo (base_file);
36                         BasePath = basedir.FullName;
37                 
38                         basedoc = Path.Combine (basedir.FullName, "index.xml");
39                 
40                         Name = ((string)XDocument.Load (basedoc).Root.Element ("Title")) ?? "UnnamedUncompiledSource";
41                         if (markName)
42                                 Name = '[' + Name + ']';
43                         Tree.RootNode.Caption = Name;
44
45                         Func<XElement, string> indexGenerator = type => {
46                                 var nsName = (string)type.Parent.Attribute ("Name");
47                                 var typeName = (string)type.Attribute ("Name");
48                                 return Path.ChangeExtension (nsName + '/' + typeName, ".xml");
49                         };
50
51                         this.Storage = new UncompiledDocStorage (BasePath);
52
53                         EcmaDoc.PopulateTreeFromIndexFile (basedoc, UriPrefix, Tree, null, null, indexGenerator);
54                 }
55
56                 protected override string UriPrefix {
57                         get {
58                                 return "uncompiled:";
59                         }
60                 }
61
62                 public override Stream GetImage (string url)
63                 {
64                         var path = Path.Combine (BasePath, "_images", url);
65                         return File.Exists (path) ? File.OpenRead (path) : (Stream)null;
66                 }
67         }
68 }