private void PreprocessRedefine(RedefineEntry redefineEntry)
 {
     XmlSchemaRedefine currentSchemaObject = redefineEntry.redefine;
     XmlSchema schema = currentSchemaObject.Schema;
     this.currentSchema = GetParentSchema(currentSchemaObject);
     this.SetSchemaDefaults(this.currentSchema);
     if (schema.IsRedefined)
     {
         base.SendValidationEvent("Sch_MultipleRedefine", currentSchemaObject, XmlSeverityType.Warning);
     }
     else
     {
         schema.IsRedefined = true;
         XmlSchema schemaToUpdate = redefineEntry.schemaToUpdate;
         ArrayList includesList = new ArrayList();
         this.GetIncludedSet(schema, includesList);
         string ns = (schemaToUpdate.TargetNamespace == null) ? string.Empty : schemaToUpdate.TargetNamespace;
         XmlSchemaObjectCollection items = currentSchemaObject.Items;
         for (int i = 0; i < items.Count; i++)
         {
             this.SetParent(items[i], currentSchemaObject);
             XmlSchemaGroup group = items[i] as XmlSchemaGroup;
             if (group != null)
             {
                 this.PreprocessGroup(group);
                 group.QualifiedName.SetNamespace(ns);
                 if (currentSchemaObject.Groups[group.QualifiedName] != null)
                 {
                     base.SendValidationEvent("Sch_GroupDoubleRedefine", group);
                 }
                 else
                 {
                     base.AddToTable(currentSchemaObject.Groups, group.QualifiedName, group);
                     XmlSchemaGroup group2 = (XmlSchemaGroup) schemaToUpdate.Groups[group.QualifiedName];
                     XmlSchema parentSchema = GetParentSchema(group2);
                     if ((group2 == null) || ((parentSchema != schema) && !includesList.Contains(parentSchema)))
                     {
                         base.SendValidationEvent("Sch_ComponentRedefineNotFound", "<group>", group.QualifiedName.ToString(), group);
                     }
                     else
                     {
                         group.Redefined = group2;
                         schemaToUpdate.Groups.Insert(group.QualifiedName, group);
                         this.CheckRefinedGroup(group);
                     }
                 }
             }
             else if (items[i] is XmlSchemaAttributeGroup)
             {
                 XmlSchemaAttributeGroup attributeGroup = (XmlSchemaAttributeGroup) items[i];
                 this.PreprocessAttributeGroup(attributeGroup);
                 attributeGroup.QualifiedName.SetNamespace(ns);
                 if (currentSchemaObject.AttributeGroups[attributeGroup.QualifiedName] != null)
                 {
                     base.SendValidationEvent("Sch_AttrGroupDoubleRedefine", attributeGroup);
                 }
                 else
                 {
                     base.AddToTable(currentSchemaObject.AttributeGroups, attributeGroup.QualifiedName, attributeGroup);
                     XmlSchemaAttributeGroup group4 = (XmlSchemaAttributeGroup) schemaToUpdate.AttributeGroups[attributeGroup.QualifiedName];
                     XmlSchema item = GetParentSchema(group4);
                     if ((group4 == null) || ((item != schema) && !includesList.Contains(item)))
                     {
                         base.SendValidationEvent("Sch_ComponentRedefineNotFound", "<attributeGroup>", attributeGroup.QualifiedName.ToString(), attributeGroup);
                     }
                     else
                     {
                         attributeGroup.Redefined = group4;
                         schemaToUpdate.AttributeGroups.Insert(attributeGroup.QualifiedName, attributeGroup);
                         this.CheckRefinedAttributeGroup(attributeGroup);
                     }
                 }
             }
             else if (items[i] is XmlSchemaComplexType)
             {
                 XmlSchemaComplexType complexType = (XmlSchemaComplexType) items[i];
                 this.PreprocessComplexType(complexType, false);
                 complexType.QualifiedName.SetNamespace(ns);
                 if (currentSchemaObject.SchemaTypes[complexType.QualifiedName] != null)
                 {
                     base.SendValidationEvent("Sch_ComplexTypeDoubleRedefine", complexType);
                 }
                 else
                 {
                     base.AddToTable(currentSchemaObject.SchemaTypes, complexType.QualifiedName, complexType);
                     XmlSchemaType type2 = (XmlSchemaType) schemaToUpdate.SchemaTypes[complexType.QualifiedName];
                     XmlSchema schema5 = GetParentSchema(type2);
                     if ((type2 == null) || ((schema5 != schema) && !includesList.Contains(schema5)))
                     {
                         base.SendValidationEvent("Sch_ComponentRedefineNotFound", "<complexType>", complexType.QualifiedName.ToString(), complexType);
                     }
                     else if (type2 is XmlSchemaComplexType)
                     {
                         complexType.Redefined = type2;
                         schemaToUpdate.SchemaTypes.Insert(complexType.QualifiedName, complexType);
                         this.CheckRefinedComplexType(complexType);
                     }
                     else
                     {
                         base.SendValidationEvent("Sch_SimpleToComplexTypeRedefine", complexType);
                     }
                 }
             }
             else if (items[i] is XmlSchemaSimpleType)
             {
                 XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType) items[i];
                 this.PreprocessSimpleType(simpleType, false);
                 simpleType.QualifiedName.SetNamespace(ns);
                 if (currentSchemaObject.SchemaTypes[simpleType.QualifiedName] != null)
                 {
                     base.SendValidationEvent("Sch_SimpleTypeDoubleRedefine", simpleType);
                 }
                 else
                 {
                     base.AddToTable(currentSchemaObject.SchemaTypes, simpleType.QualifiedName, simpleType);
                     XmlSchemaType type4 = (XmlSchemaType) schemaToUpdate.SchemaTypes[simpleType.QualifiedName];
                     XmlSchema schema6 = GetParentSchema(type4);
                     if ((type4 == null) || ((schema6 != schema) && !includesList.Contains(schema6)))
                     {
                         base.SendValidationEvent("Sch_ComponentRedefineNotFound", "<simpleType>", simpleType.QualifiedName.ToString(), simpleType);
                     }
                     else if (type4 is XmlSchemaSimpleType)
                     {
                         simpleType.Redefined = type4;
                         schemaToUpdate.SchemaTypes.Insert(simpleType.QualifiedName, simpleType);
                         this.CheckRefinedSimpleType(simpleType);
                     }
                     else
                     {
                         base.SendValidationEvent("Sch_ComplexToSimpleTypeRedefine", simpleType);
                     }
                 }
             }
         }
     }
 }
        private void PreprocessRedefine(RedefineEntry redefineEntry) {
            XmlSchemaRedefine redefine = redefineEntry.redefine;
            XmlSchema originalSchema = redefine.Schema;

            currentSchema = GetParentSchema(redefine); //Set this for correct schema context in ValidateIdAttribute & ValidateQNameAttribute for redefines
            Debug.Assert(currentSchema != null);
            SetSchemaDefaults(currentSchema);

            if (originalSchema.IsRedefined) {
                SendValidationEvent(Res.Sch_MultipleRedefine, redefine, XmlSeverityType.Warning);
                return;
            }
            originalSchema.IsRedefined = true;

            XmlSchema schemaToUpdate = redefineEntry.schemaToUpdate;
            ArrayList includesOfRedefine = new ArrayList();
            GetIncludedSet(originalSchema, includesOfRedefine);
            string targetNS = schemaToUpdate.TargetNamespace == null ? string.Empty : schemaToUpdate.TargetNamespace;

            XmlSchemaObjectCollection items = redefine.Items;
            for (int i = 0; i < items.Count; ++i) {
                SetParent(items[i], redefine);
                XmlSchemaGroup group = items[i] as XmlSchemaGroup;
                if (group != null) {
                    PreprocessGroup(group);
                    group.QualifiedName.SetNamespace(targetNS); //Since PreprocessGroup will use this.targetNamespace and that will be that of the root schema's
                    if (redefine.Groups[group.QualifiedName] != null) {
                        SendValidationEvent(Res.Sch_GroupDoubleRedefine, group);
                    }
                    else {
                        AddToTable(redefine.Groups, group.QualifiedName, group);
                        XmlSchemaGroup originalGroup = (XmlSchemaGroup)schemaToUpdate.Groups[group.QualifiedName];
                        XmlSchema parentSchema = GetParentSchema(originalGroup);
                        if (originalGroup == null || (parentSchema != originalSchema && !includesOfRedefine.Contains(parentSchema)) ) {
                            SendValidationEvent(Res.Sch_ComponentRedefineNotFound, "<group>", group.QualifiedName.ToString(), group);
                        }
                        else {
                            group.Redefined = originalGroup;
                            schemaToUpdate.Groups.Insert(group.QualifiedName, group);
                            CheckRefinedGroup(group);
                        }
                    }
                } 
                else if (items[i] is XmlSchemaAttributeGroup) {
                    XmlSchemaAttributeGroup attributeGroup = (XmlSchemaAttributeGroup)items[i];
                    PreprocessAttributeGroup(attributeGroup);
                    attributeGroup.QualifiedName.SetNamespace(targetNS); //Since PreprocessAttributeGroup will use this.targetNamespace and that will be that of the root schema's
                    if (redefine.AttributeGroups[attributeGroup.QualifiedName] != null) {
                        SendValidationEvent(Res.Sch_AttrGroupDoubleRedefine, attributeGroup);
                    }
                    else {
                        AddToTable(redefine.AttributeGroups, attributeGroup.QualifiedName, attributeGroup);
                        XmlSchemaAttributeGroup originalAttrGroup = (XmlSchemaAttributeGroup)schemaToUpdate.AttributeGroups[attributeGroup.QualifiedName];
                        XmlSchema parentSchema = GetParentSchema(originalAttrGroup);
                        if (originalAttrGroup == null || (parentSchema != originalSchema && !includesOfRedefine.Contains(parentSchema)) ) {
                            SendValidationEvent(Res.Sch_ComponentRedefineNotFound, "<attributeGroup>", attributeGroup.QualifiedName.ToString(), attributeGroup);
                        }
                        else {
                            attributeGroup.Redefined = originalAttrGroup;
                            schemaToUpdate.AttributeGroups.Insert(attributeGroup.QualifiedName, attributeGroup);
                            CheckRefinedAttributeGroup(attributeGroup);
                        }
                    }
                } 
                else if (items[i] is XmlSchemaComplexType) {
                    XmlSchemaComplexType complexType = (XmlSchemaComplexType)items[i];
                    PreprocessComplexType(complexType, false);
                    complexType.QualifiedName.SetNamespace(targetNS); //Since PreprocessComplexType will use this.targetNamespace and that will be that of the root schema's
                    if (redefine.SchemaTypes[complexType.QualifiedName] != null) {
                        SendValidationEvent(Res.Sch_ComplexTypeDoubleRedefine, complexType);
                    }
                    else {
                        AddToTable(redefine.SchemaTypes, complexType.QualifiedName, complexType);
                        XmlSchemaType originalType = (XmlSchemaType)schemaToUpdate.SchemaTypes[complexType.QualifiedName];
                        XmlSchema parentSchema = GetParentSchema(originalType);
                        if (originalType == null || (parentSchema != originalSchema && !includesOfRedefine.Contains(parentSchema)) ) {
                            SendValidationEvent(Res.Sch_ComponentRedefineNotFound, "<complexType>", complexType.QualifiedName.ToString(), complexType);
                        }
                        else if (originalType is XmlSchemaComplexType) {
                            complexType.Redefined = originalType;
                            schemaToUpdate.SchemaTypes.Insert(complexType.QualifiedName, complexType);
                            CheckRefinedComplexType(complexType);
                        }
                        else {
                            SendValidationEvent(Res.Sch_SimpleToComplexTypeRedefine, complexType);
                        }
                    }
                } 
                else if (items[i] is XmlSchemaSimpleType) {
                    XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)items[i];
                    PreprocessSimpleType(simpleType, false);
                    simpleType.QualifiedName.SetNamespace(targetNS); //Since PreprocessSimpleType will use this.targetNamespace and that will be that of the root schema's
                    if (redefine.SchemaTypes[simpleType.QualifiedName] != null) {
                        SendValidationEvent(Res.Sch_SimpleTypeDoubleRedefine, simpleType);
                    }
                    else {
                        AddToTable(redefine.SchemaTypes, simpleType.QualifiedName, simpleType);
                        XmlSchemaType originalType = (XmlSchemaType)schemaToUpdate.SchemaTypes[simpleType.QualifiedName];
                        XmlSchema parentSchema = GetParentSchema(originalType);
                        if (originalType == null || (parentSchema != originalSchema && !includesOfRedefine.Contains(parentSchema)) ) {
                            SendValidationEvent(Res.Sch_ComponentRedefineNotFound, "<simpleType>", simpleType.QualifiedName.ToString(), simpleType);
                        }
                        else if (originalType is XmlSchemaSimpleType) {
                            simpleType.Redefined = originalType;
                            schemaToUpdate.SchemaTypes.Insert(simpleType.QualifiedName, simpleType);
                            CheckRefinedSimpleType(simpleType);
                        }
                        else {
                            SendValidationEvent(Res.Sch_ComplexToSimpleTypeRedefine, simpleType);
                        }
                    }
                }
            }
        }