Example #1
0
        private static Criteria GetCriteriaLeaf(CriteriaExpression criteriaExpression)
        {
            string propName       = criteriaExpression.Left.Expression;
            string operatorString = criteriaExpression.Expression;
            object value          = criteriaExpression.Right.Expression;

            Criteria.ComparisonOp comparisonOp = CreateComparisonOperator(operatorString);
            if ((comparisonOp == Criteria.ComparisonOp.In || comparisonOp == Criteria.ComparisonOp.NotIn) && value is string)
            {
                string inValuesString = value.ToString().TrimStart('(').TrimEnd(')');
                HabaneroStringBuilder valueStringBuilder = new HabaneroStringBuilder(inValuesString);
                valueStringBuilder.RemoveQuotedSections();
                List <string> finalStrings = new List <string>();
                int           commaIndex;
                int           lastIndex = 0;
                commaIndex = valueStringBuilder.IndexOf(",");
                while (commaIndex != -1)
                {
                    HabaneroStringBuilder oneValueSubstring = valueStringBuilder.Substring(lastIndex, commaIndex - lastIndex);
                    finalStrings.Add(oneValueSubstring.PutBackQuotedSections().ToString().Trim());
                    lastIndex  = commaIndex + 1;
                    commaIndex = valueStringBuilder.IndexOf(",", lastIndex);
                }
                HabaneroStringBuilder oneValueString = valueStringBuilder.Substring(lastIndex, valueStringBuilder.ToString().Length - lastIndex);
                finalStrings.Add(oneValueString.PutBackQuotedSections().ToString().Trim());
                value = new Criteria.CriteriaValues(finalStrings);
            }
            return(new Criteria(propName, comparisonOp, value));
        }
 public void TestRemoveQuotedSections_MixedQuotes()
 {
     HabaneroStringBuilder s = new HabaneroStringBuilder(
         "A quoted \"test\" is needed to 'test' this functionality");
     s.RemoveQuotedSections();
     Assert.AreEqual("A quoted  is needed to  this functionality", s.ToString());
 }
Example #3
0
 private static Criteria GetCriteriaLeaf(CriteriaExpression criteriaExpression)
 {
     string propName = criteriaExpression.Left.Expression;
     string operatorString = criteriaExpression.Expression;
     object value = criteriaExpression.Right.Expression;
     Criteria.ComparisonOp comparisonOp = CreateComparisonOperator(operatorString);
     if ((comparisonOp == Criteria.ComparisonOp.In || comparisonOp == Criteria.ComparisonOp.NotIn) && value is string)
     {
         string inValuesString = value.ToString().TrimStart('(').TrimEnd(')');
         HabaneroStringBuilder valueStringBuilder = new HabaneroStringBuilder(inValuesString);
         valueStringBuilder.RemoveQuotedSections();
         List<string> finalStrings = new List<string>();
         int commaIndex;
         int lastIndex = 0;
         commaIndex = valueStringBuilder.IndexOf(",");
         while (commaIndex != -1) {
             HabaneroStringBuilder oneValueSubstring = valueStringBuilder.Substring(lastIndex, commaIndex-lastIndex);
             finalStrings.Add(oneValueSubstring.PutBackQuotedSections().ToString().Trim());
             lastIndex = commaIndex+1;
             commaIndex = valueStringBuilder.IndexOf(",", lastIndex);
         }
         HabaneroStringBuilder oneValueString = valueStringBuilder.Substring(lastIndex, valueStringBuilder.ToString().Length - lastIndex);
         finalStrings.Add(oneValueString.PutBackQuotedSections().ToString().Trim());
         value = new Criteria.CriteriaValues(finalStrings);
     }
     return new Criteria(propName, comparisonOp, value);
 }
        /// <summary>
        /// Returns a sub-string of the string, beginning at the start index
        /// </summary>
        /// <param name="startIndex">The start index to begin from</param>
        /// <returns>Returns a HabaneroStringBuilder object</returns>
        public HabaneroStringBuilder Substring(int startIndex)
        {
            HabaneroStringBuilder newHabaneroStringBuilder = new HabaneroStringBuilder(_string.ToString().Substring(startIndex));

            if (_quotedSections != null)
            {
                foreach (QuotedSection quote in _quotedSections)
                {
                    if (quote.pos >= startIndex)
                    {
                        newHabaneroStringBuilder._quotedSections.Add(new QuotedSection(quote.pos - startIndex, quote.quotedSection));
                    }
                }
            }
            return(newHabaneroStringBuilder);
        }
