Beispiel #1
0
        static private QueryPart StringToQueryPart(string text, bool is_prohibited)
        {
            QueryPart part;

            if (text.IndexOf('*') != -1)
            {
                part = new QueryPart_Wildcard();
                ((QueryPart_Wildcard)part).QueryString = text;
            }
            else
            {
                part = new QueryPart_Text();
                ((QueryPart_Text)part).Text = text;
            }

            part.Logic = (is_prohibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);
            return(part);
        }
Beispiel #2
0
        static private QueryPart MatchToQueryPart(Match m)
        {
            // Looping over all Matches we have got:
            // m.Groups["pm"]       plus or minus sign
            // m.Groups["key"]      keyname
            // m.Groups["quote"]    quoted string
            // m.Groups["midquote1"] + m.Groups["midquote2"] quoted midway string also represents unquoted string

            string query = m.ToString();
            // Either quote is set or midquote1 and (optionally) midquote2 is set
            string text = m.Groups ["quote"].ToString() + m.Groups ["midquote1"].ToString() + m.Groups ["midquote2"].ToString();
            string key  = m.Groups ["key"].ToString();

            bool IsProhibited = (m.Groups ["pm"].ToString() == "-");


            // check for file extensions
            // if match starts with *. or . and only contains letters we assume it's a file extension
            if (extension_re.Match(text).Success || key.ToLower() == "ext" || key.ToLower() == "extension")
            {
                QueryPart_Property query_part = new QueryPart_Property();

                query_part.Key = Property.FilenameExtensionPropKey;

                if (text.StartsWith("*."))
                {
                    query_part.Value = text.Substring(1).ToLower();
                }
                else if (text.StartsWith("."))
                {
                    query_part.Value = text.ToLower();
                }
                else
                {
                    query_part.Value = "." + text.ToLower();
                }

                query_part.Type  = PropertyType.Keyword;
                query_part.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);

                Logger.Log.Debug("Extension query: {0}", query_part.Value);

                return(query_part);
            }

            if (key == String.Empty)
            {
                Logger.Log.Debug("Parsed query '{0}' as text_query", text);

                return(StringToQueryPart(text, IsProhibited));
            }

            // FIXME: i18n-izing "date"
            if (key == "date")
            {
                try {
                    QueryPart part = DateQueryToQueryPart(text);
                    part.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);
                    return(part);
                } catch (FormatException) {
                    Log.Warn("Could not parse [{0}] as date query. Assuming text.", text);
                    return(StringToQueryPart(text, IsProhibited));
                }
            }

            // FIXME: i18n-izing "uri"
            if (key == "uri")
            {
                try {
                    QueryPart_Uri part = new QueryPart_Uri();
                    part.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);
                    part.Uri   = UriFu.UserUritoEscapedUri(text);
                    return(part);
                } catch (System.UriFormatException) {
                    Log.Warn("Could not parse [{0}] as uri query. Assuming text.", text);
                    return(StringToQueryPart(text, IsProhibited));
                }
            }

            // Special case
            if (key == "inuri")
            {
                QueryPart_Property inuri_part = new QueryPart_Property();
                inuri_part.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);
                inuri_part.Key   = "inuri";
                inuri_part.Value = text;
                inuri_part.Type  = PropertyType.Keyword;
                Log.Debug("Handing special query 'inuri:{0}'", text);
                return(inuri_part);
            }

            // Non-keyword queries by directly using property names
            // Query of form property:namespace:name=value
            // which is translated to a non-keyword query
            // namespace:name=value
            int pos;

            if (key == "property" && ((pos = text.IndexOf('=')) != -1))
            {
                QueryPart_Property part = new QueryPart_Property();
                part.Key   = text.Substring(0, pos);
                part.Value = text.Substring(pos + 1);
                part.Type  = PropertyType.Text;
                part.Logic = (IsProhibited ?      QueryPartLogic.Prohibited : QueryPartLogic.Required);
                Logger.Log.Debug("Parsed query '" + query +
                                 "' as prop query:key=" + part.Key +
                                 ", value=" + part.Value +
                                 " and property type=" + part.Type);

                return(part);
            }

            // keyword queries by directly using property names
            // Query of form keyword:namespace:name=value
            // which is translated to a keyword query
            // namespace:name=value
            if (key == "keyword" && ((pos = text.IndexOf('=')) != -1))
            {
                QueryPart_Property part = new QueryPart_Property();
                part.Key   = text.Substring(0, pos);
                part.Value = text.Substring(pos + 1);
                part.Type  = PropertyType.Keyword;
                part.Logic = (IsProhibited ?      QueryPartLogic.Prohibited : QueryPartLogic.Required);
                Logger.Log.Debug("Parsed query '" + query +
                                 "' as prop query:key=" + part.Key +
                                 ", value=" + part.Value +
                                 " and property type=" + part.Type);

                return(part);
            }

            if ((pos = text.IndexOf('*')) >= 0)
            {
                QueryPart_Wildcard wild = new QueryPart_Wildcard();
                wild.QueryString  = text;
                wild.PropertyOnly = true;
                return(wild);
            }

            string[] prop_string = null;
            bool     is_present;

            PropertyType[] prop_type;
            int            num;

            is_present = PropertyKeywordFu.GetMapping(key, out num, out prop_string, out prop_type);
            // if key is not present in the mapping, assume the query is a text query
            // i.e. if token is foo:bar and there is no mappable property named foo,
            // assume "foo:bar" as text query
            // FIXME the analyzer changes the text query "foo:bar" to "foo bar"
            // which might not be the right thing to do

            if (!is_present)
            {
                Logger.Log.Warn("Could not find property, parsed query '{0}' as text_query", query);

                return(StringToQueryPart(query, IsProhibited));
            }

            if (num == 1)
            {
                QueryPart_Property query_part_prop = new QueryPart_Property();
                query_part_prop.Key   = prop_string [0];
                query_part_prop.Value = text;
                query_part_prop.Type  = prop_type [0];
                query_part_prop.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);

                Logger.Log.Debug("Parsed query '" + query +
                                 "' as prop query:key=" + query_part_prop.Key +
                                 ", value=" + query_part_prop.Value +
                                 " and property type=" + query_part_prop.Type);

                return(query_part_prop);
            }

            // Multiple property queries are mapped to this keyword query
            // Create an OR query from them
            // FIXME: Would anyone want an AND query ?

            QueryPart_Or query_part_or = new QueryPart_Or();

            query_part_or.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);

            Logger.Log.Debug("Parsed query '{0}' as OR of {1} queries:", query, num);

            for (int i = 0; i < num; ++i)
            {
                QueryPart_Property query_part_prop = new QueryPart_Property();
                query_part_prop.Key   = prop_string [i];
                query_part_prop.Value = text;
                query_part_prop.Type  = prop_type [i];
                query_part_prop.Logic = QueryPartLogic.Required;

                Log.Debug("\t:key={0}, value={1} and property type={2}", query_part_prop.Key, query_part_prop.Value, query_part_prop.Type);
                query_part_or.Add(query_part_prop);
            }

            return(query_part_or);
        }
