Validate() private méthode

private Validate ( XmlReader reader, XmlResolver resolver, XmlSchemaSet schemaSet, ValidationEventHandler valEventHandler ) : XmlReader
reader System.Xml.XmlReader
resolver System.Xml.XmlResolver
schemaSet XmlSchemaSet
valEventHandler ValidationEventHandler
Résultat System.Xml.XmlReader
 private XmlReader GetValidatingReader(XmlReader reader, XmlSchemaSet schemas, ValidationEventHandler validationEvent, XmlSchemaType schemaType, XmlSchemaElement schemaElement, XmlSchemaAttribute schemaAttribute)
 {
     if (schemaAttribute != null)
     {
         return schemaAttribute.Validate(reader, null, schemas, validationEvent);
     }
     if (schemaElement != null)
     {
         return schemaElement.Validate(reader, null, schemas, validationEvent);
     }
     if (schemaType != null)
     {
         return schemaType.Validate(reader, null, schemas, validationEvent);
     }
     XmlReaderSettings settings = new XmlReaderSettings {
         ConformanceLevel = ConformanceLevel.Auto,
         ValidationType = ValidationType.Schema,
         Schemas = schemas
     };
     settings.ValidationEventHandler += validationEvent;
     return XmlReader.Create(reader, settings);
 }
Exemple #2
0
        /// <summary>
        /// Schema Component:
        ///			QName, SimpleType, Scope, Default|Fixed, annotation
        /// </summary>
        internal override int Validate(ValidationEventHandler h, XmlSchema schema)
        {
            if (IsValidated(schema.ValidationId))
            {
                return(errorCount);
            }

            // -- Attribute Declaration Schema Component --
            // {name}, {target namespace} -> QualifiedName. Already Compile()d.
            // {type definition} -> attributeType. From SchemaType or SchemaTypeName.
            // {scope} -> ParentIsSchema | isRedefineChild.
            // {value constraint} -> ValidatedFixedValue, ValidatedDefaultValue.
            // {annotation}
            // -- Attribute Use Schema Component --
            // {required}
            // {attribute declaration}
            // {value constraint}

            // First, fill type information for type reference
            if (SchemaType != null)
            {
                SchemaType.Validate(h, schema);
                attributeType = SchemaType;
            }
            else if (SchemaTypeName != null && SchemaTypeName != XmlQualifiedName.Empty)
            {
                // If type is null, then it is missing sub components .
                XmlSchemaType type = schema.FindSchemaType(SchemaTypeName);
                if (type is XmlSchemaComplexType)
                {
                    error(h, "An attribute can't have complexType Content");
                }
                else if (type != null)                          // simple type
                {
                    errorCount   += type.Validate(h, schema);
                    attributeType = type;
                }
                else if (SchemaTypeName == XmlSchemaComplexType.AnyTypeName)
                {
                    attributeType = XmlSchemaComplexType.AnyType;
                }
                else if (XmlSchemaUtil.IsBuiltInDatatypeName(SchemaTypeName))
                {
                    attributeType = XmlSchemaDatatype.FromName(SchemaTypeName);
                    if (attributeType == null)
                    {
                        error(h, "Invalid xml schema namespace datatype was specified.");
                    }
                }
                // otherwise, it might be missing sub components.
                else if (!schema.IsNamespaceAbsent(SchemaTypeName.Namespace))
                {
                    error(h, "Referenced schema type " + SchemaTypeName + " was not found in the corresponding schema.");
                }
            }

            // Then, fill type information for the type references for the referencing attributes
            if (RefName != null && RefName != XmlQualifiedName.Empty)
            {
                referencedAttribute = schema.FindAttribute(RefName);
                // If el is null, then it is missing sub components .
                if (referencedAttribute != null)
                {
                    errorCount += referencedAttribute.Validate(h, schema);
                }
                // otherwise, it might be missing sub components.
                else if (!schema.IsNamespaceAbsent(RefName.Namespace))
                {
                    error(h, "Referenced attribute " + RefName + " was not found in the corresponding schema.");
                }
            }

            if (attributeType == null)
            {
                attributeType = XmlSchemaSimpleType.AnySimpleType;
            }

            // Validate {value constraints}
            if (defaultValue != null || fixedValue != null)
            {
                XmlSchemaDatatype datatype = attributeType as XmlSchemaDatatype;
                if (datatype == null)
                {
                    datatype = ((XmlSchemaSimpleType)attributeType).Datatype;
                }
                if (datatype.TokenizedType == XmlTokenizedType.QName)
                {
                    error(h, "By the defection of the W3C XML Schema specification, it is impossible to supply QName default or fixed values.");
                }
                else
                {
                    try {
                        if (defaultValue != null)
                        {
                            validatedDefaultValue = datatype.Normalize(defaultValue);
                            datatype.ParseValue(validatedDefaultValue, null, null);
                        }
                    } catch (Exception ex) {
                        // FIXME: This is not a good way to handle exception.
                        error(h, "The Attribute's default value is invalid with its type definition.", ex);
                    }
                    try {
                        if (fixedValue != null)
                        {
                            validatedFixedValue      = datatype.Normalize(fixedValue);
                            validatedFixedTypedValue = datatype.ParseValue(validatedFixedValue, null, null);
                        }
                    } catch (Exception ex) {
                        // FIXME: This is not a good way to handle exception.
                        error(h, "The Attribute's fixed value is invalid with its type definition.", ex);
                    }
                }
            }
            if (Use == XmlSchemaUse.None)
            {
                validatedUse = XmlSchemaUse.Optional;
            }
            else
            {
                validatedUse = Use;
            }

#if NET_2_0
            if (attributeType != null)
            {
                attributeSchemaType = attributeType as XmlSchemaSimpleType;
                if (attributeType == XmlSchemaSimpleType.AnySimpleType)
                {
                    attributeSchemaType = XmlSchemaSimpleType.XsAnySimpleType;
                }
                if (attributeSchemaType == null)
                {
                    attributeSchemaType = XmlSchemaType.GetBuiltInSimpleType(SchemaTypeName);
                }
            }
#endif

            ValidationId = schema.ValidationId;
            return(errorCount);
        }
