~ubuntu-branches/ubuntu/jaunty/beagle/jaunty-security

« back to all changes in this revision

Viewing changes to beagled/PropertyKeywordFu.cs

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-05-04 00:31:32 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20080504003132-2tkm5o8moo5952ri
Tags: 0.3.7-2ubuntu1
 * Merge from Debian unstable. (LP: #225746) Remaining Ubuntu changes:
  - debian/control:
    + Rename ice{weasel,dove}-beagle to {mozilla,thunderbird}-beagle and
      and update the dependencies accordingly.
    + Change Maintainer to Ubuntu Mono Team.
  - debian/rules:
    + Install the mozilla-beagle and thunderbird-beagle extensions.
  - ice{dove,weasel}.dirs:
    + Renamed to {mozilla,thunderbird}-beagle.dirs.
    + Fixed paths to point to usr/lib/{firefox,thunderbird}

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
using System;
28
28
using System.Collections;
 
29
using System.Collections.Generic;
29
30
using System.IO;
30
31
using System.Text;
 
32
using System.Xml;
 
33
using System.Xml.Serialization;
31
34
 
32
35
using Beagle;
33
36
using Beagle.Util;
34
37
 
35
38
namespace Beagle.Daemon {
36
 
        
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;
43
 
                
44
 
                public PropertyType Type { 
45
 
                        get { return type;} 
46
 
                }
47
 
 
48
 
                public string PropertyName { 
49
 
                        get { return property_name;} 
50
 
                }
51
 
                
52
 
                public string Description { 
53
 
                        get { return short_desc; } 
54
 
                }
55
 
 
56
 
                public PropertyDetail (PropertyType type, string property_name) {
57
 
                    this.type = type;
58
 
                    this.property_name = property_name;
59
 
                    this.short_desc = "";
60
 
                }
61
 
                
62
 
                public PropertyDetail (PropertyType type, string property_name, string desc) {
63
 
                    this.type = type;
64
 
                    this.property_name = property_name;
65
 
                    this.short_desc = desc;
66
 
                }
67
 
        }
68
 
        
 
39
 
69
40
        public class PropertyKeywordFu {
70
41
                // mapping
71
42
                private static Hashtable property_table;
73
44
                // static class
74
45
                private PropertyKeywordFu () { }
75
46
 
76
 
                static PropertyKeywordFu () {
77
 
                        PopulatePropertyTable ();
78
 
                }
79
 
 
80
47
                public static IEnumerable Keys {
81
48
                        get { return property_table.Keys; }
82
49
                }
86
53
                                yield break;
87
54
 
88
55
                        object o = property_table [keyword];
89
 
                        if (o is PropertyDetail)
 
56
                        if (o is QueryKeywordMapping)
90
57
                                yield return o;
91
58
                        else if (o is ArrayList) {
92
 
                                foreach (PropertyDetail detail in ((ArrayList) o))
93
 
                                        yield return detail;
 
59
                                foreach (QueryKeywordMapping mapping in ((ArrayList) o))
 
60
                                        yield return mapping;
94
61
                        }
95
62
                }
96
63
 
97
 
                // FIXME handle i18n issues... user might use a i18n-ised string for "title"
98
 
                private static void PopulatePropertyTable () {
 
64
                ////////////////////////////////////////////////////////
 
65
 
 
66
                public static void ReadKeywordMappings ()
 
67
                {
99
68
                        property_table = new Hashtable ();
100
 
                        
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.
103
 
                        
104
 
                        property_table.Add ("title",
105
 
                                            new PropertyDetail (PropertyType.Text, "dc:title", "Title"));
106
 
                        
107
 
                        property_table.Add ("creator",
108
 
                                            new PropertyDetail (PropertyType.Text, "dc:creator", "Creator of the content"));
109
 
 
110
 
                        property_table.Add ("author",
111
 
                                            new PropertyDetail (PropertyType.Text, "dc:author", "Author of the content"));
112
 
 
113
 
                        property_table.Add ("summary",
114
 
                                            new PropertyDetail (PropertyType.Text, "dc:subject", "Brief description of the content"));
115
 
 
116
 
                        property_table.Add ("source",
117
 
                                            new PropertyDetail (PropertyType.Keyword, "beagle:Source", "Name of the backend"));
118
 
 
119
 
                        property_table.Add ("type",
120
 
                                            new PropertyDetail (PropertyType.Keyword, "beagle:HitType", "Hittype of the content e.g. File, IMLog, MailMessage"));
121
 
 
122
 
                        property_table.Add ("mimetype",
123
 
                                            new PropertyDetail (PropertyType.Keyword, "beagle:MimeType", "Mimetype of the content"));
124
 
 
125
 
                        property_table.Add ("filetype",
126
 
                                            new PropertyDetail (PropertyType.Keyword, "beagle:FileType", "Type of content for HitType File"));
127
 
                                            
128
 
                        
 
69
 
 
70
                        // FIXME: No need for a SerializerFactory here, since we need the serializer
 
71
                        // only once
 
72
                        XmlSerializerFactory xsf = new XmlSerializerFactory();
 
73
                        XmlSerializer xs = xsf.CreateSerializer (typeof (QueryMapping), new Type[] { typeof (QueryKeywordMapping)});
 
74
 
 
75
                        QueryMapping query_mapping = null;
 
76
 
 
77
                        // <keyword name, can override>
 
78
                        Dictionary<string, bool> mapping_override = new Dictionary<string, bool> ();
 
79
 
 
80
                        using (Stream s = File.OpenRead (Path.Combine (PathFinder.ConfigDataDir, "query-mapping.xml"))) {
 
81
                                try {                   
 
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;
 
86
                                        }
 
87
                                } catch (XmlException e) {
 
88
                                        Logger.Log.Error (e, "Unable to parse global query-mapping.xml");
 
89
                                }
 
90
                        }
 
91
 
 
92
                        // Override global mappings by local mappings
 
93
 
 
94
                        if (! File.Exists (Path.Combine (PathFinder.StorageDir, "query-mapping.xml")))
 
95
                                return;
 
96
 
 
97
                        using (Stream s = File.OpenRead (Path.Combine (PathFinder.StorageDir, "query-mapping.xml"))) {
 
98
                                try {                   
 
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;
 
104
                                                }
 
105
 
 
106
                                                PropertyKeywordFu.RegisterMapping (mapping);
 
107
                                        }
 
108
                                } catch (XmlException e) {
 
109
                                        Logger.Log.Error (e, "Unable to parse local query-mapping.xml");
 
110
                                }
 
111
                        }
