Parse() public method

public Parse ( XmlReader reader, string targetNamespace ) : SchemaType
reader System.Xml.XmlReader
targetNamespace string
return SchemaType
 public XsdDateTime(string text, XsdDateTimeFlags kinds)
 {
     this = new XsdDateTime();
     Parser parser = new Parser();
     if (!parser.Parse(text, kinds))
     {
         throw new FormatException(Res.GetString("XmlConvert_BadFormat", new object[] { text, kinds }));
     }
     this.InitiateXsdDateTime(parser);
 }
 internal static bool TryParse(string text, XsdDateTimeFlags kinds, out XsdDateTime result)
 {
     Parser parser = new Parser();
     if (!parser.Parse(text, kinds))
     {
         result = new XsdDateTime();
         return false;
     }
     result = new XsdDateTime(parser);
     return true;
 }
Beispiel #3
0
 /// <summary>
 /// Constructs an XsdDateTime from a string using specific format.
 /// </summary>
 public XsdDateTime(string text, XsdDateTimeFlags kinds) : this()
 {
     Parser parser = new Parser();
     if (!parser.Parse(text, kinds))
     {
         throw new FormatException(SR.Format(SR.XmlConvert_BadFormat, text, kinds));
     }
     InitiateXsdDateTime(parser);
 }
