internal override int Validate(ValidationEventHandler h, XmlSchema schema) { if (IsValidated(schema.CompilationId)) { return(errorCount); } if (redefined == null && redefinedObject != null) { redefinedObject.Compile(h, schema); redefined = (XmlSchemaAttributeGroup)redefinedObject; redefined.Validate(h, schema); } XmlSchemaObjectCollection actualAttributes = null; /* * if (this.redefined != null) { * actualAttributes = new XmlSchemaObjectCollection (); * foreach (XmlSchemaObject obj in Attributes) { * XmlSchemaAttributeGroupRef grp = obj as XmlSchemaAttributeGroupRef; * if (grp != null && grp.QualifiedName == this.QualifiedName) * actualAttributes.Add (redefined); * else * actualAttributes.Add (obj); * } * } * else */ actualAttributes = Attributes; attributeUses = new XmlSchemaObjectTable(); errorCount += XmlSchemaUtil.ValidateAttributesResolved(attributeUses, h, schema, actualAttributes, AnyAttribute, ref anyAttributeUse, redefined, false); ValidationId = schema.ValidationId; return(errorCount); }
//<attributeGroup // id = ID // name = NCName // ref = QName // Not present in this class. // {any attributes with non-schema namespace . . .}> // Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?)) //</attributeGroup> internal static XmlSchemaAttributeGroup Read(XmlSchemaReader reader, ValidationEventHandler h) { XmlSchemaAttributeGroup attrgrp = new XmlSchemaAttributeGroup(); reader.MoveToElement(); if (reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname) { error(h, "Should not happen :1: XmlSchemaAttributeGroup.Read, name=" + reader.Name, null); reader.SkipToEnd(); return(null); } attrgrp.LineNumber = reader.LineNumber; attrgrp.LinePosition = reader.LinePosition; attrgrp.SourceUri = reader.BaseURI; while (reader.MoveToNextAttribute()) { if (reader.Name == "id") { attrgrp.Id = reader.Value; } else if (reader.Name == "name") { attrgrp.name = reader.Value; } else if ((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace) { error(h, reader.Name + " is not a valid attribute for attributeGroup in this context", null); } else { XmlSchemaUtil.ReadUnhandledAttribute(reader, attrgrp); } } reader.MoveToElement(); if (reader.IsEmptyElement) { return(attrgrp); } //Content: 1.annotation?, 2.(attribute | attributeGroup)*, 3.anyAttribute? int level = 1; while (reader.ReadNextElement()) { if (reader.NodeType == XmlNodeType.EndElement) { if (reader.LocalName != xmlname) { error(h, "Should not happen :2: XmlSchemaAttributeGroup.Read, name=" + reader.Name, null); } break; } if (level <= 1 && reader.LocalName == "annotation") { level = 2; //Only one annotation XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader, h); if (annotation != null) { attrgrp.Annotation = annotation; } continue; } if (level <= 2) { if (reader.LocalName == "attribute") { level = 2; XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader, h); if (attr != null) { attrgrp.Attributes.Add(attr); } continue; } if (reader.LocalName == "attributeGroup") { level = 2; XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader, h); if (attr != null) { attrgrp.attributes.Add(attr); } continue; } } if (level <= 3 && reader.LocalName == "anyAttribute") { level = 4; XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader, h); if (anyattr != null) { attrgrp.AnyAttribute = anyattr; } continue; } reader.RaiseInvalidElementError(); } return(attrgrp); }
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 static void ReadContent(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h) { reader.MoveToElement(); if (reader.LocalName != "schema" && reader.NamespaceURI != XmlSchema.Namespace && reader.NodeType != XmlNodeType.Element) { error(h, "UNREACHABLE CODE REACHED: Method: Schema.ReadContent, " + reader.LocalName + ", " + reader.NamespaceURI, null); } //(include | import | redefine | annotation)*, //((simpleType | complexType | group | attributeGroup | element | attribute | notation | annotation)* int level = 1; while (reader.ReadNextElement()) { if (reader.NodeType == XmlNodeType.EndElement) { if (reader.LocalName != xmlname) { error(h, "Should not happen :2: XmlSchema.Read, name=" + reader.Name, null); } break; } if (level <= 1) { if (reader.LocalName == "include") { XmlSchemaInclude include = XmlSchemaInclude.Read(reader, h); if (include != null) { schema.includes.Add(include); } continue; } if (reader.LocalName == "import") { XmlSchemaImport import = XmlSchemaImport.Read(reader, h); if (import != null) { schema.includes.Add(import); } continue; } if (reader.LocalName == "redefine") { XmlSchemaRedefine redefine = XmlSchemaRedefine.Read(reader, h); if (redefine != null) { schema.includes.Add(redefine); } continue; } if (reader.LocalName == "annotation") { XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader, h); if (annotation != null) { schema.items.Add(annotation); } continue; } } if (level <= 2) { level = 2; if (reader.LocalName == "simpleType") { XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader, h); if (stype != null) { schema.items.Add(stype); } continue; } if (reader.LocalName == "complexType") { XmlSchemaComplexType ctype = XmlSchemaComplexType.Read(reader, h); if (ctype != null) { schema.items.Add(ctype); } continue; } if (reader.LocalName == "group") { XmlSchemaGroup group = XmlSchemaGroup.Read(reader, h); if (group != null) { schema.items.Add(group); } continue; } if (reader.LocalName == "attributeGroup") { XmlSchemaAttributeGroup attributeGroup = XmlSchemaAttributeGroup.Read(reader, h); if (attributeGroup != null) { schema.items.Add(attributeGroup); } continue; } if (reader.LocalName == "element") { XmlSchemaElement element = XmlSchemaElement.Read(reader, h); if (element != null) { schema.items.Add(element); } continue; } if (reader.LocalName == "attribute") { XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader, h); if (attr != null) { schema.items.Add(attr); } continue; } if (reader.LocalName == "notation") { XmlSchemaNotation notation = XmlSchemaNotation.Read(reader, h); if (notation != null) { schema.items.Add(notation); } continue; } if (reader.LocalName == "annotation") { XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader, h); if (annotation != null) { schema.items.Add(annotation); } continue; } } reader.RaiseInvalidElementError(); } }
void DoCompile(ValidationEventHandler handler, List <CompiledSchemaMemo> handledUris, XmlSchemaSet col, XmlResolver resolver) { SetParent(); CompilationId = col.CompilationId; schemas = col; if (!schemas.Contains(this)) // e.g. xs:import { schemas.Add(this); } attributeGroups.Clear(); attributes.Clear(); elements.Clear(); groups.Clear(); notations.Clear(); schemaTypes.Clear(); named_identities.Clear(); ids.Clear(); compilationItems.Clear(); //1. Union and List are not allowed in block default if (BlockDefault != XmlSchemaDerivationMethod.All) { if ((BlockDefault & XmlSchemaDerivationMethod.List) != 0) { error(handler, "list is not allowed in blockDefault attribute"); } if ((BlockDefault & XmlSchemaDerivationMethod.Union) != 0) { error(handler, "union is not allowed in blockDefault attribute"); } } //2. Substitution is not allowed in finaldefault. if (FinalDefault != XmlSchemaDerivationMethod.All) { if ((FinalDefault & XmlSchemaDerivationMethod.Substitution) != 0) { error(handler, "substitution is not allowed in finalDefault attribute"); } } //3. id must be of type ID XmlSchemaUtil.CompileID(Id, this, IDCollection, handler); //4. targetNamespace should be of type anyURI or absent if (TargetNamespace != null) { if (TargetNamespace.Length == 0) { error(handler, "The targetNamespace attribute cannot have have empty string as its value."); } if (!XmlSchemaUtil.CheckAnyUri(TargetNamespace)) { error(handler, TargetNamespace + " is not a valid value for targetNamespace attribute of schema"); } } //5. version should be of type normalizedString if (!XmlSchemaUtil.CheckNormalizedString(Version)) { error(handler, Version + "is not a valid value for version attribute of schema"); } // Compile the content of this schema for (int i = 0; i < Items.Count; i++) { compilationItems.Add(Items [i]); } // First, we run into inclusion schemas to collect // compilation target items into compiledItems. for (int i = 0; i < Includes.Count; i++) { ProcessExternal(handler, handledUris, resolver, Includes [i] as XmlSchemaExternal, col); } // Compilation phase. // At least each Compile() must give unique (qualified) name for each component. // It also checks self-resolvable properties correctness. // Post compilation schema information contribution is not done here. // It should be done by Validate(). for (int i = 0; i < compilationItems.Count; i++) { XmlSchemaObject obj = compilationItems [i]; if (obj is XmlSchemaAnnotation) { int numerr = ((XmlSchemaAnnotation)obj).Compile(handler, this); errorCount += numerr; } else if (obj is XmlSchemaAttribute) { XmlSchemaAttribute attr = (XmlSchemaAttribute)obj; int numerr = attr.Compile(handler, this); errorCount += numerr; if (numerr == 0) { XmlSchemaUtil.AddToTable(Attributes, attr, attr.QualifiedName, handler); } } else if (obj is XmlSchemaAttributeGroup) { XmlSchemaAttributeGroup attrgrp = (XmlSchemaAttributeGroup)obj; int numerr = attrgrp.Compile(handler, this); errorCount += numerr; if (numerr == 0) { XmlSchemaUtil.AddToTable( AttributeGroups, attrgrp, attrgrp.QualifiedName, handler); } } else if (obj is XmlSchemaComplexType) { XmlSchemaComplexType ctype = (XmlSchemaComplexType)obj; int numerr = ctype.Compile(handler, this); errorCount += numerr; if (numerr == 0) { XmlSchemaUtil.AddToTable( schemaTypes, ctype, ctype.QualifiedName, handler); } } else if (obj is XmlSchemaSimpleType) { XmlSchemaSimpleType stype = (XmlSchemaSimpleType)obj; stype.islocal = false; //This simple type is toplevel int numerr = stype.Compile(handler, this); errorCount += numerr; if (numerr == 0) { XmlSchemaUtil.AddToTable( SchemaTypes, stype, stype.QualifiedName, handler); } } else if (obj is XmlSchemaElement) { XmlSchemaElement elem = (XmlSchemaElement)obj; elem.parentIsSchema = true; int numerr = elem.Compile(handler, this); errorCount += numerr; if (numerr == 0) { XmlSchemaUtil.AddToTable( Elements, elem, elem.QualifiedName, handler); } } else if (obj is XmlSchemaGroup) { XmlSchemaGroup grp = (XmlSchemaGroup)obj; int numerr = grp.Compile(handler, this); errorCount += numerr; if (numerr == 0) { XmlSchemaUtil.AddToTable( Groups, grp, grp.QualifiedName, handler); } } else if (obj is XmlSchemaNotation) { XmlSchemaNotation ntn = (XmlSchemaNotation)obj; int numerr = ntn.Compile(handler, this); errorCount += numerr; if (numerr == 0) { XmlSchemaUtil.AddToTable( Notations, ntn, ntn.QualifiedName, handler); } } else { ValidationHandler.RaiseValidationEvent( handler, null, String.Format("Object of Type {0} is not valid in Item Property of Schema", obj.GetType().Name), null, this, null, XmlSeverityType.Error); } } }
//<redefine // id = ID // schemaLocation = anyURI // {any attributes with non-schema namespace . . .}> // Content: (annotation | (simpleType | complexType | group | attributeGroup))* //</redefine> internal static XmlSchemaRedefine Read(XmlSchemaReader reader, ValidationEventHandler h) { XmlSchemaRedefine redefine = new XmlSchemaRedefine(); reader.MoveToElement(); if (reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname) { error(h, "Should not happen :1: XmlSchemaRedefine.Read, name=" + reader.Name, null); reader.Skip(); return(null); } redefine.LineNumber = reader.LineNumber; redefine.LinePosition = reader.LinePosition; redefine.SourceUri = reader.BaseURI; while (reader.MoveToNextAttribute()) { if (reader.Name == "id") { redefine.Id = reader.Value; } else if (reader.Name == "schemaLocation") { redefine.SchemaLocation = reader.Value; } else if ((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace) { error(h, reader.Name + " is not a valid attribute for redefine", null); } else { XmlSchemaUtil.ReadUnhandledAttribute(reader, redefine); } } reader.MoveToElement(); if (reader.IsEmptyElement) { return(redefine); } //(annotation | (simpleType | complexType | group | attributeGroup))* while (reader.ReadNextElement()) { if (reader.NodeType == XmlNodeType.EndElement) { if (reader.LocalName != xmlname) { error(h, "Should not happen :2: XmlSchemaRedefine.Read, name=" + reader.Name, null); } break; } if (reader.LocalName == "annotation") { XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader, h); if (annotation != null) { redefine.items.Add(annotation); } continue; } if (reader.LocalName == "simpleType") { XmlSchemaSimpleType simpleType = XmlSchemaSimpleType.Read(reader, h); if (simpleType != null) { redefine.items.Add(simpleType); } continue; } if (reader.LocalName == "complexType") { XmlSchemaComplexType complexType = XmlSchemaComplexType.Read(reader, h); if (complexType != null) { redefine.items.Add(complexType); } continue; } if (reader.LocalName == "group") { XmlSchemaGroup group = XmlSchemaGroup.Read(reader, h); if (group != null) { redefine.items.Add(group); } continue; } if (reader.LocalName == "attributeGroup") { XmlSchemaAttributeGroup attributeGroup = XmlSchemaAttributeGroup.Read(reader, h); if (attributeGroup != null) { redefine.items.Add(attributeGroup); } continue; } reader.RaiseInvalidElementError(); } return(redefine); }