Пример #1
0
        public virtual Filter GetFilter(XmlElement e)
        {
            string field          = DOMUtils.GetAttributeWithInheritanceOrFail(e, "fieldName");
            string lowerTerm      = DOMUtils.GetAttributeOrFail(e, "lowerTerm");
            string upperTerm      = DOMUtils.GetAttributeOrFail(e, "upperTerm");
            bool   lowerInclusive = DOMUtils.GetAttribute(e, "includeLower", true);
            bool   upperInclusive = DOMUtils.GetAttribute(e, "includeUpper", true);
            int    precisionStep  = DOMUtils.GetAttribute(e, "precisionStep", NumericUtils.PRECISION_STEP_DEFAULT);

            string type = DOMUtils.GetAttribute(e, "type", "int");

            try
            {
                Filter filter;
                if (type.Equals("int", StringComparison.OrdinalIgnoreCase))
                {
                    filter = NumericRangeFilter.NewInt32Range(field, precisionStep, Convert
                                                              .ToInt32(lowerTerm, CultureInfo.InvariantCulture), Convert.ToInt32(upperTerm, CultureInfo.InvariantCulture), lowerInclusive,
                                                              upperInclusive);
                }
                else if (type.Equals("long", StringComparison.OrdinalIgnoreCase))
                {
                    filter = NumericRangeFilter.NewInt64Range(field, precisionStep, Convert
                                                              .ToInt64(lowerTerm, CultureInfo.InvariantCulture), Convert.ToInt64(upperTerm, CultureInfo.InvariantCulture), lowerInclusive,
                                                              upperInclusive);
                }
                else if (type.Equals("double", StringComparison.OrdinalIgnoreCase))
                {
                    filter = NumericRangeFilter.NewDoubleRange(field, precisionStep, Convert
                                                               .ToDouble(lowerTerm, CultureInfo.InvariantCulture), Convert.ToDouble(upperTerm, CultureInfo.InvariantCulture), lowerInclusive,
                                                               upperInclusive);
                }
                else if (type.Equals("float", StringComparison.OrdinalIgnoreCase))
                {
                    filter = NumericRangeFilter.NewSingleRange(field, precisionStep, Convert
                                                               .ToSingle(lowerTerm, CultureInfo.InvariantCulture), Convert.ToSingle(upperTerm, CultureInfo.InvariantCulture), lowerInclusive,
                                                               upperInclusive);
                }
                else
                {
                    throw new ParserException("type attribute must be one of: [long, int, double, float]");
                }
                return(filter);
            }
            catch (FormatException nfe)
            {
                if (strictMode)
                {
                    throw new ParserException("Could not parse lowerTerm or upperTerm into a number", nfe);
                }
                return(NO_MATCH_FILTER);
            }
        }