28
28
using System.Collections;
29
using System.Collections.Generic;
33
using System.Xml.Serialization;
35
38
namespace Beagle.Daemon {
37
public class PropertyDetail {
38
private PropertyType type;
39
private string property_name;
40
// a short description, so that frontends can get the description from here
41
// and they dont need to guess the meaning of a query keyword (being too nice ?!)
42
private string short_desc;
44
public PropertyType Type {
48
public string PropertyName {
49
get { return property_name;}
52
public string Description {
53
get { return short_desc; }
56
public PropertyDetail (PropertyType type, string property_name) {
58
this.property_name = property_name;
62
public PropertyDetail (PropertyType type, string property_name, string desc) {
64
this.property_name = property_name;
65
this.short_desc = desc;
69
40
public class PropertyKeywordFu {
71
42
private static Hashtable property_table;
88
55
object o = property_table [keyword];
89
if (o is PropertyDetail)
56
if (o is QueryKeywordMapping)
91
58
else if (o is ArrayList) {
92
foreach (PropertyDetail detail in ((ArrayList) o))
59
foreach (QueryKeywordMapping mapping in ((ArrayList) o))
97
// FIXME handle i18n issues... user might use a i18n-ised string for "title"
98
private static void PopulatePropertyTable () {
64
////////////////////////////////////////////////////////
66
public static void ReadKeywordMappings ()
99
68
property_table = new Hashtable ();
101
// Mapping between human query keywords and beagle index property keywords
102
// These are some of the standard mapping which is available to all backends and filters.
104
property_table.Add ("title",
105
new PropertyDetail (PropertyType.Text, "dc:title", "Title"));
107
property_table.Add ("creator",
108
new PropertyDetail (PropertyType.Text, "dc:creator", "Creator of the content"));
110
property_table.Add ("author",
111
new PropertyDetail (PropertyType.Text, "dc:author", "Author of the content"));
113
property_table.Add ("summary",
114
new PropertyDetail (PropertyType.Text, "dc:subject", "Brief description of the content"));
116
property_table.Add ("source",
117
new PropertyDetail (PropertyType.Keyword, "beagle:Source", "Name of the backend"));
119
property_table.Add ("type",
120
new PropertyDetail (PropertyType.Keyword, "beagle:HitType", "Hittype of the content e.g. File, IMLog, MailMessage"));
122
property_table.Add ("mimetype",
123
new PropertyDetail (PropertyType.Keyword, "beagle:MimeType", "Mimetype of the content"));
125
property_table.Add ("filetype",
126
new PropertyDetail (PropertyType.Keyword, "beagle:FileType", "Type of content for HitType File"));
70
// FIXME: No need for a SerializerFactory here, since we need the serializer
72
XmlSerializerFactory xsf = new XmlSerializerFactory();
73
XmlSerializer xs = xsf.CreateSerializer (typeof (QueryMapping), new Type[] { typeof (QueryKeywordMapping)});
75
QueryMapping query_mapping = null;
77
// <keyword name, can override>
78
Dictionary<string, bool> mapping_override = new Dictionary<string, bool> ();
80
using (Stream s = File.OpenRead (Path.Combine (PathFinder.ConfigDataDir, "query-mapping.xml"))) {
82
query_mapping = (QueryMapping) xs.Deserialize (s);
83
foreach (QueryKeywordMapping mapping in query_mapping.Mappings) {
84
PropertyKeywordFu.RegisterMapping (mapping);
85
mapping_override [mapping.Keyword] = true;
87
} catch (XmlException e) {
88
Logger.Log.Error (e, "Unable to parse global query-mapping.xml");
92
// Override global mappings by local mappings
94
if (! File.Exists (Path.Combine (PathFinder.StorageDir, "query-mapping.xml")))
97
using (Stream s = File.OpenRead (Path.Combine (PathFinder.StorageDir, "query-mapping.xml"))) {
99
query_mapping = (QueryMapping) xs.Deserialize (s);
100
foreach (QueryKeywordMapping mapping in query_mapping.Mappings) {
101
if (mapping_override.ContainsKey (mapping.Keyword)) {
102
property_table.Remove (mapping.Keyword);
103
mapping_override [mapping.Keyword] = false;
106
PropertyKeywordFu.RegisterMapping (mapping);
108
} catch (XmlException e) {
109
Logger.Log.Error (e, "Unable to parse local query-mapping.xml");
131
public static void RegisterMapping (PropertyKeywordMapping mapping)
114
private static void RegisterMapping (QueryKeywordMapping mapping)
133
116
// If multiple mapping as registered, create an OR query for them
134
117
// Store the multiple matchings in a list
135
118
if (property_table.Contains (mapping.Keyword)) {
136
119
object o = property_table [mapping.Keyword];
137
120
if (o is ArrayList) {
138
((ArrayList)o).Add (new PropertyDetail (
139
mapping.IsKeyword ? PropertyType.Keyword : PropertyType.Text,
140
mapping.PropertyName,
141
mapping.Description));
142
} else if (o is PropertyDetail) {
121
((ArrayList)o).Add (mapping);
122
} else if (o is QueryKeywordMapping) {
143
123
ArrayList list = new ArrayList (2);
145
list.Add (new PropertyDetail (
146
mapping.IsKeyword ? PropertyType.Keyword : PropertyType.Text,
147
mapping.PropertyName,
148
mapping.Description));
149
126
property_table [mapping.Keyword] = list;
154
property_table.Add (mapping.Keyword,
156
mapping.IsKeyword ? PropertyType.Keyword : PropertyType.Text,
157
mapping.PropertyName,
158
mapping.Description));
131
property_table.Add (mapping.Keyword, mapping);
134
////////////////////////////////////////////////////////
161
136
// return false if property not found!
162
public static bool GetPropertyDetails (string keyword, out int num, out string[] name, out PropertyType[] type) {
137
public static bool GetMapping (string keyword, out int num, out string[] name, out PropertyType[] type) {
175
150
type = new PropertyType [num];
177
152
for (int i = 0; i < num; ++i) {
178
PropertyDetail detail = (PropertyDetail) (list [i]);
179
name [i] = detail.PropertyName;
180
type [i] = detail.Type;
153
QueryKeywordMapping mapping = (QueryKeywordMapping) (list [i]);
154
name [i] = mapping.PropertyName;
155
type [i] = (mapping.IsKeyword ? PropertyType.Keyword : PropertyType.Text);
182
} else if (o is PropertyDetail) {
157
} else if (o is QueryKeywordMapping) {
184
159
name = new string [num];
185
160
type = new PropertyType [num];
186
name [0] = ((PropertyDetail) o).PropertyName;
187
type [0] = ((PropertyDetail) o).Type;
161
name [0] = ((QueryKeywordMapping) o).PropertyName;
162
type [0] = (((QueryKeywordMapping) o).IsKeyword ? PropertyType.Keyword : PropertyType.Text);
168
public class QueryMapping
170
private List<QueryKeywordMapping> property_mappings = new List<QueryKeywordMapping> ();
173
[XmlArrayItem (ElementName="Mapping", Type=typeof (QueryKeywordMapping))]
174
public List<QueryKeywordMapping> Mappings {
175
get { return property_mappings; }
176
set { property_mappings = value; }
179
public QueryMapping () { }