Beispiel #4
0
 private bool LoadSchema(string uri)
 {
     if (_xmlResolver == null)
     {
         return false;
     }
     uri = _NameTable.Add(uri);
     if (_SchemaInfo.TargetNamespaces.ContainsKey(uri))
     {
         return false;
     }
     SchemaInfo schemaInfo = null;
     Uri _baseUri = _xmlResolver.ResolveUri(null, _reader.BaseURI);
     XmlReader reader = null;
     try
     {
         Uri ruri = _xmlResolver.ResolveUri(_baseUri, uri.Substring(x_schema.Length));
         Stream stm = (Stream)_xmlResolver.GetEntity(ruri, null, null);
         reader = new XmlTextReader(ruri.ToString(), stm, _NameTable);
         schemaInfo = new SchemaInfo();
         Parser parser = new Parser(SchemaType.XDR, _NameTable, _SchemaNames, _validationEventHandler);
         parser.XmlResolver = _xmlResolver;
         parser.Parse(reader, uri);
         schemaInfo = parser.XdrSchema;
     }
     catch (XmlException e)
     {
         SendValidationEvent(SR.Sch_CannotLoadSchema, new string[] { uri, e.Message }, XmlSeverityType.Warning);
         schemaInfo = null;
     }
     finally
     {
         if (reader != null)
         {
             reader.Close();
         }
     }
     if (schemaInfo != null && schemaInfo.ErrorCount == 0)
     {
         _SchemaInfo.Add(schemaInfo, _validationEventHandler);
         return true;
     }
     return false;
 }
        private void LoadExternals(XmlSchema schema) {
            if (schema.IsProcessing) {
                return;
            }
            schema.IsProcessing = true;
            for (int i = 0; i < schema.Includes.Count; ++i) {
                Uri includeLocation = null;
                //CASE 1: If the Schema object of the include has been set 
                XmlSchemaExternal include = (XmlSchemaExternal)schema.Includes[i];
                XmlSchema includedSchema = include.Schema;
                if (includedSchema != null) {
                    // already loaded
                    includeLocation = includedSchema.BaseUri;
                    if (includeLocation != null && schemaLocations[includeLocation] == null) {
                        schemaLocations.Add(includeLocation, includedSchema);
                    }
                    LoadExternals(includedSchema);
                    continue;
                }

                //CASE 2: Try & Parse schema from the provided location
                string schemaLocation = include.SchemaLocation;
                Uri ruri = null;
                Exception innerException = null;
                if (schemaLocation != null) {
                    try {
                        ruri = ResolveSchemaLocationUri(schema, schemaLocation);
                    }
                    catch(Exception e) {
                        ruri = null;
                        innerException = e;
                    }
                }

                if (include.Compositor == Compositor.Import) {
                    XmlSchemaImport import = include as XmlSchemaImport;
                    Debug.Assert(import != null);
                    string importNS =  import.Namespace != null ? import.Namespace : string.Empty;
                    if (!schema.ImportedNamespaces.Contains(importNS)) {
                        schema.ImportedNamespaces.Add(importNS);
                    }
                    //CASE 2.1: If the imported namespace is the XML namespace,
                    // If the parent schemaSet already has schema for XML ns loaded, use that
                    // Else if the location is null use the built-in one
                    // else go through regular processing of parsing from location
                    if (importNS == XmlReservedNs.NsXml) {
                        if (ruri == null) { //Resolved location is null, hence get the built-in one
                            include.Schema = Preprocessor.GetBuildInSchema(); 
                            continue;
                        }
                    }
                }

                //CASE 3: Parse schema from the provided location
                if (ruri == null) {
                    if (schemaLocation != null) {
                        SendValidationEvent(new XmlSchemaException(Res.Sch_InvalidIncludeLocation, null, innerException, include.SourceUri, include.LineNumber, include.LinePosition, include), XmlSeverityType.Warning);
                    }
                    continue;
                }

                if (schemaLocations[ruri] == null) { // Only if location already not processed
                    object obj = null;
                    try {
                        obj = GetSchemaEntity(ruri);
                    }
                    catch(Exception eInner) {
                        innerException = eInner;
                        obj = null;
                    }

                    if (obj != null) {
                        include.BaseUri = ruri;
                        Type returnType = obj.GetType();
                        if (typeof(XmlSchema).IsAssignableFrom(returnType)) { //To handle XmlSchema and all its derived types
                            include.Schema = (XmlSchema)obj;
                            schemaLocations.Add(ruri, include.Schema);
                            LoadExternals(include.Schema);
                        }
                        else {
                            XmlReader reader = null;
                            if (returnType.IsSubclassOf(typeof(Stream)) ) {
                                readerSettings.CloseInput = true;
                                readerSettings.XmlResolver = xmlResolver;
                                reader = XmlReader.Create((Stream)obj, readerSettings, ruri.ToString() );
                            }
                            else if (returnType.IsSubclassOf(typeof(XmlReader)) ) {
                                reader = (XmlReader)obj;
                            } 
                            else if (returnType.IsSubclassOf(typeof(TextReader))) {
                                readerSettings.CloseInput = true;
                                readerSettings.XmlResolver = xmlResolver;
                                reader = XmlReader.Create((TextReader)obj, readerSettings, ruri.ToString() );
                            }
                            if (reader == null) {
                                SendValidationEvent(Res.Sch_InvalidIncludeLocation, include, XmlSeverityType.Warning);
                                continue;
                            }
                            try {
                                Parser parser = new Parser(SchemaType.XSD, NameTable, SchemaNames, EventHandler);
                                parser.Parse(reader, null);
                                while(reader.Read());// wellformness check
                                includedSchema = parser.XmlSchema;
                                include.Schema = includedSchema;
                                schemaLocations.Add(ruri, includedSchema); 
                                LoadExternals(includedSchema);
                            }
                            catch(XmlSchemaException e) {
                                SendValidationEvent(Res.Sch_CannotLoadSchemaLocation, schemaLocation, e.Message, e.SourceUri, e.LineNumber, e.LinePosition);
                            }
                            catch(Exception eInner) {
                                SendValidationEvent(new XmlSchemaException(Res.Sch_InvalidIncludeLocation, null, eInner, include.SourceUri, include.LineNumber, include.LinePosition, include), XmlSeverityType.Warning);
                            }
                            finally {
                                reader.Close();
                            }
                        }
                    }
                    else {
                        SendValidationEvent(new XmlSchemaException(Res.Sch_InvalidIncludeLocation, null, innerException, include.SourceUri, include.LineNumber, include.LinePosition, include), XmlSeverityType.Warning);
                    }
                }
                else { //Location already in table and now seeing duplicate import / include
                    include.Schema = (XmlSchema)schemaLocations[ruri]; //Set schema object even for duplicates
                }
            }
        }
        /// <include file='doc\XmlSchemaCollection.uex' path='docs/doc[@for="XmlSchemaCollection.Add1"]/*' />
        /// <devdoc>
        ///    <para>Add the given schema into the schema collection.
        ///       If the given schema references other namespaces, the schemas for those
        ///       other namespaces are NOT automatically loaded.</para>
        /// </devdoc>
        public XmlSchema Add(String ns, XmlReader reader, XmlResolver resolver) {
            if (reader == null)
                throw new ArgumentNullException("reader");
            XmlNameTable readerNameTable = reader.NameTable;
            SchemaInfo schemaInfo = new SchemaInfo(); 
	        
            Parser parser = new Parser(SchemaType.None, readerNameTable, GetSchemaNames(readerNameTable), validationEventHandler);
            parser.XmlResolver = resolver;
            SchemaType schemaType;
            try {
                schemaType = parser.Parse(reader, ns);
            }
            catch (XmlSchemaException e) {
                SendValidationEvent(e);
                return null;
            }

            if (schemaType == SchemaType.XSD) {
				schemaInfo.SchemaType = SchemaType.XSD;
                return Add(ns, schemaInfo, parser.XmlSchema, true, resolver);
            }
            else {
                SchemaInfo xdrSchema = parser.XdrSchema;
                return Add(ns, parser.XdrSchema, null, true, resolver);
            }
        }
 private void LoadSchemaFromLocation(string uri) {
     // is x-schema
     if (!XdrBuilder.IsXdrSchema(uri)) {
         return;
     }
     string url = uri.Substring(x_schema.Length);
     XmlReader reader = null;
     SchemaInfo xdrSchema = null;
     try {
         Uri ruri = this.XmlResolver.ResolveUri(BaseUri, url);
         Stream stm = (Stream)this.XmlResolver.GetEntity(ruri,null,null);
         reader = new XmlTextReader(ruri.ToString(), stm, NameTable);
         ((XmlTextReader)reader).XmlResolver = this.XmlResolver;
         Parser parser = new Parser(SchemaType.XDR, NameTable, SchemaNames, EventHandler);
         parser.XmlResolver = this.XmlResolver;
         parser.Parse(reader, uri);
         while(reader.Read());// wellformness check
         xdrSchema = parser.XdrSchema;
     }
     catch(XmlSchemaException e) {
         SendValidationEvent(Res.Sch_CannotLoadSchema, new string[] {uri, e.Message}, XmlSeverityType.Error);
     }
     catch(Exception e) {
         SendValidationEvent(Res.Sch_CannotLoadSchema, new string[] {uri, e.Message}, XmlSeverityType.Warning);
     }
     finally {
         if (reader != null) {
             reader.Close();
         }
     }
     if (xdrSchema != null && xdrSchema.ErrorCount == 0) {
         schemaInfo.Add(xdrSchema, EventHandler);
         SchemaCollection.Add(uri, xdrSchema, null, false);
     }
 }