Example #5
0
        /// <summary>
        /// Loads all the values for the current line and returns them in a list
        /// </summary>
        /// <returns>Returns a list of values</returns>
        public List<string> GetValues()
        {
            HabaneroStringBuilder stringBuilder = new HabaneroStringBuilder(_currentLine.Replace(",\"\",", ",,"));
            stringBuilder.SetQuotes(new string[] { "\"" });
            stringBuilder.RemoveQuotedSections();
            if (stringBuilder.IndexOf("\"") > -1)
            {
                string nextLine = _reader.ReadLine();
                if (nextLine == null)
                {
                    nextLine = "";
                    throw new UserException("Unclosed quote in CSV file, line " + _lineNo);
                }
                _currentLine = _currentLine + nextLine;

                _lineNo++;
                return GetValues();
            }
            List<string> values = new List<string>();
            
            int commaPos = 0;
            int pos = 0;
            int endPos = 0;
            do
            {
                commaPos = stringBuilder.IndexOf(",", pos);
                if (commaPos == -1)
                {
                    endPos = stringBuilder.ToString().Length;
                }
                else
                {
                    endPos = commaPos;
                }
                string value = stringBuilder.Substring(pos, endPos - pos).PutBackQuotedSections().ToString().Trim();
                if ((value.StartsWith("\"") && value.EndsWith("\"")) || (value.StartsWith("'") && value.EndsWith("'")))
                {
                    value = value.Substring(1, value.Length - 2);
                }
                values.Add(value);
                pos = commaPos + 1;
            } while (commaPos != -1);
            return values;
        }
 public void TestRemoveQuotedSections_QuotedQuotes()
 {
     HabaneroStringBuilder s = new HabaneroStringBuilder("Description = 'Mark''s Car'");
     s.RemoveQuotedSections();
     Assert.AreEqual("Description = ", s.ToString());
 }
 public void TestRemoveQuotedSections_EmptyQuotes()
 {
     HabaneroStringBuilder s = new HabaneroStringBuilder("testProp = ''");
     s.RemoveQuotedSections();
     Assert.AreEqual("testProp = ", s.ToString());
 }
 public void TestRemoveQuotedSections_SimpleCriteria()
 {
     HabaneroStringBuilder s = new HabaneroStringBuilder("Name = 'Peter'");
     s.RemoveQuotedSections();
     Assert.AreEqual("Name = ", s.ToString());
 }
        public void TestDoubleQuotes()
        {
            HabaneroStringBuilder s = new HabaneroStringBuilder("Hi There Peter, what''s going on");
            Assert.AreEqual("Hi There Peter, what's going on", s.RemoveQuotedSections().ToString());
            Assert.AreEqual("Hi There Peter, what's going on", s.PutBackQuotedSections().ToString());

            s = new HabaneroStringBuilder("Hi There Peter, 'what''s going on'");
            Assert.AreEqual("Hi There Peter, ", s.RemoveQuotedSections().ToString());
            Assert.AreEqual("Hi There Peter, 'what's going on'", s.PutBackQuotedSections().ToString());

            s = new HabaneroStringBuilder("Installation,Pipeclamps,'MP HI 1/4''',4.04");
            Assert.AreEqual("Installation,Pipeclamps,,4.04", s.RemoveQuotedSections().ToString());
            Assert.AreEqual("Installation,Pipeclamps,'MP HI 1/4'',4.04", s.PutBackQuotedSections().ToString());
        }
 public void TestDropOuterQuotes()
 {
     HabaneroStringBuilder s = new HabaneroStringBuilder("'Test'");
     Assert.AreEqual("Test", s.DropOuterQuotes().ToString());
 }
 public void TestIndexOf()
 {
     HabaneroStringBuilder s = new HabaneroStringBuilder("Hello");
     Assert.AreEqual(2, s.IndexOf("l"));
     s = new HabaneroStringBuilder("This is a 'test'");
     s.RemoveQuotedSections();
     Assert.AreEqual(-1, s.IndexOf("e"));
 }
 public void TestRemoveAndPutBackQuotedSectionsAdvanced()
 {
     HabaneroStringBuilder s = new HabaneroStringBuilder(
         "Peter''s car''s engine said: 'That''s Mark''s Car' and 'That''s Eric''s car'.");
     s.RemoveQuotedSections();
     Assert.AreEqual("Peter's car's engine said:  and .", s.ToString());
     s.PutBackQuotedSections();
     Assert.AreEqual("Peter's car's engine said: 'That's Mark's Car' and 'That's Eric's car'.", s.ToString());
 }
 public void TestRemoveAndPutBackQuotedSections()
 {
     HabaneroStringBuilder s = new HabaneroStringBuilder("A quoted 'test' is needed to test this functionality");
     s.RemoveQuotedSections();
     Assert.AreEqual("A quoted  is needed to test this functionality", s.ToString());
     s.PutBackQuotedSections();
     Assert.AreEqual("A quoted 'test' is needed to test this functionality", s.ToString());
 }
