private object ValidateElementContext(XmlQualifiedName elementName, out bool invalidElementInContext)
{
object particle = null;
int errorCode = 0;
XmlQualifiedName head;
XmlSchemaElement headElement = null;
invalidElementInContext = false;
if (_context.NeedValidateChildren)
{
if (_context.IsNill)
{
SendValidationEvent(SR.Sch_ContentInNill, QNameString(_context.LocalName, _context.Namespace));
return null;
}
ContentValidator contentValidator = _context.ElementDecl.ContentValidator;
if (contentValidator.ContentType == XmlSchemaContentType.Mixed && _context.ElementDecl.Presence == SchemaDeclBase.Use.Fixed)
{ //Mixed with default or fixed
SendValidationEvent(SR.Sch_ElementInMixedWithFixed, QNameString(_context.LocalName, _context.Namespace));
return null;
}
head = elementName;
bool substitution = false;
while (true)
{
particle = _context.ElementDecl.ContentValidator.ValidateElement(head, _context, out errorCode);
if (particle != null)
{ //Match found
break;
}
if (errorCode == -2)
{ //ContentModel all group error
SendValidationEvent(SR.Sch_AllElement, elementName.ToString());
invalidElementInContext = true;
_processContents = _context.ProcessContents = XmlSchemaContentProcessing.Skip;
return null;
}
//Match not found; check for substitutionGroup
substitution = true;
headElement = GetSubstitutionGroupHead(head);
if (headElement == null)
{
break;
}
else
{
head = headElement.QualifiedName;
}
}
if (substitution)
{
XmlSchemaElement matchedElem = particle as XmlSchemaElement;
if (matchedElem == null)
{ //It matched an xs:any in that position
particle = null;
}
else if (matchedElem.RefName.IsEmpty)
{ //It is not element ref but a local element
//If the head and matched particle are not hte same, then this is not substitutable, duped by a localElement with same QName
SendValidationEvent(SR.Sch_InvalidElementSubstitution, BuildElementName(elementName), BuildElementName(matchedElem.QualifiedName));
invalidElementInContext = true;
_processContents = _context.ProcessContents = XmlSchemaContentProcessing.Skip;
}
else
{ //Correct substitution head found
particle = _compiledSchemaInfo.GetElement(elementName); //Re-assign correct particle
_context.NeedValidateChildren = true; //This will be reset to false once member match is not found
}
}
if (particle == null)
{
ElementValidationError(elementName, _context, _eventHandler, _nsResolver, _sourceUriString, _positionInfo.LineNumber, _positionInfo.LinePosition, _schemaSet);
invalidElementInContext = true;
_processContents = _context.ProcessContents = XmlSchemaContentProcessing.Skip;
}
}
return particle;
}