2 // QueryStringParser.cs
4 // Copyright (C) 2004-2005 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
;
29 using System
.Text
.RegularExpressions
;
32 using FSQ
=Beagle
.Daemon
.FileSystemQueryable
;
34 namespace Beagle
.Daemon
{
36 public class QueryStringParser
{
38 private QueryStringParser () { }
// a static class
40 // Returns an ICollection of QueryPart objects.
41 static public ICollection
Parse (string query_string
)
44 /* This is our regular Expression Pattern:
45 * we expect something like this:
50 ([+-]?) # Required or Prohibited (optional)
51 (\w+:)? # Key (optional)
53 (\"([^\"]*)\"?)# quoted
59 string Pattern
= "(?<pm>[+-]?) ( (?<key>\\w+) :)? ( (\"(?<quote>[^\"]*)\"?) | (?<unquote>[^\\s\"]+) )";
61 Regex r
= new Regex (Pattern
, RegexOptions
.IgnorePatternWhitespace
);
63 Match m
= r
.Match (query_string
);
66 parts
= new ArrayList ();
68 ArrayList or_list
= null;
72 QueryPart query_part
= MatchToQueryPart (m
);
74 if (or_list
!= null) {
75 or_list
.Add (query_part
);
79 Match next
= m
.NextMatch ();
81 // Trap the OR operator
82 // If the next match is an or, start an or_list
83 // (if we don't have one already) and skip
84 // ahead to the next part.
86 && (next
.Groups
["key"].ToString () == "")
87 && (next
.Groups
["unquote"].ToString ().ToUpper () == "OR") ) {
89 if (or_list
== null) {
90 or_list
= new ArrayList ();
91 or_list
.Add (query_part
);
98 // If we have a non-empty or-list going,
99 // Create the appropriate QueryPart and add it
101 if (or_list
!= null) {
103 QueryPart_Or or_part
= new QueryPart_Or ();
104 or_part
.Logic
= QueryPartLogic
.Required
;
106 foreach (QueryPart sub_part
in or_list
)
107 or_part
.Add (sub_part
);
113 // Add the next text part
114 if (query_part
!= null)
115 parts
.Add (query_part
);
120 // If we ended with an or_parts list, do the right thing.
121 if (or_list
!= null) {
123 QueryPart_Or or_part
= new QueryPart_Or ();
124 or_part
.Logic
= QueryPartLogic
.Required
;
126 foreach (QueryPart sub_part
in or_list
)
127 or_part
.Add (sub_part
);
135 static private QueryPart
MatchToQueryPart(Match m
)
137 // Looping over all Matches we have got:
138 // m.Groups["pm"] plus or minus sign
139 // m.Groups["key"] keyname
140 // m.Groups["quote"] quoted string
141 // m.Groups["unquote"] unquoted string
143 string query
= m
.ToString ();
144 string unquote
= m
.Groups
["unquote"].ToString ();
145 string text
= m
.Groups
["quote"].ToString () + m
.Groups
["unquote"].ToString (); // only one of both is set.
146 string key
= m
.Groups
["key"].ToString ();
148 bool IsProhibited
= (m
.Groups
["pm"].ToString () == "-");
151 // check for file extensions
152 // if match starts with . and only contains letters we assume it's a file extension
153 Regex extension_re
= new Regex (@"^\.\w*$");
155 if (extension_re
.Match (unquote
).Success
|| key
.ToLower () == "ext") {
157 QueryPart_Property query_part
= new QueryPart_Property ();
159 query_part
.Key
= FSQ
.FileSystemQueryable
.FilenameExtensionPropKey
;
160 if (!text
.StartsWith ("."))
161 query_part
.Value
= "." + text
.ToLower ();
163 query_part
.Value
= text
.ToLower (); // the whole .abc part
164 query_part
.Type
= PropertyType
.Keyword
;
165 query_part
.Logic
= (IsProhibited
? QueryPartLogic
.Prohibited
: QueryPartLogic
.Required
);
167 Logger
.Log
.Debug ("Extension query: {0}", query_part
.Value
);
174 QueryPart_Text query_part
= new QueryPart_Text ();
175 query_part
.Text
= text
;
176 query_part
.Logic
= (IsProhibited
? QueryPartLogic
.Prohibited
: QueryPartLogic
.Required
);
178 Logger
.Log
.Debug ("Parsed query '{0}' as text_query", text
);
183 string prop_string
= null;
185 PropertyType prop_type
;
187 is_present
= PropertyKeywordFu
.GetPropertyDetails (key
, out prop_string
, out prop_type
);
188 // if key is not present in the mapping, assume the query is a text query
189 // i.e. if token is foo:bar and there is no mappable property named foo,
190 // assume "foo:bar" as text query
191 // FIXME the analyzer changes the text query "foo:bar" to "foo bar"
192 // which might not be the right thing to do
196 QueryPart_Text query_part
= new QueryPart_Text ();
197 query_part
.Text
= query
;
198 query_part
.Logic
= (IsProhibited
? QueryPartLogic
.Prohibited
: QueryPartLogic
.Required
);
200 Logger
.Log
.Debug ("Could not find property, parsed query '{0}' as text_query", query
);
205 QueryPart_Property query_part_prop
= new QueryPart_Property ();
206 query_part_prop
.Key
= key
;
207 query_part_prop
.Value
= text
;
209 // if query was of type ext:mp3 or extension:mp3
210 // change value to .mp3
211 // if query was of type ext:
212 // change value to ""
213 // Change extension query value to lowercase - thats how they are stored on disk
214 if (query_part_prop
.Key
== FSQ
.FileSystemQueryable
.FilenameExtensionPropKey
&&
215 query_part_prop
.Value
!= String
.Empty
)
216 query_part_prop
.Value
= "." + query_part_prop
.Value
.ToLower ();
218 query_part_prop
.Type
= prop_type
;
219 query_part_prop
.Logic
= (IsProhibited
? QueryPartLogic
.Prohibited
: QueryPartLogic
.Required
);
221 Logger
.Log
.Debug ("Parsed query '" + query
+
222 "' as prop query:key=" + query_part_prop
.Key
+
223 ", value=" + query_part_prop
.Value
+
224 " and property type=" + query_part_prop
.Type
);
226 return query_part_prop
;