private TypeModel CreateTypeModel(Uri source, XmlSchemaGroup group, NamespaceModel namespaceModel, XmlQualifiedName qualifiedName, List <DocumentationModel> docs)
        {
            var name = "I" + _configuration.NamingProvider.GroupTypeNameFromQualifiedName(qualifiedName);

            if (namespaceModel != null)
            {
                name = namespaceModel.GetUniqueTypeName(name);
            }

            var interfaceModel = new InterfaceModel(_configuration)
            {
                Name          = name,
                Namespace     = namespaceModel,
                XmlSchemaName = qualifiedName
            };

            interfaceModel.Documentation.AddRange(docs);

            if (namespaceModel != null)
            {
                namespaceModel.Types[name] = interfaceModel;
            }

            if (!qualifiedName.IsEmpty)
            {
                var key = BuildKey(group, qualifiedName);
                Types[key] = interfaceModel;
            }

            var particle   = group.Particle;
            var items      = GetElements(particle);
            var properties = CreatePropertiesForElements(source, interfaceModel, particle, items.Where(i => !(i.XmlParticle is XmlSchemaGroupRef)));

            interfaceModel.Properties.AddRange(properties);
            var interfaces = items.Select(i => i.XmlParticle).OfType <XmlSchemaGroupRef>()
                             .Select(i => (InterfaceModel)CreateTypeModel(CodeUtilities.CreateUri(i.SourceUri), Groups[i.RefName], i.RefName));

            interfaceModel.AddInterfaces(interfaces);

            return(interfaceModel);
        }
        private TypeModel CreateTypeModel(Uri source, XmlSchemaAttributeGroup attributeGroup, NamespaceModel namespaceModel, XmlQualifiedName qualifiedName, List <DocumentationModel> docs)
        {
            var name = "I" + _configuration.NamingProvider.AttributeGroupTypeNameFromQualifiedName(qualifiedName);

            if (namespaceModel != null)
            {
                name = namespaceModel.GetUniqueTypeName(name);
            }

            var interfaceModel = new InterfaceModel(_configuration)
            {
                Name          = name,
                Namespace     = namespaceModel,
                XmlSchemaName = qualifiedName
            };

            interfaceModel.Documentation.AddRange(docs);

            if (namespaceModel != null)
            {
                namespaceModel.Types[name] = interfaceModel;
            }

            if (!qualifiedName.IsEmpty)
            {
                var key = BuildKey(attributeGroup, qualifiedName);
                Types[key] = interfaceModel;
            }

            var items      = attributeGroup.Attributes;
            var properties = CreatePropertiesForAttributes(source, interfaceModel, items.OfType <XmlSchemaAttribute>());

            interfaceModel.Properties.AddRange(properties);
            var interfaces = items.OfType <XmlSchemaAttributeGroupRef>()
                             .Select(a => (InterfaceModel)CreateTypeModel(CodeUtilities.CreateUri(a.SourceUri), AttributeGroups[a.RefName], a.RefName));

            interfaceModel.AddInterfaces(interfaces);

            return(interfaceModel);
        }
        private TypeModel CreateTypeModel(XmlSchemaSimpleType simpleType, NamespaceModel namespaceModel, XmlQualifiedName qualifiedName, List <DocumentationModel> docs)
        {
            var restrictions = new List <RestrictionModel>();

            if (simpleType.Content is XmlSchemaSimpleTypeRestriction typeRestriction)
            {
                var enumFacets = typeRestriction.Facets.OfType <XmlSchemaEnumerationFacet>().ToList();
                // If there's a pattern restriction mixed into the enumeration values, we'll generate a string to play it safe.
                var isEnum = enumFacets.Count > 0 && !typeRestriction.Facets.OfType <XmlSchemaPatternFacet>().Any();
                if (isEnum)
                {
                    // we got an enum
                    var name = _configuration.NamingProvider.EnumTypeNameFromQualifiedName(qualifiedName);
                    if (namespaceModel != null)
                    {
                        name = namespaceModel.GetUniqueTypeName(name);
                    }

                    var enumModel = new EnumModel(_configuration)
                    {
                        Name          = name,
                        Namespace     = namespaceModel,
                        XmlSchemaName = qualifiedName,
                        XmlSchemaType = simpleType,
                        IsAnonymous   = string.IsNullOrEmpty(simpleType.QualifiedName.Name),
                    };

                    enumModel.Documentation.AddRange(docs);

                    foreach (var facet in enumFacets.DistinctBy(f => f.Value))
                    {
                        var value = new EnumValueModel
                        {
                            Name  = _configuration.NamingProvider.EnumMemberNameFromValue(enumModel.Name, facet.Value),
                            Value = facet.Value
                        };

                        var valueDocs = GetDocumentation(facet);
                        value.Documentation.AddRange(valueDocs);

                        var deprecated = facet.Annotation != null && facet.Annotation.Items.OfType <XmlSchemaAppInfo>()
                                         .Any(a => a.Markup.Any(m => m.Name == "annox:annotate" && m.HasChildNodes && m.FirstChild.Name == "jl:Deprecated"));
                        value.IsDeprecated = deprecated;

                        enumModel.Values.Add(value);
                    }

                    enumModel.Values = EnsureEnumValuesUnique(enumModel.Values);
                    if (namespaceModel != null)
                    {
                        namespaceModel.Types[enumModel.Name] = enumModel;
                    }

                    if (!qualifiedName.IsEmpty)
                    {
                        var key = BuildKey(simpleType, qualifiedName);
                        Types[key] = enumModel;
                    }

                    return(enumModel);
                }

                restrictions = GetRestrictions(typeRestriction.Facets.Cast <XmlSchemaFacet>(), simpleType).Where(r => r != null).Sanitize().ToList();
            }

            var simpleModelName = _configuration.NamingProvider.SimpleTypeNameFromQualifiedName(qualifiedName);

            if (namespaceModel != null)
            {
                simpleModelName = namespaceModel.GetUniqueTypeName(simpleModelName);
            }

            var simpleModel = new SimpleModel(_configuration)
            {
                Name          = simpleModelName,
                Namespace     = namespaceModel,
                XmlSchemaName = qualifiedName,
                XmlSchemaType = simpleType,
                ValueType     = simpleType.Datatype.GetEffectiveType(_configuration, restrictions),
            };

            simpleModel.Documentation.AddRange(docs);
            simpleModel.Restrictions.AddRange(restrictions);

            if (namespaceModel != null)
            {
                namespaceModel.Types[simpleModel.Name] = simpleModel;
            }

            if (!qualifiedName.IsEmpty)
            {
                var key = BuildKey(simpleType, qualifiedName);
                Types[key] = simpleModel;
            }

            return(simpleModel);
        }
        private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType, NamespaceModel namespaceModel, XmlQualifiedName qualifiedName, List <DocumentationModel> docs)
        {
            var name = _configuration.NamingProvider.ComplexTypeNameFromQualifiedName(qualifiedName);

            if (namespaceModel != null)
            {
                name = namespaceModel.GetUniqueTypeName(name);
            }

            var classModel = new ClassModel(_configuration)
            {
                Name           = name,
                Namespace      = namespaceModel,
                XmlSchemaName  = qualifiedName,
                XmlSchemaType  = complexType,
                IsAbstract     = complexType.IsAbstract,
                IsAnonymous    = string.IsNullOrEmpty(complexType.QualifiedName.Name),
                IsMixed        = complexType.IsMixed,
                IsSubstitution = complexType.Parent is XmlSchemaElement && !((XmlSchemaElement)complexType.Parent).SubstitutionGroup.IsEmpty
            };

            classModel.Documentation.AddRange(docs);

            if (namespaceModel != null)
            {
                namespaceModel.Types[classModel.Name] = classModel;
            }

            if (!qualifiedName.IsEmpty)
            {
                var key = BuildKey(complexType, qualifiedName);
                Types[key] = classModel;
            }

            if (complexType.BaseXmlSchemaType != null && complexType.BaseXmlSchemaType.QualifiedName != AnyType)
            {
                var baseModel = CreateTypeModel(source, complexType.BaseXmlSchemaType, complexType.BaseXmlSchemaType.QualifiedName);
                classModel.BaseClass = baseModel;
                if (baseModel is ClassModel baseClassModel)
                {
                    baseClassModel.DerivedTypes.Add(classModel);
                }
            }

            XmlSchemaParticle particle = null;

            if (classModel.BaseClass != null)
            {
                if (complexType.ContentModel.Content is XmlSchemaComplexContentExtension complexContent)
                {
                    particle = complexContent.Particle;
                }

                // If it's a restriction, do not duplicate elements on the derived class, they're already in the base class.
                // See https://msdn.microsoft.com/en-us/library/f3z3wh0y.aspx
            }
            else
            {
                particle = complexType.Particle ?? complexType.ContentTypeParticle;
            }

            var items = GetElements(particle, complexType).ToList();

            if (_configuration.GenerateInterfaces)
            {
                var interfaces = items.Select(i => i.XmlParticle).OfType <XmlSchemaGroupRef>()
                                 .Select(i => (InterfaceModel)CreateTypeModel(CodeUtilities.CreateUri(i.SourceUri), Groups[i.RefName], i.RefName)).ToList();

                classModel.AddInterfaces(interfaces);
            }

            var properties = CreatePropertiesForElements(source, classModel, particle, items);

            classModel.Properties.AddRange(properties);

            XmlSchemaObjectCollection attributes = null;

            if (classModel.BaseClass != null)
            {
                if (complexType.ContentModel.Content is XmlSchemaComplexContentExtension complexContent)
                {
                    attributes = complexContent.Attributes;
                }
                else if (complexType.ContentModel.Content is XmlSchemaSimpleContentExtension simpleContent)
                {
                    attributes = simpleContent.Attributes;
                }

                // If it's a restriction, do not duplicate attributes on the derived class, they're already in the base class.
                // See https://msdn.microsoft.com/en-us/library/f3z3wh0y.aspx
            }
            else
            {
                attributes = complexType.Attributes;
            }

            if (attributes != null)
            {
                var attributeProperties = CreatePropertiesForAttributes(source, classModel, attributes.Cast <XmlSchemaObject>());
                classModel.Properties.AddRange(attributeProperties);

                if (_configuration.GenerateInterfaces)
                {
                    var attributeInterfaces = attributes.OfType <XmlSchemaAttributeGroupRef>()
                                              .Select(i => (InterfaceModel)CreateTypeModel(CodeUtilities.CreateUri(i.SourceUri), AttributeGroups[i.RefName], i.RefName));
                    classModel.AddInterfaces(attributeInterfaces);
                }
            }

            if (complexType.AnyAttribute != null)
            {
                var property = new PropertyModel(_configuration)
                {
                    OwningType = classModel,
                    Name       = "AnyAttribute",
                    Type       = new SimpleModel(_configuration)
                    {
                        ValueType = typeof(XmlAttribute), UseDataTypeAttribute = false
                    },
                    IsAttribute  = true,
                    IsCollection = true,
                    IsAny        = true
                };

                var attributeDocs = GetDocumentation(complexType.AnyAttribute);
                property.Documentation.AddRange(attributeDocs);

                classModel.Properties.Add(property);
            }

            return(classModel);
        }
