Inheritance: ClrBasePropertyInfo
        private void SetPropertyFlags(ClrPropertyInfo propertyInfo, GroupingInfo currentGroupingInfo, XmlSchemaType propertyType)
        {
            ClrPropertyInfo isNullable = propertyInfo;

            isNullable.IsNullable       = isNullable.IsNullable | (currentGroupingInfo.ContentModelType == ContentModelType.Choice ? true : currentGroupingInfo.IsOptional);
            propertyInfo.VerifyRequired = this.configSettings.VerifyRequired;
            if (currentGroupingInfo.IsRepeating)
            {
                propertyInfo.IsList = true;
            }
            string          propertyName = propertyInfo.PropertyName;
            ClrPropertyInfo prevProperty = null;

            if (!this.propertyNameTypeTable.TryGetValue(propertyName, out prevProperty))
            {
                this.propertyNameTypeTable.Add(propertyName, propertyInfo);
            }
            else
            {
                currentGroupingInfo.HasRecurrentElements = true;
                propertyInfo.IsDuplicate = true;
                prevProperty.IsList      = true;
            }
        }
Example #2
0
 private void ProcessGroup(GroupingInfo grouping, List <ClrAnnotation> annotations)
 {
     typeBuilder.StartGrouping(grouping);
     foreach (ContentInfo child in grouping.Children)
     {
         if (child.ContentType == ContentType.Property)
         {
             ClrPropertyInfo propertyInfo = child as ClrPropertyInfo;
             propertyInfo.UpdateTypeReference(currentFullTypeName, currentNamespace, nameMappings);
             typeBuilder.CreateProperty(propertyInfo, annotations);
         }
         else if (child.ContentType == ContentType.WildCardProperty)
         {
             ClrWildCardPropertyInfo propertyInfo = child as ClrWildCardPropertyInfo;
             typeBuilder.CreateProperty(propertyInfo, annotations);
         }
         else
         {
             Debug.Assert(child.ContentType == ContentType.Grouping);
             ProcessGroup(child as GroupingInfo, annotations);
         }
     }
     typeBuilder.EndGrouping();
 }
        private ClrPropertyInfo BuildProperty(XmlSchemaAttribute attribute, bool fromBaseType, bool isNew)
        {
            string          schemaName   = attribute.QualifiedName.Name;
            string          schemaNs     = attribute.QualifiedName.Namespace;
            string          propertyName = this.localSymbolTable.AddAttribute(attribute);
            ClrPropertyInfo propertyInfo = new ClrPropertyInfo(propertyName, schemaNs, schemaName, (attribute.Use == XmlSchemaUse.Required ? Occurs.One : Occurs.ZeroOrOne))
            {
                Origin         = SchemaOrigin.Attribute,
                FromBaseType   = fromBaseType,
                IsNew          = isNew,
                VerifyRequired = this.configSettings.VerifyRequired
            };
            XmlSchemaSimpleType schemaType = attribute.AttributeSchemaType;
            XmlQualifiedName    qName      = schemaType.QualifiedName;

            if (qName.IsEmpty)
            {
                qName = attribute.QualifiedName;
            }
            propertyInfo.TypeReference = this.BuildTypeReference(schemaType, qName, false, true);
            Debug.Assert(schemaType.Datatype != null);
            this.SetFixedDefaultValue(attribute, propertyInfo);
            return(propertyInfo);
        }
        private ClrPropertyInfo BuildProperty(XmlSchemaAttribute attribute, bool fromBaseType, bool isNew)
        {
            string schemaName = attribute.QualifiedName.Name;
            string schemaNs   = attribute.QualifiedName.Namespace;

            string          propertyName       = localSymbolTable.AddAttribute(attribute);
            Occurs          attrPropertyOccurs = attribute.Use == XmlSchemaUse.Required ? Occurs.One : Occurs.ZeroOrOne;
            ClrPropertyInfo propertyInfo       = new ClrPropertyInfo(propertyName, schemaNs, schemaName, attrPropertyOccurs);

            propertyInfo.Origin         = SchemaOrigin.Attribute;
            propertyInfo.FromBaseType   = fromBaseType;
            propertyInfo.IsNew          = isNew;
            propertyInfo.VerifyRequired = configSettings.VerifyRequired;

            XmlSchemaSimpleType schemaType = attribute.AttributeSchemaType;
            // http://linqtoxsd.codeplex.com/WorkItem/View.aspx?WorkItemId=4106
            ClrTypeReference typeRef =
                BuildTypeReference(schemaType, attribute.AttributeSchemaType.QualifiedName, false, true);

            propertyInfo.TypeReference = typeRef;
            Debug.Assert(schemaType.Datatype != null);
            SetFixedDefaultValue(attribute, propertyInfo);
            return(propertyInfo);
        }
