Exemple #1
0
        private void ByAttribute()
        {
            TokenQueue cq  = new TokenQueue(tq.ChompBalanced('[', ']'));         // content queue
            string     key = cq.ConsumeToAny("=", "!=", "^=", "$=", "*=", "~="); // eq, not, start, end, contain, match, (no val)

            if (key.Length == 0)
            {
                throw Failure.EmptyString("key");
            }

            cq.ConsumeWhitespace();

            if (cq.IsEmpty)
            {
                if (key.StartsWith("^", StringComparison.Ordinal))
                {
                    evals.Add(new Evaluator.AttributeStarting(key.Substring(1)));
                }
                else
                {
                    evals.Add(new Evaluator.Attribute(key));
                }
            }
            else
            {
                if (cq.MatchChomp("="))
                {
                    evals.Add(new Evaluator.AttributeWithValue(key, cq.Remainder()));
                }

                else if (cq.MatchChomp("!="))
                {
                    evals.Add(new Evaluator.AttributeWithValueNot(key, cq.Remainder()));
                }

                else if (cq.MatchChomp("^="))
                {
                    evals.Add(new Evaluator.AttributeWithValueStarting(key, cq.Remainder()));
                }

                else if (cq.MatchChomp("$="))
                {
                    evals.Add(new Evaluator.AttributeWithValueEnding(key, cq.Remainder()));
                }

                else if (cq.MatchChomp("*="))
                {
                    evals.Add(new Evaluator.AttributeWithValueContaining(key, cq.Remainder()));
                }

                else if (cq.MatchChomp("~="))
                {
                    evals.Add(new Evaluator.AttributeWithValueMatching(key, new Regex(cq.Remainder())));
                }
                else
                {
                    throw DomFailure.CannotParseAttributeQuery(query, cq.Remainder());
                }
            }
        }
Exemple #2
0
        private void ByAttribute()
        {
            TokenQueue cq  = new TokenQueue(_tq.ChompBalanced('[', ']'));        // content queue
            string     key = cq.ConsumeToAny("=", "!=", "^=", "$=", "*=", "~="); // eq, not, start, end, contain, match, (no val)

            if (string.IsNullOrEmpty(key))
            {
                throw new Exception("key is empty.");
            }

            cq.ConsumeWhitespace();

            if (cq.IsEmpty)
            {
                if (key.StartsWith("^"))
                {
                    _evals.Add(new Evaluator.AttributeStarting(key.Substring(1)));
                }
                else
                {
                    _evals.Add(new Evaluator.Attribute(key));
                }
            }
            else
            {
                if (cq.MatchChomp("="))
                {
                    _evals.Add(new Evaluator.AttributeWithValue(key, cq.Remainder()));
                }
                else if (cq.MatchChomp("!="))
                {
                    _evals.Add(new Evaluator.AttributeWithValueNot(key, cq.Remainder()));
                }
                else if (cq.MatchChomp("^="))
                {
                    _evals.Add(new Evaluator.AttributeWithValueStarting(key, cq.Remainder()));
                }
                else if (cq.MatchChomp("$="))
                {
                    _evals.Add(new Evaluator.AttributeWithValueEnding(key, cq.Remainder()));
                }
                else if (cq.MatchChomp("*="))
                {
                    _evals.Add(new Evaluator.AttributeWithValueContaining(key, cq.Remainder()));
                }
                else if (cq.MatchChomp("~="))
                {
                    _evals.Add(new Evaluator.AttributeWithValueMatching(key, new Regex(cq.Remainder())));
                }
                else
                {
                    throw new Selector.SelectorParseException("Could not parse attribute query '{0}': unexpected token at '{1}'", _query, cq.Remainder());
                }
            }
        }
 /// <summary>Parse the query</summary>
 /// <returns>Evaluator</returns>
 internal virtual Evaluator Parse()
 {
     tq.ConsumeWhitespace();
     if (tq.MatchesAny(combinators))
     {
         // if starts with a combinator, use root as elements
         evals.Add(new StructuralEvaluator.Root());
         Combinator(tq.Consume());
     }
     else
     {
         FindElements();
     }
     while (!tq.IsEmpty())
     {
         // hierarchy and extras
         bool seenWhite = tq.ConsumeWhitespace();
         if (tq.MatchesAny(combinators))
         {
             Combinator(tq.Consume());
         }
         else
         {
             if (seenWhite)
             {
                 Combinator(' ');
             }
             else
             {
                 // E.class, E#id, E[attr] etc. AND
                 FindElements();
             }
         }
     }
     // take next el, #. etc off queue
     if (evals.Count == 1)
     {
         return(evals[0]);
     }
     return(new CombiningEvaluator.And(evals));
 }