Example #14
0
        /// <summary>
        /// Parses the expression into a linked list of expression objects
        /// </summary>
        /// <param name="expression">The expression to parse</param>
        private void parseExpression(HabaneroStringBuilder expression)
        {
            HabaneroStringBuilder quotesRemovedExpression = expression;
            //Remove any sections sorounded by opening and closing ' (single quote)
            quotesRemovedExpression.RemoveQuotedSections();

            //Upper case such that the operator or Or and OR etc are identical.
            String expressionWithoutQuotes = quotesRemovedExpression.ToString().ToUpper();
            //Check if the first character is an opening bracket if it is then find the closing bracket
            // and set this as the left expression.
            if (expressionWithoutQuotes.IndexOf("(") == 0)
            {
                int bracketCount = 1;
                int bracketSearchPos = 0;
                while ((bracketCount > 0) && (bracketSearchPos < expressionWithoutQuotes.Length - 1))
                {
                    bracketSearchPos++;
                    switch (expressionWithoutQuotes[bracketSearchPos])
                    {
                        case '(':
                            bracketCount++;
                            break;
                        case ')':
                            bracketCount--;
                            break;
                    }
                }
                if (bracketSearchPos == expressionWithoutQuotes.Length - 1)
                {
                    parseExpression(
                        quotesRemovedExpression.Substring(1, expressionWithoutQuotes.Length - 2).PutBackQuotedSections());
                    return;
                }
                _left = new CriteriaExpression(quotesRemovedExpression.Substring(1, bracketSearchPos - 1)
                    .PutBackQuotedSections().ToString().Trim(), _operators);
                int pos = -1;
                string foundOperator = "";
                foreach (String op in _operators)
                {
                    int thisPos = expressionWithoutQuotes.IndexOf(op, bracketSearchPos);
                    if ((thisPos == -1 || thisPos >= pos) && pos != -1) continue;
                    pos = thisPos;
                    foundOperator = op;
                }
                if (pos != -1)
                {
                    _right = new CriteriaExpression(quotesRemovedExpression.Substring(pos + foundOperator.Length)
                        .PutBackQuotedSections().ToString().Trim(), _operators);
                    _expression = foundOperator;
                }
            }
            else
            {
                foreach (String op in _operators)
                {
                    int pos = expressionWithoutQuotes.IndexOf(op);
                    if (pos == -1 || IsPosInsideBrackets(expressionWithoutQuotes, pos)) continue;
                    _left = new CriteriaExpression(quotesRemovedExpression.Substring(0, pos)
                                                       .PutBackQuotedSections().ToString().Trim(), _operators);
                    if (op.Trim() == "IN" || op.Trim() == "NOT IN")
                    {
                        var criteriaExpression = new CriteriaExpression("");
                        criteriaExpression._expression = quotesRemovedExpression.Substring(pos + op.Length)
                            .PutBackQuotedSections().ToString().Trim();
                        _right = criteriaExpression;
                    }
                    else
                    {
                        _right = new CriteriaExpression(quotesRemovedExpression.Substring(pos + op.Length)
                                                            .PutBackQuotedSections().ToString().Trim(), _operators);
                    }
                    _expression = op;
                    break;
                }
            }
            //If this was a terminal criteria i.e. it has no more children then
            // this is the expression there will be no right and left expression.
            if (string.IsNullOrEmpty(_expression))
            {
                _expression = quotesRemovedExpression.PutBackQuotedSections().DropOuterQuotes().ToString();
            }
        }
 /// <summary>
 /// Returns a sub-string of the string, beginning at the start index
 /// </summary>
 /// <param name="startIndex">The start index to begin from</param>
 /// <returns>Returns a HabaneroStringBuilder object</returns>
 public HabaneroStringBuilder Substring(int startIndex)
 {
     HabaneroStringBuilder newHabaneroStringBuilder = new HabaneroStringBuilder(_string.ToString().Substring(startIndex));
     if (_quotedSections != null)
     {
         foreach (QuotedSection quote in _quotedSections)
         {
             if (quote.pos >= startIndex)
             {
                 newHabaneroStringBuilder._quotedSections.Add(new QuotedSection(quote.pos - startIndex, quote.quotedSection));
             }
         }
     }
     return newHabaneroStringBuilder;
 }
