PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/tools/monodoc/Monodoc/error-provider.cs

http://github.com/mono/mono
C# | 332 lines | 261 code | 58 blank | 13 comment | 21 complexity | ff64d701834036f31d11bbb063ff2aa4 MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, Unlicense, Apache-2.0
  1. //
  2. // error-provider.cs
  3. //
  4. // Author:
  5. // Ben Maurer (bmaurer@users.sourceforge.net)
  6. //
  7. // (C) 2003 Ben Maurer
  8. // Copyright 2003-2011 Novell
  9. // Copyright 2011 Xamarin Inc
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.Collections.Specialized;
  14. using System.Diagnostics;
  15. using System.IO;
  16. using System.Text;
  17. using System.Xml;
  18. using System.Xml.Serialization;
  19. using System.Linq;
  20. using Mono.Lucene.Net.Index;
  21. using Mono.Lucene.Net.Documents;
  22. namespace Monodoc {
  23. #region Data Model
  24. public class ErrorDocumentation {
  25. public string ErrorName;
  26. public ErrorDetails Details;
  27. public StringCollection Examples = new StringCollection ();
  28. public ErrorDocumentation () {}
  29. public ErrorDocumentation (string ErrorName)
  30. {
  31. this.ErrorName = ErrorName;
  32. }
  33. public override string ToString ()
  34. {
  35. StringWriter w = new StringWriter ();
  36. w.WriteLine ("Error Name: {0}", ErrorName);
  37. w.WriteLine ("Details: \n", Details);
  38. w.WriteLine ("Examples: ");
  39. foreach (string s in Examples) {
  40. w.WriteLine (s);
  41. w.WriteLine ();
  42. }
  43. return w.ToString ();
  44. }
  45. public string RenderAsHtml ()
  46. {
  47. StringWriter sw = new StringWriter ();
  48. XmlWriter w = new XmlTextWriter (sw);
  49. if (HelpSource.use_css)
  50. w.WriteRaw ("<div class=\"header\" id=\"error_ref\">" +
  51. " <div class=\"subtitle\">Compiler Error Reference</div> " +
  52. " <div class=\"title\">Error " + ErrorName + " </div></div>");
  53. else
  54. w.WriteRaw (@"
  55. <table width='100%'>
  56. <tr bgcolor='#b0c4de'><td>
  57. <i>Compiler Error Reference</i>
  58. <h3>Error " + ErrorName + @"</h2>
  59. </td></tr>
  60. </table><br />");
  61. if (Details != null) {
  62. if (HelpSource.use_css)
  63. w.WriteRaw ("<div class=\"summary\">Summary</div>");
  64. else
  65. w.WriteRaw (@"<h3>Summary</h3>");
  66. Details.Summary.WriteTo (w);
  67. if (HelpSource.use_css)
  68. w.WriteRaw ("<div class=\"details\">Details</div>");
  69. else
  70. w.WriteRaw (@"<h3>Details</h3>");
  71. Details.Details.WriteTo (w);
  72. }
  73. foreach (string xmp in Examples) {
  74. if (HelpSource.use_css)
  75. w.WriteRaw ("<div class=\"code_example\">" +
  76. "<div class=\"code_ex_title\">Example</div>");
  77. else
  78. w.WriteRaw (@"<table bgcolor='#f5f5dd' border='1'>
  79. <tr><td><b><font size='-1'>Example</font></b></td></tr>
  80. <tr><td><font size='-1'><pre>");
  81. w.WriteRaw (Mono.Utilities.Colorizer.Colorize (xmp, "c#"));
  82. if (HelpSource.use_css)
  83. w.WriteRaw ("</div>");
  84. else
  85. w.WriteRaw (@"</pre></font></td></tr></table>");
  86. }
  87. w.Close ();
  88. return sw.ToString ();
  89. }
  90. }
  91. public class ErrorDetails {
  92. public XmlNode Summary;
  93. public XmlNode Details;
  94. public override string ToString ()
  95. {
  96. StringWriter w = new StringWriter ();
  97. w.WriteLine ("Summary: \n {0}", Summary.OuterXml);
  98. w.WriteLine ("Details: \n {0}", Summary.OuterXml);
  99. return w.ToString ();
  100. }
  101. }
  102. public class ErrorProviderConfig {
  103. public string FilesPath;
  104. public string Match;
  105. public int ErrorNumSubstringStart;
  106. public int ErrorNumSubstringLength;
  107. public string FriendlyFormatString;
  108. public override string ToString ()
  109. {
  110. StringWriter w = new StringWriter ();
  111. w.WriteLine ("FilesPath: {0}", FilesPath);
  112. w.WriteLine ("Match: {0}", Match);
  113. w.WriteLine ("Error Number Substring: {0} Length:{1}", ErrorNumSubstringStart, ErrorNumSubstringLength);
  114. w.WriteLine ("FriendlyFormatString: {0}", FriendlyFormatString);
  115. return w.ToString ();
  116. }
  117. public Hashtable Compile (HelpSource hs)
  118. {
  119. string [] files = Directory.GetFiles (FilesPath, Match);
  120. Hashtable ret = new Hashtable ();
  121. foreach (string s in files) {
  122. ErrorDocumentation d;
  123. hs.Message (TraceLevel.Info, s);
  124. int errorNum = 0;
  125. try {
  126. errorNum = int.Parse (Path.GetFileName (s).Substring (ErrorNumSubstringStart, ErrorNumSubstringLength));
  127. } catch {
  128. hs.Message (TraceLevel.Info, "Ignoring file {0}", s);
  129. }
  130. string errorName = String.Format (FriendlyFormatString, errorNum);
  131. d = (ErrorDocumentation)ret [errorName];
  132. if (d == null)
  133. ret [errorName] = d = new ErrorDocumentation (errorName);
  134. if (d.Details == null) {
  135. string xmlFile = Path.ChangeExtension (s, "xml");
  136. hs.Message (TraceLevel.Verbose, xmlFile);
  137. if (File.Exists (xmlFile)) {
  138. XmlSerializer cfgRdr = new XmlSerializer (typeof (ErrorDetails));
  139. d.Details = (ErrorDetails)cfgRdr.Deserialize (new XmlTextReader (xmlFile));
  140. }
  141. }
  142. // Encoding is same as used in MCS, so we will be able to do all those files
  143. using (StreamReader reader = new StreamReader (s, Encoding.GetEncoding (28591))) {
  144. d.Examples.Add (reader.ReadToEnd ());
  145. }
  146. }
  147. return ret;
  148. }
  149. }
  150. #endregion
  151. #region Monodoc Rendering
  152. public class ErrorProvider : Provider {
  153. ErrorProviderConfig config;
  154. public ErrorProvider (string configFile)
  155. {
  156. config = ReadConfig (configFile);
  157. }
  158. public static ErrorProviderConfig ReadConfig (string file)
  159. {
  160. XmlSerializer cfgRdr = new XmlSerializer (typeof (ErrorProviderConfig));
  161. ErrorProviderConfig ret = (ErrorProviderConfig)cfgRdr.Deserialize (new XmlTextReader (file));
  162. // handle path rel to the config file
  163. ret.FilesPath = Path.Combine (Path.GetDirectoryName (file), ret.FilesPath);
  164. return ret;
  165. }
  166. public override void PopulateTree (Tree tree)
  167. {
  168. // everything is done in CloseTree so we can pack
  169. }
  170. public override void CloseTree (HelpSource hs, Tree tree)
  171. {
  172. Hashtable entries = config.Compile (hs);
  173. MemoryStream ms = new MemoryStream ();
  174. XmlSerializer writer = new XmlSerializer (typeof (ErrorDocumentation));
  175. foreach (DictionaryEntry de in entries) {
  176. ErrorDocumentation d = (ErrorDocumentation)de.Value;
  177. string s = (string)de.Key;
  178. tree.LookupNode (s, "error:" + s);
  179. writer.Serialize (ms, d);
  180. ms.Position = 0;
  181. hs.PackStream (ms, s);
  182. ms.SetLength (0);
  183. }
  184. tree.Sort ();
  185. }
  186. }
  187. public class ErrorHelpSource : HelpSource {
  188. public ErrorHelpSource (string base_file, bool create) : base (base_file, create) {}
  189. public override string InlineCss {
  190. get {return base.InlineCss + css_error_code;}
  191. }
  192. public override string GetText (string url, out Node match_node)
  193. {
  194. match_node = null;
  195. string c = GetCachedText (url);
  196. if (c != null)
  197. return c;
  198. if (url == "root:")
  199. if (HelpSource.use_css)
  200. return BuildHtml (css_error_code, "<div id=\"error_ref\" class=\"header\"><div class=\"title\">Compiler Error Reference</div></div>");
  201. else
  202. return BuildHtml (String.Empty, "<table width=\"100%\" bgcolor=\"#b0c4de\" cellpadding=\"5\"><tr><td><h3>Compiler Error Reference</h3></tr></td></table>");
  203. if (!url.StartsWith ("error:"))
  204. return null;
  205. foreach (Node n in Tree.Nodes) {
  206. if (n.Element != url) continue;
  207. match_node = n;
  208. XmlSerializer reader = new XmlSerializer (typeof (ErrorDocumentation));
  209. ErrorDocumentation d = (ErrorDocumentation)reader.Deserialize (
  210. GetHelpStream (n.Element.Substring (6))
  211. );
  212. return BuildHtml (css_error_code, d.RenderAsHtml ());
  213. }
  214. return null;
  215. }
  216. public override void PopulateIndex (IndexMaker index_maker)
  217. {
  218. foreach (Node n in Tree.Nodes)
  219. index_maker.Add (n.Caption, n.Caption, n.Element);
  220. }
  221. public override void PopulateSearchableIndex (IndexWriter writer)
  222. {
  223. foreach (Node n in Tree.Nodes) {
  224. XmlSerializer reader = new XmlSerializer (typeof (ErrorDocumentation));
  225. ErrorDocumentation d = (ErrorDocumentation)reader.Deserialize (GetHelpStream (n.Element.Substring (6)));
  226. SearchableDocument doc = new SearchableDocument ();
  227. doc.title = d.ErrorName;
  228. doc.url = n.Element;
  229. doc.text = d.Details != null ? d.Details.ToString () : string.Empty;
  230. doc.examples = d.Examples.Cast<string> ().Aggregate ((e1, e2) => e1 + Environment.NewLine + e2);
  231. doc.hottext = d.ErrorName;
  232. writer.AddDocument (doc.LuceneDoc);
  233. }
  234. }
  235. public static string css_error_code = @"
  236. #error_ref {
  237. background: #debcb0;
  238. border: 2px solid #782609;
  239. }
  240. div.summary {
  241. font-size: 110%;
  242. font-weight: bolder;
  243. }
  244. div.details {
  245. font-size: 110%;
  246. font-weight: bolder;
  247. }
  248. div.code_example {
  249. background: #f5f5dd;
  250. border: 1px solid #cdcd82;
  251. border: 1px solid black;
  252. padding-left: 1em;
  253. padding-bottom: 1em;
  254. margin-top: 1em;
  255. white-space: pre;
  256. margin-bottom: 1em;
  257. }
  258. div.code_ex_title {
  259. position: relative;
  260. top: -1em;
  261. left: 30%;
  262. background: #cdcd82;
  263. border: 1px solid black;
  264. color: black;
  265. font-size: 65%;
  266. text-transform: uppercase;
  267. width: 40%;
  268. padding: 0.3em;
  269. text-align: center;
  270. }";
  271. }
  272. #endregion
  273. }