4 // Copyright (C) 2004 Novell, Inc.
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.
28 using System
.Collections
;
30 using System
.Reflection
;
33 using System
.Xml
.Serialization
;
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
);
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;
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
) {
75 matched_filters_by_flavor
[flavor
] = matched_filter
;
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
];
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
)
122 return CreateFiltersFromPath (uri
.LocalPath
);
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
)
143 if (indexable
.Filtering
== IndexableFiltering
.Always
)
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)
154 static public bool FilterIndexable (Indexable indexable
, TextCache text_cache
, out Filter filter
)
157 ICollection filters
= null;
159 if (indexable
.Filtering
== IndexableFiltering
.AlreadyFiltered
)
162 if (! ShouldWeFilterThis (indexable
)) {
163 indexable
.NoContent
= true;
169 // First, figure out which filter we should use to deal with
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
)
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
)
195 indexable
.Timestamp
= File
.GetLastWriteTimeUtc (path
);
197 Logger
.Log
.Warn ("No such file: {0}", path
);
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
.Uri
, path
, indexable
.MimeType
);
214 foreach (Filter candidate_filter
in filters
) {
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 URI
232 candidate_filter
.Uri
= indexable
.Uri
;
234 // allow the filter access to the indexable's properties
235 candidate_filter
.IndexableProperties
= indexable
.Properties
;
237 // Open the filter, copy the file's properties to the indexable,
238 // and hook up the TextReaders.
240 bool succesful_open
= false;
241 TextReader text_reader
;
242 Stream binary_stream
;
245 succesful_open
= candidate_filter
.Open (path
);
246 else if ((text_reader
= indexable
.GetTextReader ()) != null)
247 succesful_open
= candidate_filter
.Open (text_reader
);
248 else if ((binary_stream
= indexable
.GetBinaryStream ()) != null)
249 succesful_open
= candidate_filter
.Open (binary_stream
);
251 if (succesful_open
) {
252 if (candidate_filter
.Timestamp
!= DateTime
.MinValue
)
253 indexable
.Timestamp
= candidate_filter
.Timestamp
;
254 foreach (Property prop
in candidate_filter
.Properties
)
255 indexable
.AddProperty (prop
);
256 indexable
.SetTextReader (candidate_filter
.GetTextReader ());
257 indexable
.SetHotTextReader (candidate_filter
.GetHotTextReader ());
260 Logger
.Log
.Debug ("Successfully filtered {0} with {1}", path
, candidate_filter
);
262 filter
= candidate_filter
;
265 Logger
.Log
.Debug ("Unsuccessfully filtered {0} with {1}, falling back", path
, candidate_filter
);
266 candidate_filter
.Cleanup ();
271 Logger
.Log
.Debug ("None of the matching filters could process the file: {0}", path
);
276 static public bool FilterIndexable (Indexable indexable
, out Filter filter
)
278 return FilterIndexable (indexable
, null, out filter
);
281 static public bool FilterIndexable (Indexable indexable
)
283 Filter filter
= null;
285 return FilterIndexable (indexable
, null, out filter
);
288 /////////////////////////////////////////////////////////////////////////
290 private static Hashtable filter_types_by_flavor
= new Hashtable ();
291 private static Hashtable filter_versions_by_name
= new Hashtable ();
293 static private int ScanAssemblyForFilters (Assembly assembly
)
297 foreach (Type t
in ReflectionFu
.GetTypesFromAssemblyAttribute (assembly
, typeof (FilterTypesAttribute
))) {
298 Filter filter
= null;
301 filter
= (Filter
) Activator
.CreateInstance (t
);
302 } catch (Exception ex
) {
303 Logger
.Log
.Error (ex
, "Caught exception while instantiating {0}", t
);
309 filter_versions_by_name
[t
.ToString ()] = filter
.Version
;
311 foreach (FilterFlavor flavor
in filter
.SupportedFlavors
) {
312 filter_types_by_flavor
[flavor
] = t
;
313 FilterFlavor
.Flavors
.Add (flavor
);
324 /////////////////////////////////////////////////////////////////////////
326 public class FilteredStatus
329 private string filter_name
;
330 private int filter_version
;
338 [XmlAttribute ("Uri")]
339 public string UriAsString
{
341 return UriFu
.UriToEscapedString (uri
);
345 uri
= UriFu
.EscapedStringToUri (value);
349 public string FilterName
{
350 get { return filter_name; }
351 set { filter_name = value; }
354 public int FilterVersion
{
355 get { return filter_version; }
356 set { filter_version = value; }
359 public static FilteredStatus
New (Indexable indexable
, Filter filter
)
361 FilteredStatus status
= new FilteredStatus ();
363 status
.Uri
= indexable
.Uri
;
364 status
.FilterName
= filter
.GetType ().ToString ();
365 status
.FilterVersion
= filter
.Version
;