2005-04-19 Gabor Kelemen <kelemeng@gnome.hu>
[beagle.git] / BeagleClient / Indexable.cs
blobc4cc136e3df228e400ed57fb3de1d2abeec806f8
1 //
2 // Indexable.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.
28 using System;
29 using System.Collections;
30 using System.IO;
31 using System.Text;
32 using System.Xml;
33 using System.Xml.Serialization;
34 using Beagle.Util;
36 namespace Beagle {
38 public class Indexable : Versioned {
40 // The URI of the item being indexed.
41 private Uri uri = null;
43 // The URI of the contents to index
44 private Uri contentUri = null;
46 // The URI of the hot contents to index
47 private Uri hotContentUri = null;
49 // Whether the content should be deleted after indexing
50 private bool deleteContent = false;
52 // File, WebLink, MailMessage, IMLog, etc.
53 private String type = null;
55 // If applicable, otherwise set to null.
56 private String mimeType = null;
58 // List of Property objects
59 private ArrayList properties = new ArrayList ();
61 // A stream of the content to index
62 private TextReader textReader;
64 // A stream of the hot content to index
65 private TextReader hotTextReader;
67 //////////////////////////
69 static private XmlSerializer our_serializer;
71 static Indexable ()
73 our_serializer = new XmlSerializer (typeof (Indexable));
76 //////////////////////////
78 public Indexable (Uri _uri) {
79 uri = _uri;
81 type = "File";
84 public Indexable () {
85 // Only used when reading from xml
88 public static Indexable NewFromXml (string xml)
90 StringReader reader = new StringReader (xml);
91 return (Indexable) our_serializer.Deserialize (reader);
94 //////////////////////////
96 // Use Build to do any set-up that you want to defer until
97 // immediately before indexing.
98 public virtual void Build ()
103 //////////////////////////
105 [XmlIgnore]
106 public Uri Uri {
107 get { return uri; }
108 set { uri = value; }
111 [XmlAttribute ("Uri")]
112 public string UriString {
113 get { return UriFu.UriToSerializableString (uri); }
114 set { uri = UriFu.UriStringToUri (value); }
117 [XmlIgnore]
118 public Uri ContentUri {
119 get { return contentUri != null ? contentUri : Uri; }
120 set { contentUri = value; }
123 [XmlAttribute ("ContentUri")]
124 public string ContentUriString {
125 get { return UriFu.UriToSerializableString (ContentUri); }
126 set { contentUri = UriFu.UriStringToUri (value); }
129 [XmlIgnore]
130 private Uri HotContentUri {
131 get { return hotContentUri; }
132 set { hotContentUri = value; }
135 [XmlAttribute ("HotContentUri")]
136 public string HotContentUriString {
137 get { return HotContentUri != null ? UriFu.UriToSerializableString (HotContentUri) : ""; }
138 set { hotContentUri = (value != "") ? new Uri (value) : null; }
141 [XmlIgnore]
142 public Uri DisplayUri {
143 get { return uri.Scheme == "uid" ? ContentUri : Uri; }
146 [XmlAttribute]
147 public bool DeleteContent {
148 get { return deleteContent; }
149 set { deleteContent = value; }
152 [XmlAttribute]
153 public String Type {
154 get { return type; }
155 set { type = value; }
158 [XmlAttribute]
159 public String MimeType {
160 get { return mimeType; }
161 set { mimeType = value; }
164 [XmlIgnore]
165 public bool IsNonTransient {
166 get { return ! DeleteContent && ContentUri.IsFile; }
169 //////////////////////////
171 private TextReader ReaderFromUri (Uri uri)
173 TextReader reader = null;
175 if (uri != null && uri.IsFile) {
176 Stream stream = new FileStream (uri.LocalPath,
177 FileMode.Open,
178 FileAccess.Read,
179 FileShare.Read);
181 reader = new StreamReader (stream);
183 // Paranoia: never delete the thing we are actually indexing.
184 if (DeleteContent && uri != Uri)
185 File.Delete (uri.LocalPath);
188 return reader;
191 public virtual TextReader GetTextReader ()
193 if (textReader == null)
194 textReader = ReaderFromUri (ContentUri);
196 return textReader;
199 public void SetTextReader (TextReader reader)
201 textReader = reader;
204 public virtual TextReader GetHotTextReader ()
206 if (hotTextReader == null)
207 hotTextReader = ReaderFromUri (HotContentUri);
208 return hotTextReader;
211 public virtual void SetHotTextReader (TextReader reader)
213 hotTextReader = reader;
216 [XmlArrayItem (ElementName="Property", Type=typeof (Property))]
217 public ArrayList Properties {
218 get { return properties; }
221 public void AddProperty (Property prop) {
222 properties.Add (prop);
225 //////////////////////////
227 private string PropertiesAsString (bool keywords)
229 StringBuilder sb = new StringBuilder ();
230 foreach (Property prop in Properties) {
231 if (prop.IsSearched
232 && keywords ? prop.IsKeyword : ! prop.IsKeyword) {
233 if (sb.Length > 0)
234 sb.Append (" ");
235 sb.Append (prop.Value);
238 return sb.ToString ();
241 public string TextPropertiesAsString {
242 get { return PropertiesAsString (false); }
245 public string KeywordPropertiesAsString {
246 get { return PropertiesAsString (true); }
249 //////////////////////////
251 public override string ToString ()
253 StringWriter writer = new StringWriter ();
254 our_serializer.Serialize (writer, this);
255 writer.Close ();
256 return writer.ToString ();
259 private static Uri TextReaderToTempFileUri (TextReader reader)
261 if (reader == null)
262 return null;
264 string filename = Path.GetTempFileName ();
265 FileStream fileStream = File.OpenWrite (filename);
266 BufferedStream bufferedStream = new BufferedStream (fileStream);
267 StreamWriter writer = new StreamWriter (bufferedStream);
269 const int BUFFER_SIZE = 8192;
270 char [] buffer = new char [BUFFER_SIZE];
272 int read;
273 do {
274 read = reader.Read (buffer, 0, BUFFER_SIZE);
275 if (read > 0)
276 writer.Write (buffer, 0, read);
277 } while (read > 0);
279 writer.Close ();
281 return UriFu.PathToFileUri (filename);
284 public void StoreStream () {
285 if (textReader != null) {
286 ContentUri = TextReaderToTempFileUri (textReader);
287 DeleteContent = true;
290 if (hotTextReader != null) {
291 HotContentUri = TextReaderToTempFileUri (hotTextReader);
292 DeleteContent = true;
296 //////////////////////////
298 public override int GetHashCode ()
300 return (uri != null ? uri.GetHashCode () : 0) ^ type.GetHashCode ();