Add FilterArchive from bugzilla with some minor modifications. Archive filter needs...
[beagle.git] / BeagleClient / Property.cs
blob83293d7d1b3dd3f60c06fc797eba5871377e953b
1 //
2 // Property.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.Text;
31 using System.Xml.Serialization;
33 using Beagle.Util;
35 namespace Beagle {
37 public enum PropertyType {
38 Text = 1,
39 Keyword = 2,
40 Date = 3
43 public class Property : IComparable, ICloneable {
45 PropertyType type;
46 string key;
47 string value;
48 bool is_searched;
49 bool is_mutable;
50 bool is_stored;
52 // Commonly used property keys
53 public const string PrivateNamespace = "_private:";
54 public const string SplitFilenamePropKey = "beagle:SplitFilename";
55 public const string ExactFilenamePropKey = "beagle:ExactFilename";
56 public const string TextFilenamePropKey = "beagle:Filename";
57 public const string NoPunctFilenamePropKey = "beagle:NoPunctFilename";
58 public const string FilenameExtensionPropKey = "beagle:FilenameExtension";
59 public const string ParentDirUriPropKey = Property.PrivateNamespace + "ParentDirUri";
60 public const string IsDirectoryPropKey = Property.PrivateNamespace + "IsDirectory";
61 public const string IsChildPropKey = "beagle:IsChild";
64 [XmlAttribute]
65 public PropertyType Type {
66 get { return type; }
67 set { type = value; }
70 [XmlAttribute]
71 public string Key {
72 get { return key; }
73 set { this.key = StringFu.CleanupInvalidXmlCharacters (value); }
76 [XmlAttribute]
77 public string Value {
78 get { return value; }
79 set { this.value = StringFu.CleanupInvalidXmlCharacters (value); }
82 // If IsSearched is true, this property will can be matched by a
83 // general match-any-propety query.
84 // You can always query against the specific property, even if
85 // IsSearched is false.
86 [XmlAttribute]
87 public bool IsSearched {
88 get { return is_searched; }
89 set { is_searched = value; }
92 // When IsMutable is true, the property is stored in the secondary
93 // index so that it can more efficiently be changed later on.
94 [XmlAttribute]
95 public bool IsMutable {
96 get { return is_mutable; }
97 set { is_mutable = value; }
100 // When IsStored is false, the property will be stored as an "unstored lucene field".
101 [XmlAttribute]
102 public bool IsStored {
103 get { return is_stored; }
104 set { is_stored = value; }
107 /////////////////////////////////////
109 public Property () { }
111 public int CompareTo (object other)
113 // By convention, a non-null object always
114 // compares greater than null.
115 if (other == null)
116 return 1;
118 Property other_property = other as Property;
120 // If the other object is not a Property, compare the
121 // two objects by their hash codes.
122 if (other_property == null)
123 return this.GetHashCode ().CompareTo (other.GetHashCode ());
125 int rv;
126 rv = String.Compare (this.Key, other_property.Key);
127 if (rv != 0)
128 return rv;
130 return String.Compare (this.Value, other_property.Value);
133 public object Clone ()
135 return this.MemberwiseClone ();
138 static public Property New (string key, string value)
140 if (value == null)
141 return null;
143 Property p = new Property ();
144 p.type = PropertyType.Text;
145 p.Key = key;
146 p.Value = value;
147 p.is_searched = true;
148 p.is_stored = true;
149 return p;
152 static public Property NewKeyword (string key, object value)
154 if (value == null)
155 return null;
157 Property p = new Property ();
158 p.type = PropertyType.Keyword;
159 p.Key = key;
160 p.Value = value.ToString ();
161 p.is_searched = true;
162 p.is_stored = true;
163 return p;
166 static public Property NewUnsearched (string key, object value)
168 if (value == null)
169 return null;
171 Property p = new Property ();
172 p.type = PropertyType.Keyword;
173 p.Key = key;
174 p.Value = value.ToString ();
175 p.is_searched = false;
176 p.is_stored = true;
177 return p;
180 static public Property NewUnstored (string key, object value)
182 if (value == null)
183 return null;
185 Property p = new Property ();
186 p.type = PropertyType.Text;
187 p.Key = key;
188 p.Value = value.ToString ();
189 p.is_searched = true;
190 p.is_stored = false;
191 return p;
194 static public Property NewBool (string key, bool value)
196 return Property.NewUnsearched (key, value ? "true" : "false");
199 static public Property NewFlag (string key)
201 return NewBool (key, true);
204 static public Property NewDate (string key, DateTime dt)
206 Property p = new Property ();
207 p.type = PropertyType.Date;
208 p.Key = key;
209 p.Value = StringFu.DateTimeToString (dt);
210 p.is_searched = true;
211 p.is_stored = true;
212 return p;
215 static public Property NewDateFromString (string key, string value)
217 if (value == null)
218 return null;
220 Property p = new Property ();
221 p.type = PropertyType.Date;
222 p.Key = key;
223 // FIXME: Should probably check that value is a valid date string.
224 p.Value = value;
225 p.is_searched = true;
226 p.is_stored = true;
227 return p;
230 override public string ToString ()
232 return String.Format ("{0}={1}", Key, Value);
235 // Standard properties for files
236 // Used by FileSystem backend and filters which produce file child-indexables
237 public static IEnumerable StandardFileProperties (string name, bool mutable)
239 StringBuilder sb;
240 sb = new StringBuilder ();
242 string no_ext, ext, no_punct;
243 no_ext = Path.GetFileNameWithoutExtension (name);
244 ext = Path.GetExtension (name).ToLower ();
246 sb.Append (no_ext);
247 for (int i = 0; i < sb.Length; ++i)
248 if (! Char.IsLetterOrDigit (sb [i]))
249 sb [i] = ' ';
250 no_punct = sb.ToString ();
253 Property prop;
255 prop = Property.NewKeyword (ExactFilenamePropKey, name);
256 prop.IsMutable = mutable;
257 yield return prop;
259 prop = Property.New (TextFilenamePropKey, no_ext);
260 prop.IsMutable = mutable;
261 yield return prop;
263 prop = Property.New (NoPunctFilenamePropKey, no_punct);
264 prop.IsMutable = mutable;
265 yield return prop;
267 prop = Property.NewUnsearched (FilenameExtensionPropKey, ext);
268 prop.IsMutable = mutable;
269 yield return prop;
271 string str;
272 str = StringFu.FuzzyDivide (no_ext);
273 prop = Property.NewUnstored (SplitFilenamePropKey, str);
274 prop.IsMutable = mutable;
275 yield return prop;