Exemple #3
0
        public static int ValidateAttributesResolved(
            XmlSchemaObjectTable attributesResolved,
            ValidationEventHandler h,
            XmlSchema schema,
            XmlSchemaObjectCollection attributes,
            XmlSchemaAnyAttribute anyAttribute,
            ref XmlSchemaAnyAttribute anyAttributeUse,
            XmlSchemaAttributeGroup redefined,
            bool skipEquivalent)
        {
            int errorCount = 0;

            if (anyAttribute != null && anyAttributeUse == null)
            {
                anyAttributeUse = anyAttribute;
            }

            ArrayList newAttrNames = new ArrayList();

            foreach (XmlSchemaObject xsobj in attributes)
            {
                XmlSchemaAttributeGroupRef grpRef = xsobj as XmlSchemaAttributeGroupRef;
                if (grpRef != null)
                {
                    // Resolve attributeGroup redefinition.
                    XmlSchemaAttributeGroup grp = null;
                    if (redefined != null && grpRef.RefName == redefined.QualifiedName)
                    {
                        grp = redefined;
                    }
                    else
                    {
                        grp = schema.FindAttributeGroup(grpRef.RefName);
                    }
                    // otherwise, it might be missing sub components.
                    if (grp == null)
                    {
                        if (!schema.missedSubComponents)                        // && schema.Schemas [grpRef.RefName.Namespace] != null)
                        {
                            grpRef.error(h, "Referenced attribute group " + grpRef.RefName + " was not found in the corresponding schema.");
                        }
                        continue;
                    }
                    if (grp.AttributeGroupRecursionCheck)
                    {
                        grp.error(h, "Attribute group recursion was found: " + grpRef.RefName);
                        continue;
                    }
                    try {
                        grp.AttributeGroupRecursionCheck = true;
                        errorCount += grp.Validate(h, schema);
                    } finally {
                        grp.AttributeGroupRecursionCheck = false;
                    }
                    if (grp.AnyAttributeUse != null)
                    {
                        if (anyAttribute == null)
                        {
                            anyAttributeUse = grp.AnyAttributeUse;
                        }
                    }
                    foreach (DictionaryEntry entry in grp.AttributeUses)
                    {
                        XmlSchemaAttribute attr = (XmlSchemaAttribute)entry.Value;

                        if (StrictMsCompliant && attr.Use == XmlSchemaUse.Prohibited)
                        {
                            continue;
                        }

                        if (attr.RefName != null && attr.RefName != XmlQualifiedName.Empty && (!skipEquivalent || !AreAttributesEqual(attr, attributesResolved [attr.RefName] as XmlSchemaAttribute)))
                        {
                            AddToTable(attributesResolved, attr, attr.RefName, h);
                        }
                        else if (!skipEquivalent || !AreAttributesEqual(attr, attributesResolved [attr.QualifiedName] as XmlSchemaAttribute))
                        {
                            AddToTable(attributesResolved, attr, attr.QualifiedName, h);
                        }
                    }
                }
                else
                {
                    XmlSchemaAttribute attr = xsobj as XmlSchemaAttribute;
                    if (attr != null)
                    {
                        errorCount += attr.Validate(h, schema);

                        if (newAttrNames.Contains(attr.QualifiedName))
                        {
                            attr.error(h, String.Format("Duplicate attributes was found for '{0}'", attr.QualifiedName));
                        }
                        newAttrNames.Add(attr.QualifiedName);


                        if (StrictMsCompliant && attr.Use == XmlSchemaUse.Prohibited)
                        {
                            continue;
                        }

                        if (attr.RefName != null && attr.RefName != XmlQualifiedName.Empty && (!skipEquivalent || !AreAttributesEqual(attr, attributesResolved [attr.RefName] as XmlSchemaAttribute)))
                        {
                            AddToTable(attributesResolved, attr, attr.RefName, h);
                        }
                        else if (!skipEquivalent || !AreAttributesEqual(attr, attributesResolved [attr.QualifiedName] as XmlSchemaAttribute))
                        {
                            AddToTable(attributesResolved, attr, attr.QualifiedName, h);
                        }
                    }
                    else
                    {
                        if (anyAttribute != null)
                        {
                            anyAttributeUse = (XmlSchemaAnyAttribute)xsobj;
                            anyAttribute.Validate(h, schema);
                        }
                    }
                }
            }
            return(errorCount);
        }
 private XmlReader GetValidatingReader( XmlReader reader, XmlSchemaSet schemas, ValidationEventHandler validationEvent, XmlSchemaType schemaType, XmlSchemaElement schemaElement, XmlSchemaAttribute schemaAttribute ) {
     if (schemaAttribute != null) {
         return schemaAttribute.Validate(reader, null, schemas, validationEvent);
     }
     else if (schemaElement != null) {
         return schemaElement.Validate(reader, null, schemas, validationEvent);
     }
     else if (schemaType != null) {
         return schemaType.Validate(reader, null, schemas, validationEvent);
     }
     Debug.Assert( schemas != null, "schemas != null" );
     XmlReaderSettings readerSettings = new XmlReaderSettings();
     readerSettings.ConformanceLevel = ConformanceLevel.Auto;
     readerSettings.ValidationType = ValidationType.Schema;
     readerSettings.Schemas = schemas;
     readerSettings.ValidationEventHandler += validationEvent;
     return XmlReader.Create( reader, readerSettings );
 }
