Compute lucene-style scores for our hits.
[beagle.git] / Filters / FilterMonodoc.cs
blob39a7345812905338ea922a8afefcf98521f9ffe3
1 //
2 // FilterMonodoc.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.IO;
29 using System.Xml;
30 using System.Text;
31 using System.Threading;
33 using Beagle.Daemon;
34 using Beagle.Util;
36 using ICSharpCode.SharpZipLib.Zip;
38 namespace Beagle.Filters {
40 public class FilterMonodoc : Filter {
42 ZipFile archive = null;
44 public FilterMonodoc ()
46 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("application/monodoc"));
47 // FIXME: Autoconf to find the monodoc prefix
48 AddSupportedFlavor (new FilterFlavor ("file:///usr/lib/monodoc/sources/*", ".zip", null, 0));
51 override protected void DoOpen (FileInfo file)
53 try {
54 archive = new ZipFile (file.FullName);
55 } catch (Exception e) {
56 Logger.Log.Error ("Error while filtering Monodoc archive: {0}", e);
57 Error ();
61 override protected void DoPullProperties ()
63 if (archive == null) {
64 Error ();
65 return;
68 foreach (ZipEntry entry in archive) {
69 if (entry.Name.IndexOf (".") != -1)
70 continue;
72 XmlDocument document = new XmlDocument ();
73 document.Load (archive.GetInputStream (entry));
75 XmlNode type = document.SelectSingleNode ("/Type");
77 if (type == null)
78 continue;
80 Indexable type_indexable = TypeNodeToIndexable (type, FileInfo);
81 AddChildIndexable (type_indexable);
83 foreach(XmlNode member in type.SelectNodes ("Members/Member")) {
84 Indexable member_indexable = MemberNodeToIndexable (member,
85 FileInfo,
86 type.Attributes ["FullName"].Value);
87 AddChildIndexable (member_indexable);
91 // If we've successfully crawled the file but haven't
92 // found any indexables, we shouldn't consider it
93 // successfull at all.
94 if (ChildIndexables.Count == 0) {
95 Error ();
96 return;
99 Finished ();
102 static private Indexable TypeNodeToIndexable (XmlNode node, FileInfo file)
104 Indexable indexable = new Indexable (UriFu.PathToFileUri (file + "#T:" + node.Attributes ["FullName"].Value));
106 indexable.MimeType = "text/html";
107 indexable.Type = "MonodocEntry";
109 indexable.AddProperty (Property.NewKeyword ("fixme:type", "type"));
110 indexable.AddProperty (Property.NewKeyword ("fixme:name", "T:" + node.Attributes["FullName"].Value));
112 StringReader reader = new StringReader (node.SelectSingleNode ("Docs").InnerXml);
113 indexable.SetTextReader (reader);
115 return indexable;
118 static private Indexable MemberNodeToIndexable(XmlNode node, FileInfo file, string parentName)
120 char memberType = MemberTypeToChar (node.SelectSingleNode ("MemberType").InnerText);
121 StringBuilder memberFullName = new StringBuilder ();
123 memberFullName.Append (memberType + ":"+ parentName);
125 if (memberType != 'C')
126 memberFullName.Append ("." + node.Attributes["MemberName"].Value);
128 if (memberType == 'C' || memberType == 'M' || memberType == 'E') {
129 memberFullName.Append ("(");
130 bool inside = false;
132 foreach (XmlNode parameter in node.SelectNodes ("Parameters/Parameter")) {
133 if (!inside) inside = true; else memberFullName.Append(",");
134 memberFullName.Append (parameter.Attributes["Type"].Value);
137 memberFullName.Append (")");
140 Indexable indexable = new Indexable (UriFu.PathToFileUri (file + "#" + memberFullName));
142 indexable.MimeType = "text/html";
143 indexable.Type = "MonodocEntry";
145 indexable.AddProperty (Property.NewKeyword ("fixme:type", node.SelectSingleNode ("MemberType").InnerText.ToLower ()));
146 indexable.AddProperty (Property.New ("fixme:name", memberFullName.ToString ()));
148 StringReader reader = new StringReader (node.SelectSingleNode ("Docs").InnerXml);
149 indexable.SetTextReader (reader);
151 return indexable;
154 static private char MemberTypeToChar (string memberType)
156 switch (memberType) {
157 case "Constructor":
158 return 'C';
159 case "Event":
160 return 'E';
161 case "Property":
162 return 'P';
163 case "Field":
164 return 'F';
165 case "Method":
166 return 'M';
167 default:
168 return 'U';