5 // Ben Maurer (bmaurer@users.sourceforge.net)
8 // Copyright 2003-2011 Novell
9 // Copyright 2011 Xamarin Inc
13 using System.Collections.Generic;
17 using System.Xml.Serialization;
19 using Lucene.Net.Index;
20 using Lucene.Net.Documents;
22 namespace Monodoc.Providers
24 public class ErrorProviderConfig
26 public string FilesPath;
28 public int ErrorNumSubstringStart;
29 public int ErrorNumSubstringLength;
30 public string FriendlyFormatString;
32 public override string ToString ()
34 var sb = new StringBuilder ();
35 var w = new StringWriter (sb);
37 w.WriteLine ("FilesPath: {0}", FilesPath);
38 w.WriteLine ("Match: {0}", Match);
39 w.WriteLine ("Error Number Substring: {0} Length:{1}", ErrorNumSubstringStart, ErrorNumSubstringLength);
40 w.WriteLine ("FriendlyFormatString: {0}", FriendlyFormatString);
45 public Dictionary<string, ErrorDocumentation> Compile (HelpSource hs)
47 string[] files = Directory.GetFiles (FilesPath, Match);
48 var ret = new Dictionary<string, ErrorDocumentation> ();
50 foreach (string s in files) {
55 errorNum = int.Parse (Path.GetFileName (s).Substring (ErrorNumSubstringStart, ErrorNumSubstringLength));
57 Console.WriteLine ("Ignoring file {0}", s);
60 string errorName = String.Format (FriendlyFormatString, errorNum);
62 if (!ret.TryGetValue (errorName, out d))
63 ret[errorName] = d = new ErrorDocumentation (errorName);
65 if (d.Details == null) {
66 string xmlFile = Path.ChangeExtension (s, "xml");
67 if (File.Exists (xmlFile)) {
68 XmlSerializer cfgRdr = new XmlSerializer (typeof (ErrorDetails));
69 d.Details = (ErrorDetails)cfgRdr.Deserialize (new XmlTextReader (xmlFile));
72 // Encoding is same as used in MCS, so we will be able to do all those files
73 using (StreamReader reader = new StreamReader (s, Encoding.GetEncoding (28591))) {
74 d.Examples.Add (reader.ReadToEnd ());
82 public class ErrorDocumentation
84 public string ErrorName;
85 public ErrorDetails Details;
86 public List<string> Examples = new List<string> ();
88 public ErrorDocumentation () {}
89 public ErrorDocumentation (string ErrorName)
91 this.ErrorName = ErrorName;
95 public class ErrorDetails
97 public XmlNode Summary;
98 public XmlNode Details;
101 public class ErrorProvider : Provider
103 ErrorProviderConfig config;
105 public ErrorProvider (string configFile)
107 config = ReadConfig (configFile);
110 public static ErrorProviderConfig ReadConfig (string file)
112 XmlSerializer cfgRdr = new XmlSerializer (typeof (ErrorProviderConfig));
113 ErrorProviderConfig ret = (ErrorProviderConfig)cfgRdr.Deserialize (new XmlTextReader (file));
114 // handle path rel to the config file
115 ret.FilesPath = Path.Combine (Path.GetDirectoryName (file), ret.FilesPath);
119 public override void PopulateTree (Tree tree)
121 // everything is done in CloseTree so we can pack
124 public override void CloseTree (HelpSource hs, Tree tree)
126 var entries = config.Compile (hs);
127 MemoryStream ms = new MemoryStream ();
128 XmlSerializer writer = new XmlSerializer (typeof (ErrorDocumentation));
130 foreach (var de in entries) {
131 ErrorDocumentation d = de.Value;
134 tree.RootNode.GetOrCreateNode (s, "error:" + s);
136 writer.Serialize (ms, d);
138 hs.Storage.Store (s, ms);
142 tree.RootNode.Sort ();
146 public class ErrorHelpSource : HelpSource
148 public ErrorHelpSource (string base_file, bool create) : base (base_file, create)
152 public override string GetText (string id)
154 return TreeDumper.ExportToTocXml (Tree.RootNode, "Compiler Error Reference", "In this section:");
157 protected override string UriPrefix {
163 public override bool IsGeneratedContent (string id)
165 return id == "root:";
168 public override DocumentType GetDocumentTypeForId (string id)
170 return id == "root:" ? DocumentType.TocXml : DocumentType.ErrorXml;
173 public override string GetInternalIdForUrl (string url, out Node node, out Dictionary<string, string> context)
175 var result = base.GetInternalIdForUrl (url, out node, out context);
176 return result.ToLower ();
179 public override void PopulateIndex (IndexMaker index_maker)
181 foreach (Node n in Tree.RootNode.ChildNodes)
182 index_maker.Add (n.Caption, n.Caption, n.Element);
185 public override void PopulateSearchableIndex (IndexWriter writer)
187 foreach (Node n in Tree.RootNode.ChildNodes) {
188 XmlSerializer reader = new XmlSerializer (typeof (ErrorDocumentation));
189 ErrorDocumentation d = (ErrorDocumentation)reader.Deserialize (GetHelpStream (n.Element.Substring (6)));
190 SearchableDocument doc = new SearchableDocument ();
191 doc.Title = d.ErrorName;
193 doc.Text = d.Details != null ? d.Details.ToString () : string.Empty;
194 doc.Examples = d.Examples.Cast<string> ().Aggregate ((e1, e2) => e1 + Environment.NewLine + e2);
195 doc.HotText = d.ErrorName;
196 writer.AddDocument (doc.LuceneDoc);