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.
29 using System
.Collections
;
33 namespace Beagle
.Daemon
{
35 public class FilterFlavor
{
37 private string uri
= null;
38 private string mime_type
= null;
39 private string extension
= null;
41 private int priority
= 0;
45 set { uri = IsWild (value) ? null : value; }
48 public string Extension
{
49 get { return extension; }
50 set { extension = IsWild (value) ? null : value; }
53 public string MimeType
{
54 get { return mime_type; }
55 set { mime_type = IsWild (value) ? null : value; }
59 get { return priority; }
60 set { priority = value; }
63 public FilterFlavor (string uri
, string extension
, string mime_type
, int priority
)
66 this.extension
= extension
;
67 this.mime_type
= mime_type
;
68 this.priority
= priority
;
71 private bool IsWild (string str
)
77 foreach (char c
in str
)
83 public bool IsMatch (Uri uri
, string extension
, string mime_type
)
85 if (Uri
!= null && (uri
== null || !StringFu
.GlobMatch (Uri
, uri
.ToString ())))
88 if (Extension
!= null && (extension
== null || !StringFu
.GlobMatch (Extension
, extension
)))
91 if (MimeType
!= null && (mime_type
== null || !StringFu
.GlobMatch (MimeType
, mime_type
)))
100 int weight
= priority
;
104 if (Extension
!= null)
106 if (MimeType
!= null)
113 ////////////////////////////////////////////
115 public override string ToString ()
120 ret
+= String
.Format ("Uri: {0}", Uri
);
122 if (Extension
!= null)
123 ret
+= String
.Format ("Extension: {0}", Extension
);
125 if (MimeType
!= null)
126 ret
+= String
.Format ("MimeType: {0}", MimeType
);
131 public class WeightComparer
: IComparer
133 public int Compare (object obj1
, object obj2
)
135 FilterFlavor flav1
= (FilterFlavor
) obj1
;
136 FilterFlavor flav2
= (FilterFlavor
) obj2
;
138 return flav1
.Weight
.CompareTo (flav2
.Weight
);
142 public class Hasher
: IHashCodeProvider
144 public int GetHashCode (object o
)
146 return o
.ToString ().GetHashCode ();
150 static WeightComparer the_comparer
= new WeightComparer ();
151 static Hasher the_hasher
= new Hasher ();
153 public static Hashtable
NewHashtable ()
155 return new Hashtable (the_hasher
, the_comparer
);
158 ////////////////////////////////////////////
160 static private ArrayList flavors
= new ArrayList ();
162 static public ArrayList Flavors
{
163 get { return flavors; }
166 public static FilterFlavor
NewFromMimeType (string mime_type
) {
167 return new FilterFlavor (null, null, mime_type
, 0);
170 public static FilterFlavor
NewFromExtension (string extension
) {
171 return new FilterFlavor (null, extension
, null, 0);