private void InitiateXsdDateTime(Parser parser)
 {
     this.dt = new DateTime(parser.year, parser.month, parser.day, parser.hour, parser.minute, parser.second);
     if (parser.fraction != 0)
     {
         this.dt = this.dt.AddTicks((long) parser.fraction);
     }
     this.extra = (uint) ((((((int) parser.typeCode) << 0x18) | (((int) parser.kind) << 0x10)) | (parser.zoneHour << 8)) | parser.zoneMinute);
 }
 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;
 }
Ejemplo n.º 4
0
 private void ValidateElement() {
     elementName.Init(reader.LocalName, XmlSchemaDatatype.XdrCanonizeUri(reader.NamespaceURI, NameTable, SchemaNames));
     ValidateChildElement();
     if (SchemaNames.IsXDRRoot(elementName.Name, elementName.Namespace) && reader.Depth > 0) {
         inlineSchemaParser = new Parser(SchemaType.XDR, NameTable, SchemaNames, EventHandler);
         inlineSchemaParser.StartParsing(reader, null);
         inlineSchemaParser.ParseReaderNode();
     }
     else {
         ProcessElement();
     }
 }
Ejemplo n.º 5
0
 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);
     }
 }
Ejemplo n.º 6
0
 private void ProcessInlineSchema() {
     if (!inlineSchemaParser.ParseReaderNode()) { // Done
             inlineSchemaParser.FinishParsing();
             SchemaInfo xdrSchema = inlineSchemaParser.XdrSchema;
             if (xdrSchema != null && xdrSchema.ErrorCount == 0) {
                 foreach(string inlineNS in xdrSchema.TargetNamespaces.Keys) {
                     if (!this.schemaInfo.HasSchema(inlineNS)) {
                         schemaInfo.Add(xdrSchema, EventHandler);
                         SchemaCollection.Add(inlineNS, xdrSchema, null, false);
                         break;
                     }
                 }
             }
             inlineSchemaParser = null;
     }
 }
 private XsdDateTime(Parser parser)
 {
     this = new XsdDateTime();
     this.InitiateXsdDateTime(parser);
 }
Ejemplo n.º 8
0
 private void InitiateXsdDateTime(Parser parser)
 {
     dt = new DateTime(parser.year, parser.month, parser.day, parser.hour, parser.minute, parser.second);
     if (parser.fraction != 0)
     {
         dt = dt.AddTicks(parser.fraction);
     }
     extra = (uint)(((int)parser.typeCode << TypeShift) | ((int)parser.kind << KindShift) | (parser.zoneHour << ZoneHourShift) | parser.zoneMinute);
 }
 private void ProcessInlineSchema() {
     Debug.Assert(inlineSchemaParser != null);
     if (coreReader.Read()) {
         if (coreReader.NodeType == XmlNodeType.Element) {
             attributeCount = coreReaderAttributeCount = coreReader.AttributeCount;
         }
         else { //Clear attributes info if nodeType is not element
             ClearAttributesInfo();
         }
         if (!inlineSchemaParser.ParseReaderNode()) {
             inlineSchemaParser.FinishParsing();
             XmlSchema schema = inlineSchemaParser.XmlSchema;
             validator.AddSchema(schema);
             inlineSchemaParser = null;
             validationState = ValidatingReaderState.Read;
         }
     }
 }
Ejemplo n.º 10
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;
 }
