コード例 #1
0
ファイル: SchemaParser.cs プロジェクト: homuroll/GroboXsd
        private static SchemaSimpleType ParseRestriction([NotNull] XmlElement element, [NotNull] Context context, [NotNull] string name,
                                                         [CanBeNull] string[] description)
        {
            var baseTypeName = element.GetAttribute("base");
            var baseType     = string.IsNullOrEmpty(baseTypeName) ? null : context.GetTypeDefinition(baseTypeName);

            if (baseType == null)
            {
                var subType = GetSingleSchemaChildNode(element, "simpleType");
                if (subType != null)
                {
                    baseType = ParseSimpleType(subType, context, "anonymousType");
                }
            }
            var    restriction        = new SchemaSimpleTypeRestriction();
            var    values             = new HashSet <string>();
            var    patterns           = new List <string>();
            string patternDescription = null;

            foreach (var child in GetSchemaChildNodes(element))
            {
                var value = child.GetAttribute("value");
                switch (child.LocalName)
                {
                case "simpleType":
                    break;

                case "enumeration":
                    if (values.Contains(value))
                    {
                        throw new InvalidOperationException(string.Format("Duplicate enumeration value: '{0}'", value));
                    }
                    values.Add(value);
                    break;

                case "length":
                    if (restriction.Length != null)
                    {
                        throw new InvalidOperationException("Duplicate 'length' facet");
                    }
                    int length;
                    if (!int.TryParse(value, NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingWhite, CultureInfo.InvariantCulture, out length))
                    {
                        throw new InvalidOperationException(string.Format("'length' attribute is an integer but was '{0}'", value));
                    }
                    restriction.Length = length;
                    break;

                case "minLength":
                    if (restriction.MinLength != null)
                    {
                        throw new InvalidOperationException("Duplicate 'minLength' facet");
                    }
                    int minLength;
                    if (!int.TryParse(value, NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingWhite, CultureInfo.InvariantCulture, out minLength))
                    {
                        throw new InvalidOperationException(string.Format("'minLength' attribute is an integer but was '{0}'", value));
                    }
                    restriction.MinLength = minLength;
                    break;

                case "maxLength":
                    if (restriction.MaxLength != null)
                    {
                        throw new InvalidOperationException("Duplicate 'maxLength' facet");
                    }
                    int maxLength;
                    if (!int.TryParse(value, NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingWhite, CultureInfo.InvariantCulture, out maxLength))
                    {
                        throw new InvalidOperationException(string.Format("'maxLength' attribute is an integer but was '{0}'", value));
                    }
                    restriction.MaxLength = maxLength;
                    break;

                case "pattern":
                    patterns.Add(value);
                    if (string.IsNullOrEmpty(patternDescription))
                    {
                        patternDescription = child.GetAttribute("descriptionError", NamespaceManager.DatabaseType);
                    }
                    break;

                case "minInclusive":
                    if (restriction.MinInclusive != null)
                    {
                        throw new InvalidOperationException("Duplicate 'minInclusive' facet");
                    }
                    restriction.MinInclusive = value;
                    break;

                case "minExclusive":
                    if (restriction.MinExclusive != null)
                    {
                        throw new InvalidOperationException("Duplicate 'minExclusive' facet");
                    }
                    restriction.MinExclusive = value;
                    break;

                case "maxInclusive":
                    if (restriction.MaxInclusive != null)
                    {
                        throw new InvalidOperationException("Duplicate 'maxInclusive' facet");
                    }
                    restriction.MaxInclusive = value;
                    break;

                case "maxExclusive":
                    if (restriction.MaxExclusive != null)
                    {
                        throw new InvalidOperationException("Duplicate 'maxExclusive' facet");
                    }
                    restriction.MaxExclusive = value;
                    break;

                case "totalDigits":
                    int totalDigits;
                    if (!int.TryParse(value, out totalDigits))
                    {
                        throw new InvalidOperationException(string.Format("'totalDigits' attribute is an integer but was '{0}'", value));
                    }
                    if (restriction.TotalDigits != null)
                    {
                        throw new InvalidOperationException("Duplicate 'totalDigits' facet");
                    }
                    restriction.TotalDigits = totalDigits;
                    break;

                case "fractionDigits":
                    int fractionalDigits;
                    if (!int.TryParse(value, out fractionalDigits))
                    {
                        throw new InvalidOperationException(string.Format("'fractionDigits' attribute is an integer but was '{0}'", value));
                    }
                    if (restriction.FractionDigits != null)
                    {
                        throw new InvalidOperationException("Duplicate 'fractionDigits' facet");
                    }
                    restriction.FractionDigits = fractionalDigits;
                    break;

                case "whiteSpace":
                    switch (value)
                    {
                    case "preserve":
                        restriction.WhiteSpace = SchemaSimpleTypeRestriction.WhiteSpaceEnum.Preserve;
                        break;

                    case "replace":
                        restriction.WhiteSpace = SchemaSimpleTypeRestriction.WhiteSpaceEnum.Replace;
                        break;

                    case "collapse":
                        restriction.WhiteSpace = SchemaSimpleTypeRestriction.WhiteSpaceEnum.Collapse;
                        break;

                    default:
                        throw new InvalidOperationException(string.Format("Invalid 'whiteSpace' facet value: '{0}'", value));
                    }
                    break;

                default:
                    throw new InvalidOperationException(string.Format("Facet '{0}' is not supported", child.LocalName));
                }
            }
            if (values.Count > 0)
            {
                restriction.Values = values.ToArray();
            }
            if (patterns.Count > 0)
            {
                restriction.Patterns = patterns.ToArray();
            }
            restriction.PatternDescription = patternDescription;
            if (restriction.MinLength != null && restriction.MaxLength != null && restriction.MinLength > restriction.MaxLength)
            {
                throw new InvalidOperationException("The value of 'minLength' facet cannot be greater than the value of 'maxLength' facet");
            }
            if (restriction.Length != null && (restriction.MinLength != null || restriction.MaxLength != null))
            {
                throw new InvalidOperationException("The 'length' facet cannot be filled along with the either of the 'minLength' or the 'maxLength' facets");
            }
            return(new SchemaSimpleType(baseType, name, restriction, description));
        }
コード例 #2
0
ファイル: SchemaSimpleType.cs プロジェクト: homuroll/GroboXsd
 public SchemaSimpleType([CanBeNull] SchemaTypeBase baseType, [NotNull] string name,
                         [CanBeNull] SchemaSimpleTypeRestriction restriction, [CanBeNull] string[] description)
     : base(baseType, name, description)
 {
     Restriction = restriction;
 }