Some more fixes wrt child-indexables. Namely, fix proper handling of child indexables...
[beagle.git] / beagled / FilterFactory.cs
blob1d9c4c022420b187614465b228b09fcbb8d204cb
1 //
2 // FilterFactory.cs
3 //
4 // Copyright (C) 2004 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.Collections;
29 using System.IO;
30 using System.Reflection;
32 using System.Xml;
33 using System.Xml.Serialization;
35 using Beagle.Util;
37 namespace Beagle.Daemon {
39 public class FilterFactory {
41 static private bool Debug = false;
43 static FilterFactory ()
45 ReflectionFu.ScanEnvironmentForAssemblies ("BEAGLE_FILTER_PATH", PathFinder.FilterDir,
46 delegate (Assembly a) {
47 int n = ScanAssemblyForFilters (a);
48 Logger.Log.Debug ("Loaded {0} filter{1} from {2}",
49 n, n == 1 ? "" : "s", a.Location);
50 });
53 /////////////////////////////////////////////////////////////////////////
56 static private ICollection CreateFilters (Uri uri, string extension, string mime_type)
58 Hashtable matched_filters_by_flavor = FilterFlavor.NewHashtable ();
60 foreach (FilterFlavor flavor in filter_types_by_flavor.Keys) {
61 if (flavor.IsMatch (uri, extension, mime_type)) {
62 Filter matched_filter = null;
64 try {
65 matched_filter = (Filter) Activator.CreateInstance ((Type) filter_types_by_flavor [flavor]);
67 if (flavor.MimeType != null)
68 matched_filter.MimeType = flavor.MimeType;
69 if (flavor.Extension != null)
70 matched_filter.Extension = flavor.Extension;
72 } catch (Exception e) {
73 continue;
75 matched_filters_by_flavor [flavor] = matched_filter;
79 if (Debug) {
80 foreach (DictionaryEntry entry in matched_filters_by_flavor) {
81 FilterFlavor flav = (FilterFlavor) entry.Key;
82 Filter filter = (Filter) entry.Value;
84 Logger.Log.Debug ("Found matching filter: {0}, Weight: {1}", filter, flav.Weight);
88 return matched_filters_by_flavor.Values;
91 static public int GetFilterVersion (string filter_name)
93 if (filter_versions_by_name.Contains (filter_name)) {
94 return (int) filter_versions_by_name [filter_name];
95 } else {
96 return -1;
100 /////////////////////////////////////////////////////////////////////////
102 static public ICollection CreateFiltersFromMimeType (string mime_type)
104 return CreateFilters (null, null, mime_type);
107 static public ICollection CreateFilterFromExtension (string extension)
109 return CreateFilters (null, extension, null);
112 static public ICollection CreateFiltersFromPath (string path)
114 string guessed_mime_type = XdgMime.GetMimeType (path);
115 string extension = Path.GetExtension (path);
116 return CreateFilters (UriFu.PathToFileUri (path), extension, guessed_mime_type);
119 static public ICollection CreateFiltersFromUri (Uri uri)
121 if (uri.IsFile)
122 return CreateFiltersFromPath (uri.LocalPath);
123 else
124 return CreateFilters (uri, null, null);
127 static public ICollection CreateFiltersFromIndexable (Indexable indexable)
129 string path = indexable.ContentUri.LocalPath;
130 string extension = Path.GetExtension (path);
131 string mime_type = indexable.MimeType;
132 return CreateFilters (UriFu.PathToFileUri (path), extension, mime_type);
135 /////////////////////////////////////////////////////////////////////////
137 static private bool ShouldWeFilterThis (Indexable indexable)
139 if (indexable.Filtering == IndexableFiltering.Never
140 || indexable.NoContent)
141 return false;
143 if (indexable.Filtering == IndexableFiltering.Always)
144 return true;
146 // Our default behavior is to try to filter non-transient file
147 // indexable and indexables with a specific mime type attached.
148 if (indexable.IsNonTransient || indexable.MimeType != null)
149 return true;
151 return false;
154 static public bool FilterIndexable (Indexable indexable, TextCache text_cache, out Filter filter)
156 filter = null;
157 ICollection filters = null;
159 if (indexable.Filtering == IndexableFiltering.AlreadyFiltered)
160 return false;
162 if (! ShouldWeFilterThis (indexable)) {
163 indexable.NoContent = true;
164 return false;
167 string path = null;
169 // First, figure out which filter we should use to deal with
170 // the indexable.
172 // If a specific mime type is specified, try to index as that type.
173 if (indexable.MimeType != null)
174 filters = CreateFiltersFromMimeType (indexable.MimeType);
176 if (indexable.ContentUri.IsFile) {
177 path = indexable.ContentUri.LocalPath;
179 // Otherwise sniff the mime-type from the file
180 if (indexable.MimeType == null)
181 indexable.MimeType = XdgMime.GetMimeType (path);
183 if (filters == null || filters.Count == 0) {
184 filters = CreateFiltersFromIndexable (indexable);
187 if (Directory.Exists (path)) {
188 indexable.MimeType = "inode/directory";
189 indexable.NoContent = true;
190 if (! indexable.ValidTimestamp && indexable.IsNonTransient)
191 indexable.Timestamp = Directory.GetLastWriteTimeUtc (path);
192 } else if (File.Exists (path)) {
193 // Set the timestamp to the best possible estimate (if no timestamp was set by the backend)
194 if (! indexable.ValidTimestamp && indexable.IsNonTransient)
195 indexable.Timestamp = File.GetLastWriteTimeUtc (path);
196 } else {
197 Logger.Log.Warn ("No such file: {0}", path);
198 return false;
202 // We don't know how to filter this, so there is nothing else to do.
203 if (filters.Count == 0) {
204 if (! indexable.NoContent) {
205 indexable.NoContent = true;
207 Logger.Log.Debug ("No filter for {0} ({1}) [{2}]", indexable.DisplayUri, path, indexable.MimeType);
208 return false;
211 return true;
214 foreach (Filter candidate_filter in filters) {
215 if (Debug)
216 Logger.Log.Debug ("Testing filter: {0}", candidate_filter);
218 // Hook up the snippet writer.
219 if (candidate_filter.SnippetMode && text_cache != null) {
220 if (candidate_filter.OriginalIsText && indexable.IsNonTransient) {
221 text_cache.MarkAsSelfCached (indexable.Uri);
222 } else if (indexable.CacheContent) {
223 TextWriter writer = text_cache.GetWriter (indexable.Uri);
224 candidate_filter.AttachSnippetWriter (writer);
228 if (indexable.Crawled)
229 candidate_filter.EnableCrawlMode ();
231 // Set the filter's URIs
232 candidate_filter.Uri = indexable.Uri;
233 candidate_filter.DisplayUri = indexable.DisplayUri;
235 // allow the filter access to the indexable's properties
236 candidate_filter.IndexableProperties = indexable.Properties;
238 // Open the filter, copy the file's properties to the indexable,
239 // and hook up the TextReaders.
241 bool succesful_open = false;
242 TextReader text_reader;
243 Stream binary_stream;
245 if (path != null)
246 succesful_open = candidate_filter.Open (path);
247 else if ((text_reader = indexable.GetTextReader ()) != null)
248 succesful_open = candidate_filter.Open (text_reader);
249 else if ((binary_stream = indexable.GetBinaryStream ()) != null)
250 succesful_open = candidate_filter.Open (binary_stream);
252 if (succesful_open) {
253 if (candidate_filter.Timestamp != DateTime.MinValue)
254 indexable.Timestamp = candidate_filter.Timestamp;
255 foreach (Property prop in candidate_filter.Properties)
256 indexable.AddProperty (prop);
257 indexable.SetTextReader (candidate_filter.GetTextReader ());
258 indexable.SetHotTextReader (candidate_filter.GetHotTextReader ());
260 if (Debug)
261 Logger.Log.Debug ("Successfully filtered {0} with {1}", path, candidate_filter);
263 filter = candidate_filter;
264 return true;
265 } else if (Debug) {
266 Logger.Log.Debug ("Unsuccessfully filtered {0} with {1}, falling back", path, candidate_filter);
267 candidate_filter.Cleanup ();
271 if (Debug)
272 Logger.Log.Debug ("None of the matching filters could process the file: {0}", path);
274 return false;
277 static public bool FilterIndexable (Indexable indexable, out Filter filter)
279 return FilterIndexable (indexable, null, out filter);
282 static public bool FilterIndexable (Indexable indexable)
284 Filter filter = null;
286 return FilterIndexable (indexable, null, out filter);
289 /////////////////////////////////////////////////////////////////////////
291 private static Hashtable filter_types_by_flavor = new Hashtable ();
292 private static Hashtable filter_versions_by_name = new Hashtable ();
294 static private int ScanAssemblyForFilters (Assembly assembly)
296 int count = 0;
298 foreach (Type t in ReflectionFu.GetTypesFromAssemblyAttribute (assembly, typeof (FilterTypesAttribute))) {
299 Filter filter = null;
301 try {
302 filter = (Filter) Activator.CreateInstance (t);
303 } catch (Exception ex) {
304 Logger.Log.Error (ex, "Caught exception while instantiating {0}", t);
307 if (filter == null)
308 continue;
310 filter_versions_by_name [t.ToString ()] = filter.Version;
312 foreach (FilterFlavor flavor in filter.SupportedFlavors) {
313 filter_types_by_flavor [flavor] = t;
314 FilterFlavor.Flavors.Add (flavor);
317 ++count;
320 return count;
325 /////////////////////////////////////////////////////////////////////////
327 public class FilteredStatus
329 private Uri uri;
330 private string filter_name;
331 private int filter_version;
333 [XmlIgnore]
334 public Uri Uri {
335 get { return uri; }
336 set { uri = value; }
339 [XmlAttribute ("Uri")]
340 public string UriAsString {
341 get {
342 return UriFu.UriToEscapedString (uri);
345 set {
346 uri = UriFu.EscapedStringToUri (value);
350 public string FilterName {
351 get { return filter_name; }
352 set { filter_name = value; }
355 public int FilterVersion {
356 get { return filter_version; }
357 set { filter_version = value; }
360 public static FilteredStatus New (Indexable indexable, Filter filter)
362 FilteredStatus status = new FilteredStatus ();
364 status.Uri = indexable.Uri;
365 status.FilterName = filter.GetType ().ToString ();
366 status.FilterVersion = filter.Version;
368 return status;