Exemple #1
0
 private void AddImport(string current, string import)
 {
     foreach (XmlSchema schema in schemas.Schemas(current))
     {
         bool exists = false;
         foreach (XmlSchemaExternal e in schema.Includes)
         {
             Import imp = e as Import;
             if (imp != null &&
                 imp.Namespace == import)
             {
                 exists = true;
             }
         }
         if (exists)
         {
             continue;
         }
         Import newimp = new Import();
         newimp.Namespace = import;
         schema.Includes.Add(newimp);
     }
 }
Exemple #2
0
        //<import
        //  id = ID
        //  namespace = anyURI
        //  schemaLocation = anyURI
        //  {any attributes with non-schema namespace . . .}>
        //  Content: (annotation?)
        //</import>
        internal static XmlSchemaImport Read(XmlSchemaReader reader, ValidationEventHandler h)
        {
            XmlSchemaImport import = new XmlSchemaImport();

            reader.MoveToElement();

            if (reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != "import")
            {
                error(h, "Should not happen :1: XmlSchemaImport.Read, name=" + reader.Name, null);
                reader.SkipToEnd();
                return(null);
            }

            import.LineNumber   = reader.LineNumber;
            import.LinePosition = reader.LinePosition;
            import.SourceUri    = reader.BaseURI;

            while (reader.MoveToNextAttribute())
            {
                if (reader.Name == "id")
                {
                    import.Id = reader.Value;
                }
                else if (reader.Name == "namespace")
                {
                    import.nameSpace = reader.Value;
                }
                else if (reader.Name == "schemaLocation")
                {
                    import.SchemaLocation = reader.Value;
                }
                else if ((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
                {
                    error(h, reader.Name + " is not a valid attribute for import", null);
                }
                else
                {
                    XmlSchemaUtil.ReadUnhandledAttribute(reader, import);
                }
            }

            reader.MoveToElement();
            if (reader.IsEmptyElement)
            {
                return(import);
            }

            //  Content: (annotation?)
            int level = 1;

            while (reader.ReadNextElement())
            {
                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (reader.LocalName != xmlname)
                    {
                        error(h, "Should not happen :2: XmlSchemaImport.Read, name=" + reader.Name, null);
                    }
                    break;
                }
                if (level <= 1 && reader.LocalName == "annotation")
                {
                    level = 2;                          //Only one annotation
                    XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader, h);
                    if (annotation != null)
                    {
                        import.Annotation = annotation;
                    }
                    continue;
                }
                reader.RaiseInvalidElementError();
            }
            return(import);
        }
Exemple #3
0
        private static void ReadContent(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
        {
            reader.MoveToElement();
            if (reader.LocalName != "schema" && reader.NamespaceURI != XmlSchema.Namespace && reader.NodeType != XmlNodeType.Element)
            {
                error(h, "UNREACHABLE CODE REACHED: Method: Schema.ReadContent, " + reader.LocalName + ", " + reader.NamespaceURI, null);
            }

            //(include | import | redefine | annotation)*,
            //((simpleType | complexType | group | attributeGroup | element | attribute | notation | annotation)*
            int level = 1;

            while (reader.ReadNextElement())
            {
                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (reader.LocalName != xmlname)
                    {
                        error(h, "Should not happen :2: XmlSchema.Read, name=" + reader.Name, null);
                    }
                    break;
                }
                if (level <= 1)
                {
                    if (reader.LocalName == "include")
                    {
                        XmlSchemaInclude include = XmlSchemaInclude.Read(reader, h);
                        if (include != null)
                        {
                            schema.includes.Add(include);
                        }
                        continue;
                    }
                    if (reader.LocalName == "import")
                    {
                        XmlSchemaImport import = XmlSchemaImport.Read(reader, h);
                        if (import != null)
                        {
                            schema.includes.Add(import);
                        }
                        continue;
                    }
                    if (reader.LocalName == "redefine")
                    {
                        XmlSchemaRedefine redefine = XmlSchemaRedefine.Read(reader, h);
                        if (redefine != null)
                        {
                            schema.includes.Add(redefine);
                        }
                        continue;
                    }
                    if (reader.LocalName == "annotation")
                    {
                        XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader, h);
                        if (annotation != null)
                        {
                            schema.items.Add(annotation);
                        }
                        continue;
                    }
                }
                if (level <= 2)
                {
                    level = 2;
                    if (reader.LocalName == "simpleType")
                    {
                        XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader, h);
                        if (stype != null)
                        {
                            schema.items.Add(stype);
                        }
                        continue;
                    }
                    if (reader.LocalName == "complexType")
                    {
                        XmlSchemaComplexType ctype = XmlSchemaComplexType.Read(reader, h);
                        if (ctype != null)
                        {
                            schema.items.Add(ctype);
                        }
                        continue;
                    }
                    if (reader.LocalName == "group")
                    {
                        XmlSchemaGroup group = XmlSchemaGroup.Read(reader, h);
                        if (group != null)
                        {
                            schema.items.Add(group);
                        }
                        continue;
                    }
                    if (reader.LocalName == "attributeGroup")
                    {
                        XmlSchemaAttributeGroup attributeGroup = XmlSchemaAttributeGroup.Read(reader, h);
                        if (attributeGroup != null)
                        {
                            schema.items.Add(attributeGroup);
                        }
                        continue;
                    }
                    if (reader.LocalName == "element")
                    {
                        XmlSchemaElement element = XmlSchemaElement.Read(reader, h);
                        if (element != null)
                        {
                            schema.items.Add(element);
                        }
                        continue;
                    }
                    if (reader.LocalName == "attribute")
                    {
                        XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader, h);
                        if (attr != null)
                        {
                            schema.items.Add(attr);
                        }
                        continue;
                    }
                    if (reader.LocalName == "notation")
                    {
                        XmlSchemaNotation notation = XmlSchemaNotation.Read(reader, h);
                        if (notation != null)
                        {
                            schema.items.Add(notation);
                        }
                        continue;
                    }
                    if (reader.LocalName == "annotation")
                    {
                        XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader, h);
                        if (annotation != null)
                        {
                            schema.items.Add(annotation);
                        }
                        continue;
                    }
                }
                reader.RaiseInvalidElementError();
            }
        }
Exemple #4
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);
            }
        }