Tokenize 001234 as 1234. Include a testing function in NoiseFilter to figure out...
[beagle.git] / beagled / QueryStringParser.cs
blob6e32c799a42ae3386c5ca8ad925cb282a91f4075
1 //
2 // QueryStringParser.cs
3 //
4 // Copyright (C) 2004-2005 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.Collections;
29 using System.Text.RegularExpressions;
31 using Beagle.Util;
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:
46 * -key:"Value String"
47 * key:Value
48 * or
49 * Value
50 ([+-]?) # Required or Prohibited (optional)
51 (\w+:)? # Key (optional)
52 ( # Query Text
53 (\"([^\"]*)\"?)# quoted
54 | # or
55 ([^\s\"]+) # unquoted
59 string Pattern = "(?<pm>[+-]?) ( (?<key>\\w+) :)? ( (\"(?<quote>[^\"]*)\"?) | (?<unquote>[^\\s\"]+) )";
61 Regex r = new Regex (Pattern, RegexOptions.IgnorePatternWhitespace);
63 Match m = r.Match (query_string);
65 ArrayList parts;
66 parts = new ArrayList ();
68 ArrayList or_list = null;
70 while (m.Success) {
72 QueryPart query_part = MatchToQueryPart (m);
74 if (or_list != null) {
75 or_list.Add (query_part);
76 query_part = null;
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.
85 if ( next.Success
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);
94 m = next.NextMatch();
95 continue;
98 // If we have a non-empty or-list going,
99 // Create the appropriate QueryPart and add it
100 // to the list.
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);
109 parts.Add (or_part);
110 or_list = null;
113 // Add the next text part
114 if (query_part != null)
115 parts.Add (query_part);
117 m=next;
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);
130 return parts;
133 static private QueryPart StringToQueryPart (string text, bool is_prohibited)
135 QueryPart part;
137 if (text.IndexOf ('*') != -1) {
138 part = new QueryPart_Wildcard ();
139 ((QueryPart_Wildcard) part).QueryString = text;
140 } else {
141 part = new QueryPart_Text ();
142 ((QueryPart_Text) part).Text = text;
145 part.Logic = (is_prohibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);
146 return part;
149 static private QueryPart MatchToQueryPart (Match m)
151 // Looping over all Matches we have got:
152 // m.Groups["pm"] plus or minus sign
153 // m.Groups["key"] keyname
154 // m.Groups["quote"] quoted string
155 // m.Groups["unquote"] unquoted string
157 string query = m.ToString ();
158 string unquote = m.Groups["unquote"].ToString ();
159 string text = m.Groups ["quote"].ToString () + m.Groups ["unquote"].ToString (); // only one of both is set.
160 string key = m.Groups ["key"].ToString ();
162 bool IsProhibited = (m.Groups ["pm"].ToString () == "-");
165 // check for file extensions
166 // if match starts with *. or . and only contains letters we assume it's a file extension
167 Regex extension_re = new Regex (@"^\**\.\w*$");
169 if (extension_re.Match (text).Success || key.ToLower () == "ext" || key.ToLower () == "extension") {
171 QueryPart_Property query_part = new QueryPart_Property ();
173 query_part.Key = Property.FilenameExtensionPropKey;
175 if (text.StartsWith ("*."))
176 query_part.Value = text.Substring (1).ToLower ();
177 else if (text.StartsWith ("."))
178 query_part.Value = text.ToLower ();
179 else
180 query_part.Value = "." + text.ToLower ();
182 query_part.Type = PropertyType.Keyword;
183 query_part.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);
185 Logger.Log.Debug ("Extension query: {0}", query_part.Value);
187 return query_part;
190 if (key == "") {
192 Logger.Log.Debug ("Parsed query '{0}' as text_query", text);
194 return StringToQueryPart (text, IsProhibited);
197 string prop_string = null;
198 bool is_present;
199 PropertyType prop_type;
201 is_present = PropertyKeywordFu.GetPropertyDetails (key, out prop_string, out prop_type);
202 // if key is not present in the mapping, assume the query is a text query
203 // i.e. if token is foo:bar and there is no mappable property named foo,
204 // assume "foo:bar" as text query
205 // FIXME the analyzer changes the text query "foo:bar" to "foo bar"
206 // which might not be the right thing to do
208 if (!is_present) {
210 Logger.Log.Debug ("Could not find property, parsed query '{0}' as text_query", query);
212 return StringToQueryPart (query, IsProhibited);
215 QueryPart_Property query_part_prop = new QueryPart_Property ();
216 query_part_prop.Key = prop_string;
217 query_part_prop.Value = text;
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;