* tools/Info.cs: Add --list-backends, --list-static-indexes to
[beagle.git] / beagled / FilterFactory.cs
blob0456c98f3f11a7d4156edf5be1a0f683d2a28ade
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 foreach (DictionaryEntry entry in matched_filters_by_flavor) {
80 FilterFlavor flav = (FilterFlavor) entry.Key;
81 Filter filter = (Filter) entry.Value;
83 if (Debug)
84 Logger.Log.Debug ("Found matching filter: {0}, Weight: {1}", filter, flav.Weight);
87 return matched_filters_by_flavor.Values;
90 static public int GetFilterVersion (string filter_name)
92 if (filter_versions_by_name.Contains (filter_name)) {
93 return (int) filter_versions_by_name [filter_name];
94 } else {
95 return -1;
99 /////////////////////////////////////////////////////////////////////////
101 static public ICollection CreateFiltersFromMimeType (string mime_type)
103 return CreateFilters (null, null, mime_type);
106 static public ICollection CreateFilterFromExtension (string extension)
108 return CreateFilters (null, extension, null);
111 static public ICollection CreateFiltersFromPath (string path)
113 string guessed_mime_type = XdgMime.GetMimeType (path);
114 string extension = Path.GetExtension (path);
115 return CreateFilters (UriFu.PathToFileUri (path), extension, guessed_mime_type);
118 static public ICollection CreateFiltersFromUri (Uri uri)
120 if (uri.IsFile)
121 return CreateFiltersFromPath (uri.LocalPath);
122 else
123 return CreateFilters (uri, null, null);
126 static public ICollection CreateFiltersFromIndexable (Indexable indexable)
128 string path = indexable.ContentUri.LocalPath;
129 string extension = Path.GetExtension (path);
130 string mime_type = indexable.MimeType;
131 return CreateFilters (UriFu.PathToFileUri (path), extension, mime_type);
134 /////////////////////////////////////////////////////////////////////////
136 static public TextReader FilterFile (string path)
138 return FilterFile (path, null);
141 static public TextReader FilterFile (string path, string mime_type)
143 if (mime_type == null)
144 mime_type = XdgMime.GetMimeType (path);
146 if (mime_type == null)
147 return null;
149 ICollection filters = CreateFilters (UriFu.PathToFileUri (path), Path.GetExtension (path), mime_type);
150 TextReader reader;
152 foreach (Filter candidate_filter in filters) {
153 if (Debug)
154 Logger.Log.Debug ("Testing filter: {0}", candidate_filter);
156 // Open the filter, and hook up the TextReader.
157 if (candidate_filter.Open (path)) {
158 reader = candidate_filter.GetTextReader ();
160 if (Debug)
161 Logger.Log.Debug ("Successfully filtered {0} with {1}", path, candidate_filter);
163 return reader;
164 } else if (Debug) {
165 Logger.Log.Debug ("Unsuccessfully filtered {0} with {1}, falling back", path, candidate_filter);
169 return null;
172 static private bool ShouldWeFilterThis (Indexable indexable)
174 if (indexable.Filtering == IndexableFiltering.Never
175 || indexable.NoContent)
176 return false;
178 if (indexable.Filtering == IndexableFiltering.Always)
179 return true;
181 // Our default behavior is to try to filter non-transient file
182 // indexable and indexables with a specific mime type attached.
183 if (indexable.IsNonTransient || indexable.MimeType != null)
184 return true;
186 return false;
189 static public bool FilterIndexable (Indexable indexable, TextCache text_cache, out Filter filter)
191 filter = null;
192 ICollection filters = null;
194 if (indexable.Filtering == IndexableFiltering.AlreadyFiltered)
195 return false;
197 if (! ShouldWeFilterThis (indexable)) {
198 indexable.NoContent = true;
199 return false;
202 string path = null;
204 // First, figure out which filter we should use to deal with
205 // the indexable.
207 // If a specific mime type is specified, try to index as that type.
208 if (indexable.MimeType != null)
209 filters = CreateFiltersFromMimeType (indexable.MimeType);
211 if (indexable.ContentUri.IsFile) {
212 path = indexable.ContentUri.LocalPath;
214 // Otherwise sniff the mime-type from the file
215 if (indexable.MimeType == null)
216 indexable.MimeType = XdgMime.GetMimeType (path);
218 if (filters == null || filters.Count == 0) {
219 filters = CreateFiltersFromIndexable (indexable);
222 if (Directory.Exists (path)) {
223 indexable.MimeType = "inode/directory";
224 indexable.NoContent = true;
225 indexable.Timestamp = Directory.GetLastWriteTimeUtc (path);
226 } else if (File.Exists (path)) {
227 indexable.Timestamp = File.GetLastWriteTimeUtc (path);
228 } else {
229 Logger.Log.Warn ("No such file: {0}", path);
230 return false;
234 // We don't know how to filter this, so there is nothing else to do.
235 if (filters.Count == 0) {
236 if (! indexable.NoContent) {
237 indexable.NoContent = true;
239 Logger.Log.Debug ("No filter for {0} ({1})", path != null ? path : indexable.Uri.ToString (), indexable.MimeType);
240 return false;
243 return true;
246 foreach (Filter candidate_filter in filters) {
247 if (Debug)
248 Logger.Log.Debug ("Testing filter: {0}", candidate_filter);
250 // Hook up the snippet writer.
251 if (candidate_filter.SnippetMode && text_cache != null) {
252 if (candidate_filter.OriginalIsText && indexable.IsNonTransient) {
253 text_cache.MarkAsSelfCached (indexable.Uri);
254 } else if (indexable.CacheContent) {
255 TextWriter writer = text_cache.GetWriter (indexable.Uri);
256 candidate_filter.AttachSnippetWriter (writer);
260 if (indexable.Crawled)
261 candidate_filter.EnableCrawlMode ();
263 // Set the filter's URI
264 candidate_filter.Uri = indexable.Uri;
266 // allow the filter access to the indexable's properties
267 candidate_filter.IndexableProperties = indexable.Properties;
269 // Open the filter, copy the file's properties to the indexable,
270 // and hook up the TextReaders.
272 bool succesful_open = false;
273 TextReader text_reader;
274 Stream binary_stream;
276 if (path != null)
277 succesful_open = candidate_filter.Open (path);
278 else if ((text_reader = indexable.GetTextReader ()) != null)
279 succesful_open = candidate_filter.Open (text_reader);
280 else if ((binary_stream = indexable.GetBinaryStream ()) != null)
281 succesful_open = candidate_filter.Open (binary_stream);
283 if (succesful_open) {
284 foreach (Property prop in candidate_filter.Properties)
285 indexable.AddProperty (prop);
286 indexable.SetTextReader (candidate_filter.GetTextReader ());
287 indexable.SetHotTextReader (candidate_filter.GetHotTextReader ());
289 if (Debug)
290 Logger.Log.Debug ("Successfully filtered {0} with {1}", path, candidate_filter);
292 filter = candidate_filter;
293 return true;
294 } else if (Debug) {
295 Logger.Log.Debug ("Unsuccessfully filtered {0} with {1}, falling back", path, candidate_filter);
296 candidate_filter.Cleanup ();
300 if (Debug)
301 Logger.Log.Debug ("None of the matching filters could process the file: {0}", path);
303 return false;
306 static public bool FilterIndexable (Indexable indexable, out Filter filter)
308 return FilterIndexable (indexable, null, out filter);
311 static public bool FilterIndexable (Indexable indexable)
313 Filter filter = null;
315 return FilterIndexable (indexable, null, out filter);
318 /////////////////////////////////////////////////////////////////////////
320 private static Hashtable filter_types_by_flavor = new Hashtable ();
321 private static Hashtable filter_versions_by_name = new Hashtable ();
323 static private int ScanAssemblyForFilters (Assembly assembly)
325 int count = 0;
327 foreach (Type t in ReflectionFu.ScanAssemblyForClass (assembly, typeof (Filter))) {
328 Filter filter = null;
330 try {
331 filter = (Filter) Activator.CreateInstance (t);
332 } catch (Exception ex) {
333 Logger.Log.Error ("Caught exception while instantiating {0}", t);
334 Logger.Log.Error (ex);
337 if (filter == null)
338 continue;
340 filter_versions_by_name [t.ToString ()] = filter.Version;
342 foreach (FilterFlavor flavor in filter.SupportedFlavors) {
343 filter_types_by_flavor [flavor] = t;
344 FilterFlavor.Flavors.Add (flavor);
347 ++count;
350 return count;
355 /////////////////////////////////////////////////////////////////////////
357 public class FilteredStatus
359 private Uri uri;
360 private string filter_name;
361 private int filter_version;
363 [XmlIgnore]
364 public Uri Uri {
365 get { return uri; }
366 set { uri = value; }
369 [XmlAttribute ("Uri")]
370 public string UriAsString {
371 get {
372 return UriFu.UriToSerializableString (uri);
375 set {
376 uri = UriFu.UriStringToUri (value);
380 public string FilterName {
381 get { return filter_name; }
382 set { filter_name = value; }
385 public int FilterVersion {
386 get { return filter_version; }
387 set { filter_version = value; }
390 public static FilteredStatus New (Indexable indexable, Filter filter)
392 FilteredStatus status = new FilteredStatus ();
394 status.Uri = indexable.Uri;
395 status.FilterName = filter.GetType ().ToString ();
396 status.FilterVersion = filter.Version;
398 return status;