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
;
31 using System
.Xml
.Serialization
;
37 public enum PropertyType
{
43 public class Property
: IComparable
, ICloneable
{
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";
65 public PropertyType Type
{
73 set { this.key = StringFu.CleanupInvalidXmlCharacters (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.
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.
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".
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.
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 ());
126 rv
= String
.Compare (this.Key
, other_property
.Key
);
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)
143 Property p
= new Property ();
144 p
.type
= PropertyType
.Text
;
147 p
.is_searched
= true;
152 static public Property
NewKeyword (string key
, object value)
157 Property p
= new Property ();
158 p
.type
= PropertyType
.Keyword
;
160 p
.Value
= value.ToString ();
161 p
.is_searched
= true;
166 static public Property
NewUnsearched (string key
, object value)
171 Property p
= new Property ();
172 p
.type
= PropertyType
.Keyword
;
174 p
.Value
= value.ToString ();
175 p
.is_searched
= false;
180 static public Property
NewUnstored (string key
, object value)
185 Property p
= new Property ();
186 p
.type
= PropertyType
.Text
;
188 p
.Value
= value.ToString ();
189 p
.is_searched
= true;
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
;
209 p
.Value
= StringFu
.DateTimeToString (dt
);
210 p
.is_searched
= true;
215 static public Property
NewDateFromString (string key
, string value)
220 Property p
= new Property ();
221 p
.type
= PropertyType
.Date
;
223 // FIXME: Should probably check that value is a valid date string.
225 p
.is_searched
= true;
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
)
240 sb
= new StringBuilder ();
242 string no_ext
, ext
, no_punct
;
243 no_ext
= Path
.GetFileNameWithoutExtension (name
);
244 ext
= Path
.GetExtension (name
).ToLower ();
247 for (int i
= 0; i
< sb
.Length
; ++i
)
248 if (! Char
.IsLetterOrDigit (sb
[i
]))
250 no_punct
= sb
.ToString ();
255 prop
= Property
.NewKeyword (ExactFilenamePropKey
, name
);
256 prop
.IsMutable
= mutable
;
259 prop
= Property
.New (TextFilenamePropKey
, no_ext
);
260 prop
.IsMutable
= mutable
;
263 prop
= Property
.New (NoPunctFilenamePropKey
, no_punct
);
264 prop
.IsMutable
= mutable
;
267 prop
= Property
.NewUnsearched (FilenameExtensionPropKey
, ext
);
268 prop
.IsMutable
= mutable
;
272 str
= StringFu
.FuzzyDivide (no_ext
);
273 prop
= Property
.NewUnstored (SplitFilenamePropKey
, str
);
274 prop
.IsMutable
= mutable
;