xbuild: use the AdditionalReferencePath items to locate assemblies
[mono.git] / mcs / class / monodoc / Monodoc / providers / ecma-provider.cs
1 //
2 // The ecmaspec provider is for ECMA specifications
3 //
4 // Authors:
5 //      John Luke (jluke@cfl.rr.com)
6 //      Ben Maurer (bmaurer@users.sourceforge.net)
7 //
8 // Use like this:
9 //   mono assembler.exe --ecmaspec DIRECTORY --out name
10 //
11
12 using System;
13 using System.Linq;
14 using System.IO;
15 using System.Text;
16 using System.Xml;
17 using System.Xml.Linq;
18 using System.Collections.Generic;
19
20 using Lucene.Net.Index;
21 using Lucene.Net.Documents;
22
23 using Monodoc.Ecma;
24 using Mono.Utilities;
25
26 namespace Monodoc.Providers
27 {
28         public class EcmaProvider : Provider
29         {
30                 HashSet<string> directories = new HashSet<string> ();
31
32                 public EcmaProvider ()
33                 {
34                 }
35
36                 public EcmaProvider (string baseDir)
37                 {
38                         AddDirectory (baseDir);
39                 }
40
41                 public void AddDirectory (string directory)
42                 {
43                         if (string.IsNullOrEmpty (directory))
44                                 throw new ArgumentNullException ("directory");
45
46                         directories.Add (directory);
47                 }
48
49                 public override void PopulateTree (Tree tree)
50                 {
51                         var storage = tree.HelpSource.Storage;
52                         var nsSummaries = new Dictionary<string, XElement> ();
53                         int resID = 0;
54
55                         foreach (var asm in directories) {
56                                 var indexFilePath = Path.Combine (asm, "index.xml");
57                                 if (!File.Exists (indexFilePath)) {
58                                         Console.Error.WriteLine ("Warning: couldn't process directory `{0}' as it has no index.xml file", asm);
59                                         continue;
60                                 }
61
62                                 EcmaDoc.PopulateTreeFromIndexFile (indexFilePath, EcmaHelpSource.EcmaPrefix, tree, storage, nsSummaries, _ => resID++.ToString ());
63                         }
64
65                         foreach (var summary in nsSummaries)
66                                 storage.Store ("xml.summary." + summary.Key, summary.Value.ToString ());
67
68                         var masterSummary = new XElement ("elements",
69                                                           directories
70                                                           .SelectMany (d => Directory.EnumerateFiles (d, "ns-*.xml"))
71                                                           .Select (ExtractNamespaceSummary));
72                         storage.Store ("mastersummary.xml", masterSummary.ToString ());
73                 }
74
75                 XElement ExtractNamespaceSummary (string nsFile)
76                 {
77                         using (var reader = XmlReader.Create (nsFile)) {
78                                 reader.ReadToFollowing ("Namespace");
79                                 var name = reader.GetAttribute ("Name");
80                                 var summary = reader.ReadToFollowing ("summary") ? XElement.Load (reader.ReadSubtree ()) : new XElement ("summary");
81                                 var remarks = reader.ReadToFollowing ("remarks") ? XElement.Load (reader.ReadSubtree ()) : new XElement ("remarks");
82
83                                 return new XElement ("namespace",
84                                                      new XAttribute ("ns", name ?? string.Empty),
85                                                      summary,
86                                                      remarks);
87                         }
88                 }
89
90                 public override void CloseTree (HelpSource hs, Tree tree)
91                 {
92                         AddImages (hs);
93                         AddExtensionMethods (hs);
94                 }
95
96                 void AddEcmaXml (HelpSource hs)
97                 {
98                         var xmls = directories
99                                 .SelectMany (Directory.EnumerateDirectories) // Assemblies
100                                 .SelectMany (Directory.EnumerateDirectories) // Namespaces
101                                 .SelectMany (Directory.EnumerateFiles)
102                                 .Where (f => f.EndsWith (".xml")); // Type XML files
103
104                         int resID = 0;
105                         foreach (var xml in xmls)
106                                 using (var file = File.OpenRead (xml))
107                                         hs.Storage.Store ((resID++).ToString (), file);
108                 }
109
110                 void AddImages (HelpSource hs)
111                 {
112                         var imgs = directories
113                                 .SelectMany (Directory.EnumerateDirectories)
114                                 .Select (d => Path.Combine (d, "_images"))
115                                 .Where (Directory.Exists)
116                                 .SelectMany (Directory.EnumerateFiles);
117
118                         foreach (var img in imgs)
119                                 using (var file = File.OpenRead (img))
120                                         hs.Storage.Store (Path.GetFileName (img), file);
121                 }
122
123                 void AddExtensionMethods (HelpSource hs)
124                 {
125                         var extensionMethods = directories
126                                 .SelectMany (Directory.EnumerateDirectories)
127                                 .Select (d => Path.Combine (d, "index.xml"))
128                                 .Where (File.Exists)
129                                 .Select (f => {
130                                         using (var file = File.OpenRead (f)) {
131                                                 var reader = XmlReader.Create (file);
132                                                 reader.ReadToFollowing ("ExtensionMethods");
133                                                 return reader.ReadInnerXml ();
134                                         }
135                             })
136                             .DefaultIfEmpty (string.Empty);
137
138                         hs.Storage.Store ("ExtensionMethods.xml",
139                                           "<ExtensionMethods>" + extensionMethods.Aggregate (string.Concat) + "</ExtensionMethods>");
140                 }
141
142                 IEnumerable<string> GetEcmaXmls ()
143                 {
144                         return directories
145                                 .SelectMany (Directory.EnumerateDirectories) // Assemblies
146                                 .SelectMany (Directory.EnumerateDirectories) // Namespaces
147                                 .SelectMany (Directory.EnumerateFiles)
148                                 .Where (f => f.EndsWith (".xml")); // Type XML files
149                 }
150         }
151
152         public class EcmaHelpSource : HelpSource
153         {
154                 internal const string EcmaPrefix = "ecma:";
155                 LRUCache<string, Node> cache = new LRUCache<string, Node> (4);
156
157                 public EcmaHelpSource (string base_file, bool create) : base (base_file, create)
158                 {
159                 }
160
161                 protected EcmaHelpSource () : base ()
162                 {
163                 }
164
165                 protected override string UriPrefix {
166                         get {
167                                 return EcmaPrefix;
168                         }
169                 }
170
171                 public override bool CanHandleUrl (string url)
172                 {
173                         if (url.Length > 2 && url[1] == ':') {
174                                 switch (url[0]) {
175                                 case 'T':
176                                 case 'M':
177                                 case 'C':
178                                 case 'P':
179                                 case 'E':
180                                 case 'F':
181                                 case 'N':
182                                 case 'O':
183                                         return true;
184                                 }
185                         }
186                         return base.CanHandleUrl (url);
187                 }
188
189                 // Clean the extra paramers in the id
190                 public override Stream GetHelpStream (string id)
191                 {
192                         var idParts = id.Split ('?');
193                         var name = idParts[0];
194                         if (name == "root:")
195                                 name = "mastersummary.xml";
196                         return base.GetHelpStream (name);
197                 }
198
199                 public override Stream GetCachedHelpStream (string id)
200                 {
201                         var idParts = id.Split ('?');
202                         return base.GetCachedHelpStream (idParts[0]);
203                 }
204
205                 public override DocumentType GetDocumentTypeForId (string id)
206                 {
207                         return DocumentType.EcmaXml;
208                 }
209
210                 public override string GetPublicUrl (Node node)
211                 {
212                         string url = string.Empty;
213                         var type = EcmaDoc.GetNodeType (node);
214                         //Console.WriteLine ("GetPublicUrl {0} : {1} [{2}]", node.Element, node.Caption, type.ToString ());
215                         switch (type) {
216                         case EcmaNodeType.Namespace:
217                                 return node.Element; // A namespace node has already a well formated internal url
218                         case EcmaNodeType.Type:
219                                 return MakeTypeNodeUrl (node);
220                         case EcmaNodeType.Meta:
221                                 return MakeTypeNodeUrl (GetNodeTypeParent (node)) + GenerateMetaSuffix (node);
222                         case EcmaNodeType.Member:
223                                 var typeChar = EcmaDoc.GetNodeMemberTypeChar (node);
224                                 var parentNode = GetNodeTypeParent (node);
225                                 var typeNode = MakeTypeNodeUrl (parentNode).Substring (2);
226                                 return typeChar + ":" + typeNode + MakeMemberNodeUrl (typeChar, node);
227                         default:
228                                 return null;
229                         }
230                 }
231
232                 string MakeTypeNodeUrl (Node node)
233                 {
234                         // A Type node has a Element property of the form: 'ecma:{number}#{typename}/'
235                         var hashIndex = node.Element.IndexOf ('#');
236                         var typeName = node.Element.Substring (hashIndex + 1, node.Element.Length - hashIndex - 2);
237                         return "T:" + node.Parent.Caption + '.' + typeName.Replace ('.', '+');
238                 }
239
240                 string MakeMemberNodeUrl (char typeChar, Node node)
241                 {
242                         // We clean inner type ctor name which may contain the outer type name
243                         var caption = node.Caption;
244
245                         // Sanitize constructor caption of inner types
246                         if (typeChar == 'C') {
247                                 int lastDot = -1;
248                                 for (int i = 0; i < caption.Length && caption[i] != '('; i++)
249                                         lastDot = caption[i] == '.' ? i : lastDot;
250                                 return lastDot == -1 ? '.' + caption : caption.Substring (lastDot);
251                         }
252
253                         /* We handle type conversion operator by checking if the name contains " to "
254                          * (as in 'foo to bar') and we generate a corresponding conversion signature
255                          */
256                         if (typeChar == 'O' && caption.IndexOf (" to ") != -1) {
257                                 var parts = caption.Split (' ');
258                                 return "." + node.Parent.Caption + "(" + parts[0] + ", " + parts[2] + ")";
259                         }
260
261                         /* The goal here is to treat method which are explicit interface definition
262                          * such as 'void IDisposable.Dispose ()' for which the caption is a dot
263                          * expression thus colliding with the ecma parser.
264                          * If the first non-alpha character in the caption is a dot then we have an
265                          * explicit member implementation (we assume the interface has namespace)
266                          */
267                         var firstNonAlpha = caption.FirstOrDefault (c => !char.IsLetterOrDigit (c));
268                         if (firstNonAlpha == '.')
269                                 return "$" + caption;
270
271                         return "." + caption;
272                 }
273
274                 Node GetNodeTypeParent (Node node)
275                 {
276                         // Type nodes are always at level 2 so we just need to get there
277                         while (node != null && node.Parent != null && !node.Parent.Parent.Element.StartsWith ("root:/", StringComparison.OrdinalIgnoreCase))
278                                 node = node.Parent;
279                         return node;
280                 }
281
282                 string GenerateMetaSuffix (Node node)
283                 {
284                         string suffix = string.Empty;
285                         // A meta node has always a type element to begin with
286                         while (EcmaDoc.GetNodeType (node) != EcmaNodeType.Type) {
287                                 suffix = '/' + node.Element + suffix;
288                                 node = node.Parent;
289                         }
290                         return suffix;
291                 }
292
293                 public override string GetInternalIdForUrl (string url, out Node node, out Dictionary<string, string> context)
294                 {
295                         var id = string.Empty;
296                         node = null;
297                         context = null;
298
299                         if (!url.StartsWith (UriPrefix, StringComparison.OrdinalIgnoreCase)) {
300                                 node = MatchNode (url);
301                                 if (node == null)
302                                         return null;
303                                 id = node.GetInternalUrl ();
304                         }
305
306                         string hash;
307                         id = GetInternalIdForInternalUrl (id, out hash);
308                         context = EcmaDoc.GetContextForEcmaNode (hash, SourceID.ToString (), node);
309
310                         return id;
311                 }
312
313                 public string GetInternalIdForInternalUrl (string internalUrl, out string hash)
314                 {
315                         var id = internalUrl;
316                         if (id.StartsWith (UriPrefix, StringComparison.OrdinalIgnoreCase))
317                                 id = id.Substring (UriPrefix.Length);
318                         else if (id.StartsWith ("N:", StringComparison.OrdinalIgnoreCase))
319                                 id = "xml.summary." + id.Substring ("N:".Length);
320
321                         var hashIndex = id.IndexOf ('#');
322                         hash = string.Empty;
323                         if (hashIndex != -1) {
324                                 hash = id.Substring (hashIndex + 1);
325                                 id = id.Substring (0, hashIndex);
326                         }
327
328                         return id;
329                 }
330
331                 public override Node MatchNode (string url)
332                 {
333                         Node node = null;
334                         if ((node = cache.Get (url)) == null) {
335                                 node = EcmaDoc.MatchNodeWithEcmaUrl (url, Tree);
336                                 if (node != null)
337                                         cache.Put (url, node);
338                         }
339                         return node;
340                 }
341
342                 public override void PopulateIndex (IndexMaker index_maker)
343                 {
344                         foreach (Node ns_node in Tree.RootNode.ChildNodes){
345                                 foreach (Node type_node in ns_node.ChildNodes){
346                                         string typename = type_node.Caption.Substring (0, type_node.Caption.IndexOf (' '));
347                                         string full = ns_node.Caption + "." + typename;
348
349                                         string doc_tag = GetKindFromCaption (type_node.Caption);
350                                         string url = type_node.PublicUrl;
351
352                                         //
353                                         // Add MonoMac/MonoTouch [Export] attributes, those live only in classes
354                                         //
355                                         XDocument type_doc = null;
356                                         ILookup<string, XElement> prematchedMembers = null;
357                                         bool hasExports = doc_tag == "Class" && (ns_node.Caption.StartsWith ("MonoTouch") || ns_node.Caption.StartsWith ("MonoMac"));
358                                         if (hasExports) {
359                                                 try {
360                                                         string rest, hash;
361                                                         var id = GetInternalIdForInternalUrl (type_node.GetInternalUrl (), out hash);
362                                                         type_doc = XDocument.Load (GetHelpStream (id));
363                                                         prematchedMembers = type_doc.Root.Element ("Members").Elements ("Member").ToLookup (n => (string)n.Attribute ("MemberName"), n => n);
364                                                 } catch (Exception e) {
365                                                         Console.WriteLine ("Problem processing {0} for MonoTouch/MonoMac exports\n\n{0}", e);
366                                                         hasExports = false;
367                                                 }
368                                         }
369
370                                         if (doc_tag == "Class" || doc_tag == "Structure" || doc_tag == "Interface"){
371                                                 index_maker.Add (type_node.Caption, typename, url);
372                                                 index_maker.Add (full + " " + doc_tag, full, url);
373
374                                                 foreach (Node c in type_node.ChildNodes){
375                                                         switch (c.Caption){
376                                                         case "Constructors":
377                                                                 index_maker.Add ("  constructors", typename+"0", url + "/C");
378                                                                 break;
379                                                         case "Fields":
380                                                                 index_maker.Add ("  fields", typename+"1", url + "/F");
381                                                                 break;
382                                                         case "Events":
383                                                                 index_maker.Add ("  events", typename+"2", url + "/E");
384                                                                 break;
385                                                         case "Properties":
386                                                                 index_maker.Add ("  properties", typename+"3", url + "/P");
387                                                                 break;
388                                                         case "Methods":
389                                                                 index_maker.Add ("  methods", typename+"4", url + "/M");
390                                                                 break;
391                                                         case "Operators":
392                                                                 index_maker.Add ("  operators", typename+"5", url + "/O");
393                                                                 break;
394                                                         }
395                                                 }
396
397                                                 //
398                                                 // Now repeat, but use a different sort key, to make sure we come after
399                                                 // the summary data above, start the counter at 6
400                                                 //
401                                                 string keybase = typename + "6.";
402
403                                                 foreach (Node c in type_node.ChildNodes){
404                                                         var type = c.Caption[0];
405
406                                                         foreach (Node nc in c.ChildNodes) {
407                                                                 string res = nc.Caption;
408                                                                 string nurl = nc.PublicUrl;
409
410                                                                 // Process exports
411                                                                 if (hasExports && (type == 'C' || type == 'M' || type == 'P')) {
412                                                                         try {
413                                                                                 var member = GetMemberFromCaption (type_doc, type == 'C' ? ".ctor" : res, false, prematchedMembers);
414                                                                                 var exports = member.Descendants ("AttributeName").Where (a => a.Value.Contains ("Foundation.Export"));
415                                                                                 foreach (var exportNode in exports) {
416                                                                                         var parts = exportNode.Value.Split ('"');
417                                                                                         if (parts.Length != 3) {
418                                                                                                 Console.WriteLine ("Export attribute not found or not usable in {0}", exportNode);
419                                                                                         } else {
420                                                                                                 var export = parts[1];
421                                                                                                 index_maker.Add (export + " selector", export, nurl);
422                                                                                         }
423                                                                                 }
424                                                                         } catch (Exception e) {
425                                                                                 Console.WriteLine ("Problem processing {0}/{1} for MonoTouch/MonoMac exports\n\n{2}", nurl, res, e);
426                                                                         }
427                                                                 }
428
429                                                                 switch (type){
430                                                                 case 'C':
431                                                                         break;
432                                                                 case 'F':
433                                                                         index_maker.Add (String.Format ("{0}.{1} field", typename, res),
434                                                                                          keybase + res, nurl);
435                                                                         index_maker.Add (String.Format ("{0} field", res), res, nurl);
436                                                                         break;
437                                                                 case 'E':
438                                                                         index_maker.Add (String.Format ("{0}.{1} event", typename, res),
439                                                                                          keybase + res, nurl);
440                                                                         index_maker.Add (String.Format ("{0} event", res), res, nurl);
441                                                                         break;
442                                                                 case 'P':
443                                                                         index_maker.Add (String.Format ("{0}.{1} property", typename, res),
444                                                                                          keybase + res, nurl);
445                                                                         index_maker.Add (String.Format ("{0} property", res), res, nurl);
446                                                                         break;
447                                                                 case 'M':
448                                                                         index_maker.Add (String.Format ("{0}.{1} method", typename, res),
449                                                                                          keybase + res, nurl);
450                                                                         index_maker.Add (String.Format ("{0} method", res), res, nurl);
451                                                                         break;
452                                                                 case 'O':
453                                                                         index_maker.Add (String.Format ("{0}.{1} operator", typename, res),
454                                                                                          keybase + res, nurl);
455                                                                         break;
456                                                                 }
457                                                         }
458                                                 }
459                                         } else if (doc_tag == "Enumeration"){
460                                                 //
461                                                 // Enumerations: add the enumeration values
462                                                 //
463                                                 index_maker.Add (type_node.Caption, typename, url);
464                                                 index_maker.Add (full + " " + doc_tag, full, url);
465
466                                                 // Now, pull the values.
467                                                 string rest, hash;
468                                                 var id = GetInternalIdForInternalUrl (type_node.GetInternalUrl (), out hash);
469                                                 var xdoc = XDocument.Load (GetHelpStream (id));
470                                                 if (xdoc == null)
471                                                         continue;
472
473                                                 var members = xdoc.Root.Element ("Members").Elements ("Members");
474                                                 if (members == null)
475                                                         continue;
476
477                                                 foreach (var member_node in members){
478                                                         string enum_value = member_node.Attribute ("MemberName").Value;
479                                                         string caption = enum_value + " value";
480                                                         index_maker.Add (caption, caption, url);
481                                                 }
482                                         } else if (doc_tag == "Delegate"){
483                                                 index_maker.Add (type_node.Caption, typename, url);
484                                                 index_maker.Add (full + " " + doc_tag, full, url);
485                                         }
486                                 }
487                         }
488                 }
489
490
491                 public override void PopulateSearchableIndex (IndexWriter writer)
492                 {
493                         StringBuilder text = new StringBuilder ();
494                         SearchableDocument searchDoc = new SearchableDocument ();
495
496                         foreach (Node ns_node in Tree.RootNode.ChildNodes) {
497                                 foreach (Node type_node in ns_node.ChildNodes) {
498                                         string typename = type_node.Caption.Substring (0, type_node.Caption.IndexOf (' '));
499                                         string full = ns_node.Caption + "." + typename;
500                                         string url = type_node.PublicUrl;
501                                         string doc_tag = GetKindFromCaption (type_node.Caption);
502                                         string rest, hash;
503                                         var id = GetInternalIdForInternalUrl (type_node.GetInternalUrl (), out hash);
504                                         var xdoc = XDocument.Load (GetHelpStream (id));
505                                         if (xdoc == null)
506                                                 continue;
507                                         if (string.IsNullOrEmpty (doc_tag))
508                                                 continue;
509
510                                         // For classes, structures or interfaces add a doc for the overview and
511                                         // add a doc for every constructor, method, event, ...
512                                         // doc_tag == "Class" || doc_tag == "Structure" || doc_tag == "Interface"
513                                         if (doc_tag[0] == 'C' || doc_tag[0] == 'S' || doc_tag[0] == 'I') {
514                                                 // Adds a doc for every overview of every type
515                                                 SearchableDocument doc = searchDoc.Reset ();
516                                                 doc.Title = type_node.Caption;
517                                                 doc.HotText = typename;
518                                                 doc.Url = url;
519                                                 doc.FullTitle = full;
520
521                                                 var node_sel = xdoc.Root.Element ("Docs");
522                                                 text.Clear ();
523                                                 GetTextFromNode (node_sel, text);
524                                                 doc.Text = text.ToString ();
525
526                                                 text.Clear ();
527                                                 GetExamples (node_sel, text);
528                                                 doc.Examples = text.ToString ();
529
530                                                 writer.AddDocument (doc.LuceneDoc);
531                                                 var exportParsable = doc_tag[0] == 'C' && (ns_node.Caption.StartsWith ("MonoTouch") || ns_node.Caption.StartsWith ("MonoMac"));
532
533                                                 //Add docs for contructors, methods, etc.
534                                                 foreach (Node c in type_node.ChildNodes) { // c = Constructors || Fields || Events || Properties || Methods || Operators
535                                                         if (c.Element == "*")
536                                                                 continue;
537                                                         const float innerTypeBoost = 0.2f;
538
539                                                         IEnumerable<Node> ncnodes = c.ChildNodes;
540                                                         // The rationale is that we need to properly handle method overloads
541                                                         // so for those method node which have children, flatten them
542                                                         if (c.Caption == "Methods") {
543                                                                 ncnodes = ncnodes
544                                                                         .Where (n => n.ChildNodes == null || n.ChildNodes.Count == 0)
545                                                                         .Concat (ncnodes.Where (n => n.ChildNodes.Count > 0).SelectMany (n => n.ChildNodes));
546                                                         } else if (c.Caption == "Operators") {
547                                                                 ncnodes = ncnodes
548                                                                         .Where (n => !n.Caption.EndsWith ("Conversion"))
549                                                                         .Concat (ncnodes.Where (n => n.Caption.EndsWith ("Conversion")).SelectMany (n => n.ChildNodes));
550                                                         }
551
552                                                         var prematchedMembers = xdoc.Root.Element ("Members").Elements ("Member").ToLookup (n => (string)n.Attribute ("MemberName"), n => n);
553
554                                                         foreach (Node nc in ncnodes) {
555                                                                 XElement docsNode = null;
556                                                                 try {
557                                                                         docsNode = GetDocsFromCaption (xdoc, c.Caption[0] == 'C' ? ".ctor" : nc.Caption, c.Caption[0] == 'O', prematchedMembers);
558                                                                 } catch {}
559                                                                 if (docsNode == null) {
560                                                                         Console.Error.WriteLine ("Problem: {0}", nc.PublicUrl);
561                                                                         continue;
562                                                                 }
563
564                                                                 SearchableDocument doc_nod = searchDoc.Reset ();
565                                                                 doc_nod.Title = LargeName (nc) + " " + EcmaDoc.EtcKindToCaption (c.Caption[0]);
566                                                                 doc_nod.FullTitle = ns_node.Caption + '.' + typename + "::" + nc.Caption;
567                                                                 doc_nod.HotText = string.Empty;
568
569                                                                 /* Disable constructors hottext indexing as it's often "polluting" search queries
570                                                                    because it has the same hottext than standard types */
571                                                                 if (c.Caption != "Constructors") {
572                                                                         //dont add the parameters to the hottext
573                                                                         int ppos = nc.Caption.IndexOf ('(');
574                                                                         doc_nod.HotText = ppos != -1 ? nc.Caption.Substring (0, ppos) : nc.Caption;
575                                                                 }
576
577                                                                 var urlnc = nc.PublicUrl;
578                                                                 doc_nod.Url = urlnc;
579
580                                                                 text.Clear ();
581                                                                 GetTextFromNode (docsNode, text);
582                                                                 doc_nod.Text = text.ToString ();
583
584                                                                 text.Clear ();
585                                                                 GetExamples (docsNode, text);
586                                                                 doc_nod.Examples = text.ToString ();
587
588                                                                 Document lucene_doc = doc_nod.LuceneDoc;
589                                                                 lucene_doc.Boost = innerTypeBoost;
590                                                                 writer.AddDocument (lucene_doc);
591
592                                                                 // Objective-C binding specific parsing of [Export] attributes
593                                                                 if (exportParsable) {
594                                                                         try {
595                                                                                 var exports = docsNode.Parent.Elements ("Attributes").Elements ("Attribute").Elements ("AttributeName")
596                                                                                         .Select (a => (string)a).Where (txt => txt.Contains ("Foundation.Export"));
597
598                                                                                 foreach (var exportNode in exports) {
599                                                                                         var parts = exportNode.Split ('"');
600                                                                                         if (parts.Length != 3) {
601                                                                                                 Console.WriteLine ("Export attribute not found or not usable in {0}", exportNode);
602                                                                                                 continue;
603                                                                                         }
604
605                                                                                         var export = parts[1];
606                                                                                         var export_node = searchDoc.Reset ();
607                                                                                         export_node.Title = export + " Export";
608                                                                                         export_node.FullTitle = ns_node.Caption + '.' + typename + "::" + export;
609                                                                                         export_node.Url = urlnc;
610                                                                                         export_node.HotText = export;
611                                                                                         export_node.Text = string.Empty;
612                                                                                         export_node.Examples = string.Empty;
613                                                                                         lucene_doc = export_node.LuceneDoc;
614                                                                                         lucene_doc.Boost = innerTypeBoost;
615                                                                                         writer.AddDocument (lucene_doc);
616                                                                                 }
617                                                                         } catch (Exception e){
618                                                                                 Console.WriteLine ("Problem processing {0} for MonoTouch/MonoMac exports\n\n{0}", e);
619                                                                         }
620                                                                 }
621                                                         }
622                                                 }
623                                         // doc_tag == "Enumeration"
624                                         } else if (doc_tag[0] == 'E'){
625                                                 var members = xdoc.Root.Element ("Members").Elements ("Member");
626                                                 if (members == null)
627                                                         continue;
628
629                                                 text.Clear ();
630                                                 foreach (var member_node in members) {
631                                                         string enum_value = (string)member_node.Attribute ("MemberName");
632                                                         text.Append (enum_value);
633                                                         text.Append (" ");
634                                                         GetTextFromNode (member_node.Element ("Docs"), text);
635                                                         text.AppendLine ();
636                                                 }
637
638                                                 SearchableDocument doc = searchDoc.Reset ();
639
640                                                 text.Clear ();
641                                                 GetExamples (xdoc.Root.Element ("Docs"), text);
642                                                 doc.Examples = text.ToString ();
643
644                                                 doc.Title = type_node.Caption;
645                                                 doc.HotText = (string)xdoc.Root.Attribute ("Name");
646                                                 doc.FullTitle = full;
647                                                 doc.Url = url;
648                                                 doc.Text = text.ToString();
649                                                 writer.AddDocument (doc.LuceneDoc);
650                                         // doc_tag == "Delegate"
651                                         } else if (doc_tag[0] == 'D'){
652                                                 SearchableDocument doc = searchDoc.Reset ();
653                                                 doc.Title = type_node.Caption;
654                                                 doc.HotText = (string)xdoc.Root.Attribute ("Name");
655                                                 doc.FullTitle = full;
656                                                 doc.Url = url;
657
658                                                 var node_sel = xdoc.Root.Element ("Docs");
659
660                                                 text.Clear ();
661                                                 GetTextFromNode (node_sel, text);
662                                                 doc.Text = text.ToString();
663
664                                                 text.Clear ();
665                                                 GetExamples (node_sel, text);
666                                                 doc.Examples = text.ToString();
667
668                                                 writer.AddDocument (doc.LuceneDoc);
669                                         }
670                                 }
671                         }
672                 }
673
674                 string GetKindFromCaption (string s)
675                 {
676                         int p = s.LastIndexOf (' ');
677                         if (p > 0)
678                                 return s.Substring (p + 1);
679                         return null;
680                 }
681
682                 // Extract the interesting text from the docs node
683                 void GetTextFromNode (XElement n, StringBuilder sb)
684                 {
685                         // Include the text content of the docs
686                         sb.AppendLine (n.Value);
687                         foreach (var tag in n.Descendants ())
688                                 //include the url to which points the see tag and the name of the parameter
689                                 if ((tag.Name.LocalName.Equals ("see", StringComparison.Ordinal) || tag.Name.LocalName.Equals ("paramref", StringComparison.Ordinal))
690                                     && tag.HasAttributes)
691                                         sb.AppendLine ((string)tag.Attributes ().First ());
692                 }
693
694                 // Extract the code nodes from the docs
695                 void GetExamples (XElement n, StringBuilder sb)
696                 {
697                         foreach (var code in n.Descendants ("code"))
698                                 sb.Append ((string)code);
699                 }
700
701                 // Extract a large name for the Node
702                 static string LargeName (Node matched_node)
703                 {
704                         string[] parts = matched_node.GetInternalUrl ().Split('/', '#');
705                         if (parts.Length == 3 && parts[2] != String.Empty) //List of Members, properties, events, ...
706                                 return parts[1] + ": " + matched_node.Caption;
707                         else if(parts.Length >= 4) //Showing a concrete Member, property, ...
708                                 return parts[1] + "." + matched_node.Caption;
709                         else
710                                 return matched_node.Caption;
711                 }
712
713                 XElement GetMemberFromCaption (XDocument xdoc, string caption, bool isOperator, ILookup<string, XElement> prematchedMembers)
714                 {
715                         string name;
716                         IList<string> args;
717                         var doc = xdoc.Root.Element ("Members").Elements ("Member");
718
719                         if (isOperator) {
720                                 // The first case are explicit and implicit conversion operators which are grouped specifically
721                                 if (caption.IndexOf (" to ") != -1) {
722                                         var convArgs = caption.Split (new[] { " to " }, StringSplitOptions.None);
723                                         return doc
724                                                 .First (n => (AttrEq (n, "MemberName", "op_Explicit") || AttrEq (n, "MemberName", "op_Implicit"))
725                                                         && ((string)n.Element ("ReturnValue").Element ("ReturnType")).Equals (convArgs[1], StringComparison.Ordinal)
726                                                         && AttrEq (n.Element ("Parameters").Element ("Parameter"), "Type", convArgs[0]));
727                                 } else {
728                                         return doc.First (m => AttrEq (m, "MemberName", "op_" + caption));
729                                 }
730                         }
731
732                         TryParseCaption (caption, out name, out args);
733
734                         if (!string.IsNullOrEmpty (name)) { // Filter member by name
735                                 var prematched = prematchedMembers[name];
736                                 doc = prematched.Any () ? prematched : doc.Where (m => AttrEq (m, "MemberName", name));
737                         }
738                         if (args != null && args.Count > 0) // Filter member by its argument list
739                                 doc = doc.Where (m => m.Element ("Parameters").Elements ("Parameter").Attributes ("Type").Select (a => (string)a).SequenceEqual (args));
740
741                         return doc.First ();
742                 }
743
744                 XElement GetDocsFromCaption (XDocument xdoc, string caption, bool isOperator, ILookup<string, XElement> prematchedMembers)
745                 {
746                         return GetMemberFromCaption (xdoc, caption, isOperator, prematchedMembers).Element ("Docs");
747                 }
748
749                 // A simple stack-based parser to detect single type definition separated by commas
750                 IEnumerable<string> ExtractArguments (string rawArgList)
751                 {
752                         var sb = new System.Text.StringBuilder ();
753                         int genericDepth = 0;
754                         int arrayDepth = 0;
755
756                         for (int i = 0; i < rawArgList.Length; i++) {
757                                 char c = rawArgList[i];
758                                 switch (c) {
759                                 case ',':
760                                         if (genericDepth == 0 && arrayDepth == 0) {
761                                                 yield return sb.ToString ();
762                                                 sb.Clear ();
763                                                 continue;
764                                         }
765                                         break;
766                                 case '<':
767                                         genericDepth++;
768                                         break;
769                                 case '>':
770                                         genericDepth--;
771                                         break;
772                                 case '[':
773                                         arrayDepth++;
774                                         break;
775                                 case ']':
776                                         arrayDepth--;
777                                         break;
778                                 }
779                                 sb.Append (c);
780                         }
781                         if (sb.Length > 0)
782                                 yield return sb.ToString ();
783                 }
784
785                 void TryParseCaption (string caption, out string name, out IList<string> argList)
786                 {
787                         name = null;
788                         argList = null;
789                         int parenIdx = caption.IndexOf ('(');
790                         // In case of simple name, there is no need for processing
791                         if (parenIdx == -1) {
792                                 name = caption;
793                                 return;
794                         }
795                         name = caption.Substring (0, parenIdx);
796                         // Now we gather the argument list if there is any
797                         var rawArgList = caption.Substring (parenIdx + 1, caption.Length - parenIdx - 2); // Only take what's inside the parens
798                         if (string.IsNullOrEmpty (rawArgList))
799                                 return;
800
801                         argList = ExtractArguments (rawArgList).Select (arg => arg.Trim ()).ToList ();
802                 }
803
804                 bool AttrEq (XElement element, string attributeName, string expectedValue)
805                 {
806                         return ((string)element.Attribute (attributeName)).Equals (expectedValue, StringComparison.Ordinal);
807                 }
808         }
809 }