Exemple #5
0
		/// <summary>
		/// Schema Component: 
		///			QName, SimpleType, Scope, Default|Fixed, annotation
		/// </summary>
		internal override int Validate(ValidationEventHandler h, XmlSchema schema)
		{
			if(IsValidated (schema.ValidationId))
				return errorCount;

			// -- Attribute Declaration Schema Component --
			// {name}, {target namespace} -> QualifiedName. Already Compile()d.
			// {type definition} -> attributeType. From SchemaType or SchemaTypeName.
			// {scope} -> ParentIsSchema | isRedefineChild.
			// {value constraint} -> ValidatedFixedValue, ValidatedDefaultValue.
			// {annotation}
			// -- Attribute Use Schema Component --
			// {required}
			// {attribute declaration}
			// {value constraint}

			// First, fill type information for type reference
			if (SchemaType != null) {
				SchemaType.Validate (h, schema);
				attributeType = SchemaType;
			}
			else if (SchemaTypeName != null && SchemaTypeName != XmlQualifiedName.Empty)
			{
				// If type is null, then it is missing sub components .
				XmlSchemaType type = schema.FindSchemaType (SchemaTypeName);
				if (type is XmlSchemaComplexType)
					error(h,"An attribute can't have complexType Content");
				else if (type != null) {	// simple type
					errorCount += type.Validate (h, schema);
					attributeType = type;
				}
				else if (SchemaTypeName == XmlSchemaComplexType.AnyTypeName)
					attributeType = XmlSchemaComplexType.AnyType;
				else if (XmlSchemaUtil.IsBuiltInDatatypeName (SchemaTypeName)) {
					attributeType = XmlSchemaDatatype.FromName (SchemaTypeName);
					if (attributeType == null)
						error (h, "Invalid xml schema namespace datatype was specified.");
				}
				// otherwise, it might be missing sub components.
				else if (!schema.IsNamespaceAbsent (SchemaTypeName.Namespace))
					error (h, "Referenced schema type " + SchemaTypeName + " was not found in the corresponding schema.");
			}

			// Then, fill type information for the type references for the referencing attributes
			if (RefName != null && RefName != XmlQualifiedName.Empty)
			{
				referencedAttribute = schema.FindAttribute (RefName);
				// If el is null, then it is missing sub components .
				if (referencedAttribute != null)
					errorCount += referencedAttribute.Validate (h, schema);
				// otherwise, it might be missing sub components.
				else if (!schema.IsNamespaceAbsent (RefName.Namespace))
					error (h, "Referenced attribute " + RefName + " was not found in the corresponding schema.");
			}

			if (attributeType == null)
				attributeType = XmlSchemaSimpleType.AnySimpleType;

			// Validate {value constraints}
			if (defaultValue != null || fixedValue != null) {
				XmlSchemaDatatype datatype = attributeType as XmlSchemaDatatype;
				if (datatype == null)
					datatype = ((XmlSchemaSimpleType) attributeType).Datatype;
				if (datatype.TokenizedType == XmlTokenizedType.QName)
					error (h, "By the defection of the W3C XML Schema specification, it is impossible to supply QName default or fixed values.");
				else {
					try {
						if (defaultValue != null) {
							validatedDefaultValue = datatype.Normalize (defaultValue);
							datatype.ParseValue (validatedDefaultValue, null, null);
						}
					} catch (Exception ex) {
						// FIXME: This is not a good way to handle exception.
						error (h, "The Attribute's default value is invalid with its type definition.", ex);
					}
					try {
						if (fixedValue != null) {
							validatedFixedValue = datatype.Normalize (fixedValue);
							validatedFixedTypedValue = datatype.ParseValue (validatedFixedValue, null, null);
						}
					} catch (Exception ex) {
						// FIXME: This is not a good way to handle exception.
						error (h, "The Attribute's fixed value is invalid with its type definition.", ex);
					}
				}
			}
			if (Use == XmlSchemaUse.None)
				validatedUse = XmlSchemaUse.Optional;
			else
				validatedUse = Use;

#if NET_2_0
			if (attributeType != null) {
				attributeSchemaType = attributeType as XmlSchemaSimpleType;
				if (attributeType == XmlSchemaSimpleType.AnySimpleType)
					attributeSchemaType = XmlSchemaSimpleType.XsAnySimpleType;
				if (attributeSchemaType == null)
					attributeSchemaType = XmlSchemaType.GetBuiltInSimpleType (SchemaTypeName);
			}
#endif

			ValidationId = schema.ValidationId;
			return errorCount;
		}