Ejemplo n.º 11
0
        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
                }
            }
        }
 private void ProcessElementEvent()
 {
     if ((this.processInlineSchema && this.IsXSDRoot(this.coreReader.LocalName, this.coreReader.NamespaceURI)) && (this.coreReader.Depth > 0))
     {
         this.xmlSchemaInfo.Clear();
         this.attributeCount = this.coreReaderAttributeCount = this.coreReader.AttributeCount;
         if (!this.coreReader.IsEmptyElement)
         {
             this.inlineSchemaParser = new System.Xml.Schema.Parser(SchemaType.XSD, this.coreReaderNameTable, this.validator.SchemaSet.GetSchemaNames(this.coreReaderNameTable), this.validationEvent);
             this.inlineSchemaParser.StartParsing(this.coreReader, null);
             this.inlineSchemaParser.ParseReaderNode();
             this.validationState = ValidatingReaderState.ParseInlineSchema;
         }
         else
         {
             this.validationState = ValidatingReaderState.ClearAttributes;
         }
     }
     else
     {
         this.atomicValue = null;
         this.originalAtomicValueString = null;
         this.xmlSchemaInfo.Clear();
         if (this.manageNamespaces)
         {
             this.nsManager.PushScope();
         }
         string xsiSchemaLocation = null;
         string xsiNoNamespaceSchemaLocation = null;
         string xsiNil = null;
         string xsiType = null;
         if (this.coreReader.MoveToFirstAttribute())
         {
             do
             {
                 string namespaceURI = this.coreReader.NamespaceURI;
                 string localName = this.coreReader.LocalName;
                 if (Ref.Equal(namespaceURI, this.NsXsi))
                 {
                     if (Ref.Equal(localName, this.XsiSchemaLocation))
                     {
                         xsiSchemaLocation = this.coreReader.Value;
                     }
                     else if (Ref.Equal(localName, this.XsiNoNamespaceSchemaLocation))
                     {
                         xsiNoNamespaceSchemaLocation = this.coreReader.Value;
                     }
                     else if (Ref.Equal(localName, this.XsiType))
                     {
                         xsiType = this.coreReader.Value;
                     }
                     else if (Ref.Equal(localName, this.XsiNil))
                     {
                         xsiNil = this.coreReader.Value;
                     }
                 }
                 if (this.manageNamespaces && Ref.Equal(this.coreReader.NamespaceURI, this.NsXmlNs))
                 {
                     this.nsManager.AddNamespace((this.coreReader.Prefix.Length == 0) ? string.Empty : this.coreReader.LocalName, this.coreReader.Value);
                 }
             }
             while (this.coreReader.MoveToNextAttribute());
             this.coreReader.MoveToElement();
         }
         this.validator.ValidateElement(this.coreReader.LocalName, this.coreReader.NamespaceURI, this.xmlSchemaInfo, xsiType, xsiNil, xsiSchemaLocation, xsiNoNamespaceSchemaLocation);
         this.ValidateAttributes();
         this.validator.ValidateEndOfAttributes(this.xmlSchemaInfo);
         if (this.coreReader.IsEmptyElement)
         {
             this.ProcessEndElementEvent();
         }
         this.validationState = ValidatingReaderState.ClearAttributes;
     }
 }
Ejemplo n.º 13
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();
                }
            }
        }
Ejemplo n.º 14
0
 private void ValidateElement()
 {
     elementName.Init(reader.LocalName, reader.NamespaceURI);
     object particle = ValidateChildElement();
     if (IsXSDRoot(elementName.Name, elementName.Namespace) && reader.Depth > 0)
     {
         _inlineSchemaParser = new Parser(SchemaType.XSD, NameTable, SchemaNames, EventHandler);
         _inlineSchemaParser.StartParsing(reader, null);
         ProcessInlineSchema();
     }
     else
     {
         ProcessElement(particle);
     }
 }
Ejemplo n.º 15
0
 private void ProcessInlineSchema()
 {
     if (!_inlineSchemaParser.ParseReaderNode())
     { // Done
         _inlineSchemaParser.FinishParsing();
         XmlSchema schema = _inlineSchemaParser.XmlSchema;
         string inlineNS = null;
         if (schema != null && schema.ErrorCount == 0)
         {
             try
             {
                 SchemaInfo inlineSchemaInfo = new SchemaInfo();
                 inlineSchemaInfo.SchemaType = SchemaType.XSD;
                 inlineNS = schema.TargetNamespace == null ? string.Empty : schema.TargetNamespace;
                 if (!SchemaInfo.TargetNamespaces.ContainsKey(inlineNS))
                 {
                     if (SchemaCollection.Add(inlineNS, inlineSchemaInfo, schema, true) != null)
                     { //If no errors on compile
                         //Add to validator's SchemaInfo
                         SchemaInfo.Add(inlineSchemaInfo, EventHandler);
                     }
                 }
             }
             catch (XmlSchemaException e)
             {
                 SendValidationEvent(SR.Sch_CannotLoadSchema, new string[] { BaseUri.AbsoluteUri, e.Message }, XmlSeverityType.Error);
             }
         }
         _inlineSchemaParser = null;
     }
 }
Ejemplo n.º 16
0
 /// <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;
 }
        /// <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 ProcessInlineSchema()
 {
     if (this.coreReader.Read())
     {
         if (this.coreReader.NodeType == XmlNodeType.Element)
         {
             this.attributeCount = this.coreReaderAttributeCount = this.coreReader.AttributeCount;
         }
         else
         {
             this.ClearAttributesInfo();
         }
         if (!this.inlineSchemaParser.ParseReaderNode())
         {
             this.inlineSchemaParser.FinishParsing();
             XmlSchema xmlSchema = this.inlineSchemaParser.XmlSchema;
             this.validator.AddSchema(xmlSchema);
             this.inlineSchemaParser = null;
             this.validationState = ValidatingReaderState.Read;
         }
     }
 }
