示例#1
0
        public static bool TryParse([NotNull] string value, DateTimeTypeCode typeCode, out XsdDateTimeWrapper result)
        {
            object xsdDateTime;

            if (tryParseDelegate(value, typeCode, out xsdDateTime))
            {
                result = new XsdDateTimeWrapper(xsdDateTime);
                return(true);
            }
            result = null;
            return(false);
        }
        private static XsdDateTimeWrapper ParseDate(string value, DateTimeTypeCode typeCode)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(null);
            }
            XsdDateTimeWrapper result;

            if (!XsdDateTimeWrapper.TryParse(value, typeCode, out result))
            {
                throw new InvalidOperationException(string.Format("Unable to parse XsdDateTime from '{0}'", value));
            }
            return(result);
        }
        protected override SchemaAutomatonError ExecuteInternal([CanBeNull] string value, [NotNull] string nodeType, [NotNull] string name, int lineNumber, int linePosition)
        {
            string preparedValue = value;

            if (prepareValue != null && value != null)
            {
                preparedValue = prepareValue(value);
            }
            preparedValue = preparedValue ?? "";
            if (regexes != null && regexes.Length > 0)
            {
                if (!regexes.Any(t => t.IsMatch(preparedValue)))
                {
                    return(new SchemaAutomatonError.SchemaAutomatonError9(nodeType, name, value, "pattern", patternDescription, lineNumber, linePosition));
                }
            }
            if (typeCode == DateTimeTypeCode.None)
            {
                if (isNumber)
                {
                    if (string.IsNullOrEmpty(preparedValue))
                    {
                        throw new InvalidOperationException();
                    }
                    var     parsed        = false;
                    decimal parsedDecimal = 0;
                    // Number
                    if (maxInclusiveDecimal != null)
                    {
                        parsedDecimal = decimal.Parse(preparedValue, decimalStyle, CultureInfo.InvariantCulture);
                        parsed        = true;
                        if (parsedDecimal.CompareTo(maxInclusiveDecimal.Value) > 0)
                        {
                            return(new SchemaAutomatonError.SchemaAutomatonError9(nodeType, name, value, "maxInclusive", schemaSimpleType.Restriction.MaxInclusive, lineNumber, linePosition));
                        }
                    }
                    if (maxExclusiveDecimal != null)
                    {
                        if (!parsed)
                        {
                            parsedDecimal = decimal.Parse(preparedValue, decimalStyle, CultureInfo.InvariantCulture);
                            parsed        = true;
                        }
                        if (parsedDecimal.CompareTo(maxExclusiveDecimal.Value) >= 0)
                        {
                            return(new SchemaAutomatonError.SchemaAutomatonError9(nodeType, name, value, "maxExclusive", schemaSimpleType.Restriction.MaxExclusive, lineNumber, linePosition));
                        }
                    }
                    if (minInclusiveDecimal != null)
                    {
                        if (!parsed)
                        {
                            parsedDecimal = decimal.Parse(preparedValue, decimalStyle, CultureInfo.InvariantCulture);
                            parsed        = true;
                        }
                        if (parsedDecimal.CompareTo(minInclusiveDecimal.Value) < 0)
                        {
                            return(new SchemaAutomatonError.SchemaAutomatonError9(nodeType, name, value, "minInclusive", schemaSimpleType.Restriction.MinInclusive, lineNumber, linePosition));
                        }
                    }
                    if (minExclusiveDecimal != null)
                    {
                        if (!parsed)
                        {
                            parsedDecimal = decimal.Parse(preparedValue, decimalStyle, CultureInfo.InvariantCulture);
                            parsed        = true;
                        }
                        if (parsedDecimal.CompareTo(minExclusiveDecimal.Value) <= 0)
                        {
                            return(new SchemaAutomatonError.SchemaAutomatonError9(nodeType, name, value, "minExclusive", schemaSimpleType.Restriction.MinExclusive, lineNumber, linePosition));
                        }
                    }
                    if (totalDigits != null)
                    {
                        int actualTotalDigits, actualFractionDigits;
                        DecimalSimpleTypeExecutor.Check(preparedValue, out actualTotalDigits, out actualFractionDigits);
                        if (actualTotalDigits > totalDigits.Value)
                        {
                            return(new SchemaAutomatonError.SchemaAutomatonError9(nodeType, name, value, "totalDigits", totalDigits.Value, lineNumber, linePosition));
                        }
                        if (fractionDigits != null && actualFractionDigits > fractionDigits.Value)
                        {
                            return(new SchemaAutomatonError.SchemaAutomatonError9(nodeType, name, value, "fractionDigits", fractionDigits.Value, lineNumber, linePosition));
                        }
                    }
                    if (decimalValues != null && decimalValues.Count > 0)
                    {
                        if (!parsed)
                        {
                            parsedDecimal = decimal.Parse(preparedValue, decimalStyle, CultureInfo.InvariantCulture);
                            parsed        = true;
                        }
                        if (!decimalValues.Contains(parsedDecimal))
                        {
                            return(new SchemaAutomatonError.SchemaAutomatonError9(nodeType, name, value, "enumeration", schemaSimpleType.Restriction.Values, lineNumber, linePosition));
                        }
                    }
                }
            }
            else
            {
                if (string.IsNullOrEmpty(preparedValue))
                {
                    throw new InvalidOperationException();
                }
                // Date
                XsdDateTimeWrapper parsed = null;
                if (maxInclusiveDate != null)
                {
                    if (!XsdDateTimeWrapper.TryParse(preparedValue, typeCode, out parsed))
                    {
                        throw new InvalidOperationException();
                    }
                    if (parsed.CompareTo(maxInclusiveDate) > 0)
                    {
                        return(new SchemaAutomatonError.SchemaAutomatonError9(nodeType, name, value, "maxInclusive", schemaSimpleType.Restriction.MaxInclusive, lineNumber, linePosition));
                    }
                }
                if (maxExclusiveDate != null)
                {
                    if (parsed == null)
                    {
                        if (!XsdDateTimeWrapper.TryParse(preparedValue, typeCode, out parsed))
                        {
                            throw new InvalidOperationException();
                        }
                    }
                    if (parsed.CompareTo(maxExclusiveDate) >= 0)
                    {
                        return(new SchemaAutomatonError.SchemaAutomatonError9(nodeType, name, value, "maxExclusive", schemaSimpleType.Restriction.MaxExclusive, lineNumber, linePosition));
                    }
                }
                if (minInclusiveDate != null)
                {
                    if (parsed == null)
                    {
                        if (!XsdDateTimeWrapper.TryParse(preparedValue, typeCode, out parsed))
                        {
                            throw new InvalidOperationException();
                        }
                    }
                    if (parsed.CompareTo(minInclusiveDate) < 0)
                    {
                        return(new SchemaAutomatonError.SchemaAutomatonError9(nodeType, name, value, "minInclusive", schemaSimpleType.Restriction.MinInclusive, lineNumber, linePosition));
                    }
                }
                if (minExclusiveDate != null)
                {
                    if (parsed == null)
                    {
                        if (!XsdDateTimeWrapper.TryParse(preparedValue, typeCode, out parsed))
                        {
                            throw new InvalidOperationException();
                        }
                    }
                    if (parsed.CompareTo(minExclusiveDate) <= 0)
                    {
                        return(new SchemaAutomatonError.SchemaAutomatonError9(nodeType, name, value, "minExclusive", schemaSimpleType.Restriction.MinExclusive, lineNumber, linePosition));
                    }
                }
                if (xsdDateTimeValues != null)
                {
                    if (parsed == null)
                    {
                        if (!XsdDateTimeWrapper.TryParse(preparedValue, typeCode, out parsed))
                        {
                            throw new InvalidOperationException();
                        }
                    }
                    if (!xsdDateTimeValues.Any(z => z != null && z.CompareTo(parsed) == 0))
                    {
                        return(new SchemaAutomatonError.SchemaAutomatonError9(nodeType, name, value, "enumeration", schemaSimpleType.Restriction.Values, lineNumber, linePosition));
                    }
                }
            }
            if (typeCode == DateTimeTypeCode.None && !isNumber)
            {
                if (length != null)
                {
                    if (string.IsNullOrEmpty(preparedValue))
                    {
                        return(new SchemaAutomatonError.SchemaAutomatonError16(nodeType, name, lineNumber, linePosition));
                    }
                    if (preparedValue.Length != length.Value)
                    {
                        return(new SchemaAutomatonError.SchemaAutomatonError6(nodeType, name, value, length.Value, lineNumber, linePosition));
                    }
                }
                if (maxLength != null)
                {
                    if (preparedValue.Length > maxLength.Value)
                    {
                        return(new SchemaAutomatonError.SchemaAutomatonError7(nodeType, name, value, maxLength.Value, lineNumber, linePosition));
                    }
                }
                if (minLength != null)
                {
                    if (minLength.Value > 0 && string.IsNullOrEmpty(preparedValue))
                    {
                        return(new SchemaAutomatonError.SchemaAutomatonError16(nodeType, name, lineNumber, linePosition));
                    }
                    if (!string.IsNullOrEmpty(preparedValue) && preparedValue.Length < minLength.Value)
                    {
                        return(new SchemaAutomatonError.SchemaAutomatonError8(nodeType, name, value, minLength.Value, lineNumber, linePosition));
                    }
                }
                if (stringValues != null && stringValues.Count > 0 && !stringValues.Contains(preparedValue))
                {
                    return(new SchemaAutomatonError.SchemaAutomatonError9(nodeType, name, value, "enumeration", schemaSimpleType.Restriction.Values, lineNumber, linePosition));
                }
            }
            return(null);
        }
        public SchemaSimpleTypeExecutor([CanBeNull] ISchemaSimpleTypeExecutor baseExecutor, [NotNull] SchemaSimpleType schemaSimpleType)
            : base(baseExecutor)
        {
            this.schemaSimpleType = schemaSimpleType;
            var restriction = schemaSimpleType.Restriction;

            if (restriction != null)
            {
                length    = restriction.Length;
                minLength = restriction.MinLength;
                maxLength = restriction.MaxLength;
                var atomicBaseType = schemaSimpleType.AtomicBaseType;
                if (ReferenceEquals(atomicBaseType, SchemaSimpleType.Integer) ||
                    ReferenceEquals(atomicBaseType, SchemaSimpleType.Int) ||
                    ReferenceEquals(atomicBaseType, SchemaSimpleType.Decimal))
                {
                    minInclusiveDecimal = ParseDecimal(restriction.MinInclusive);
                    minExclusiveDecimal = ParseDecimal(restriction.MinExclusive);
                    maxInclusiveDecimal = ParseDecimal(restriction.MaxInclusive);
                    maxExclusiveDecimal = ParseDecimal(restriction.MaxExclusive);
                    isNumber            = true;
                }
                if (ReferenceEquals(atomicBaseType, SchemaSimpleType.Date) ||
                    ReferenceEquals(atomicBaseType, SchemaSimpleType.GYear) ||
                    ReferenceEquals(atomicBaseType, SchemaSimpleType.GMonth))
                {
                    if (ReferenceEquals(atomicBaseType, SchemaSimpleType.Date))
                    {
                        typeCode = DateTimeTypeCode.Date;
                    }
                    else if (ReferenceEquals(atomicBaseType, SchemaSimpleType.GYear))
                    {
                        typeCode = DateTimeTypeCode.GYear;
                    }
                    else if (ReferenceEquals(atomicBaseType, SchemaSimpleType.GMonth))
                    {
                        typeCode = DateTimeTypeCode.GMonth;
                    }
                    else
                    {
                        throw new InvalidOperationException();
                    }
                    minInclusiveDate = ParseDate(restriction.MinInclusive, typeCode);
                    minExclusiveDate = ParseDate(restriction.MinExclusive, typeCode);
                    maxInclusiveDate = ParseDate(restriction.MaxInclusive, typeCode);
                    maxExclusiveDate = ParseDate(restriction.MaxExclusive, typeCode);
                }
                totalDigits    = restriction.TotalDigits;
                fractionDigits = restriction.FractionDigits;
                if (fractionDigits != null && totalDigits == null)
                {
                    throw new InvalidOperationException("The 'totalDigits' facet must be specified along with the 'fractionDigits' facet");
                }
                regexes = (restriction.Patterns ?? new string[0]).Select(SchemaRegexParser.Parse).ToArray();
                if (restriction.Patterns != null && restriction.Patterns.Length > 0)
                {
                    if (!string.IsNullOrEmpty(restriction.PatternDescription))
                    {
                        patternDescription = restriction.PatternDescription;
                    }
                    else if (schemaSimpleType.Description != null && schemaSimpleType.Description.Length > 0)
                    {
                        patternDescription = string.Join("; ", schemaSimpleType.Description);
                    }
                    else
                    {
                        patternDescription = GetPatternsDescription(restriction.Patterns);
                    }
                }
                if (!isNumber)
                {
                    if (typeCode == DateTimeTypeCode.None)
                    {
                        stringValues = restriction.Values == null || restriction.Values.Length == 0
                                           ? null
                                           : new HashSet <string>(restriction.Values);
                    }
                    else
                    {
                        xsdDateTimeValues = restriction.Values == null || restriction.Values.Length == 0
                                                ? null
                                                : restriction.Values.Select(s => string.IsNullOrEmpty(s) ? null : XsdDateTimeWrapper.Parse(s, typeCode)).ToList();
                    }
                }
                else
                {
                    decimalValues = restriction.Values == null || restriction.Values.Length == 0
                                        ? null
                                        : restriction.Values.Select(s => decimal.Parse(s, decimalStyle, CultureInfo.InvariantCulture)).ToList();
                }
                switch (restriction.WhiteSpace)
                {
                case SchemaSimpleTypeRestriction.WhiteSpaceEnum.Replace:
                    prepareValue = CDataNormalize;
                    break;

                case SchemaSimpleTypeRestriction.WhiteSpaceEnum.Collapse:
                    prepareValue = NonCDataNormalize;
                    break;
                }
            }
        }
示例#5
0
 public int CompareTo(XsdDateTimeWrapper other)
 {
     return(compareDelegate(xsdDateTime, other.xsdDateTime));
 }