129
112
                }
130
113
 
131
 
                public static void RegisterMapping (PropertyKeywordMapping mapping)
 
114
                private static void RegisterMapping (QueryKeywordMapping mapping)
132
115
                {
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);
144
124
                                        list.Add (o);
145
 
                                        list.Add (new PropertyDetail ( 
146
 
                                                mapping.IsKeyword ? PropertyType.Keyword : PropertyType.Text,
147
 
                                                mapping.PropertyName, 
148
 
                                                mapping.Description));
 
125
                                        list.Add (mapping);
149
126
                                        property_table [mapping.Keyword] = list;
150
127
                                }
151
128
                                return;
152
129
                        }
153
130
 
154
 
                        property_table.Add (mapping.Keyword,
155
 
                                            new PropertyDetail ( 
156
 
                                                mapping.IsKeyword ? PropertyType.Keyword : PropertyType.Text,
157
 
                                                mapping.PropertyName, 
158
 
                                                mapping.Description));
 
131
                        property_table.Add (mapping.Keyword, mapping);
159
132
                }
160
133
 
 
134
                ////////////////////////////////////////////////////////
 
135
 
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) {
163
138
                        num = 0;
164
139
                        name = null;
165
140
                        type = null;
175
150
                                type = new PropertyType [num];
176
151
 
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);
181
156
                                }
182
 
                        } else if (o is PropertyDetail) {
 
157
                        } else if (o is QueryKeywordMapping) {
183
158
                                num = 1;
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);
188
163
                        }
189
164
                        return true;
190
165
                }
191
166
        }
 
167
 
 
168
        public class QueryMapping
 
169
        {
 
170
                private List<QueryKeywordMapping> property_mappings = new List<QueryKeywordMapping> ();
 
171
 
 
172
                [XmlArray]
 
173
                [XmlArrayItem (ElementName="Mapping", Type=typeof (QueryKeywordMapping))]
 
174
                public List<QueryKeywordMapping> Mappings {
 
175
                        get { return property_mappings; }
 
176
                        set { property_mappings = value; }
 
177
                }
 
178
 
 
179
                public QueryMapping () { }
 
180
        }
192
181
}