Ejemplo n.º 19
0
 private XsdDateTime(Parser parser) : this() {
     InitiateXsdDateTime(parser);
 }
Ejemplo n.º 20
0
 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;
 }
        private void ProcessElementEvent() {
            if (this.processInlineSchema && IsXSDRoot(coreReader.LocalName, coreReader.NamespaceURI) && coreReader.Depth > 0) {
                xmlSchemaInfo.Clear();
                attributeCount = coreReaderAttributeCount = coreReader.AttributeCount;
                if (!coreReader.IsEmptyElement) { //If its not empty schema, then parse else ignore
                    inlineSchemaParser = new Parser(SchemaType.XSD, coreReaderNameTable, validator.SchemaSet.GetSchemaNames(coreReaderNameTable), validationEvent);
                    inlineSchemaParser.StartParsing(coreReader, null);
                    inlineSchemaParser.ParseReaderNode();
                    validationState = ValidatingReaderState.ParseInlineSchema;
                }
                else {
                    validationState = ValidatingReaderState.ClearAttributes;
                }
            }
            else { //Validate element

                //Clear previous data
                atomicValue = null;
                originalAtomicValueString = null;
                xmlSchemaInfo.Clear();

                if (manageNamespaces) {
                    nsManager.PushScope();
                }
                //Find Xsi attributes that need to be processed before validating the element
                string xsiSchemaLocation = null;
                string xsiNoNamespaceSL = null;
                string xsiNil = null;
                string xsiType = null;
                if (coreReader.MoveToFirstAttribute()) {
                    do {
                        string objectNs = coreReader.NamespaceURI;
                        string objectName = coreReader.LocalName;
                        if (Ref.Equal(objectNs, NsXsi)) {
                            if (Ref.Equal(objectName, XsiSchemaLocation)) {
                                xsiSchemaLocation = coreReader.Value;
                            }
                            else if (Ref.Equal(objectName, XsiNoNamespaceSchemaLocation)) {
                                xsiNoNamespaceSL = coreReader.Value;
                            }
                            else if (Ref.Equal(objectName, XsiType)) {
                                xsiType = coreReader.Value;
                            }
                            else if (Ref.Equal(objectName, XsiNil)) {
                                xsiNil = coreReader.Value;
                            }
                        }
                        if (manageNamespaces && Ref.Equal(coreReader.NamespaceURI, NsXmlNs)) {
                            nsManager.AddNamespace(coreReader.Prefix.Length == 0 ? string.Empty : coreReader.LocalName, coreReader.Value);
                        }

                    } while (coreReader.MoveToNextAttribute());
                    coreReader.MoveToElement();
                }
                validator.ValidateElement(coreReader.LocalName, coreReader.NamespaceURI, xmlSchemaInfo, xsiType, xsiNil, xsiSchemaLocation, xsiNoNamespaceSL);
                ValidateAttributes();
                validator.ValidateEndOfAttributes(xmlSchemaInfo);
                if (coreReader.IsEmptyElement) {
                    ProcessEndElementEvent();
                }
                validationState = ValidatingReaderState.ClearAttributes;
            }
        }
        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;
        }
Ejemplo n.º 23
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);
 }