Beispiel #8
0
        private bool LoadSchema(string uri, string url) {
            bool expectXdr = false;

            uri = nameTable.Add(uri);
            if (SchemaInfo.HasSchema(uri)) {
                return false;
            }

            SchemaInfo schemaInfo = null;
            if (schemaCollection != null)
                schemaInfo = schemaCollection.GetSchemaInfo(uri);
            if (schemaInfo != null) {
                /*
                if (SkipProcess(schemaInfo.SchemaType))
                    return false;
                */
                if (!IsCorrectSchemaType(schemaInfo.SchemaType)) {
                    throw new XmlException(Res.Xml_MultipleValidaitonTypes, string.Empty, this.positionInfo.LineNumber, this.positionInfo.LinePosition);
                }
                SchemaInfo.Add(uri, schemaInfo, validationEventHandler);
                return true;
            }

            if (this.xmlResolver == null)
                return false;

            if (url == null && IsXdrSchema(uri)) {
                /*                 
                        */
                if (ValidationFlag != ValidationType.XDR && ValidationFlag != ValidationType.Auto) {
                    return false;
                }
                url = uri.Substring(x_schema.Length);
                expectXdr = true;
            }
            if (url == null) {
                return false;
            }

            XmlSchema schema = null;
            XmlReader reader = null;
            try {
                Uri ruri = this.xmlResolver.ResolveUri(baseUri, url);
                Stream stm = (Stream)this.xmlResolver.GetEntity(ruri,null,null);
                reader = new XmlTextReader(ruri.ToString(), stm, nameTable);
                schemaInfo = new SchemaInfo(schemaNames);

                Parser sparser = new Parser(schemaCollection, nameTable, schemaNames, validationEventHandler);
                schema = sparser.Parse(reader, uri, schemaInfo);

                while(reader.Read());// wellformness check
            }
            catch(XmlSchemaException e) {
                SendValidationEvent(Res.Sch_CannotLoadSchema, new string[] {uri, e.Message}, XmlSeverityType.Error);
                schemaInfo = null;
            }
            catch(Exception e) {
                SendValidationEvent(Res.Sch_CannotLoadSchema, new string[] {uri, e.Message}, XmlSeverityType.Warning);
                schemaInfo = null;
            }
            finally {
                if (reader != null) {
                    reader.Close();
                }
            }
            if (schemaInfo != null) {
                int errorCount = 0;
                if (schema != null) {
                    if (expectXdr) {
                        throw new XmlException(Res.Sch_XSCHEMA, string.Empty, this.positionInfo.LineNumber, this.positionInfo.LinePosition);
                    }

                    if (schema.ErrorCount == 0) {
                        schema.Compile(schemaCollection, nameTable, schemaNames, validationEventHandler, uri, schemaInfo, true);
                    }
                    errorCount += schema.ErrorCount;
                }
                else {
                    errorCount += schemaInfo.ErrorCount;
                }
                if (errorCount == 0) {
                    if (SkipProcess(schemaInfo.SchemaType))
                       return false;

                    if (!IsCorrectSchemaType(schemaInfo.SchemaType)) {
                        throw new XmlException(Res.Xml_MultipleValidaitonTypes, string.Empty, this.positionInfo.LineNumber, this.positionInfo.LinePosition);
                    }
                    SchemaInfo.Add(uri, schemaInfo, validationEventHandler);
                    schemaCollection.Add(uri, schemaInfo, schema, false);
                    return true;
                }
            }
            return false;
        }
        private void LoadExternals(XmlSchema schema, XmlSchemaCollection xsc) {
            if (schema.IsProcessing) {
                return;
            }
            schema.IsProcessing = true;
            for (int i = 0; i < schema.Includes.Count; ++i) {
                XmlSchemaExternal include = (XmlSchemaExternal)schema.Includes[i];
                Uri includeLocation = null;
                //CASE 1: If the Schema object of the include has been set 
                if (include.Schema != null) {
                    // already loaded
                    if (include is XmlSchemaImport && ((XmlSchemaImport)include).Namespace == XmlReservedNs.NsXml) {
                        buildinIncluded = true;
                    }
                    else {
                        includeLocation = include.BaseUri;
                        if (includeLocation != null && schemaLocations[includeLocation] == null) {
                            schemaLocations.Add(includeLocation, includeLocation);
                        }
                        LoadExternals(include.Schema, xsc);
                    }
                    continue;
                }

                //CASE 2: If the include has been already added to the schema collection directly
                if (xsc != null && include is XmlSchemaImport) { //Added for SchemaCollection compatibility
                    XmlSchemaImport import = (XmlSchemaImport)include;
                    string importNS =  import.Namespace != null ? import.Namespace : string.Empty;
                    include.Schema = xsc[importNS]; //Fetch it from the collection
                    if (include.Schema != null) {
                        include.Schema   = include.Schema.Clone();
                        if (include.Schema.BaseUri != null && schemaLocations[include.Schema.BaseUri] == null) {
                            schemaLocations.Add(include.Schema.BaseUri, include.Schema.BaseUri);
                        }
                        //To avoid re-including components that were already included through a different path
                        Uri subUri = null;
                        for (int j = 0; j < include.Schema.Includes.Count; ++j) {
                            XmlSchemaExternal subInc = (XmlSchemaExternal)include.Schema.Includes[j];
                            if (subInc is XmlSchemaImport) {
                                XmlSchemaImport subImp = (XmlSchemaImport)subInc;
                                subUri = subImp.BaseUri != null ? subImp.BaseUri : (subImp.Schema != null && subImp.Schema.BaseUri != null ? subImp.Schema.BaseUri : null);
                                if (subUri != null) {
                                    if(schemaLocations[subUri] != null) {
                                        subImp.Schema = null; //So that the components are not included again
                                    }
                                    else { //if its not there already, add it
                                        schemaLocations.Add(subUri, subUri); //The schema for that location is available
                                    }
                                }
                            }
                        }
                        continue;
                    }
                }

                 //CASE 3: If the imported namespace is the XML namespace, load built-in schema
                if (include is XmlSchemaImport && ((XmlSchemaImport)include).Namespace == XmlReservedNs.NsXml) {
                    if (!buildinIncluded) {
                        buildinIncluded = true;
                        include.Schema = Preprocessor.GetBuildInSchema();
                    }
                    continue;
                }
                
                //CASE4: Parse schema from the provided location
                string schemaLocation = include.SchemaLocation;
                if (schemaLocation == null) {
                    continue;
                }

                Uri ruri = ResolveSchemaLocationUri(schema, schemaLocation);

                if (ruri != null && schemaLocations[ruri] == null) {
                    Stream stream = GetSchemaEntity(ruri);
                    if (stream != null) {
                        include.BaseUri = ruri;
                        schemaLocations.Add(ruri, ruri);
                        XmlTextReader reader = new XmlTextReader(ruri.ToString(), stream, NameTable);
                        reader.XmlResolver = xmlResolver;
                        try {
                            Parser parser = new Parser(SchemaType.XSD, NameTable, SchemaNames, EventHandler);
                            parser.Parse(reader, null);
                            while(reader.Read());// wellformness check
                            include.Schema = parser.XmlSchema;
                            LoadExternals(include.Schema, xsc);
                        }
                        catch(XmlSchemaException e) {
                            SendValidationEventNoThrow(new XmlSchemaException(Res.Sch_CannotLoadSchema, new string[] {schemaLocation, e.Message}, e.SourceUri, e.LineNumber, e.LinePosition), XmlSeverityType.Error);
                        }
                        catch(Exception) {
                            SendValidationEvent(Res.Sch_InvalidIncludeLocation, include, XmlSeverityType.Warning);
                        }
                        finally {
                            reader.Close();
                        }
                        
                    }
                    else {
                        SendValidationEvent(Res.Sch_InvalidIncludeLocation, include, XmlSeverityType.Warning);
                    }
                }
                
            }
            schema.IsProcessing = false;
        }
 internal XmlSchema ParseSchema(string targetNamespace, XmlReader reader) {
     XmlNameTable readerNameTable = reader.NameTable;
     SchemaNames schemaNames = GetSchemaNames(readerNameTable);
     Parser parser = new Parser(SchemaType.XSD, readerNameTable, schemaNames, eventHandler);
     parser.XmlResolver = readerSettings.GetXmlResolver();
     SchemaType schemaType;
     try {
         schemaType = parser.Parse(reader, targetNamespace);
     }
     catch(XmlSchemaException e) {
         SendValidationEvent(e, XmlSeverityType.Error);
         return null;
     }
     return parser.XmlSchema;
 }
