* Filters/FilterPackage.cs, Filters/FilterRPM.cs,
[beagle.git] / BeagleClient / Property.cs
blob7275d5999909359a336f0ebc51006db1974d9e92
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.Xml.Serialization;
30 using Beagle.Util;
32 namespace Beagle {
34 public enum PropertyType {
35 Text = 1,
36 Keyword = 2,
37 Date = 3
40 public class Property : IComparable, ICloneable {
42 PropertyType type;
43 string key;
44 string value;
45 bool is_searched;
46 bool is_mutable;
47 bool is_stored;
49 [XmlAttribute]
50 public PropertyType Type {
51 get { return type; }
52 set { type = value; }
55 [XmlAttribute]
56 public string Key {
57 get { return key; }
58 set { this.key = StringFu.CleanupInvalidXmlCharacters (value); }
61 [XmlAttribute]
62 public string Value {
63 get { return value; }
64 set { this.value = StringFu.CleanupInvalidXmlCharacters (value); }
67 // If IsSearched is true, this property will can be matched by a
68 // general match-any-propety query.
69 // You can always query against the specific property, even if
70 // IsSearched is false.
71 [XmlAttribute]
72 public bool IsSearched {
73 get { return is_searched; }
74 set { is_searched = value; }
77 // When IsMutable is true, the property is stored in the secondary
78 // index so that it can more efficiently be changed later on.
79 [XmlAttribute]
80 public bool IsMutable {
81 get { return is_mutable; }
82 set { is_mutable = value; }
85 // When IsStored is false, the property will be stored as an "unstored lucene field".
86 [XmlAttribute]
87 public bool IsStored {
88 get { return is_stored; }
89 set { is_stored = value; }
92 /////////////////////////////////////
94 public Property () { }
96 public int CompareTo (object other)
98 // By convention, a non-null object always
99 // compares greater than null.
100 if (other == null)
101 return 1;
103 Property other_property = other as Property;
105 // If the other object is not a Property, compare the
106 // two objects by their hash codes.
107 if (other_property == null)
108 return this.GetHashCode ().CompareTo (other.GetHashCode ());
110 int rv;
111 rv = String.Compare (this.Key, other_property.Key);
112 if (rv != 0)
113 return rv;
115 return String.Compare (this.Value, other_property.Value);
118 public object Clone ()
120 return this.MemberwiseClone ();
123 static public Property New (string key, string value)
125 if (value == null)
126 return null;
128 Property p = new Property ();
129 p.type = PropertyType.Text;
130 p.Key = key;
131 p.Value = value;
132 p.is_searched = true;
133 p.is_stored = true;
134 return p;
137 static public Property NewKeyword (string key, object value)
139 if (value == null)
140 return null;
142 Property p = new Property ();
143 p.type = PropertyType.Keyword;
144 p.Key = key;
145 p.Value = value.ToString ();
146 p.is_searched = true;
147 p.is_stored = true;
148 return p;
151 static public Property NewUnsearched (string key, object value)
153 if (value == null)
154 return null;
156 Property p = new Property ();
157 p.type = PropertyType.Keyword;
158 p.Key = key;
159 p.Value = value.ToString ();
160 p.is_searched = false;
161 p.is_stored = true;
162 return p;
165 static public Property NewUnstored (string key, object value)
167 if (value == null)
168 return null;
170 Property p = new Property ();
171 p.type = PropertyType.Text;
172 p.Key = key;
173 p.Value = value.ToString ();
174 p.is_searched = true;
175 p.is_stored = false;
176 return p;
179 static public Property NewBool (string key, bool value)
181 return Property.NewUnsearched (key, value ? "true" : "false");
184 static public Property NewFlag (string key)
186 return NewBool (key, true);
189 static public Property NewDate (string key, DateTime dt)
191 Property p = new Property ();
192 p.type = PropertyType.Date;
193 p.Key = key;
194 p.Value = StringFu.DateTimeToString (dt);
195 p.is_searched = true;
196 p.is_stored = true;
197 return p;
200 static public Property NewDateFromString (string key, string value)
202 if (value == null)
203 return null;
205 Property p = new Property ();
206 p.type = PropertyType.Date;
207 p.Key = key;
208 // FIXME: Should probably check that value is a valid date string.
209 p.Value = value;
210 p.is_searched = true;
211 p.is_stored = true;
212 return p;
215 override public string ToString ()
217 return String.Format ("{0}={1}", Key, Value);