bool busy; // only for avoiding infinite loop on illegal recursion cases. internal override XmlSchemaParticle GetOptimizedParticle(bool isTop) { if (busy) { return(XmlSchemaParticle.Empty); } if (OptimizedParticle != null) { return(OptimizedParticle); } busy = true; XmlSchemaGroup g = referencedGroup != null ? referencedGroup : schema.Groups [RefName] as XmlSchemaGroup; if (g != null && g.Particle != null) { OptimizedParticle = g.Particle; OptimizedParticle = OptimizedParticle.GetOptimizedParticle(isTop); if (OptimizedParticle != XmlSchemaParticle.Empty && (ValidatedMinOccurs != 1 || ValidatedMaxOccurs != 1)) { OptimizedParticle = OptimizedParticle.GetShallowClone(); OptimizedParticle.OptimizedParticle = null; OptimizedParticle.MinOccurs = this.MinOccurs; OptimizedParticle.MaxOccurs = this.MaxOccurs; OptimizedParticle.CompileOccurence(null, null); } } else { OptimizedParticle = XmlSchemaParticle.Empty; } busy = false; return(OptimizedParticle); }
internal override int Validate(ValidationEventHandler h, XmlSchema schema) { if (IsValidated(schema.ValidationId)) { return(errorCount); } referencedGroup = schema.Groups [RefName] as XmlSchemaGroup; // it might be missing sub components. if (referencedGroup == null) { if (!schema.IsNamespaceAbsent(RefName.Namespace)) { error(h, "Referenced group " + RefName + " was not found in the corresponding schema."); } } // See Errata E1-26: minOccurs=0 is now allowed. else if (referencedGroup.Particle is XmlSchemaAll && ValidatedMaxOccurs != 1) { error(h, "Group reference to -all- particle must have schema component {maxOccurs}=1."); } if (TargetGroup != null) { TargetGroup.Validate(h, schema); } ValidationId = schema.ValidationId; 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); } } }
//From the Errata //<group // id = ID // name = NCName // {any attributes with non-schema namespace . . .}> // Content: (annotation?, (all | choice | sequence)?) //</group> internal static XmlSchemaGroup Read(XmlSchemaReader reader, ValidationEventHandler h) { XmlSchemaGroup group = new XmlSchemaGroup(); reader.MoveToElement(); if (reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname) { error(h, "Should not happen :1: XmlSchemaGroup.Read, name=" + reader.Name, null); reader.Skip(); return(null); } group.LineNumber = reader.LineNumber; group.LinePosition = reader.LinePosition; group.SourceUri = reader.BaseURI; while (reader.MoveToNextAttribute()) { if (reader.Name == "id") { group.Id = reader.Value; } else if (reader.Name == "name") { group.name = reader.Value; } else if ((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace) { error(h, reader.Name + " is not a valid attribute for group", null); } else { XmlSchemaUtil.ReadUnhandledAttribute(reader, group); } } reader.MoveToElement(); if (reader.IsEmptyElement) { return(group); } // Content: (annotation?, (all | choice | sequence)?) int level = 1; while (reader.ReadNextElement()) { if (reader.NodeType == XmlNodeType.EndElement) { if (reader.LocalName != xmlname) { error(h, "Should not happen :2: XmlSchemaGroup.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) { group.Annotation = annotation; } continue; } if (level <= 2) { if (reader.LocalName == "all") { level = 3; XmlSchemaAll all = XmlSchemaAll.Read(reader, h); if (all != null) { group.Particle = all; } continue; } if (reader.LocalName == "choice") { level = 3; XmlSchemaChoice choice = XmlSchemaChoice.Read(reader, h); if (choice != null) { group.Particle = choice; } continue; } if (reader.LocalName == "sequence") { level = 3; XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader, h); if (sequence != null) { group.Particle = sequence; } continue; } } reader.RaiseInvalidElementError(); } return(group); }
//<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); }