Example #5
0
        private void ProcessWrapperTypes()
        {
            if (wrapperRootElements == null)
            {
                //No Globalelements with global types
                return;
            }

            XWrapperTypedElementBuilder wrapperBuilder    = new XWrapperTypedElementBuilder(settings);
            XSimpleTypedElementBuilder  simpleTypeBuilder = new XSimpleTypedElementBuilder(settings);

            TypeBuilder     builder = null;
            ClrPropertyInfo typedValPropertyInfo = null;

            foreach (ClrWrapperTypeInfo typeInfo in wrapperRootElements)
            {
                SetFullTypeName(typeInfo, null);
                ClrTypeReference innerType = typeInfo.InnerType;
                if (innerType.IsSimpleType)
                {
                    typedValPropertyInfo = InitializeTypedValuePropertyInfo(typeInfo, typedValPropertyInfo, innerType);
                    simpleTypeBuilder.Init(typedValPropertyInfo.ClrTypeName, innerType.IsSchemaList);
                    simpleTypeBuilder.CreateTypeDeclaration(typeInfo);
                    simpleTypeBuilder.CreateFunctionalConstructor(typeInfo.Annotations);
                    typedValPropertyInfo.SetFixedDefaultValue(typeInfo);
                    simpleTypeBuilder.CreateProperty(typedValPropertyInfo, typeInfo.Annotations);
                    simpleTypeBuilder.AddTypeToTypeManager(elementDictionaryAddStatements,
                                                           Constants.ElementDictionaryField);
                    simpleTypeBuilder.ApplyAnnotations(typeInfo);
                    builder = simpleTypeBuilder;
                }
                else
                {
                    string innerTypeName     = null;
                    string innerTypeFullName =
                        innerType.GetClrFullTypeName(typeInfo.clrtypeNs, nameMappings, out innerTypeName);
                    string innerTypeNs = innerType.Namespace;

                    CodeNamespace       innerTypeCodeNamespace = GetCodeNamespace(innerTypeNs);
                    CodeTypeDeclaration innerTypeDecl          = GetCodeTypeDeclaration(innerTypeName, innerTypeCodeNamespace);
                    TypeAttributes      innerTypeAttributes    = TypeAttributes.Class;
                    if (innerTypeDecl != null)
                    {
                        innerTypeAttributes = innerTypeDecl.TypeAttributes;
                    }
                    else if (innerTypeName != Constants.XTypedElement)
                    {
                        continue;
                    }

                    currentNamespace = typeInfo.clrtypeNs;
                    wrapperBuilder.Init(innerTypeFullName, innerTypeNs, innerTypeAttributes);
                    wrapperBuilder.CreateTypeDeclaration(typeInfo);
                    wrapperBuilder.CreateFunctionalConstructor(typeInfo.Annotations);
                    wrapperBuilder.ApplyAnnotations(typeInfo);
                    wrapperBuilder.AddTypeToTypeManager(elementDictionaryAddStatements, wrapperDictionaryAddStatements);

                    if (!typeInfo.HasBaseContentType)
                    {
                        //Add innerType properties only if the wrapper's type is not the same as the substitutionGroup head type
                        ClrWrappingPropertyInfo wrappingPropertyInfo = null;
                        //Create forwarding properties
                        if (innerTypeName != Constants.XTypedElement)
                        {
                            //If the wrapped type is xs:anyType, no forwarding properties to create
                            wrappingPropertyInfo = new ClrWrappingPropertyInfo();

                            foreach (CodeTypeMember member in innerTypeDecl.Members)
                            {
                                CodeMemberProperty memberProperty = member as CodeMemberProperty;
                                if (ForwardProperty(memberProperty))
                                {
                                    //Do not forward over TypeManager, SchemaName etc
                                    wrappingPropertyInfo.Init(memberProperty);
                                    wrapperBuilder.CreateProperty(wrappingPropertyInfo, typeInfo.Annotations);
                                }
                            }
                        }
                    }

                    builder = wrapperBuilder;
                }

                builder.ImplementInterfaces(settings.EnableServiceReference);
                codeNamespace = GetCodeNamespace(typeInfo.clrtypeNs);
                codeNamespace.Types.Add(builder.TypeDeclaration);

                List <CodeTypeDeclaration> types;
                codeNamespace = GetCodeNamespace(typeInfo.clrtypeNs);

                if (!xroots.TryGetValue(codeNamespace, out types))
                {
                    types = new List <CodeTypeDeclaration>();
                    xroots.Add(codeNamespace, types);
                }

                types.Add(builder.TypeDeclaration);
            }
        }
Example #6
0
 private ClrPropertyInfo InitializeTypedValuePropertyInfo(ClrTypeInfo typeInfo, ClrPropertyInfo typedValPropertyInfo, ClrTypeReference innerType)
 {
     if (typedValPropertyInfo == null)
     {
         typedValPropertyInfo        = new ClrPropertyInfo(Constants.SInnerTypePropertyName, string.Empty, Constants.SInnerTypePropertyName, Occurs.One);
         typedValPropertyInfo.Origin = SchemaOrigin.Text;
     }
     else
     {
         typedValPropertyInfo.Reset();
     }
     typedValPropertyInfo.TypeReference = innerType;
     if (typeInfo.IsSubstitutionMember())
     {
         typedValPropertyInfo.IsNew = true;
     }
     typedValPropertyInfo.UpdateTypeReference(currentFullTypeName, currentNamespace, nameMappings);
     return(typedValPropertyInfo);
 }