Beispiel #11
0
        private void LoadSchemaFromLocation(string uri, string url)
        {
            XmlReader reader = null;
            SchemaInfo schemaInfo = null;

            try
            {
                Uri ruri = this.XmlResolver.ResolveUri(BaseUri, url);
                Stream stm = (Stream)this.XmlResolver.GetEntity(ruri, null, null);
                reader = new XmlTextReader(ruri.ToString(), stm, NameTable);
                //XmlSchema schema = SchemaCollection.Add(uri, reader, this.XmlResolver);

                Parser parser = new Parser(SchemaType.XSD, NameTable, SchemaNames, EventHandler);
                parser.XmlResolver = this.XmlResolver;
                SchemaType schemaType = parser.Parse(reader, uri);

                schemaInfo = new SchemaInfo();
                schemaInfo.SchemaType = schemaType;
                if (schemaType == SchemaType.XSD)
                {
                    if (SchemaCollection.EventHandler == null)
                    {
                        SchemaCollection.EventHandler = this.EventHandler;
                    }
                    SchemaCollection.Add(uri, schemaInfo, parser.XmlSchema, true);
                }
                //Add to validator's SchemaInfo
                SchemaInfo.Add(schemaInfo, EventHandler);

                while (reader.Read()) ;// wellformness check
            }
            catch (XmlSchemaException e)
            {
                schemaInfo = null;
                SendValidationEvent(SR.Sch_CannotLoadSchema, new string[] { uri, e.Message }, XmlSeverityType.Error);
            }
            catch (Exception e)
            {
                schemaInfo = null;
                SendValidationEvent(SR.Sch_CannotLoadSchema, new string[] { uri, e.Message }, XmlSeverityType.Warning);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
 /// <include file='doc\XmlSchema.uex' path='docs/doc[@for="XmlSchema.Read2"]/*' />
 /// <devdoc>
 ///    <para>[To be supplied.]</para>
 /// </devdoc>
 public static XmlSchema Read(XmlReader reader, ValidationEventHandler validationEventHandler) {
     XmlNameTable nameTable = reader.NameTable;
     Parser parser = new Parser(SchemaType.XSD, nameTable, new SchemaNames(nameTable), validationEventHandler);
     try {
         parser.Parse(reader, null);
     }
     catch(XmlSchemaException e) {
         if (validationEventHandler != null) {
             validationEventHandler(null, new ValidationEventArgs(e));
         } 
         else {
             throw e;
         }
         return null;
     }
     return parser.XmlSchema;
 }