Example #16
0
        /// <summary>
        /// Parses the expression into a linked list of expression objects
        /// </summary>
        /// <param name="expression">The expression to parse</param>
        private void parseExpression(HabaneroStringBuilder expression)
        {
            HabaneroStringBuilder quotesRemovedExpression = expression;

            //Remove any sections sorounded by opening and closing ' (single quote)
            quotesRemovedExpression.RemoveQuotedSections();

            //Upper case such that the operator or Or and OR etc are identical.
            String expressionWithoutQuotes = quotesRemovedExpression.ToString().ToUpper();

            //Check if the first character is an opening bracket if it is then find the closing bracket
            // and set this as the left expression.
            if (expressionWithoutQuotes.IndexOf("(") == 0)
            {
                int bracketCount     = 1;
                int bracketSearchPos = 0;
                while ((bracketCount > 0) && (bracketSearchPos < expressionWithoutQuotes.Length - 1))
                {
                    bracketSearchPos++;
                    switch (expressionWithoutQuotes[bracketSearchPos])
                    {
                    case '(':
                        bracketCount++;
                        break;

                    case ')':
                        bracketCount--;
                        break;
                    }
                }
                if (bracketSearchPos == expressionWithoutQuotes.Length - 1)
                {
                    parseExpression(
                        quotesRemovedExpression.Substring(1, expressionWithoutQuotes.Length - 2).PutBackQuotedSections());
                    return;
                }
                _left = new CriteriaExpression(quotesRemovedExpression.Substring(1, bracketSearchPos - 1)
                                               .PutBackQuotedSections().ToString().Trim(), _operators);
                int    pos           = -1;
                string foundOperator = "";
                foreach (String op in _operators)
                {
                    int thisPos = expressionWithoutQuotes.IndexOf(op, bracketSearchPos);
                    if ((thisPos == -1 || thisPos >= pos) && pos != -1)
                    {
                        continue;
                    }
                    pos           = thisPos;
                    foundOperator = op;
                }
                if (pos != -1)
                {
                    _right = new CriteriaExpression(quotesRemovedExpression.Substring(pos + foundOperator.Length)
                                                    .PutBackQuotedSections().ToString().Trim(), _operators);
                    _expression = foundOperator;
                }
            }
            else
            {
                foreach (String op in _operators)
                {
                    int pos = expressionWithoutQuotes.IndexOf(op);
                    if (pos == -1 || IsPosInsideBrackets(expressionWithoutQuotes, pos))
                    {
                        continue;
                    }
                    _left = new CriteriaExpression(quotesRemovedExpression.Substring(0, pos)
                                                   .PutBackQuotedSections().ToString().Trim(), _operators);
                    if (op.Trim() == "IN" || op.Trim() == "NOT IN")
                    {
                        var criteriaExpression = new CriteriaExpression("");
                        criteriaExpression._expression = quotesRemovedExpression.Substring(pos + op.Length)
                                                         .PutBackQuotedSections().ToString().Trim();
                        _right = criteriaExpression;
                    }
                    else
                    {
                        _right = new CriteriaExpression(quotesRemovedExpression.Substring(pos + op.Length)
                                                        .PutBackQuotedSections().ToString().Trim(), _operators);
                    }
                    _expression = op;
                    break;
                }
            }
            //If this was a terminal criteria i.e. it has no more children then
            // this is the expression there will be no right and left expression.
            if (string.IsNullOrEmpty(_expression))
            {
                _expression = quotesRemovedExpression.PutBackQuotedSections().DropOuterQuotes().ToString();
            }
        }