Exemple #1
0
        private void ParseToken(QueryToken token)
        {
            switch (token.ID)
            {
            case TokenID.OpenParen:
                DepthPush();
                break;

            case TokenID.CloseParen:
                DepthPop();
                break;

            case TokenID.Not:
                NodePush(new QueryListNode(Keyword.Not));
                break;

            case TokenID.Or:
            case TokenID.And:
                // Only push a node if the current_parent is not the same as this token
                if (current_parent.Keyword == Keyword.Not ||
                    current_parent.Keyword == (token.ID == TokenID.Or ? Keyword.And : Keyword.Or))
                {
                    var list = new QueryListNode(token.ID == TokenID.Or ? Keyword.Or : Keyword.And);
                    var p    = current_parent.Parent;

                    if (p != null)
                    {
                        current_parent.Parent.RemoveChild(current_parent);
                    }

                    if (current_parent.Keyword == Keyword.Not || current_parent.ChildCount > 1)
                    {
                        list.AddChild(current_parent);
                    }
                    else
                    {
                        list.TakeChildren(current_parent);
                    }

                    current_parent = p;
                    NodePush(list);
                }
                break;

            case TokenID.Term:
                NodePush(QueryTermNode.ParseUserQuery(field_set, token.Term));
                break;
            }
        }
Exemple #2
0
        public static QueryTermNode ParseUserQuery(QueryFieldSet field_set, string token)
        {
            var term = new QueryTermNode();

            // See if the query specifies a field, and if so, pull out the operator as well
            var field_alias = field_set.FindAlias(token);

            if (field_alias != null)
            {
                term.Field = field_set [field_alias];

                foreach (var val in term.Field.CreateQueryValues())
                {
                    term.Value = val;

                    var op_alias = term.Value.OperatorSet.FindAlias(token);
                    if (op_alias != null)
                    {
                        term.Operator = term.Value.OperatorSet [op_alias];
                        var field_separator = token.IndexOf(op_alias);
                        var temp            = token.Substring(field_separator + op_alias.Length);

                        term.Value.ParseUserQuery(temp);

                        if (!term.Value.IsEmpty)
                        {
                            break;
                        }
                    }

                    term.Operator = null;
                    term.Value    = null;
                }
            }

            if (term.Value == null)
            {
                term.Field    = null;
                term.Value    = QueryValue.CreateFromUserQuery(token, term.Field);
                term.Operator = StringQueryValue.Contains;
            }

            return(term);
        }
Exemple #3
0
        private QueryNode Parse(XmlElement node, QueryListNode parent)
        {
            if (node == null)
            {
                return(null);
            }

            QueryListNode list = null;

            //Console.WriteLine ("Parsing node: {0}", node.Name);
            switch (node.Name.ToLower())
            {
            case "and":
                list = new QueryListNode(Keyword.And);
                break;

            case "or":
                list = new QueryListNode(Keyword.Or);
                break;

            case "not":
                list = new QueryListNode(Keyword.Not);
                break;

            default:
                var term = new QueryTermNode();

                // Get the field (if any) that this term applies to
                if (node["field"] != null)
                {
                    term.Field = field_set [node["field"].GetAttribute("name")];
                }

                // Get the value
                term.Value = QueryValue.CreateFromXml(node, term.Field);

                // Get the operator from the term's name
                term.Operator = term.Value.OperatorSet [node.Name];


                if (parent != null)
                {
                    parent.AddChild(term);
                }

                return(term);
            }

            if (list != null)
            {
                if (parent != null)
                {
                    parent.AddChild(list);
                }

                // Recursively parse the children of a QueryListNode
                foreach (XmlNode child in node.ChildNodes)
                {
                    Parse(child as XmlElement, list);
                }
            }

            return(list);
        }