示例#5
0
        private TypeModel CreateTypeModel(Uri source, XmlSchemaSimpleType simpleType, NamespaceModel namespaceModel, XmlQualifiedName qualifiedName, List <DocumentationModel> docs)
        {
            var restrictions = new List <RestrictionModel>();

            if (simpleType.Content is XmlSchemaSimpleTypeRestriction typeRestriction)
            {
                var enumFacets = typeRestriction.Facets.OfType <XmlSchemaEnumerationFacet>().ToList();
                var isEnum     = (enumFacets.Count == typeRestriction.Facets.Count && enumFacets.Count != 0) ||
                                 (EnumTypes.Contains(typeRestriction.BaseTypeName.Name) && enumFacets.Any());
                if (isEnum)
                {
                    // we got an enum
                    var name = ToTitleCase(qualifiedName.Name);
                    if (namespaceModel != null)
                    {
                        name = namespaceModel.GetUniqueTypeName(name);
                    }

                    var enumModel = new EnumModel(_configuration)
                    {
                        Name          = name,
                        Namespace     = namespaceModel,
                        XmlSchemaName = qualifiedName,
                        XmlSchemaType = simpleType,
                    };

                    enumModel.Documentation.AddRange(docs);

                    foreach (var facet in enumFacets.DistinctBy(f => f.Value))
                    {
                        var value = new EnumValueModel
                        {
                            Name  = _configuration.NamingProvider.EnumMemberNameFromValue(enumModel.Name, facet.Value),
                            Value = facet.Value
                        };

                        var valueDocs = GetDocumentation(facet);
                        value.Documentation.AddRange(valueDocs);

                        var deprecated = facet.Annotation != null && facet.Annotation.Items.OfType <XmlSchemaAppInfo>()
                                         .Any(a => a.Markup.Any(m => m.Name == "annox:annotate" && m.HasChildNodes && m.FirstChild.Name == "jl:Deprecated"));
                        value.IsDeprecated = deprecated;

                        enumModel.Values.Add(value);
                    }

                    if (namespaceModel != null)
                    {
                        namespaceModel.Types[enumModel.Name] = enumModel;
                    }

                    if (!qualifiedName.IsEmpty)
                    {
                        Types[qualifiedName] = enumModel;
                    }

                    return(enumModel);
                }

                restrictions = GetRestrictions(typeRestriction.Facets.Cast <XmlSchemaFacet>(), simpleType).Where(r => r != null).Sanitize().ToList();
            }

            var simpleModelName = ToTitleCase(qualifiedName.Name);

            if (namespaceModel != null)
            {
                simpleModelName = namespaceModel.GetUniqueTypeName(simpleModelName);
            }

            var simpleModel = new SimpleModel(_configuration)
            {
                Name          = simpleModelName,
                Namespace     = namespaceModel,
                XmlSchemaName = qualifiedName,
                XmlSchemaType = simpleType,
                ValueType     = simpleType.Datatype.GetEffectiveType(_configuration),
            };

            simpleModel.Documentation.AddRange(docs);
            simpleModel.Restrictions.AddRange(restrictions);

            if (namespaceModel != null)
            {
                namespaceModel.Types[simpleModel.Name] = simpleModel;
            }

            if (!qualifiedName.IsEmpty)
            {
                Types[qualifiedName] = simpleModel;
            }

            return(simpleModel);
        }