Ejemplo n.º 24
0
        internal void Validate() {
            if (inlineSchemaParser != null) {
                if (!inlineSchemaParser.ParseReaderNode()) { // Done
                    XmlSchema schema = inlineSchemaParser.FinishParsing();
                    bool add = true;
                    if (schema != null) {
                        inlineNs = schema.TargetNamespace == null ? string.Empty : schema.TargetNamespace;
                        if (!SchemaInfo.HasSchema(inlineNs) && schema.ErrorCount == 0) {
                            schema.Compile(schemaCollection, nameTable, schemaNames, validationEventHandler, null, inlineSchemaInfo, true);
                            add = schema.ErrorCount == 0;
                        }
                        else {
                            add = false;
                        }
                    }
                    else {
                        inlineNs = inlineSchemaInfo.CurrentSchemaNamespace;
                        add = !SchemaInfo.HasSchema(inlineNs);
                    }

                    if (add) {
                        SchemaInfo.Add(inlineNs, inlineSchemaInfo, validationEventHandler);
                        schemaCollection.Add(inlineNs, inlineSchemaInfo, schema, false);
                    }
                    inlineSchemaParser = null;
                    inlineSchemaInfo = null;
                    inlineNs = null;
                }
            }
            else {
                this.name = QualifiedName(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                if (HasSchema) {
                    if (context != null) {
                        while (context != null && reader.Depth <= context.Depth) {
                            EndChildren();
                        }
                        ValidateElementContent(reader.NodeType);
                    }
                    if (reader.Depth == 0 && reader.NodeType == XmlNodeType.Element && SchemaInfo.SchemaType == SchemaType.DTD) {
                        if (!SchemaInfo.DocTypeName.Equals(this.name)) {
                            SendValidationEvent(Res.Sch_RootMatchDocType);
                        }
                    }
                }
                if (reader.NodeType == XmlNodeType.Element) {
                    if (schemaNames.IsSchemaRoot(this.name) &&
                        !SkipProcess(this.name == schemaNames.QnXsdSchema ? SchemaType.XSD : SchemaType.XDR) &&
                        IsCorrectSchemaType(this.name == schemaNames.QnXsdSchema ? SchemaType.XSD : SchemaType.XDR)) {
                        inlineSchemaInfo = new SchemaInfo(schemaNames);
                        inlineSchemaParser = new Parser(schemaCollection, nameTable, schemaNames, validationEventHandler);
                        inlineSchemaParser.StartParsing(reader, null, inlineSchemaInfo);
                        inlineSchemaParser.ParseReaderNode();
                    }
                    else {
                        ProcessElement();
                    }
                }
            }
        }
Ejemplo n.º 25
0
        // SxS: This function calls ValidateElement on XmlSchemaValidator which is annotated with ResourceExposure attribute.
        // Since the resource names (namespace location) are not provided directly by the user (they are read from the source
        // document) and the function does not expose any resources it is fine to suppress the SxS warning.
        private async Task ProcessElementEventAsync()
        {
            if (_processInlineSchema && IsXSDRoot(_coreReader.LocalName, _coreReader.NamespaceURI) && _coreReader.Depth > 0)
            {
                _xmlSchemaInfo.Clear();
                _attributeCount = _coreReaderAttributeCount = _coreReader.AttributeCount;
                if (!_coreReader.IsEmptyElement)
                { //If its not empty schema, then parse else ignore
                    _inlineSchemaParser = new Parser(SchemaType.XSD, _coreReaderNameTable, _validator.SchemaSet.GetSchemaNames(_coreReaderNameTable), _validationEvent);
                    await _inlineSchemaParser.StartParsingAsync(_coreReader, null).ConfigureAwait(false);
                    _inlineSchemaParser.ParseReaderNode();
                    _validationState = ValidatingReaderState.ParseInlineSchema;
                }
                else
                {
                    _validationState = ValidatingReaderState.ClearAttributes;
                }
            }
            else
            { //Validate element
                //Clear previous data
                _atomicValue = null;
                _originalAtomicValueString = null;
                _xmlSchemaInfo.Clear();

                if (_manageNamespaces)
                {
                    _nsManager.PushScope();
                }
                //Find Xsi attributes that need to be processed before validating the element
                string xsiSchemaLocation = null;
                string xsiNoNamespaceSL = null;
                string xsiNil = null;
                string xsiType = null;
                if (_coreReader.MoveToFirstAttribute())
                {
                    do
                    {
                        string objectNs = _coreReader.NamespaceURI;
                        string objectName = _coreReader.LocalName;
                        if (Ref.Equal(objectNs, _nsXsi))
                        {
                            if (Ref.Equal(objectName, _xsiSchemaLocation))
                            {
                                xsiSchemaLocation = _coreReader.Value;
                            }
                            else if (Ref.Equal(objectName, _xsiNoNamespaceSchemaLocation))
                            {
                                xsiNoNamespaceSL = _coreReader.Value;
                            }
                            else if (Ref.Equal(objectName, _xsiType))
                            {
                                xsiType = _coreReader.Value;
                            }
                            else if (Ref.Equal(objectName, _xsiNil))
                            {
                                xsiNil = _coreReader.Value;
                            }
                        }
                        if (_manageNamespaces && Ref.Equal(_coreReader.NamespaceURI, _nsXmlNs))
                        {
                            _nsManager.AddNamespace(_coreReader.Prefix.Length == 0 ? string.Empty : _coreReader.LocalName, _coreReader.Value);
                        }
                    } while (_coreReader.MoveToNextAttribute());
                    _coreReader.MoveToElement();
                }
                _validator.ValidateElement(_coreReader.LocalName, _coreReader.NamespaceURI, _xmlSchemaInfo, xsiType, xsiNil, xsiSchemaLocation, xsiNoNamespaceSL);
                ValidateAttributes();
                _validator.ValidateEndOfAttributes(_xmlSchemaInfo);
                if (_coreReader.IsEmptyElement)
                {
                    await ProcessEndElementEventAsync().ConfigureAwait(false);
                }
                _validationState = ValidatingReaderState.ClearAttributes;
            }
        }
Ejemplo n.º 26
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;
        }