Beispiel #3
0
                static private QueryPart MatchToQueryPart (Match m)
                {
                        // Looping over all Matches we have got:
                        // m.Groups["pm"]       plus or minus sign
                        // m.Groups["key"]      keyname
                        // m.Groups["quote"]    quoted string
                        // m.Groups["midquote1"] + m.Groups["midquote2"] quoted midway string also represents unquoted string

                        string query = m.ToString ();
                        // Either quote is set or midquote1 and (optionally) midquote2 is set
                        string text = m.Groups ["quote"].ToString () + m.Groups ["midquote1"].ToString () + m.Groups ["midquote2"].ToString ();
                        string key = m.Groups ["key"].ToString ();

                        bool IsProhibited = (m.Groups ["pm"].ToString () == "-");


                        // check for file extensions
                        // if match starts with *. or . and only contains letters we assume it's a file extension
                        if (extension_re.Match (text).Success || key.ToLower () == "ext" || key.ToLower () == "extension") {

                                QueryPart_Property query_part = new QueryPart_Property ();

                                query_part.Key = Property.FilenameExtensionPropKey;

                                if (text.StartsWith ("*."))
                                        query_part.Value = text.Substring (1).ToLower ();
                                else if (text.StartsWith ("."))
                                        query_part.Value = text.ToLower ();
                                else
                                        query_part.Value = "." + text.ToLower ();

                                query_part.Type = PropertyType.Keyword;
                                query_part.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);

                                Logger.Log.Debug ("Extension query: {0}", query_part.Value);

                                return query_part;
                        }

                        if (key == String.Empty) {

                                Logger.Log.Debug ("Parsed query '{0}' as text_query", text);

                                return StringToQueryPart (text, IsProhibited);
                        }

                        // FIXME: i18n-izing "date"
                        if (key == "date") {
                                try {
                                        QueryPart part = DateQueryToQueryPart (text);
                                        part.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);
                                        return part;
                                } catch (FormatException) {
                                        Log.Warn ("Could not parse [{0}] as date query. Assuming text.", text);
                                        return StringToQueryPart (text, IsProhibited);
                                }
                        }

                        // FIXME: i18n-izing "uri"
                        if (key == "uri") {
                                try {
                                        QueryPart_Uri part = new QueryPart_Uri ();
                                        part.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);
                                        part.Uri = UriFu.UserUritoEscapedUri (text);
                                        return part;
                                } catch (System.UriFormatException) {
                                        Log.Warn ("Could not parse [{0}] as uri query. Assuming text.", text);
                                        return StringToQueryPart (text, IsProhibited);
                                }
                        }

                        // Special case
                        if (key == "inuri") {
                                QueryPart_Property inuri_part = new QueryPart_Property ();
                                inuri_part.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);
                                inuri_part.Key = "inuri";
                                inuri_part.Value = text;
                                inuri_part.Type = PropertyType.Keyword;
                                Log.Debug ("Handing special query 'inuri:{0}'", text);
                                return inuri_part;
                        }

                        // Non-keyword queries by directly using property names
                        // Query of form property:namespace:name=value
                        // which is translated to a non-keyword query
                        // namespace:name=value
                        int pos;
                        if (key == "property" && ((pos = text.IndexOf ('=')) != -1)) {
                                QueryPart_Property part = new QueryPart_Property ();
                                part.Key = text.Substring (0, pos);
                                part.Value = text.Substring (pos + 1);
                                part.Type = PropertyType.Text;
                                part.Logic = (IsProhibited ?      QueryPartLogic.Prohibited : QueryPartLogic.Required);
                                Logger.Log.Debug ("Parsed query '"          + query +
                                                  "' as prop query:key="    + part.Key +
                                                  ", value="                + part.Value +
                                                  " and property type="     + part.Type);

                                return part;
                        }

                        // keyword queries by directly using property names
                        // Query of form keyword:namespace:name=value
                        // which is translated to a keyword query
                        // namespace:name=value
                        if (key == "keyword" && ((pos = text.IndexOf ('=')) != -1)) {
                                QueryPart_Property part = new QueryPart_Property ();
                                part.Key = text.Substring (0, pos);
                                part.Value = text.Substring (pos + 1);
                                part.Type = PropertyType.Keyword;
                                part.Logic = (IsProhibited ?      QueryPartLogic.Prohibited : QueryPartLogic.Required);
                                Logger.Log.Debug ("Parsed query '"          + query +
                                                  "' as prop query:key="    + part.Key +
                                                  ", value="                + part.Value +
                                                  " and property type="     + part.Type);

                                return part;
                        }

                        if ((pos = text.IndexOf('*')) >= 0) {
                            QueryPart_Wildcard wild = new QueryPart_Wildcard();
                            wild.QueryString = text;
                            wild.PropertyOnly = true;
                            return wild;
                        }

                        string[] prop_string = null;
                        bool is_present;
                        PropertyType[] prop_type;
                        int num;

                        is_present = PropertyKeywordFu.GetMapping (key, out num, out prop_string, out prop_type);
                        // if key is not present in the mapping, assume the query is a text query
                        // i.e. if token is foo:bar and there is no mappable property named foo,
                        // assume "foo:bar" as text query
                        // FIXME the analyzer changes the text query "foo:bar" to "foo bar"
                        // which might not be the right thing to do

                        if (!is_present) {

                                Logger.Log.Warn ("Could not find property, parsed query '{0}' as text_query", query);

                                return StringToQueryPart (query, IsProhibited);
                        }

                        if (num == 1) {
                                QueryPart_Property query_part_prop = new QueryPart_Property ();
                                query_part_prop.Key = prop_string [0];
                                query_part_prop.Value = text;
                                query_part_prop.Type = prop_type [0];
                                query_part_prop.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);

                                Logger.Log.Debug ("Parsed query '"          + query +
                                                  "' as prop query:key="    + query_part_prop.Key +
                                                  ", value="                + query_part_prop.Value +
                                                  " and property type="     + query_part_prop.Type);

                                return query_part_prop;
                        }

                        // Multiple property queries are mapped to this keyword query
                        // Create an OR query from them
                        // FIXME: Would anyone want an AND query ?

                        QueryPart_Or query_part_or = new QueryPart_Or ();
                        query_part_or.Logic = (IsProhibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);

                        Logger.Log.Debug ("Parsed query '{0}' as OR of {1} queries:", query, num);

                        for (int i = 0; i < num; ++i) {
                                QueryPart_Property query_part_prop = new QueryPart_Property ();
                                query_part_prop.Key = prop_string [i];
                                query_part_prop.Value = text;
                                query_part_prop.Type = prop_type [i];
                                query_part_prop.Logic = QueryPartLogic.Required;

                                Log.Debug ("\t:key={0}, value={1} and property type={2}", query_part_prop.Key, query_part_prop.Value, query_part_prop.Type);
                                query_part_or.Add (query_part_prop);
                        }

                        return query_part_or;
                }
Beispiel #4
0
                static private QueryPart StringToQueryPart (string text, bool is_prohibited)
                {
                        QueryPart part;

                        if (text.IndexOf ('*') != -1) {
                                part = new QueryPart_Wildcard ();
                                ((QueryPart_Wildcard) part).QueryString = text;
                        } else {
                                part = new QueryPart_Text ();
                                ((QueryPart_Text) part).Text = text;
                        }

                        part.Logic = (is_prohibited ? QueryPartLogic.Prohibited : QueryPartLogic.Required);
                        return part;
                }