5 // Ben Maurer (bmaurer@users.sourceforge.net)
11 using System.Collections;
12 using System.Collections.Specialized;
16 using System.Xml.Serialization;
21 public class ErrorDocumentation {
22 public string ErrorName;
23 public ErrorDetails Details;
24 public StringCollection Examples = new StringCollection ();
26 public ErrorDocumentation () {}
27 public ErrorDocumentation (string ErrorName)
29 this.ErrorName = ErrorName;
32 public override string ToString ()
34 StringWriter w = new StringWriter ();
36 w.WriteLine ("Error Name: {0}", ErrorName);
37 w.WriteLine ("Details: \n", Details);
39 w.WriteLine ("Examples: ");
40 foreach (string s in Examples) {
48 public string RenderAsHtml ()
50 StringWriter sw = new StringWriter ();
51 XmlWriter w = new XmlTextWriter (sw);
52 if (HelpSource.use_css)
53 w.WriteRaw ("<div class=\"header\" id=\"error_ref\">" +
54 " <div class=\"subtitle\">Compiler Error Reference</div> " +
55 " <div class=\"title\">Error " + ErrorName + " </div></div>");
60 <tr bgcolor='#b0c4de'><td>
61 <i>Compiler Error Reference</i>
62 <h3>Error " + ErrorName + @"</h2>
67 if (Details != null) {
68 if (HelpSource.use_css)
69 w.WriteRaw ("<div class=\"summary\">Summary</div>");
71 w.WriteRaw (@"<h3>Summary</h3>");
72 Details.Summary.WriteTo (w);
74 if (HelpSource.use_css)
75 w.WriteRaw ("<div class=\"details\">Details</div>");
77 w.WriteRaw (@"<h3>Details</h3>");
79 Details.Details.WriteTo (w);
82 foreach (string xmp in Examples) {
83 if (HelpSource.use_css)
84 w.WriteRaw ("<div class=\"code_example\">" +
85 "<div class=\"code_ex_title\">Example</div>");
87 w.WriteRaw (@"<table bgcolor='#f5f5dd' border='1'>
88 <tr><td><b><font size='-1'>Example</font></b></td></tr>
89 <tr><td><font size='-1'><pre>");
90 w.WriteRaw (Mono.Utilities.Colorizer.Colorize (xmp, "c#"));
91 if (HelpSource.use_css)
92 w.WriteRaw ("</div>");
94 w.WriteRaw (@"</pre></font></td></tr></table>");
99 return sw.ToString ();
103 public class ErrorDetails {
104 public XmlNode Summary;
105 public XmlNode Details;
107 public override string ToString ()
109 StringWriter w = new StringWriter ();
111 w.WriteLine ("Summary: \n {0}", Summary.OuterXml);
112 w.WriteLine ("Details: \n {0}", Summary.OuterXml);
114 return w.ToString ();
118 public class ErrorProviderConfig {
119 public string FilesPath;
121 public int ErrorNumSubstringStart;
122 public int ErrorNumSubstringLength;
123 public string FriendlyFormatString;
125 public override string ToString ()
127 StringWriter w = new StringWriter ();
129 w.WriteLine ("FilesPath: {0}", FilesPath);
130 w.WriteLine ("Match: {0}", Match);
131 w.WriteLine ("Error Number Substring: {0} Length:{1}", ErrorNumSubstringStart, ErrorNumSubstringLength);
132 w.WriteLine ("FriendlyFormatString: {0}", FriendlyFormatString);
134 return w.ToString ();
137 public Hashtable Compile ()
139 string [] files = Directory.GetFiles (FilesPath, Match);
140 Hashtable ret = new Hashtable ();
142 foreach (string s in files) {
143 ErrorDocumentation d;
145 Console.WriteLine (s);
150 errorNum = int.Parse (Path.GetFileName (s).Substring (ErrorNumSubstringStart, ErrorNumSubstringLength));
152 Console.WriteLine ("Ignoring file {0}", s);
155 string errorName = String.Format (FriendlyFormatString, errorNum);
157 d = (ErrorDocumentation)ret [errorName];
159 ret [errorName] = d = new ErrorDocumentation (errorName);
161 if (d.Details == null) {
162 string xmlFile = Path.ChangeExtension (s, "xml");
163 Console.WriteLine (xmlFile);
164 if (File.Exists (xmlFile)) {
165 XmlSerializer cfgRdr = new XmlSerializer (typeof (ErrorDetails));
166 d.Details = (ErrorDetails)cfgRdr.Deserialize (new XmlTextReader (xmlFile));
169 // Encoding is same as used in MCS, so we will be able to do all those files
170 using (StreamReader reader = new StreamReader (s, Encoding.GetEncoding (28591))) {
171 d.Examples.Add (reader.ReadToEnd ());
181 #region Monodoc Rendering
182 public class ErrorProvider : Provider {
183 ErrorProviderConfig config;
185 public ErrorProvider (string configFile)
187 config = ReadConfig (configFile);
190 public static ErrorProviderConfig ReadConfig (string file)
192 XmlSerializer cfgRdr = new XmlSerializer (typeof (ErrorProviderConfig));
193 ErrorProviderConfig ret = (ErrorProviderConfig)cfgRdr.Deserialize (new XmlTextReader (file));
194 // handle path rel to the config file
195 ret.FilesPath = Path.Combine (Path.GetDirectoryName (file), ret.FilesPath);
199 public override void PopulateTree (Tree tree)
201 // everything is done in CloseTree so we can pack
204 public override void CloseTree (HelpSource hs, Tree tree)
206 Hashtable entries = config.Compile ();
207 MemoryStream ms = new MemoryStream ();
208 XmlSerializer writer = new XmlSerializer (typeof (ErrorDocumentation));
210 foreach (DictionaryEntry de in entries) {
211 ErrorDocumentation d = (ErrorDocumentation)de.Value;
212 string s = (string)de.Key;
214 tree.LookupNode (s, "error:" + s);
216 writer.Serialize (ms, d);
218 hs.PackStream (ms, s);
227 public class ErrorHelpSource : HelpSource {
229 public ErrorHelpSource (string base_file, bool create) : base (base_file, create) {}
231 public override string GetText (string url, out Node match_node)
236 if (HelpSource.use_css)
237 return BuildHtml (css_error_code, "<div id=\"error_ref\" class=\"header\"><div class=\"title\">Compiler Error Reference</div></div>");
239 return BuildHtml (String.Empty, "<table width=\"100%\" bgcolor=\"#b0c4de\" cellpadding=\"5\"><tr><td><h3>Compiler Error Reference</h3></tr></td></table>");
241 if (!url.StartsWith ("error:"))
244 foreach (Node n in Tree.Nodes) {
245 if (n.Element != url) continue;
248 XmlSerializer reader = new XmlSerializer (typeof (ErrorDocumentation));
249 ErrorDocumentation d = (ErrorDocumentation)reader.Deserialize (
250 GetHelpStream (n.Element.Substring (6))
252 return BuildHtml (css_error_code, d.RenderAsHtml ());
258 public override void PopulateIndex (IndexMaker index_maker)
260 foreach (Node n in Tree.Nodes)
261 index_maker.Add (n.Caption, n.Caption, n.Element);
264 public static string css_error_code = @"
267 border: 2px solid #782609;
279 border: 1px solid #cdcd82;
280 border: 1px solid black;
292 border: 1px solid black;
295 text-transform: uppercase;