2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Collections.Specialized;
5 using System.Configuration;
8 using System.Reflection;
9 using System.Runtime.InteropServices;
12 using MonkeyDoc.Providers;
13 using Lucene.Net.Analysis.Standard;
14 using Lucene.Net.Index;
18 public class RootTree : Tree
20 public const int MonodocVersion = 2;
21 const string RootNamespace = "root:/";
23 static List<string> uncompiledHelpSourcePaths = new List<string>();
24 HashSet<string> loadedSourceFiles = new HashSet<string>();
25 List<HelpSource> helpSources = new List<HelpSource>();
26 Dictionary<string, Node> nameToNode = new Dictionary<string, Node>();
27 Dictionary<string, HelpSource> nameToHelpSource = new Dictionary<string, HelpSource>();
29 public IList<HelpSource> HelpSources {
31 return this.helpSources.AsReadOnly();
35 public DateTime LastHelpSourceTime {
42 int platform = (int)Environment.OSVersion.Platform;
43 return platform == 4 || platform == 128 || platform == 6;
47 RootTree () : base (null, "Mono Documentation", "root:")
49 base.RootNode.EnsureNodes();
50 this.LastHelpSourceTime = DateTime.Now;
53 public static void AddUncompiledSource (string path)
55 uncompiledHelpSourcePaths.Add (path);
58 public static RootTree LoadTree ()
60 return RootTree.LoadTree (RootTree.ProbeBaseDirectories ());
63 static string ProbeBaseDirectories ()
67 result = Settings.Get ("docPath") ?? ".";
73 public static RootTree LoadTree (string basedir, bool includeExternal = true)
75 if (string.IsNullOrEmpty (basedir))
76 throw new ArgumentNullException ("basedir");
77 if (!Directory.Exists (basedir))
78 throw new ArgumentException ("basedir", string.Format ("Base documentation directory at '{0}' doesn't exist", basedir));
80 XmlDocument xmlDocument = new XmlDocument ();
81 string filename = Path.Combine (basedir, "monodoc.xml");
82 xmlDocument.Load (filename);
83 IEnumerable<string> sourceFiles = Directory.EnumerateFiles (Path.Combine (basedir, "sources"), "*.source");
85 sourceFiles = sourceFiles.Concat (RootTree.ProbeExternalDirectorySources ());
86 return RootTree.LoadTree (basedir, xmlDocument, sourceFiles);
89 static IEnumerable<string> ProbeExternalDirectorySources ()
91 IEnumerable<string> enumerable = Enumerable.Empty<string> ();
93 string path = Settings.Get ("docExternalPath");
94 enumerable = enumerable.Concat (System.IO.Directory.EnumerateFiles (path, "*.source"));
98 if (Directory.Exists ("/Library/Frameworks/Mono.framework/External/monodoc"))
99 enumerable = enumerable.Concat (Directory.EnumerateFiles ("/Library/Frameworks/Mono.framework/External/monodoc", "*.source"));
103 public static RootTree LoadTree (string indexDir, XmlDocument docTree, IEnumerable<string> sourceFiles)
105 if (docTree == null) {
106 docTree = new XmlDocument ();
107 using (Stream manifestResourceStream = typeof (RootTree).Assembly.GetManifestResourceStream ("monodoc.xml")) {
108 docTree.Load (manifestResourceStream);
112 sourceFiles = (sourceFiles ?? new string[0]);
113 RootTree rootTree = new RootTree ();
114 rootTree.basedir = indexDir;
115 XmlNodeList xml_node_list = docTree.SelectNodes ("/node/node");
116 rootTree.nameToNode["root"] = rootTree.RootNode;
117 rootTree.nameToNode["libraries"] = rootTree.RootNode;
118 rootTree.Populate (rootTree.RootNode, xml_node_list);
120 if (rootTree.LookupEntryPoint ("various") == null) {
121 Console.Error.WriteLine ("No 'various' doc node! Check monodoc.xml!");
122 Node rootNode = rootTree.RootNode;
125 foreach (string current in sourceFiles)
126 rootTree.AddSourceFile (current);
128 foreach (string path in uncompiledHelpSourcePaths) {
129 var hs = new Providers.EcmaUncompiledHelpSource (path);
130 hs.RootTree = rootTree;
131 rootTree.helpSources.Add (hs);
132 string epath = "extra-help-source-" + hs.Name;
133 Node hsn = rootTree.RootNode.CreateNode (hs.Name, "root:/" + epath);
134 rootTree.nameToHelpSource [epath] = hs;
136 foreach (Node n in hs.Tree.RootNode.Nodes)
140 RootTree.PurgeNode (rootTree.RootNode);
141 rootTree.RootNode.Sort ();
145 public void AddSource (string sourcesDir)
147 IEnumerable<string> enumerable = Directory.EnumerateFiles (sourcesDir, "*.source");
148 foreach (string current in enumerable)
149 if (!this.AddSourceFile (current))
150 Console.Error.WriteLine ("Error: Could not load source file {0}", current);
153 public bool AddSourceFile (string sourceFile)
155 if (this.loadedSourceFiles.Contains (sourceFile))
158 Node node = this.LookupEntryPoint ("various") ?? base.RootNode;
159 XmlDocument xmlDocument = new XmlDocument ();
161 xmlDocument.Load (sourceFile);
167 XmlNodeList extra_nodes = xmlDocument.SelectNodes ("/monodoc/node");
168 if (extra_nodes.Count > 0)
169 this.Populate (node, extra_nodes);
171 XmlNodeList sources = xmlDocument.SelectNodes ("/monodoc/source");
172 if (sources == null) {
173 Console.Error.WriteLine ("Error: No <source> section found in the {0} file", sourceFile);
177 loadedSourceFiles.Add (sourceFile);
178 foreach (XmlNode xmlNode in sources) {
179 XmlAttribute a = xmlNode.Attributes["provider"];
181 Console.Error.WriteLine ("Error: no provider in <source>");
184 string provider = a.InnerText;
185 a = xmlNode.Attributes["basefile"];
187 Console.Error.WriteLine ("Error: no basefile in <source>");
190 string basefile = a.InnerText;
191 a = xmlNode.Attributes["path"];
193 Console.Error.WriteLine ("Error: no path in <source>");
196 string path = a.InnerText;
197 string basefilepath = Path.Combine (Path.GetDirectoryName (sourceFile), basefile);
198 HelpSource helpSource = RootTree.GetHelpSource (provider, basefilepath);
199 if (helpSource != null) {
200 helpSource.RootTree = this;
201 this.helpSources.Add (helpSource);
202 this.nameToHelpSource[path] = helpSource;
203 Node node2 = this.LookupEntryPoint (path);
205 Console.Error.WriteLine ("node `{0}' is not defined on the documentation map", path);
208 foreach (Node current in helpSource.Tree.RootNode.Nodes) {
209 node2.AddNode (current);
217 static bool PurgeNode (Node node)
220 if (!node.Documented)
222 List<Node> list = new List<Node> ();
223 foreach (Node current in node.Nodes)
225 bool flag = RootTree.PurgeNode (current);
231 result = (node.Nodes.Count == list.Count);
232 foreach (Node current2 in list)
234 node.DeleteNode (current2);
240 public static string[] GetSupportedFormats ()
252 public static HelpSource GetHelpSource (string provider, string basefilepath)
259 result = new XhtmlHelpSource (basefilepath, false);
262 result = new ManHelpSource (basefilepath, false);
265 result = new ErrorHelpSource (basefilepath, false);
268 result = new EcmaSpecHelpSource (basefilepath, false);
271 result = new EcmaHelpSource (basefilepath, false);
274 Console.Error.WriteLine ("Error: Unknown provider specified: {0}", provider);
278 } catch (FileNotFoundException) {
279 Console.Error.WriteLine ("Error: did not find one of the files in sources/" + basefilepath);
285 public static Provider GetProvider (string provider, params string[] basefilepaths)
289 return new EcmaProvider (basefilepaths[0]);
291 return new EcmaSpecProvider (basefilepaths[0]);
293 return new ErrorProvider (basefilepaths[0]);
295 return new ManProvider (basefilepaths);
298 return new XhtmlProvider (basefilepaths[0]);
301 throw new NotSupportedException (provider);
304 void Populate (Node parent, XmlNodeList xml_node_list)
306 foreach (XmlNode xmlNode in xml_node_list) {
307 XmlAttribute e = xmlNode.Attributes["parent"];
309 if (e != null && this.nameToNode.TryGetValue (e.InnerText, out parent2)) {
310 xmlNode.Attributes.Remove (e);
311 Populate (parent2, xmlNode.SelectNodes ("."));
314 e = xmlNode.Attributes["label"];
316 Console.Error.WriteLine ("`label' attribute missing in <node>");
319 string label = e.InnerText;
320 e = xmlNode.Attributes["name"];
322 Console.Error.WriteLine ("`name' attribute missing in <node>");
325 string name = e.InnerText;
326 Node orCreateNode = parent.GetOrCreateNode (label, "root:/" + name);
327 orCreateNode.EnsureNodes ();
328 this.nameToNode[name] = orCreateNode;
329 XmlNodeList xmlNodeList = xmlNode.SelectNodes ("./node");
330 if (xmlNodeList != null) {
331 this.Populate (orCreateNode, xmlNodeList);
336 public Node LookupEntryPoint (string name)
339 if (!this.nameToNode.TryGetValue (name, out result)) {
345 public TOutput RenderUrl<TOutput> (string url, IDocGenerator<TOutput> generator, out Node node, HelpSource hintSource = null)
348 string internalId = null;
349 HelpSource hs = GetHelpSourceAndIdForUrl (url, hintSource, out internalId, out node);
350 return generator.Generate (hs, internalId);
353 public HelpSource GetHelpSourceAndIdForUrl (string url, HelpSource hintSource, out string internalId, out Node node)
358 if (url.StartsWith ("root:/", StringComparison.OrdinalIgnoreCase))
359 return this.GetHelpSourceAndIdFromName (url.Substring ("root:/".Length), out internalId, out node);
361 HelpSource helpSource = hintSource;
362 if (helpSource == null || string.IsNullOrEmpty (internalId = helpSource.GetInternalIdForUrl (url, out node))) {
363 foreach (var hs in helpSources.Where (h => h.CanHandleUrl (url))) {
364 if (!string.IsNullOrEmpty (internalId = hs.GetInternalIdForUrl (url, out node))) {
374 public HelpSource GetHelpSourceAndIdFromName (string name, out string internalId, out Node node)
376 internalId = "root:";
377 node = this.LookupEntryPoint (name);
379 return node == null ? null : node.Nodes.Select (n => n.Tree.HelpSource).Where (hs => hs != null).Distinct ().FirstOrDefault ();
382 public HelpSource GetHelpSourceFromId (int id)
384 return (id < 0 || id >= this.helpSources.Count) ? null : this.helpSources[id];
387 public Stream GetImage (string url)
389 if (url.StartsWith ("source-id:", StringComparison.OrdinalIgnoreCase)) {
390 string text = url.Substring (10);
391 int num = text.IndexOf (":");
392 string text2 = text.Substring (0, num);
395 id = int.Parse (text2);
397 Console.Error.WriteLine ("Failed to parse source-id url: {0} `{1}'", url, text2);
400 HelpSource helpSourceFromId = this.GetHelpSourceFromId (id);
401 return helpSourceFromId.GetImage (text.Substring (num + 1));
403 Assembly assembly = Assembly.GetAssembly (typeof (RootTree));
404 return assembly.GetManifestResourceStream (url);
407 public IndexReader GetIndex ()
410 string text = Path.Combine (this.basedir, "monodoc.index");
411 if (File.Exists (text))
412 return IndexReader.Load (text);
414 text = Path.Combine (Settings.Get ("monodocIndexDirectory"), "monodoc.index");
415 return IndexReader.Load (text);
421 public static void MakeIndex ()
423 RootTree rootTree = RootTree.LoadTree ();
424 rootTree.GenerateIndex ();
427 public void GenerateIndex ()
429 IndexMaker indexMaker = new IndexMaker ();
430 foreach (HelpSource current in this.helpSources)
431 current.PopulateIndex (indexMaker);
432 string text = Path.Combine (this.basedir, "monodoc.index");
434 indexMaker.Save (text);
435 } catch (UnauthorizedAccessException) {
436 text = Path.Combine (Settings.Get ("docPath"), "monodoc.index");
438 indexMaker.Save (text);
439 } catch (UnauthorizedAccessException) {
440 Console.WriteLine ("Unable to write index file in {0}", Path.Combine (Settings.Get ("docPath"), "monodoc.index"));
445 RootTree.chmod (text, 420);
447 Console.WriteLine ("Documentation index at {0} updated", text);
450 public SearchableIndex GetSearchIndex ()
453 string text = Path.Combine (this.basedir, "search_index");
454 if (System.IO.Directory.Exists (text))
455 return SearchableIndex.Load (text);
456 text = Path.Combine (Settings.Get ("docPath"), "search_index");
457 return SearchableIndex.Load (text);
463 public static void MakeSearchIndex ()
465 RootTree rootTree = RootTree.LoadTree ();
466 rootTree.GenerateSearchIndex ();
469 public void GenerateSearchIndex ()
471 Console.WriteLine ("Loading the monodoc tree...");
472 string text = Path.Combine (this.basedir, "search_index");
473 IndexWriter indexWriter;
474 var analyzer = new StandardAnalyzer (Lucene.Net.Util.Version.LUCENE_CURRENT);
475 var directory = Lucene.Net.Store.FSDirectory.Open (text);
478 if (!Directory.Exists (text))
479 Directory.CreateDirectory (text);
480 indexWriter = new IndexWriter (directory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
481 } catch (UnauthorizedAccessException) {
483 text = Path.Combine (Settings.Get ("docPath"), "search_index");
484 if (!Directory.Exists (text))
485 Directory.CreateDirectory (text);
486 indexWriter = new IndexWriter (directory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
487 } catch (UnauthorizedAccessException) {
488 Console.WriteLine ("You don't have permissions to write on " + text);
492 Console.WriteLine ("Collecting and adding documents...");
493 foreach (HelpSource current in this.helpSources) {
494 current.PopulateSearchableIndex (indexWriter);
496 Console.WriteLine ("Closing...");
497 indexWriter.Optimize ();
498 indexWriter.Close ();
502 static extern int chmod (string filename, int mode);