Exemple #6
0
        public static int ValidateAttributesResolved(XmlSchemaObjectTable attributesResolved, ValidationEventHandler h, XmlSchema schema, XmlSchemaObjectCollection attributes, XmlSchemaAnyAttribute anyAttribute, ref XmlSchemaAnyAttribute anyAttributeUse, XmlSchemaAttributeGroup redefined, bool skipEquivalent)
        {
            int num = 0;

            if (anyAttribute != null && anyAttributeUse == null)
            {
                anyAttributeUse = anyAttribute;
            }
            ArrayList arrayList = new ArrayList();

            foreach (XmlSchemaObject xmlSchemaObject in attributes)
            {
                XmlSchemaAttributeGroupRef xmlSchemaAttributeGroupRef = xmlSchemaObject as XmlSchemaAttributeGroupRef;
                if (xmlSchemaAttributeGroupRef != null)
                {
                    XmlSchemaAttributeGroup xmlSchemaAttributeGroup = null;
                    if (redefined != null && xmlSchemaAttributeGroupRef.RefName == redefined.QualifiedName)
                    {
                        xmlSchemaAttributeGroup = redefined;
                    }
                    else
                    {
                        xmlSchemaAttributeGroup = schema.FindAttributeGroup(xmlSchemaAttributeGroupRef.RefName);
                    }
                    if (xmlSchemaAttributeGroup == null)
                    {
                        if (!schema.missedSubComponents)
                        {
                            xmlSchemaAttributeGroupRef.error(h, "Referenced attribute group " + xmlSchemaAttributeGroupRef.RefName + " was not found in the corresponding schema.");
                        }
                    }
                    else if (xmlSchemaAttributeGroup.AttributeGroupRecursionCheck)
                    {
                        xmlSchemaAttributeGroup.error(h, "Attribute group recursion was found: " + xmlSchemaAttributeGroupRef.RefName);
                    }
                    else
                    {
                        try
                        {
                            xmlSchemaAttributeGroup.AttributeGroupRecursionCheck = true;
                            num += xmlSchemaAttributeGroup.Validate(h, schema);
                        }
                        finally
                        {
                            xmlSchemaAttributeGroup.AttributeGroupRecursionCheck = false;
                        }
                        if (xmlSchemaAttributeGroup.AnyAttributeUse != null && anyAttribute == null)
                        {
                            anyAttributeUse = xmlSchemaAttributeGroup.AnyAttributeUse;
                        }
                        foreach (object obj in xmlSchemaAttributeGroup.AttributeUses)
                        {
                            XmlSchemaAttribute xmlSchemaAttribute = (XmlSchemaAttribute)((DictionaryEntry)obj).Value;
                            if (!XmlSchemaUtil.StrictMsCompliant || xmlSchemaAttribute.Use != XmlSchemaUse.Prohibited)
                            {
                                if (xmlSchemaAttribute.RefName != null && xmlSchemaAttribute.RefName != XmlQualifiedName.Empty && (!skipEquivalent || !XmlSchemaUtil.AreAttributesEqual(xmlSchemaAttribute, attributesResolved[xmlSchemaAttribute.RefName] as XmlSchemaAttribute)))
                                {
                                    XmlSchemaUtil.AddToTable(attributesResolved, xmlSchemaAttribute, xmlSchemaAttribute.RefName, h);
                                }
                                else if (!skipEquivalent || !XmlSchemaUtil.AreAttributesEqual(xmlSchemaAttribute, attributesResolved[xmlSchemaAttribute.QualifiedName] as XmlSchemaAttribute))
                                {
                                    XmlSchemaUtil.AddToTable(attributesResolved, xmlSchemaAttribute, xmlSchemaAttribute.QualifiedName, h);
                                }
                            }
                        }
                    }
                }
                else
                {
                    XmlSchemaAttribute xmlSchemaAttribute2 = xmlSchemaObject as XmlSchemaAttribute;
                    if (xmlSchemaAttribute2 != null)
                    {
                        num += xmlSchemaAttribute2.Validate(h, schema);
                        if (arrayList.Contains(xmlSchemaAttribute2.QualifiedName))
                        {
                            xmlSchemaAttribute2.error(h, string.Format("Duplicate attributes was found for '{0}'", xmlSchemaAttribute2.QualifiedName));
                        }
                        arrayList.Add(xmlSchemaAttribute2.QualifiedName);
                        if (!XmlSchemaUtil.StrictMsCompliant || xmlSchemaAttribute2.Use != XmlSchemaUse.Prohibited)
                        {
                            if (xmlSchemaAttribute2.RefName != null && xmlSchemaAttribute2.RefName != XmlQualifiedName.Empty && (!skipEquivalent || !XmlSchemaUtil.AreAttributesEqual(xmlSchemaAttribute2, attributesResolved[xmlSchemaAttribute2.RefName] as XmlSchemaAttribute)))
                            {
                                XmlSchemaUtil.AddToTable(attributesResolved, xmlSchemaAttribute2, xmlSchemaAttribute2.RefName, h);
                            }
                            else if (!skipEquivalent || !XmlSchemaUtil.AreAttributesEqual(xmlSchemaAttribute2, attributesResolved[xmlSchemaAttribute2.QualifiedName] as XmlSchemaAttribute))
                            {
                                XmlSchemaUtil.AddToTable(attributesResolved, xmlSchemaAttribute2, xmlSchemaAttribute2.QualifiedName, h);
                            }
                        }
                    }
                    else if (anyAttribute == null)
                    {
                        anyAttributeUse = (XmlSchemaAnyAttribute)xmlSchemaObject;
                        anyAttribute.Validate(h, schema);
                    }
                }
            }
            return(num);
        }