File: error-provider.cs

package info (click to toggle)
mono 6.12.0.199%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,192 kB
  • sloc: cs: 11,181,844; xml: 2,850,076; ansic: 689,413; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,845; makefile: 19,951; sh: 15,030; python: 4,771; pascal: 925; sql: 859; sed: 16; php: 1
file content (200 lines) | stat: -rw-r--r-- 5,440 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// error-provider.cs
//
// Author:
//   Ben Maurer (bmaurer@users.sourceforge.net)
//
// (C) 2003 Ben Maurer
// Copyright 2003-2011 Novell
// Copyright 2011 Xamarin Inc
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Linq;
using Lucene.Net.Index;
using Lucene.Net.Documents;

namespace Monodoc.Providers
{
	public class ErrorProviderConfig
	{
		public string FilesPath;
		public string Match;
		public int ErrorNumSubstringStart;
		public int ErrorNumSubstringLength;
		public string FriendlyFormatString;

		public override string ToString ()
		{
			var sb = new StringBuilder ();
			var w = new StringWriter (sb);
			
			w.WriteLine ("FilesPath: {0}", FilesPath);
			w.WriteLine ("Match: {0}", Match);
			w.WriteLine ("Error Number Substring: {0} Length:{1}", ErrorNumSubstringStart, ErrorNumSubstringLength);
			w.WriteLine ("FriendlyFormatString: {0}", FriendlyFormatString);
			
			return w.ToString ();
		}
		
		public Dictionary<string, ErrorDocumentation> Compile (HelpSource hs)
		{
			string[] files = Directory.GetFiles (FilesPath, Match);
			var ret = new Dictionary<string, ErrorDocumentation> ();
			
			foreach (string s in files) {
				ErrorDocumentation d;
				int errorNum = 0;

				try {
					errorNum = int.Parse (Path.GetFileName (s).Substring (ErrorNumSubstringStart, ErrorNumSubstringLength));
				} catch {
					Console.WriteLine ("Ignoring file {0}", s);
				}
				
				string errorName = String.Format (FriendlyFormatString, errorNum);
				
				if (!ret.TryGetValue (errorName, out d))
					ret[errorName] = d = new ErrorDocumentation (errorName);

				if (d.Details == null) {
					string xmlFile = Path.ChangeExtension (s, "xml");
					if (File.Exists (xmlFile)) {
						XmlSerializer cfgRdr = new XmlSerializer (typeof (ErrorDetails));
						d.Details = (ErrorDetails)cfgRdr.Deserialize (new XmlTextReader (xmlFile));
					}
				}
				// Encoding is same as used in MCS, so we will be able to do all those files
				using (StreamReader reader = new StreamReader (s, Encoding.GetEncoding (28591))) {
					d.Examples.Add (reader.ReadToEnd ());
				}
			}
			
			return ret;
		}
	}

	public class ErrorDocumentation
	{
		public string ErrorName;
		public ErrorDetails Details;
		public List<string> Examples = new List<string> ();
		
		public ErrorDocumentation () {}
		public ErrorDocumentation (string ErrorName)
		{
			this.ErrorName = ErrorName;
		}
	}
	
	public class ErrorDetails
	{
		public XmlNode Summary;
		public XmlNode Details;
	}

	public class ErrorProvider : Provider
	{
		ErrorProviderConfig config;
		
		public ErrorProvider (string configFile)
		{
			config = ReadConfig (configFile);
		}
		
		public static ErrorProviderConfig ReadConfig (string file)
		{
			XmlSerializer cfgRdr = new XmlSerializer (typeof (ErrorProviderConfig));
			ErrorProviderConfig ret = (ErrorProviderConfig)cfgRdr.Deserialize (new XmlTextReader (file));
			// handle path rel to the config file
			ret.FilesPath = Path.Combine (Path.GetDirectoryName (file), ret.FilesPath);
			return ret;
		}
	
		public override void PopulateTree (Tree tree)
		{
			// everything is done in CloseTree so we can pack
		}
	
		public override void CloseTree (HelpSource hs, Tree tree)
		{
			var entries = config.Compile (hs);
			MemoryStream ms = new MemoryStream ();
			XmlSerializer writer = new XmlSerializer (typeof (ErrorDocumentation));
			
			foreach (var de in entries) {
				ErrorDocumentation d = de.Value;
				string s = de.Key;

				tree.RootNode.GetOrCreateNode (s, "error:" + s);
				
				writer.Serialize (ms, d);
				ms.Position = 0;
				hs.Storage.Store (s, ms);
				ms.SetLength (0);
			}
			
			tree.RootNode.Sort ();
		}
	}
	
	public class ErrorHelpSource : HelpSource
	{		
		public ErrorHelpSource (string base_file, bool create) : base (base_file, create)
		{
		}

		public override string GetText (string id)
		{
			return TreeDumper.ExportToTocXml (Tree.RootNode, "Compiler Error Reference", "In this section:");
		}
		
		protected override string UriPrefix {
			get {
				return "error:";
			}
		}

		public override bool IsGeneratedContent (string id)
		{
			return id == "root:";
		}

		public override DocumentType GetDocumentTypeForId (string id)
		{
			return id == "root:" ? DocumentType.TocXml : DocumentType.ErrorXml;
		}

		public override string GetInternalIdForUrl (string url, out Node node, out Dictionary<string, string> context)
		{
			var result = base.GetInternalIdForUrl (url, out node, out context);
			return result.ToLower ();
		}
		
		public override void PopulateIndex (IndexMaker index_maker)
		{
			foreach (Node n in Tree.RootNode.ChildNodes)
				index_maker.Add (n.Caption, n.Caption, n.Element);
		}

		public override void PopulateSearchableIndex (IndexWriter writer) 
		{
			foreach (Node n in Tree.RootNode.ChildNodes) {
				XmlSerializer reader = new XmlSerializer (typeof (ErrorDocumentation));
				ErrorDocumentation d = (ErrorDocumentation)reader.Deserialize (GetHelpStream (n.Element.Substring (6)));
				SearchableDocument doc = new SearchableDocument ();
				doc.Title = d.ErrorName;
				doc.Url = n.Element;
				doc.Text = d.Details != null ? d.Details.ToString () : string.Empty;
				doc.Examples = d.Examples.Cast<string> ().Aggregate ((e1, e2) => e1 + Environment.NewLine + e2);
				doc.HotText = d.ErrorName;
				writer.AddDocument (doc.LuceneDoc);
			}
		}
	}
}