Ejemplo n.º 1
0
        private static decimal?ParseDecimal(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(null);
            }
            int totalDigits, fractionDigits;

            if (!DecimalSimpleTypeExecutor.Check(value, out totalDigits, out fractionDigits))
            {
                throw new InvalidOperationException(string.Format("Unable to parse decimal from '{0}'", value));
            }
            decimal result;

            if (!decimal.TryParse(value, decimalStyle, CultureInfo.InvariantCulture, out result))
            {
                throw new InvalidOperationException(string.Format("Integer value '{0}' is too large for a decimal", value));
            }
            return(result);
        }
Ejemplo n.º 2
0
        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);
        }