Example #7
0
 private ClrPropertyInfo InitializeTypedValuePropertyInfo(ClrTypeInfo typeInfo, ClrPropertyInfo typedValPropertyInfo, ClrTypeReference innerType)
 {
     if (typedValPropertyInfo == null) {
         typedValPropertyInfo = new ClrPropertyInfo(Constants.SInnerTypePropertyName, string.Empty, Constants.SInnerTypePropertyName, Occurs.One);
         typedValPropertyInfo.Origin = SchemaOrigin.Text;
     }
     else {
         typedValPropertyInfo.Reset();
     }
     typedValPropertyInfo.TypeReference = innerType;
     if (typeInfo.IsSubstitutionMember()) {
         typedValPropertyInfo.IsNew = true;
     }
     typedValPropertyInfo.UpdateTypeReference(currentFullTypeName, currentNamespace, nameMappings);
     return typedValPropertyInfo;
 }
        private ClrPropertyInfo BuildComplexTypeTextProperty(XmlSchemaElement parentElement, XmlSchemaComplexType schemaType)
        {
            string          identifier;
            ClrPropertyInfo clrPropertyInfo;

            Debug.Assert(schemaType != null);
            Debug.Assert(schemaType.GetContentType() == XmlSchemaContentType.TextOnly);
            ClrPropertyInfo textProperty = new ClrPropertyInfo("TypedValue", string.Empty, "TypedValue", Occurs.One)
            {
                Origin = SchemaOrigin.Text
            };
            ClrTypeReference typeRef   = null;
            bool             anonymous = false;
            XmlSchemaType    baseType  = schemaType.BaseXmlSchemaType;

            if (!(baseType is XmlSchemaSimpleType))
            {
                if (schemaType.HasFacetRestrictions())
                {
                    goto Label1;
                }
                clrPropertyInfo = null;
                return(clrPropertyInfo);
            }
            else
            {
                typeRef   = this.BuildTypeReference(baseType, baseType.QualifiedName, false, true);
                anonymous = false;
                if (!this.textPropInheritanceTracker.ContainsKey(schemaType))
                {
                    this.textPropInheritanceTracker.Add(schemaType, textProperty);
                }
            }
            if (anonymous)
            {
                if (parentElement == null)
                {
                    this.localSymbolTable.AddComplexRestrictedContentType(schemaType, typeRef);
                }
                else
                {
                    identifier = this.localSymbolTable.AddLocalElement(parentElement);
                    this.localSymbolTable.AddAnonymousType(identifier, parentElement, typeRef);
                }
            }
            textProperty.TypeReference = typeRef;
            clrPropertyInfo            = textProperty;
            return(clrPropertyInfo);

Label1:
            XmlSchemaSimpleType st = schemaType.GetBaseSimpleType();

            Debug.Assert(st != null);
            typeRef          = this.BuildTypeReference(st, st.QualifiedName, true, true);
            typeRef.Validate = true;
            anonymous        = true;
            ClrPropertyInfo baseProp = null;

            if (this.textPropInheritanceTracker.TryGetValue(baseType, out baseProp))
            {
                textProperty.IsOverride = true;
                if (!baseProp.IsOverride)
                {
                    baseProp.IsVirtual = true;
                }
            }
            if (anonymous)
            {
                if (parentElement == null)
                {
                    this.localSymbolTable.AddComplexRestrictedContentType(schemaType, typeRef);
                }
                else
                {
                    identifier = this.localSymbolTable.AddLocalElement(parentElement);
                    this.localSymbolTable.AddAnonymousType(identifier, parentElement, typeRef);
                }
            }
            textProperty.TypeReference = typeRef;
            clrPropertyInfo            = textProperty;
            return(clrPropertyInfo);
        }
 private void SetFixedDefaultValue(XmlSchemaAttribute attribute, ClrPropertyInfo propertyInfo)
 {
     //saves fixed/default value in the corresponding property
     //Currently only consider fixed/default values for simple types
     if (attribute.RefName != null && !attribute.RefName.IsEmpty)
     {
         XmlSchemaAttribute globalAtt = (XmlSchemaAttribute)this.schemas.GlobalAttributes[attribute.RefName];
         propertyInfo.FixedValue = globalAtt.FixedValue;
         propertyInfo.DefaultValue = globalAtt.DefaultValue;
     }
     else
     {
         propertyInfo.FixedValue = attribute.FixedValue;
         propertyInfo.DefaultValue = attribute.DefaultValue;
     }
     if (attribute.AttributeSchemaType.DerivedBy == XmlSchemaDerivationMethod.Union)
     {
         string value = propertyInfo.FixedValue;
         if (value == null)
             value = propertyInfo.DefaultValue;
         if (value != null)
         {
             propertyInfo.unionDefaultType = attribute.AttributeSchemaType.Datatype.ParseValue(value, new NameTable(), null).GetType();
         }
     }
 }
        private ClrPropertyInfo BuildProperty(XmlSchemaAttribute attribute, bool fromBaseType, bool isNew)
        {
            string schemaName = attribute.QualifiedName.Name;
            string schemaNs = attribute.QualifiedName.Namespace;

            string propertyName = localSymbolTable.AddAttribute(attribute);
            Occurs attrPropertyOccurs = attribute.Use == XmlSchemaUse.Required ? Occurs.One : Occurs.ZeroOrOne;
            ClrPropertyInfo propertyInfo = new ClrPropertyInfo(propertyName, schemaNs, schemaName, attrPropertyOccurs);
            propertyInfo.Origin = SchemaOrigin.Attribute;
            propertyInfo.FromBaseType = fromBaseType;
            propertyInfo.IsNew = isNew;
            propertyInfo.VerifyRequired = configSettings.VerifyRequired;

            XmlSchemaSimpleType schemaType = attribute.AttributeSchemaType;
            var qName = schemaType.QualifiedName;
            if(qName.IsEmpty)
            {
                qName = attribute.QualifiedName;
            }
            // http://linqtoxsd.codeplex.com/WorkItem/View.aspx?WorkItemId=4106
            ClrTypeReference typeRef = BuildTypeReference(
                schemaType,
                qName,
                false,
                true);
            propertyInfo.TypeReference = typeRef;
            Debug.Assert(schemaType.Datatype != null);
            SetFixedDefaultValue(attribute, propertyInfo);
            return propertyInfo;
        }
        private void AppendCardinalityInformation(List <ClrAnnotation> annotations,
                                                  ClrBasePropertyInfo basePropertyInfo,
                                                  XmlSchemaObject schemaObject,
                                                  bool isInChoice,
                                                  bool isInNestedGroup)
        {
            ClrPropertyInfo propertyInfo = basePropertyInfo as ClrPropertyInfo;

            string text = String.Empty;

            text += "Occurrence: ";

            if (propertyInfo.IsOptional)
            {
                text += "optional";
            }
            else
            {
                text += "required";
            }


            if (propertyInfo.IsStar ||
                propertyInfo.IsPlus)
            {
                text += ", repeating";
            }

            if (isInChoice)
            {
                text += ", choice";
            }

            // Append the occurrence message
            AppendMessage(annotations, "summary", text);

            if (isInNestedGroup)
            {
                AppendMessage(annotations, "summary", "Setter: Appends");
            }

            if (propertyInfo.IsSubstitutionHead)
            {
                bool fComma = false;
                text = "Substitution members: ";
                foreach (XmlSchemaElement xse in propertyInfo.SubstitutionMembers)
                {
                    if (!fComma)
                    {
                        fComma = true;
                    }
                    else
                    {
                        text += ", ";
                    }

                    text += xse.Name;
                }

                AppendMessage(annotations, "summary", text);
            }
        }
        private void ProcessWrapperTypes()
        {
            List <CodeTypeDeclaration> types;

            if (this.wrapperRootElements != null)
            {
                XWrapperTypedElementBuilder wrapperBuilder    = new XWrapperTypedElementBuilder();
                XSimpleTypedElementBuilder  simpleTypeBuilder = new XSimpleTypedElementBuilder();
                TypeBuilder     builder = null;
                ClrPropertyInfo typedValPropertyInfo = null;
                foreach (ClrWrapperTypeInfo typeInfo in this.wrapperRootElements)
                {
                    this.SetFullTypeName(typeInfo, null);
                    ClrTypeReference innerType = typeInfo.InnerType;
                    if (!innerType.IsSimpleType)
                    {
                        string innerTypeName                    = null;
                        string innerTypeFullName                = innerType.GetClrFullTypeName(typeInfo.clrtypeNs, this.nameMappings, out innerTypeName);
                        string innerTypeNs                      = innerType.Namespace;
                        CodeTypeDeclaration innerTypeDecl       = this.GetCodeTypeDeclaration(innerTypeName, this.GetCodeNamespace(innerTypeNs));
                        TypeAttributes      innerTypeAttributes = TypeAttributes.NotPublic;
                        if (innerTypeDecl != null)
                        {
                            innerTypeAttributes = innerTypeDecl.TypeAttributes;
                        }
                        else if (innerTypeName != "XTypedElement")
                        {
                            continue;
                        }
                        this.currentNamespace = typeInfo.clrtypeNs;
                        wrapperBuilder.Init(innerTypeFullName, innerTypeNs, innerTypeAttributes);
                        wrapperBuilder.CreateTypeDeclaration(typeInfo);
                        wrapperBuilder.CreateFunctionalConstructor(typeInfo.Annotations);
                        wrapperBuilder.ApplyAnnotations(typeInfo);
                        wrapperBuilder.AddTypeToTypeManager(CodeDomTypesGenerator.elementDictionaryAddStatements, CodeDomTypesGenerator.wrapperDictionaryAddStatements);
                        if (!typeInfo.HasBaseContentType)
                        {
                            ClrWrappingPropertyInfo wrappingPropertyInfo = null;
                            if (innerTypeName != "XTypedElement")
                            {
                                wrappingPropertyInfo = new ClrWrappingPropertyInfo();
                                foreach (CodeTypeMember member in innerTypeDecl.Members)
                                {
                                    CodeMemberProperty memberProperty = member as CodeMemberProperty;
                                    if (this.ForwardProperty(memberProperty))
                                    {
                                        wrappingPropertyInfo.Init(memberProperty);
                                        wrapperBuilder.CreateProperty(wrappingPropertyInfo, typeInfo.Annotations);
                                    }
                                }
                            }
                        }
                        builder = wrapperBuilder;
                    }
                    else
                    {
                        typedValPropertyInfo = this.InitializeTypedValuePropertyInfo(typeInfo, typedValPropertyInfo, innerType);
                        simpleTypeBuilder.Init(typedValPropertyInfo.ClrTypeName, innerType.IsSchemaList);
                        simpleTypeBuilder.CreateTypeDeclaration(typeInfo);
                        simpleTypeBuilder.CreateFunctionalConstructor(typeInfo.Annotations);
                        typedValPropertyInfo.SetFixedDefaultValue(typeInfo);
                        simpleTypeBuilder.CreateProperty(typedValPropertyInfo, typeInfo.Annotations);
                        simpleTypeBuilder.AddTypeToTypeManager(CodeDomTypesGenerator.elementDictionaryAddStatements, "elementDictionary");
                        simpleTypeBuilder.ApplyAnnotations(typeInfo);
                        builder = simpleTypeBuilder;
                    }
                    builder.ImplementInterfaces(this.settings.EnableServiceReference);
                    this.codeNamespace = this.GetCodeNamespace(typeInfo.clrtypeNs);
                    this.codeNamespace.Types.Add(builder.TypeDeclaration);
                    this.codeNamespace = this.GetCodeNamespace(typeInfo.clrtypeNs);
                    if (!this.xroots.TryGetValue(this.codeNamespace, out types))
                    {
                        types = new List <CodeTypeDeclaration>();
                        this.xroots.Add(this.codeNamespace, types);
                    }
                    types.Add(builder.TypeDeclaration);
                }
            }
        }
 private ClrPropertyInfo InitializeTypedValuePropertyInfo(ClrTypeInfo typeInfo, ClrPropertyInfo typedValPropertyInfo, ClrTypeReference innerType)
 {
     if (typedValPropertyInfo != null)
     {
         typedValPropertyInfo.Reset();
     }
     else
     {
         typedValPropertyInfo = new ClrPropertyInfo("TypedValue", string.Empty, "TypedValue", Occurs.One)
         {
             Origin = SchemaOrigin.Text
         };
     }
     typedValPropertyInfo.TypeReference = innerType;
     if (typeInfo.IsSubstitutionMember())
     {
         typedValPropertyInfo.IsNew = true;
     }
     typedValPropertyInfo.UpdateTypeReference(this.currentFullTypeName, this.currentNamespace, this.nameMappings);
     return(typedValPropertyInfo);
 }
        private void TraverseParticle(XmlSchemaParticle particle, XmlSchemaComplexType baseType, ClrContentTypeInfo typeInfo, XmlSchemaDerivationMethod derivationMethod)
        {
            if (this.particleStack != null)
            {
                this.particleStack.Clear();
            }
            else
            {
                this.particleStack = new Stack <XsdToTypesConverter.ParticleData>();
            }
            if (this.propertyNameTypeTable != null)
            {
                this.propertyNameTypeTable.Clear();
            }
            else
            {
                this.propertyNameTypeTable = new Dictionary <string, ClrPropertyInfo>();
            }
            XmlSchemaParticle  baseParticle        = baseType.ContentTypeParticle;
            GroupingInfo       parentGroupInfo     = null;
            StringBuilder      regEx               = new StringBuilder();
            XmlSchemaGroupBase currentGroupBase    = null;
            GroupingInfo       currentGroupingInfo = null;
            int currentIndex = 0;

            while (true)
            {
                if ((currentGroupBase == null ? true : currentIndex <= currentGroupBase.Items.Count))
                {
                    ParticleType particleType = particle.GetParticleType();
                    switch (particleType)
                    {
                    case ParticleType.Sequence:
                    case ParticleType.Choice:
                    case ParticleType.All:
                    {
                        regEx.Append("(");
                        if (currentGroupBase != null)
                        {
                            this.particleStack.Push(new XsdToTypesConverter.ParticleData(currentGroupBase, currentGroupingInfo, currentIndex));
                            currentIndex = 0;
                        }
                        parentGroupInfo  = currentGroupingInfo;
                        currentGroupBase = particle as XmlSchemaGroupBase;
                        Debug.Assert(currentGroupBase != null);
                        currentGroupingInfo = new GroupingInfo((ContentModelType)particleType, this.GetOccurence(currentGroupBase));
                        if (parentGroupInfo != null)
                        {
                            parentGroupInfo.AddChild(currentGroupingInfo);
                            parentGroupInfo.HasChildGroups = true;
                            currentGroupingInfo.IsNested   = true;
                            if (parentGroupInfo.IsRepeating)
                            {
                                currentGroupingInfo.IsRepeating = true;
                            }
                            if (currentGroupingInfo.IsRepeating)
                            {
                                parentGroupInfo.HasRepeatingGroups = true;
                            }
                        }
                        else
                        {
                            typeInfo.AddMember(currentGroupingInfo);
                            parentGroupInfo = currentGroupingInfo;
                        }
                        break;
                    }

                    case ParticleType.Element:
                    {
                        XmlSchemaElement elem         = particle as XmlSchemaElement;
                        ClrPropertyInfo  propertyInfo = null;
                        bool             fromBaseType = false;
                        if ((derivationMethod != XmlSchemaDerivationMethod.Extension ? false : typeInfo.IsDerived))
                        {
                            if (baseParticle.ContainsElement(elem))
                            {
                                fromBaseType = true;
                            }
                            else if ((typeInfo.InlineBaseType ? false : baseType.ContainsName(elem.QualifiedName)))
                            {
                                typeInfo.InlineBaseType = true;
                            }
                        }
                        propertyInfo = this.BuildProperty(elem, fromBaseType);
                        regEx.Append(propertyInfo.PropertyName);
                        this.AppendOccurenceToRegex(propertyInfo, regEx);
                        if (currentGroupingInfo != null)
                        {
                            this.BuildAnnotationInformation(propertyInfo, elem, currentGroupingInfo.ContentModelType == ContentModelType.Choice, currentGroupingInfo.IsNested);
                            currentGroupingInfo.AddChild(propertyInfo);
                            this.SetPropertyFlags(propertyInfo, currentGroupingInfo, elem.ElementSchemaType);
                        }
                        else
                        {
                            this.BuildAnnotationInformation(propertyInfo, elem, false, false);
                            typeInfo.AddMember(propertyInfo);
                        }
                        break;
                    }

                    case ParticleType.Any:
                    {
                        regEx.Append("any");
                        XmlSchemaAny any = particle as XmlSchemaAny;
                        if ((derivationMethod != XmlSchemaDerivationMethod.Extension ? false : typeInfo.IsDerived))
                        {
                            if (baseParticle.ContainsWildCard(any))
                            {
                                typeInfo.HasElementWildCard = true;
                            }
                        }
                        ClrWildCardPropertyInfo wcPropertyInfo = this.BuildAnyProperty(any, !typeInfo.HasElementWildCard);
                        if (currentGroupingInfo != null)
                        {
                            currentGroupingInfo.AddChild(wcPropertyInfo);
                        }
                        else
                        {
                            typeInfo.AddMember(wcPropertyInfo);
                        }
                        if (!typeInfo.HasElementWildCard)
                        {
                            typeInfo.HasElementWildCard = true;
                        }
                        break;
                    }
                    }
                }
                if ((currentGroupBase == null ? true : currentIndex >= currentGroupBase.Items.Count))
                {
                    if (currentGroupBase != null)
                    {
                        regEx.Append(")");
                        this.AppendOccurenceToRegex(currentGroupingInfo, regEx);
                    }
                    if (this.particleStack.Count <= 0)
                    {
                        break;
                    }
                    bool childGroupHasRecurringElements           = currentGroupingInfo.HasRecurrentElements;
                    bool childGroupHasRepeatingGroups             = currentGroupingInfo.HasRepeatingGroups;
                    XsdToTypesConverter.ParticleData particleData = this.particleStack.Pop();
                    currentGroupBase    = particleData.currentGroupBase;
                    currentGroupingInfo = particleData.currentGroupingInfo;
                    currentGroupingInfo.HasRecurrentElements = childGroupHasRecurringElements;
                    currentGroupingInfo.HasRepeatingGroups   = childGroupHasRepeatingGroups;
                    currentIndex = particleData.currentIndex;
                    if (currentIndex >= currentGroupBase.Items.Count)
                    {
                        currentIndex++;
                    }
                    else
                    {
                        int num = currentIndex;
                        currentIndex = num + 1;
                        particle     = (XmlSchemaParticle)currentGroupBase.Items[num];
                        regEx.Append((currentGroupingInfo.ContentModelType == ContentModelType.Choice ? "|" : ", "));
                    }
                }
                else
                {
                    if (currentIndex > 0)
                    {
                        regEx.Append((currentGroupingInfo.ContentModelType == ContentModelType.Choice ? " | " : ", "));
                    }
                    int num1 = currentIndex;
                    currentIndex = num1 + 1;
                    particle     = (XmlSchemaParticle)currentGroupBase.Items[num1];
                }
            }
            if (regEx.Length != 0)
            {
                typeInfo.ContentModelRegEx = regEx.ToString();
            }
        }
        private ClrPropertyInfo BuildComplexTypeTextProperty(XmlSchemaElement parentElement, XmlSchemaComplexType schemaType)
        {
            Debug.Assert(schemaType != null);
            Debug.Assert(schemaType.GetContentType() == XmlSchemaContentType.TextOnly);

            ClrPropertyInfo textProperty = new ClrPropertyInfo(Constants.SInnerTypePropertyName, string.Empty, Constants.SInnerTypePropertyName, Occurs.One);

            textProperty.Origin = SchemaOrigin.Text;
            ClrTypeReference typeRef = null;
            bool anonymous = false;

            //Could be derived by restriction or extension
            //If first time extension, make the base simple type as the type reference
            XmlSchemaType baseType = schemaType.BaseXmlSchemaType;
            if (baseType is XmlSchemaSimpleType)
            {
                typeRef = BuildTypeReference(baseType, baseType.QualifiedName, false, true);
                anonymous = false;
                if (!textPropInheritanceTracker.ContainsKey(schemaType))
                {
                    textPropInheritanceTracker.Add(schemaType, textProperty);
                }
            }
            else if (schemaType.HasFacetRestrictions())
            {//Derived by restriction, represents the content type with restrictions as a local type
                //Make the base simple type as the type reference so that we know if it is a list, union or atomic
                XmlSchemaSimpleType st = schemaType.GetBaseSimpleType();
                Debug.Assert(st != null);
                typeRef = BuildTypeReference(st, st.QualifiedName, true, true);
                typeRef.Validate = true;
                anonymous = true;

                //Also get its base complex type and see if we need to override the content property
                ClrPropertyInfo baseProp = null;
                if (textPropInheritanceTracker.TryGetValue(baseType, out baseProp))
                {
                    textProperty.IsOverride = true;
                    if (!baseProp.IsOverride)
                    {
                        baseProp.IsVirtual = true;
                    }
                }
            }
            else
            {
                return null;
            }

            if (anonymous)
            { //anonymous type, fixed up the name later, treat complex type with restrictions as an anonymous type
                //because we need to generate a type to encapsualte these restrictions
                if (parentElement != null)
                {
                    string identifier = localSymbolTable.AddLocalElement(parentElement);
                    localSymbolTable.AddAnonymousType(identifier, parentElement, typeRef);
                }
                else
                {
                    localSymbolTable.AddComplexRestrictedContentType(schemaType, typeRef);
                }
            }

            textProperty.TypeReference = typeRef;

            return textProperty;
        }
        private ClrPropertyInfo BuildProperty(XmlSchemaElement elem, bool fromBaseType)
        {
            string identifierName = localSymbolTable.AddLocalElement(elem);

            XmlSchemaType schemaType = elem.ElementSchemaType;
            XmlQualifiedName schemaTypeName = schemaType.QualifiedName;
            string schemaName = elem.QualifiedName.Name;
            string schemaNs = elem.QualifiedName.Namespace;
            string clrNs =
                elem.FormResolved() == XmlSchemaForm.Qualified ?
                    configSettings.GetClrNamespace(schemaNs) : string.Empty;

            SchemaOrigin typeRefOrigin = SchemaOrigin.Fragment;
            bool isTypeRef = false;
            bool anonymousType = schemaTypeName.IsEmpty;
            XmlSchemaObject schemaObject = schemaType;

            ArrayList substitutionMembers = null;
            if (elem.IsGlobal())
            {
                substitutionMembers = IsSubstitutionGroupHead(elem);
                schemaTypeName = elem.QualifiedName;
                isTypeRef = true;
                typeRefOrigin = SchemaOrigin.Element;
                // For ref, get the element decl SOM object, as nameMappings are
                // keyed off the SOM object
                schemaObject = schemas.GlobalElements[schemaTypeName];
                anonymousType = false;
            }

            ClrTypeReference typeRef = BuildTypeReference(
                schemaObject, schemaTypeName, anonymousType, true);
            typeRef.Origin = typeRefOrigin;
            typeRef.IsTypeRef = isTypeRef;
            if (anonymousType && !fromBaseType)
            {
                //to fixup later.
                localSymbolTable.AddAnonymousType(identifierName, elem, typeRef);
            }
            ClrPropertyInfo propertyInfo =
                new ClrPropertyInfo(
                    identifierName, schemaNs, schemaName, GetOccurence(elem));
            propertyInfo.Origin = SchemaOrigin.Element;
            propertyInfo.FromBaseType = fromBaseType;
            propertyInfo.TypeReference = typeRef;

            //SetFixedDefaultValue(elem, propertyInfo);

            if (substitutionMembers != null)
            {
                propertyInfo.SubstitutionMembers = substitutionMembers;
            }
            //BuildAnnotationInformation(propertyInfo, elem);
            return propertyInfo;
            //Place it in the element's namespace, maybe element's parent type's namespace?
        }
        private void TraverseParticle(XmlSchemaParticle particle, XmlSchemaComplexType baseType,
                                      ClrContentTypeInfo typeInfo, XmlSchemaDerivationMethod derivationMethod)
        {
            if (particleStack == null)
            {
                particleStack = new Stack <ParticleData>();
            }
            else
            {
                particleStack.Clear();
            }

            if (propertyNameTypeTable == null)
            {
                propertyNameTypeTable = new Dictionary <string, ClrPropertyInfo>();
            }
            else
            {
                propertyNameTypeTable.Clear();
            }

            XmlSchemaParticle baseParticle = baseType.ContentTypeParticle;
            ParticleData      particleData;
            GroupingInfo      parentGroupInfo = null;
            StringBuilder     regEx           = new StringBuilder();

            XmlSchemaGroupBase currentGroupBase    = null;
            GroupingInfo       currentGroupingInfo = null;

            int currentIndex = 0;

            while (true)
            {
                // dont interrogate a particle if we are past the end of the list
                if (currentGroupBase == null || currentIndex <= currentGroupBase.Items.Count)
                {
                    ParticleType particleType = particle.GetParticleType();
                    switch (particleType)
                    {
                    case ParticleType.Element:
                    {
                        XmlSchemaElement elem         = particle as XmlSchemaElement;
                        ClrPropertyInfo  propertyInfo = null;
                        bool             fromBaseType = false;
                        if (derivationMethod == XmlSchemaDerivationMethod.Extension && typeInfo.IsDerived)
                        {
                            if (baseParticle.ContainsElement(elem))
                            {
                                fromBaseType = true;
                            }
                            else if (!typeInfo.InlineBaseType && baseType.ContainsName(elem.QualifiedName))
                            {
                                typeInfo.InlineBaseType = true;
                            }
                        }

                        propertyInfo = BuildProperty(elem, fromBaseType);
                        regEx.Append(propertyInfo.PropertyName);
                        AppendOccurenceToRegex(propertyInfo, regEx);
                        //Add to parent
                        if (currentGroupingInfo == null)
                        {
                            //Not adding property to propertyNameTypeTable as this case will occur only for pointless groups, so they have just one property
                            BuildAnnotationInformation(propertyInfo, elem, false, false);
                            typeInfo.AddMember(propertyInfo);
                        }
                        else
                        {
                            BuildAnnotationInformation(propertyInfo, elem,
                                                       currentGroupingInfo.ContentModelType == ContentModelType.Choice,
                                                       currentGroupingInfo.IsNested);
                            currentGroupingInfo.AddChild(propertyInfo);
                            SetPropertyFlags(propertyInfo, currentGroupingInfo, elem.ElementSchemaType);
                        }

                        break;
                    }

                    case ParticleType.Any:
                    {
                        regEx.Append("any");
                        XmlSchemaAny any = particle as XmlSchemaAny;

                        if (derivationMethod == XmlSchemaDerivationMethod.Extension && typeInfo.IsDerived)
                        {
                            if (baseParticle.ContainsWildCard(any))
                            {
                                typeInfo.HasElementWildCard = true;     //ANY property in the base type will be reused
                            }
                        }

                        //Note we always create a property info object to keep the original nesting structure in the schema
                        //so it can be used to create a correct FSM; on the other hand, typeInfo.HasElementWildCard will indicate whether
                        //we need to create a property in the resulting object type.
                        ClrWildCardPropertyInfo wcPropertyInfo =
                            BuildAnyProperty(any, !typeInfo.HasElementWildCard);


                        //Add to parent
                        if (currentGroupingInfo == null)
                        {
                            typeInfo.AddMember(wcPropertyInfo);
                        }
                        else
                        {
                            currentGroupingInfo.AddChild(wcPropertyInfo);
                        }

                        if (!typeInfo.HasElementWildCard)
                        {
                            typeInfo.HasElementWildCard = true;
                        }
                        break;
                    }

                    case ParticleType.Sequence:
                    case ParticleType.Choice:
                    case ParticleType.All:
                        regEx.Append("(");
                        if (currentGroupBase != null)
                        {
                            //already there is a group that we are processing, push it on stack to process sub-group
                            particleStack.Push(
                                new ParticleData(currentGroupBase, currentGroupingInfo, currentIndex));
                            currentIndex = 0;     //Re-start index for new group base
                        }

                        parentGroupInfo  = currentGroupingInfo;    //Assign parent before creating child groupInfo
                        currentGroupBase = particle as XmlSchemaGroupBase;
                        Debug.Assert(currentGroupBase != null);
                        currentGroupingInfo = new GroupingInfo((ContentModelType)((int)particleType),
                                                               GetOccurence(currentGroupBase));

                        //Add to parent
                        if (parentGroupInfo == null)
                        {
                            typeInfo.AddMember(currentGroupingInfo);
                            parentGroupInfo = currentGroupingInfo;     //Assign first time
                        }
                        else
                        {
                            parentGroupInfo.AddChild(currentGroupingInfo);
                            parentGroupInfo.HasChildGroups = true;
                            currentGroupingInfo.IsNested   = true;
                            if (parentGroupInfo.IsRepeating)
                            {
                                currentGroupingInfo.IsRepeating = true;
                            }

                            if (currentGroupingInfo.IsRepeating)
                            {
                                parentGroupInfo.HasRepeatingGroups = true;
                            }
                        }

                        break;
                    }
                }

                //Drill down into items
                if (currentGroupBase != null && currentIndex < currentGroupBase.Items.Count)
                {
                    // if this isnt the first, then we need a seperator
                    if (currentIndex > 0)
                    {
                        regEx.Append(currentGroupingInfo.ContentModelType == ContentModelType.Choice ? " | " : ", ");
                    }

                    particle = (XmlSchemaParticle)currentGroupBase.Items[currentIndex++];
                }
                else
                {
                    if (currentGroupBase != null)
                    {
                        regEx.Append(")");
                        AppendOccurenceToRegex(currentGroupingInfo, regEx);
                    }

                    if (particleStack.Count > 0)
                    {
                        bool childGroupHasRecurringElements = currentGroupingInfo.HasRecurrentElements;
                        bool childGroupHasRepeatingGroups   = currentGroupingInfo.HasRepeatingGroups;

                        particleData        = particleStack.Pop();
                        currentGroupBase    = particleData.currentGroupBase;
                        currentGroupingInfo = particleData.currentGroupingInfo;

                        currentGroupingInfo.HasRecurrentElements = childGroupHasRecurringElements;
                        currentGroupingInfo.HasRepeatingGroups   = childGroupHasRepeatingGroups;

                        currentIndex = particleData.currentIndex;
                        if (currentIndex < currentGroupBase.Items.Count)
                        {
                            particle = (XmlSchemaParticle)currentGroupBase.Items[currentIndex++];
                            regEx.Append(currentGroupingInfo.ContentModelType == ContentModelType.Choice ? "|" : ", ");
                        }
                        else
                        {
                            // we were already at the end of the parent group, so just continue
                            currentIndex++; //we are off the end of this list
                        }
                    }
                    else
                    {
                        //No more particles to process
                        break;
                    }
                }
            }

            if (regEx.Length != 0)
            {
                typeInfo.ContentModelRegEx = regEx.ToString();
            }
        }
        private ClrPropertyInfo BuildSimpleTypeTextProperty(
            XmlSchemaElement parentElement, XmlSchemaSimpleType schemaType)
        {
            Debug.Assert(schemaType != null);
            var textProperty =
                new ClrPropertyInfo(
                    Constants.SInnerTypePropertyName,
                    string.Empty,
                    Constants.SInnerTypePropertyName,
                    Occurs.One);
            textProperty.Origin = SchemaOrigin.Text;

            bool anonymous = schemaType.QualifiedName.IsEmpty;
            ClrTypeReference typeRef = BuildTypeReference(schemaType, schemaType.QualifiedName, anonymous, true);
            textProperty.TypeReference = typeRef;

            if (anonymous && parentElement != null)
            { //anonymous type, fixed up the name later
                string idenfitier = localSymbolTable.AddLocalElement(parentElement);
                localSymbolTable.AddAnonymousType(idenfitier, parentElement, typeRef);
            }

            return textProperty;
        }
        private ClrPropertyInfo BuildComplexTypeTextProperty(XmlSchemaElement parentElement,
                                                             XmlSchemaComplexType schemaType)
        {
            Debug.Assert(schemaType != null);
            Debug.Assert(schemaType.GetContentType() == XmlSchemaContentType.TextOnly);

            ClrPropertyInfo textProperty = new ClrPropertyInfo(Constants.SInnerTypePropertyName, string.Empty,
                                                               Constants.SInnerTypePropertyName, Occurs.One);

            textProperty.Origin = SchemaOrigin.Text;
            ClrTypeReference typeRef   = null;
            bool             anonymous = false;

            //Could be derived by restriction or extension
            //If first time extension, make the base simple type as the type reference
            XmlSchemaType baseType = schemaType.BaseXmlSchemaType;

            if (baseType is XmlSchemaSimpleType)
            {
                typeRef   = BuildTypeReference(baseType, baseType.QualifiedName, false, true);
                anonymous = false;
                if (!textPropInheritanceTracker.ContainsKey(schemaType))
                {
                    textPropInheritanceTracker.Add(schemaType, textProperty);
                }
            }
            else if (schemaType.HasFacetRestrictions())
            {
                //Derived by restriction, represents the content type with restrictions as a local type
                //Make the base simple type as the type reference so that we know if it is a list, union or atomic
                XmlSchemaSimpleType st = schemaType.GetBaseSimpleType();
                Debug.Assert(st != null);
                typeRef          = BuildTypeReference(st, st.QualifiedName, true, true);
                typeRef.Validate = true;
                anonymous        = true;

                //Also get its base complex type and see if we need to override the content property
                ClrPropertyInfo baseProp = null;
                if (textPropInheritanceTracker.TryGetValue(baseType, out baseProp))
                {
                    textProperty.IsOverride = true;
                    if (!baseProp.IsOverride)
                    {
                        baseProp.IsVirtual = true;
                    }
                }
            }
            else
            {
                return(null);
            }

            if (anonymous)
            {
                //anonymous type, fixed up the name later, treat complex type with restrictions as an anonymous type
                //because we need to generate a type to encapsualte these restrictions
                if (parentElement != null)
                {
                    string identifier = localSymbolTable.AddLocalElement(parentElement);
                    localSymbolTable.AddAnonymousType(identifier, parentElement, typeRef);
                }
                else
                {
                    localSymbolTable.AddComplexRestrictedContentType(schemaType, typeRef);
                }
            }

            textProperty.TypeReference = typeRef;

            return(textProperty);
        }
        private void SetPropertyFlags(ClrPropertyInfo propertyInfo, GroupingInfo currentGroupingInfo, XmlSchemaType propertyType)
        {
            propertyInfo.IsNullable |= currentGroupingInfo.ContentModelType == ContentModelType.Choice || currentGroupingInfo.IsOptional;
            propertyInfo.VerifyRequired = configSettings.VerifyRequired;

            if (currentGroupingInfo.IsRepeating)
            {
                propertyInfo.IsList = true;
            }
            string propertyName = propertyInfo.PropertyName;
            ClrPropertyInfo prevProperty = null;
            if (propertyNameTypeTable.TryGetValue(propertyName, out prevProperty))
            {
                currentGroupingInfo.HasRecurrentElements = true;
                propertyInfo.IsDuplicate = true;
                prevProperty.IsList = true; //Change the first one to list
            }
            else
            {
                propertyNameTypeTable.Add(propertyName, propertyInfo);
            }
        }
        private ClrPropertyInfo BuildProperty(XmlSchemaElement elem, bool fromBaseType)
        {
            string identifierName = localSymbolTable.AddLocalElement(elem);

            XmlSchemaType    schemaType     = elem.ElementSchemaType;
            XmlQualifiedName schemaTypeName = schemaType.QualifiedName;
            string           schemaName     = elem.QualifiedName.Name;
            string           schemaNs       = elem.QualifiedName.Namespace;
            string           clrNs          = elem.FormResolved() == XmlSchemaForm.Qualified
                ? configSettings.GetClrNamespace(schemaNs)
                : string.Empty;


            SchemaOrigin    typeRefOrigin = SchemaOrigin.Fragment;
            bool            isTypeRef     = false;
            bool            anonymousType = schemaTypeName.IsEmpty ? true : false;
            XmlSchemaObject schemaObject  = schemaType;

            ArrayList substitutionMembers = null;

            if (elem.IsGlobal())
            {
                substitutionMembers = IsSubstitutionGroupHead(elem);
                schemaTypeName      = elem.QualifiedName;
                isTypeRef           = true;
                typeRefOrigin       = SchemaOrigin.Element;
                schemaObject        =
                    schemas.GlobalElements
                    [schemaTypeName];     //For ref, get the element decl SOM object, as nameMappings are keyed off the SOM object
                anonymousType = false;
            }

            ClrTypeReference typeRef = BuildTypeReference(schemaObject, schemaTypeName, anonymousType, true);

            typeRef.Origin    = typeRefOrigin;
            typeRef.IsTypeRef = isTypeRef;
            if (anonymousType && !fromBaseType)
            {
                //to fixup later.
                localSymbolTable.AddAnonymousType(identifierName, elem, typeRef);
            }

            ClrPropertyInfo propertyInfo =
                new ClrPropertyInfo(identifierName, schemaNs, schemaName, GetOccurence(elem));

            propertyInfo.Origin        = SchemaOrigin.Element;
            propertyInfo.FromBaseType  = fromBaseType;
            propertyInfo.TypeReference = typeRef;
            propertyInfo.ClrNamespace  = clrNs;

            //SetFixedDefaultValue(elem, propertyInfo);

            if (substitutionMembers != null)
            {
                propertyInfo.SubstitutionMembers = substitutionMembers;
            }

            //BuildAnnotationInformation(propertyInfo, elem);
            return(propertyInfo);
            //Place it in the element's namespace, maybe element's parent type's namespace?
        }