Exemplo n.º 1
0
        void ProcessExternal(ValidationEventHandler handler, List <CompiledSchemaMemo> handledUris, XmlResolver resolver, XmlSchemaExternal ext, XmlSchemaSet col)
        {
            if (ext == null)
            {
                error(handler, "Object is not valid in Includes Property of XmlSchema");
                return;
            }

            // The only case we want to handle where the SchemaLocation is null is if the external is an import.
            XmlSchemaImport import = ext as XmlSchemaImport;

            if (ext.SchemaLocation == null && import == null)
            {
                return;
            }

            XmlSchema includedSchema = null;

            if (ext.SchemaLocation != null)
            {
                Stream stream = null;
                string url    = null;
                if (resolver != null)
                {
                    url = GetResolvedUri(resolver, ext.SchemaLocation);
                    foreach (var i in handledUris)
                    {
                        if (i.SourceUri.Equals(url))
                        {
                            // This schema is already handled, so simply skip (otherwise, duplicate definition errrors occur.
                            return;
                        }
                    }
                    handledUris.Add(new CompiledSchemaMemo()
                    {
                        SourceUri = url
                    });
                    try {
                        stream = resolver.GetEntity(new Uri(url), null, typeof(Stream)) as Stream;
                    } catch (Exception) {
                        // LAMESPEC: This is not good way to handle errors, but since we cannot know what kind of XmlResolver will come, so there are no mean to avoid this ugly catch.
                        warn(handler, "Could not resolve schema location URI: " + url);
                        stream = null;
                    }
                }

                // Process redefinition children in advance.
                XmlSchemaRedefine redefine = ext as XmlSchemaRedefine;
                if (redefine != null)
                {
                    for (int j = 0; j < redefine.Items.Count; j++)
                    {
                        XmlSchemaObject redefinedObj = redefine.Items [j];
                        redefinedObj.isRedefinedComponent = true;
                        redefinedObj.isRedefineChild      = true;
                        if (redefinedObj is XmlSchemaType ||
                            redefinedObj is XmlSchemaGroup ||
                            redefinedObj is XmlSchemaAttributeGroup)
                        {
                            compilationItems.Add(redefinedObj);
                        }
                        else
                        {
                            error(handler, "Redefinition is only allowed to simpleType, complexType, group and attributeGroup.");
                        }
                    }
                }

                if (stream == null)
                {
                    // It is missing schema components.
                    missedSubComponents = true;
                    return;
                }
                else
                {
                    XmlTextReader xtr = null;
                    try {
                        xtr            = new XmlTextReader(url, stream, nameTable);
                        includedSchema = XmlSchema.Read(xtr, handler);
                    } finally {
                        if (xtr != null)
                        {
                            xtr.Close();
                        }
                    }
                    includedSchema.schemas = schemas;
                }
                includedSchema.SetParent();
                ext.Schema = includedSchema;
            }

            // Set - actual - target namespace for the included schema * before compilation*.
            if (import != null)
            {
                if (ext.Schema == null && ext.SchemaLocation == null)
                {
                    // if a schema location wasn't specified, check the other schemas we have to see if one of those
                    // is a match.
                    foreach (XmlSchema schema in col.Schemas())
                    {
                        if (schema.TargetNamespace == import.Namespace)
                        {
                            includedSchema         = schema;
                            includedSchema.schemas = schemas;
                            includedSchema.SetParent();
                            ext.Schema = includedSchema;
                            break;
                        }
                    }
                    // handle case where target namespace doesn't exist in schema collection - i.e can't find it at all
                    if (includedSchema == null)
                    {
                        return;
                    }
                }
                else if (includedSchema != null)
                {
                    if (TargetNamespace == includedSchema.TargetNamespace)
                    {
                        error(handler, "Target namespace must be different from that of included schema.");
                        return;
                    }
                    else if (includedSchema.TargetNamespace != import.Namespace)
                    {
                        error(handler, "Attribute namespace and its importing schema's target namespace must be the same.");
                        return;
                    }
                }
            }
            else if (includedSchema != null)
            {
                if (TargetNamespace == null &&
                    includedSchema.TargetNamespace != null)
                {
                    includedSchema.error(handler, String.Format("On {0} element, targetNamespace is required to include a schema which has its own target namespace", ext.GetType().Name));
                    return;
                }
                else if (TargetNamespace != null &&
                         includedSchema.TargetNamespace == null)
                {
                    includedSchema.TargetNamespace = TargetNamespace;
                }
            }

            // Do not compile included schema here.
            if (includedSchema != null)
            {
                AddExternalComponentsTo(includedSchema, compilationItems, handler, handledUris, resolver, col);
            }
        }