private bool IsValidRestriction(XmlSchemaParticle derivedParticle, XmlSchemaParticle baseParticle) {
if (derivedParticle == baseParticle) {
return true;
}
else if (derivedParticle == null || derivedParticle == XmlSchemaParticle.Empty) {
return IsParticleEmptiable(baseParticle);
}
else if (baseParticle == null || baseParticle == XmlSchemaParticle.Empty) {
return false;
}
if (derivedParticle is XmlSchemaElement) { //check for derived element being head of substitutionGroup
XmlSchemaElement derivedElem = (XmlSchemaElement)derivedParticle;
derivedParticle = CannonicalizeElement(derivedElem);
}
if (baseParticle is XmlSchemaElement) {
XmlSchemaElement baseElem = (XmlSchemaElement)baseParticle;
XmlSchemaParticle newBaseParticle;
newBaseParticle = CannonicalizeElement(baseElem);
if (newBaseParticle is XmlSchemaChoice) { //Base Element is subs grp head.
return IsValidRestriction(derivedParticle, newBaseParticle);
}
else if (derivedParticle is XmlSchemaElement) {
return IsElementFromElement((XmlSchemaElement)derivedParticle, baseElem);
}
else {
restrictionErrorMsg = Res.GetString(Res.Sch_ForbiddenDerivedParticleForElem);
return false;
}
}
else if (baseParticle is XmlSchemaAny) {
if (derivedParticle is XmlSchemaElement) {
return IsElementFromAny((XmlSchemaElement)derivedParticle, (XmlSchemaAny)baseParticle);
}
else if (derivedParticle is XmlSchemaAny) {
return IsAnyFromAny((XmlSchemaAny)derivedParticle, (XmlSchemaAny)baseParticle);
}
else {
return IsGroupBaseFromAny((XmlSchemaGroupBase)derivedParticle, (XmlSchemaAny)baseParticle);
}
}
else if (baseParticle is XmlSchemaAll) {
if (derivedParticle is XmlSchemaElement) {
return IsElementFromGroupBase((XmlSchemaElement)derivedParticle, (XmlSchemaGroupBase)baseParticle);
}
else if (derivedParticle is XmlSchemaAll) {
if(IsGroupBaseFromGroupBase((XmlSchemaGroupBase)derivedParticle, (XmlSchemaGroupBase)baseParticle, true)) {
return true;
}
}
else if (derivedParticle is XmlSchemaSequence) {
if(IsSequenceFromAll((XmlSchemaSequence)derivedParticle, (XmlSchemaAll)baseParticle)) {
return true;
}
restrictionErrorMsg = Res.GetString(Res.Sch_SeqFromAll, derivedParticle.LineNumber.ToString(NumberFormatInfo.InvariantInfo), derivedParticle.LinePosition.ToString(NumberFormatInfo.InvariantInfo), baseParticle.LineNumber.ToString(NumberFormatInfo.InvariantInfo), baseParticle.LinePosition.ToString(NumberFormatInfo.InvariantInfo));
}
else if (derivedParticle is XmlSchemaChoice || derivedParticle is XmlSchemaAny) {
restrictionErrorMsg = Res.GetString(Res.Sch_ForbiddenDerivedParticleForAll);
}
return false;
}
else if (baseParticle is XmlSchemaChoice ) {
if (derivedParticle is XmlSchemaElement) {
return IsElementFromGroupBase((XmlSchemaElement)derivedParticle, (XmlSchemaGroupBase)baseParticle);
}
else if (derivedParticle is XmlSchemaChoice) {
XmlSchemaChoice baseChoice = baseParticle as XmlSchemaChoice;
XmlSchemaChoice derivedChoice = derivedParticle as XmlSchemaChoice;
if (baseChoice.Parent == null || derivedChoice.Parent == null) { //using parent property to indicate this choice was created on the fly for substitutionGroup
return IsChoiceFromChoiceSubstGroup(derivedChoice, baseChoice);
}
if(IsGroupBaseFromGroupBase(derivedChoice, baseChoice, false)) {
return true;
}
}
else if (derivedParticle is XmlSchemaSequence) {
if(IsSequenceFromChoice((XmlSchemaSequence)derivedParticle, (XmlSchemaChoice)baseParticle)) {
return true;
}
restrictionErrorMsg = Res.GetString(Res.Sch_SeqFromChoice, derivedParticle.LineNumber.ToString(NumberFormatInfo.InvariantInfo), derivedParticle.LinePosition.ToString(NumberFormatInfo.InvariantInfo), baseParticle.LineNumber.ToString(NumberFormatInfo.InvariantInfo), baseParticle.LinePosition.ToString(NumberFormatInfo.InvariantInfo));
}
else {
restrictionErrorMsg = Res.GetString(Res.Sch_ForbiddenDerivedParticleForChoice);
}
return false;
}
else if (baseParticle is XmlSchemaSequence) {
if (derivedParticle is XmlSchemaElement) {
return IsElementFromGroupBase((XmlSchemaElement)derivedParticle, (XmlSchemaGroupBase)baseParticle);
}
else if (derivedParticle is XmlSchemaSequence || (derivedParticle is XmlSchemaAll && ((XmlSchemaGroupBase)derivedParticle).Items.Count ==1)) {
if (IsGroupBaseFromGroupBase((XmlSchemaGroupBase)derivedParticle, (XmlSchemaGroupBase)baseParticle, true)) {
return true;
}
}
else {
restrictionErrorMsg = Res.GetString(Res.Sch_ForbiddenDerivedParticleForSeq);
}
return false;
}
else {
Debug.Assert(false);
}
return false;
}