Exemple #4
0
        /// <summary>
        /// Parse the query
        /// </summary>
        /// <returns>Evaluator</returns>
        private Evaluator Parse()
        {
            _tq.ConsumeWhitespace();

            if (_tq.MatchesAny(combinators))
            { // if starts with a combinator, use root as elements
                _evals.Add(new StructuralEvaluator.Root());
                Combinator(_tq.Consume());
            }
            else
            {
                FindElements();
            }

            while (!_tq.IsEmpty)
            {
                // hierarchy and extras
                bool seenWhite = _tq.ConsumeWhitespace();

                if (_tq.MatchesAny(combinators))
                { // group or
                    Combinator(_tq.Consume());
                }
                else if (seenWhite)
                {
                    Combinator(' ');
                }
                else
                {                   // E.class, E#id, E[attr] etc. AND
                    FindElements(); // take next el, #. etc off queue
                }
            }

            if (_evals.Count == 1)
            {
                return(_evals[0]);
            }

            return(new CombiningEvaluator.And(_evals));
        }
Exemple #5
0
        private void ByAttribute()
        {
            TokenQueue cq  = new TokenQueue(tq.ChompBalanced('[', ']')); // content queue
            string     key = cq.ConsumeToAny(AttributeEvals);            // eq, not, start, end, contain, match, (no val)

            Validate.NotEmpty(key);
            cq.ConsumeWhitespace();
            if (cq.IsEmpty())
            {
                if (key.StartsWith("^", StringComparison.Ordinal))
                {
                    evals.Add(new Evaluator.AttributeStarting(key.Substring(1))); /*substring*/
                }
                else
                {
                    evals.Add(new Evaluator.Attribute(key));
                }
            }
            else
            {
                if (cq.MatchChomp("="))
                {
                    evals.Add(new Evaluator.AttributeWithValue(key, cq.Remainder()));
                }
                else if (cq.MatchChomp("!="))
                {
                    evals.Add(new Evaluator.AttributeWithValueNot(key, cq.Remainder()));
                }
                else if (cq.MatchChomp("^="))
                {
                    evals.Add(new Evaluator.AttributeWithValueStarting(key, cq.Remainder()));
                }
                else if (cq.MatchChomp("$="))
                {
                    evals.Add(new Evaluator.AttributeWithValueEnding(key, cq.Remainder()));
                }
                else if (cq.MatchChomp("*="))
                {
                    evals.Add(new Evaluator.AttributeWithValueContaining(key, cq.Remainder()));
                }
                else if (cq.MatchChomp("~="))
                {
                    evals.Add(new Evaluator.AttributeWithValueMatching(key, new Regex(cq.Remainder(), RegexOptions.Compiled)));
                }
                else
                {
                    throw new Selector.SelectorParseException("Could not parse attribute query '{0}': unexpected token at '{1}'", query, cq.Remainder());
                }
            }
        }
        private void ByAttribute()
        {
            TokenQueue cq = new TokenQueue(tq.ChompBalanced('[', ']'));
            // content queue
            String key = cq.ConsumeToAny(AttributeEvals);

            // eq, not, start, end, contain, match, (no val)
            Validate.NotEmpty(key);
            cq.ConsumeWhitespace();
            if (cq.IsEmpty())
            {
                if (key.StartsWith("^"))
                {
                    evals.Add(new Evaluator.AttributeStarting(key.Substring(1)));
                }
                else
                {
                    evals.Add(new Evaluator.Attribute(key));
                }
            }
            else
            {
                if (cq.MatchChomp("="))
                {
                    evals.Add(new Evaluator.AttributeWithValue(key, cq.Remainder()));
                }
                else
                {
                    if (cq.MatchChomp("!="))
                    {
                        evals.Add(new Evaluator.AttributeWithValueNot(key, cq.Remainder()));
                    }
                    else
                    {
                        if (cq.MatchChomp("^="))
                        {
                            evals.Add(new Evaluator.AttributeWithValueStarting(key, cq.Remainder()));
                        }
                        else
                        {
                            if (cq.MatchChomp("$="))
                            {
                                evals.Add(new Evaluator.AttributeWithValueEnding(key, cq.Remainder()));
                            }
                            else
                            {
                                if (cq.MatchChomp("*="))
                                {
                                    evals.Add(new Evaluator.AttributeWithValueContaining(key, cq.Remainder()));
                                }
                                else
                                {
                                    if (cq.MatchChomp("~="))
                                    {
                                        evals.Add(new Evaluator.AttributeWithValueMatching(key, iText.IO.Util.StringUtil.RegexCompile(cq.Remainder
                                                                                                                                          ())));
                                    }
                                    else
                                    {
                                        throw new Selector.SelectorParseException("Could not parse attribute query " + PortUtil.EscapedSingleBracket
                                                                                  + "{0}" + PortUtil.EscapedSingleBracket + ": unexpected token at " + PortUtil.EscapedSingleBracket +
                                                                                  "{1}" + PortUtil.EscapedSingleBracket, query, cq.Remainder());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }