System.Xml.Schema.Preprocessor.Preprocess C# (CSharp) Method

Preprocess() private method

private Preprocess ( XmlSchema schema, string targetNamespace, ArrayList imports ) : void
schema XmlSchema
targetNamespace string
imports System.Collections.ArrayList
return void
        private void Preprocess(XmlSchema schema, string targetNamespace, ArrayList imports) {
            XmlSchema prevRootSchemaForRedefine = null;
            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(Res.Sch_InvalidTargetNamespaceAttribute, schema);
                }
                else {
                    ParseUri(tns, Res.Sch_InvalidNamespace, schema);
                }
            }
            if (schema.Version != null) {
                XmlSchemaDatatype tokenDt = DatatypeImplementation.GetSimpleTypeFromTypeCode(XmlTypeCode.Token).Datatype;
                object version;
                Exception exception = tokenDt.TryParseValue(schema.Version, null, null, out version);
                if (exception != null) {
                    SendValidationEvent(Res.Sch_AttributeValueDataTypeDetailed, new string[] { "version", schema.Version, tokenDt.TypeCodeString, exception.Message }, exception, schema);
                }
                else {
                    schema.Version = (string)version;
                }
            }
            
            //Begin processing the schema after checking targetNamespace and verifying chameleon
            Cleanup(schema);

            foreach(XmlSchemaExternal include in schema.Includes) {
                XmlSchema externalSchema = include.Schema;
                SetParent(include, schema);
                PreprocessAnnotation(include);
                string loc = include.SchemaLocation;
                if (loc != null) {
                    ParseUri(loc, Res.Sch_InvalidSchemaLocation, include);
                }
                else if ((include.Compositor == Compositor.Include || include.Compositor == Compositor.Redefine) && include.Schema == null){
                    SendValidationEvent(Res.Sch_MissRequiredAttribute, "schemaLocation", include);

                }
                
                switch (include.Compositor) {
                    case Compositor.Import:
                        XmlSchemaImport import = include as XmlSchemaImport;
                        string importNS = import.Namespace;
                        if (importNS == schema.TargetNamespace) {
                            SendValidationEvent(Res.Sch_ImportTargetNamespace, include);
                        }
                        if (externalSchema != null) {
                            if (importNS != externalSchema.TargetNamespace) {
                                SendValidationEvent(Res.Sch_MismatchTargetNamespaceImport, importNS, externalSchema.TargetNamespace, import);
                            }
                            //SetParent(externalSchema, import);
                            prevRootSchemaForRedefine = rootSchemaForRedefine;
                            rootSchemaForRedefine = externalSchema; //Make the imported schema the root schema for redefines
                            Preprocess(externalSchema, importNS, imports);
                            rootSchemaForRedefine = prevRootSchemaForRedefine; //Reset the root schema for redefines
                        }
                        else {
                            if (importNS != null) {
                                if (importNS.Length == 0) {
                                    SendValidationEvent(Res.Sch_InvalidNamespaceAttribute, importNS, include);                                        
                                }
                                else {
                                    ParseUri(importNS, Res.Sch_InvalidNamespace, include);
                                }
                            }
                        }
                        break;
                    case Compositor.Include:
                        XmlSchema includedSchema = include.Schema;
                        if (includedSchema != null) {
                            //SetParent(includedSchema, include);
                            goto default;
                        }
                        break;

                    case Compositor.Redefine:
                        if (externalSchema != null)  {
                            //SetParent(externalSchema, include); 
                            CleanupRedefine(include);
                            goto default;
                        }
                        break;

                    default: //For include, redefine common case
                        if (externalSchema.TargetNamespace != null) {
                            if (schema.TargetNamespace != externalSchema.TargetNamespace) { //namespaces for includes should be the same
                                SendValidationEvent(Res.Sch_MismatchTargetNamespaceInclude, externalSchema.TargetNamespace, schema.TargetNamespace, include);
                            }
                        }
                        else if (targetNamespace != null && targetNamespace.Length != 0) { //Chameleon redefine
                            externalSchema = GetChameleonSchema(targetNamespace, externalSchema);
                            include.Schema = externalSchema; //Reset the schema property to the cloned schema
                        }
                        Preprocess(externalSchema, schema.TargetNamespace, imports);
                        break;
                }
            }

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

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

            SetSchemaDefaults(schema);

            processedExternals.Clear();
            XmlSchemaExternal external;
            for (int i = 0; i < schema.Includes.Count; i++) {
                external = (XmlSchemaExternal) schema.Includes[i];
                XmlSchema includedSchema = external.Schema;
                if (includedSchema != null) {
                    switch (external.Compositor) {
                        case Compositor.Include:
                            if (processedExternals[includedSchema] != null) {
                                continue;
                            }
                            processedExternals.Add(includedSchema, external);
                            CopyIncludedComponents(includedSchema, schema);
                            break;

                        case Compositor.Redefine:
                            if (redefinedList == null) {
                                redefinedList = new ArrayList();
                            }
                            redefinedList.Add(new RedefineEntry(external as XmlSchemaRedefine, rootSchemaForRedefine));
                            if (processedExternals[includedSchema] != null) {
                                continue;
                            }
                            processedExternals.Add(includedSchema, external);
                            CopyIncludedComponents(includedSchema, schema);
                            break;
                        
                        case Compositor.Import:
                            if (includedSchema != rootSchema) {
                                XmlSchemaImport import = external as XmlSchemaImport;
                                string importNS =  import.Namespace != null ? import.Namespace : string.Empty;
                                if (!imports.Contains(includedSchema)) { //TODO Change ImportedSchemas to Hashtable
                                    imports.Add(includedSchema);
                                }
                                if (!rootSchema.ImportedNamespaces.Contains(importNS)) {
                                    rootSchema.ImportedNamespaces.Add(importNS);
                                }
                            } 
                            break;

                        default:
                            Debug.Assert(false);
                            break;
                    }
                }
                else if (external.Compositor == Compositor.Redefine) {
                    XmlSchemaRedefine redefine = external as XmlSchemaRedefine;
                    if (redefine.BaseUri == null) {
                        foreach(XmlSchemaObject item in redefine.Items) {
                            if (!(item is XmlSchemaAnnotation)) {
                                SendValidationEvent(Res.Sch_RedefineNoSchema, redefine);
                                break;
                            }
                        }
                    }
                }
                ValidateIdAttribute(external);
            }
           
            ArrayList removeItemsList = new ArrayList();
            foreach(XmlSchemaObject item in schema.Items) {
                SetParent(item, schema);
                if (item is XmlSchemaAttribute) {
                    XmlSchemaAttribute attribute = (XmlSchemaAttribute)item;
                    PreprocessAttribute(attribute);
                    AddToTable(schema.Attributes, attribute.QualifiedName, attribute);
                } 
                else if (item is XmlSchemaAttributeGroup) {
                    XmlSchemaAttributeGroup attributeGroup = (XmlSchemaAttributeGroup)item;
                    PreprocessAttributeGroup(attributeGroup);
                    AddToTable(schema.AttributeGroups, attributeGroup.QualifiedName, attributeGroup);
                } 
                else if (item is XmlSchemaComplexType) {
                    XmlSchemaComplexType complexType = (XmlSchemaComplexType)item;
                    PreprocessComplexType(complexType, false);
                    AddToTable(schema.SchemaTypes, complexType.QualifiedName, complexType);
                } 
                else if (item is XmlSchemaSimpleType) {
                    XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)item;
                    PreprocessSimpleType(simpleType, false);
                    AddToTable(schema.SchemaTypes, simpleType.QualifiedName, simpleType);
                } 
                else if (item is XmlSchemaElement) {
                    XmlSchemaElement element = (XmlSchemaElement)item;
                    PreprocessElement(element);
                    AddToTable(schema.Elements, element.QualifiedName, element);
                } 
                else if (item is XmlSchemaGroup) {
                    XmlSchemaGroup group = (XmlSchemaGroup)item;
                    PreprocessGroup(group);
                    AddToTable(schema.Groups, group.QualifiedName, group);
                } 
                else if (item is XmlSchemaNotation) {
                    XmlSchemaNotation notation = (XmlSchemaNotation)item;
                    PreprocessNotation(notation);
                    AddToTable(schema.Notations, notation.QualifiedName, notation);
                }
                else if (item is XmlSchemaAnnotation) {
                    PreprocessAnnotation(item as XmlSchemaAnnotation);
                }
                else {
                    SendValidationEvent(Res.Sch_InvalidCollection,(XmlSchemaObject)item);
                    removeItemsList.Add(item);
                }
            }
            foreach(XmlSchemaObject item in removeItemsList) {
                schema.Items.Remove(item);          
            }
        }