private void Preprocess()

in src/dotnet-svcutil/lib/src/FrameworkFork/Microsoft.Xml/Xml/schema/SchemaCollectionPreprocessor.cs [292:593]


        private void Preprocess(XmlSchema schema, string targetNamespace, Compositor compositor)
        {
            if (schema.IsProcessing)
            {
                return;
            }
            schema.IsProcessing = true;
            string tns = schema.TargetNamespace;
            if (tns != null)
            {
                schema.TargetNamespace = tns = NameTable.Add(tns);
                if (tns.Length == 0)
                {
                    SendValidationEvent(ResXml.Sch_InvalidTargetNamespaceAttribute, schema);
                }
                else
                {
                    try
                    {
                        XmlConvert.ToUri(tns);  // can throw
                    }
                    catch
                    {
                        SendValidationEvent(ResXml.Sch_InvalidNamespace, schema.TargetNamespace, schema);
                    }
                }
            }

            if (schema.Version != null)
            {
                try
                {
                    XmlConvert.VerifyTOKEN(schema.Version); // can throw
                }
                catch (Exception)
                {
                    SendValidationEvent(ResXml.Sch_AttributeValueDataType, "version", schema);
                }
            }
            switch (compositor)
            {
                case Compositor.Root:
                    if (targetNamespace == null && schema.TargetNamespace != null)
                    { // not specified
                        targetNamespace = schema.TargetNamespace;
                    }
                    else if (schema.TargetNamespace == null && targetNamespace != null && targetNamespace.Length == 0)
                    { // no namespace schema
                        targetNamespace = null;
                    }
                    if (targetNamespace != schema.TargetNamespace)
                    {
                        SendValidationEvent(ResXml.Sch_MismatchTargetNamespaceEx, targetNamespace, schema.TargetNamespace, schema);
                    }
                    break;
                case Compositor.Import:
                    if (targetNamespace != schema.TargetNamespace)
                    {
                        SendValidationEvent(ResXml.Sch_MismatchTargetNamespaceImport, targetNamespace, schema.TargetNamespace, schema);
                    }
                    break;
                case Compositor.Include:
                    if (schema.TargetNamespace != null)
                    {
                        if (targetNamespace != schema.TargetNamespace)
                        {
                            SendValidationEvent(ResXml.Sch_MismatchTargetNamespaceInclude, targetNamespace, schema.TargetNamespace, schema);
                        }
                    }
                    break;
            }

            for (int i = 0; i < schema.Includes.Count; ++i)
            {
                XmlSchemaExternal include = (XmlSchemaExternal)schema.Includes[i];
                SetParent(include, schema);
                PreprocessAnnotation(include);

                string loc = include.SchemaLocation;
                if (loc != null)
                {
                    try
                    {
                        XmlConvert.ToUri(loc); // can throw
                    }
                    catch
                    {
                        SendValidationEvent(ResXml.Sch_InvalidSchemaLocation, loc, include);
                    }
                }
                else if ((include is XmlSchemaRedefine || include is XmlSchemaInclude) && include.Schema == null)
                {
                    SendValidationEvent(ResXml.Sch_MissRequiredAttribute, "schemaLocation", include);
                }
                if (include.Schema != null)
                {
                    if (include is XmlSchemaRedefine)
                    {
                        Preprocess(include.Schema, schema.TargetNamespace, Compositor.Include);
                    }
                    else if (include is XmlSchemaImport)
                    {
                        if (((XmlSchemaImport)include).Namespace == null && schema.TargetNamespace == null)
                        {
                            SendValidationEvent(ResXml.Sch_ImportTargetNamespaceNull, include);
                        }
                        else if (((XmlSchemaImport)include).Namespace == schema.TargetNamespace)
                        {
                            SendValidationEvent(ResXml.Sch_ImportTargetNamespace, include);
                        }
                        Preprocess(include.Schema, ((XmlSchemaImport)include).Namespace, Compositor.Import);
                    }
                    else
                    {
                        Preprocess(include.Schema, schema.TargetNamespace, Compositor.Include);
                    }
                }
                else if (include is XmlSchemaImport)
                {
                    string ns = ((XmlSchemaImport)include).Namespace;
                    if (ns != null)
                    {
                        if (ns.Length == 0)
                        {
                            SendValidationEvent(ResXml.Sch_InvalidNamespaceAttribute, ns, include);
                        }
                        else
                        {
                            try
                            {
                                XmlConvert.ToUri(ns); //can throw
                            }
                            catch (FormatException)
                            {
                                SendValidationEvent(ResXml.Sch_InvalidNamespace, ns, include);
                            }
                        }
                    }
                }
            }

            //Begin processing the current schema passed to preprocess
            //Build the namespaces that can be referenced in the current schema
            BuildRefNamespaces(schema);

            _targetNamespace = targetNamespace == null ? string.Empty : targetNamespace;

            if (schema.BlockDefault == XmlSchemaDerivationMethod.All)
            {
                _blockDefault = XmlSchemaDerivationMethod.All;
            }
            else if (schema.BlockDefault == XmlSchemaDerivationMethod.None)
            {
                _blockDefault = XmlSchemaDerivationMethod.Empty;
            }
            else
            {
                if ((schema.BlockDefault & ~schemaBlockDefaultAllowed) != 0)
                {
                    SendValidationEvent(ResXml.Sch_InvalidBlockDefaultValue, schema);
                }
                _blockDefault = schema.BlockDefault & schemaBlockDefaultAllowed;
            }
            if (schema.FinalDefault == XmlSchemaDerivationMethod.All)
            {
                _finalDefault = XmlSchemaDerivationMethod.All;
            }
            else if (schema.FinalDefault == XmlSchemaDerivationMethod.None)
            {
                _finalDefault = XmlSchemaDerivationMethod.Empty;
            }
            else
            {
                if ((schema.FinalDefault & ~schemaFinalDefaultAllowed) != 0)
                {
                    SendValidationEvent(ResXml.Sch_InvalidFinalDefaultValue, schema);
                }
                _finalDefault = schema.FinalDefault & schemaFinalDefaultAllowed;
            }
            _elementFormDefault = schema.ElementFormDefault;
            if (_elementFormDefault == XmlSchemaForm.None)
            {
                _elementFormDefault = XmlSchemaForm.Unqualified;
            }
            _attributeFormDefault = schema.AttributeFormDefault;
            if (_attributeFormDefault == XmlSchemaForm.None)
            {
                _attributeFormDefault = XmlSchemaForm.Unqualified;
            }

            for (int i = 0; i < schema.Includes.Count; ++i)
            {
                XmlSchemaExternal include = (XmlSchemaExternal)schema.Includes[i];
                if (include is XmlSchemaRedefine)
                {
                    XmlSchemaRedefine redefine = (XmlSchemaRedefine)include;
                    if (include.Schema != null)
                    {
                        PreprocessRedefine(redefine);
                    }
                    else
                    {
                        for (int j = 0; j < redefine.Items.Count; ++j)
                        {
                            if (!(redefine.Items[j] is XmlSchemaAnnotation))
                            {
                                SendValidationEvent(ResXml.Sch_RedefineNoSchema, redefine);
                                break;
                            }
                        }
                    }
                }
                XmlSchema includedSchema = include.Schema;
                if (includedSchema != null)
                {
                    foreach (XmlSchemaElement element in includedSchema.Elements.Values)
                    {
                        AddToTable(schema.Elements, element.QualifiedName, element);
                    }
                    foreach (XmlSchemaAttribute attribute in includedSchema.Attributes.Values)
                    {
                        AddToTable(schema.Attributes, attribute.QualifiedName, attribute);
                    }
                    foreach (XmlSchemaGroup group in includedSchema.Groups.Values)
                    {
                        AddToTable(schema.Groups, group.QualifiedName, group);
                    }
                    foreach (XmlSchemaAttributeGroup attributeGroup in includedSchema.AttributeGroups.Values)
                    {
                        AddToTable(schema.AttributeGroups, attributeGroup.QualifiedName, attributeGroup);
                    }
                    foreach (XmlSchemaType type in includedSchema.SchemaTypes.Values)
                    {
                        AddToTable(schema.SchemaTypes, type.QualifiedName, type);
                    }
                    foreach (XmlSchemaNotation notation in includedSchema.Notations.Values)
                    {
                        AddToTable(schema.Notations, notation.QualifiedName, notation);
                    }
                }
                ValidateIdAttribute(include);
            }

            List<XmlSchemaObject> removeItemsList = new List<XmlSchemaObject>();
            for (int i = 0; i < schema.Items.Count; ++i)
            {
                SetParent(schema.Items[i], schema);
                XmlSchemaAttribute attribute = schema.Items[i] as XmlSchemaAttribute;
                if (attribute != null)
                {
                    PreprocessAttribute(attribute);
                    AddToTable(schema.Attributes, attribute.QualifiedName, attribute);
                }
                else if (schema.Items[i] is XmlSchemaAttributeGroup)
                {
                    XmlSchemaAttributeGroup attributeGroup = (XmlSchemaAttributeGroup)schema.Items[i];
                    PreprocessAttributeGroup(attributeGroup);
                    AddToTable(schema.AttributeGroups, attributeGroup.QualifiedName, attributeGroup);
                }
                else if (schema.Items[i] is XmlSchemaComplexType)
                {
                    XmlSchemaComplexType complexType = (XmlSchemaComplexType)schema.Items[i];
                    PreprocessComplexType(complexType, false);
                    AddToTable(schema.SchemaTypes, complexType.QualifiedName, complexType);
                }
                else if (schema.Items[i] is XmlSchemaSimpleType)
                {
                    XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)schema.Items[i];
                    PreprocessSimpleType(simpleType, false);
                    AddToTable(schema.SchemaTypes, simpleType.QualifiedName, simpleType);
                }
                else if (schema.Items[i] is XmlSchemaElement)
                {
                    XmlSchemaElement element = (XmlSchemaElement)schema.Items[i];
                    PreprocessElement(element);
                    AddToTable(schema.Elements, element.QualifiedName, element);
                }
                else if (schema.Items[i] is XmlSchemaGroup)
                {
                    XmlSchemaGroup group = (XmlSchemaGroup)schema.Items[i];
                    PreprocessGroup(group);
                    AddToTable(schema.Groups, group.QualifiedName, group);
                }
                else if (schema.Items[i] is XmlSchemaNotation)
                {
                    XmlSchemaNotation notation = (XmlSchemaNotation)schema.Items[i];
                    PreprocessNotation(notation);
                    AddToTable(schema.Notations, notation.QualifiedName, notation);
                }
                else if (!(schema.Items[i] is XmlSchemaAnnotation))
                {
                    SendValidationEvent(ResXml.Sch_InvalidCollection, schema.Items[i]);
                    removeItemsList.Add(schema.Items[i]);
                }
            }
            for (int i = 0; i < removeItemsList.Count; ++i)
            {
                schema.Items.Remove(removeItemsList[i]);
            }

            schema.IsProcessing = false;
        }