SetElementType() private method

private SetElementType ( XmlSchemaType value ) : void
value XmlSchemaType
return void
        private void CompileElement(XmlSchemaElement xe) {
            if (xe.IsProcessing) {
                SendValidationEvent(Res.Sch_ElementCircularRef, xe);
                return;
            }
            if (xe.ElementDecl != null) {
                return;
            }
            xe.IsProcessing = true;
            SchemaElementDecl decl = null;
            try {
                if (!xe.RefName.IsEmpty) {
                    XmlSchemaElement e = (XmlSchemaElement)elements[xe.RefName];
                    if (e == null) {
                        throw new XmlSchemaException(Res.Sch_UndeclaredElement, xe.RefName.ToString(), xe);
                    }  
                    CompileElement(e);
                    if (e.ElementDecl == null) {
                        throw new XmlSchemaException(Res.Sch_RefInvalidElement, xe.RefName.ToString(), xe);
                    }
                    xe.SetElementType(e.ElementSchemaType);
                    decl = e.ElementDecl.Clone();
                }
                else {
                    if (xe.SchemaType != null) {
                        xe.SetElementType(xe.SchemaType);
                    }
                    else if (!xe.SchemaTypeName.IsEmpty) {
                        xe.SetElementType(GetAnySchemaType(xe.SchemaTypeName));
                        if (xe.ElementSchemaType == null) {
                            throw new XmlSchemaException(Res.Sch_UndeclaredType, xe.SchemaTypeName.ToString(), xe);   
                        }
                    }
                    else  if (!xe.SubstitutionGroup.IsEmpty) {
                        XmlSchemaElement examplar = (XmlSchemaElement)elements[xe.SubstitutionGroup];
                        if (examplar == null) {
                            throw new XmlSchemaException(Res.Sch_UndeclaredEquivClass, xe.SubstitutionGroup.Name.ToString(CultureInfo.InvariantCulture), xe);   
                        }
                        if (examplar.IsProcessing) { //Circular subst group; already detected by now
                            return;
                        }
                        CompileElement(examplar);
                        if (examplar.ElementDecl == null) { //If head is invalid, fall back to AnyType
                            xe.SetElementType(XmlSchemaComplexType.AnyType);
                            decl = XmlSchemaComplexType.AnyType.ElementDecl.Clone();
                        }
                        else {
                            xe.SetElementType(examplar.ElementSchemaType);
                            decl = examplar.ElementDecl.Clone();
                        }
                    }
                    else {
                        xe.SetElementType(XmlSchemaComplexType.AnyType);
                        decl = XmlSchemaComplexType.AnyType.ElementDecl.Clone();
                    }
            
                    if (decl == null) {
                        Debug.Assert(xe.ElementSchemaType != null);
                        if (xe.ElementSchemaType is XmlSchemaComplexType) {
                            XmlSchemaComplexType complexType = (XmlSchemaComplexType)xe.ElementSchemaType;
                            CompileComplexType(complexType);
                            if (complexType.ElementDecl != null) {
                                decl = complexType.ElementDecl.Clone();
//                                decl.LocalElements = complexType.LocalElementDecls;
                            }
                        } 
                        else if (xe.ElementSchemaType is XmlSchemaSimpleType) {
                            XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)xe.ElementSchemaType;
                            CompileSimpleType(simpleType);
                            if (simpleType.ElementDecl != null) {
                                decl = simpleType.ElementDecl.Clone();
                            }
                        } 
                    }
                    Debug.Assert(decl != null);
                    decl.Name = xe.QualifiedName;
                    decl.IsAbstract = xe.IsAbstract;
                    XmlSchemaComplexType ct = xe.ElementSchemaType as XmlSchemaComplexType;
                    if (ct != null) {
                        decl.IsAbstract |= ct.IsAbstract; 
                    }
                    decl.IsNillable = xe.IsNillable;
                    decl.Block |= xe.BlockResolved;
                }
                if (decl.Datatype != null) {
                    decl.Datatype.VerifySchemaValid(notations, xe);
                }

                if (xe.DefaultValue != null || xe.FixedValue != null) {
                    if (decl.ContentValidator != null) {
                        if (decl.ContentValidator.ContentType == XmlSchemaContentType.TextOnly || (decl.ContentValidator.ContentType == XmlSchemaContentType.Mixed && decl.ContentValidator.IsEmptiable)) {
                            if (xe.DefaultValue != null) {
                                decl.Presence = SchemaDeclBase.Use.Default; 
                                decl.DefaultValueRaw = xe.DefaultValue;
                            }
                            else {
                                decl.Presence = SchemaDeclBase.Use.Fixed; 
                                decl.DefaultValueRaw = xe.FixedValue;
                            }
                            if (decl.Datatype != null) {
                                if (decl.Datatype.TypeCode == XmlTypeCode.Id) {
                                    SendValidationEvent(Res.Sch_DefaultIdValue, xe);
                                }
                                else {
                                    decl.DefaultValueTyped = decl.Datatype.ParseValue(decl.DefaultValueRaw, NameTable, new SchemaNamespaceManager(xe), true);
                                }
                            }
                            else { //Mixed with emptiable particle
                                decl.DefaultValueTyped = DatatypeImplementation.AnySimpleType.Datatype.ParseValue(decl.DefaultValueRaw, NameTable, new SchemaNamespaceManager(xe));
                            }
                        }
                        else {
                            throw new XmlSchemaException(Res.Sch_ElementCannotHaveValue, xe);
                        }
                    }
                }
                if (xe.HasConstraints) {
                    XmlSchemaObjectCollection constraints = xe.Constraints;
                    CompiledIdentityConstraint[] compiledConstraints = new CompiledIdentityConstraint[constraints.Count];
                    int idx = 0;
                    for (int i = 0; i < constraints.Count; ++i) {
                        XmlSchemaIdentityConstraint constraint = (XmlSchemaIdentityConstraint)constraints[i];
                        CompileIdentityConstraint(constraint);
                        compiledConstraints[idx ++] = constraint.CompiledConstraint;
                    }
                    decl.Constraints = compiledConstraints;
                }
                decl.SchemaElement = xe; //So this is available for PSVI
                xe.ElementDecl = decl;
            } 
            catch (XmlSchemaException e) {
                if (e.SourceSchemaObject == null) {
                    e.SetSource(xe);
                }
                SendValidationEvent(e);
                xe.ElementDecl = SchemaElementDecl.Empty;
            } 
            finally {
                xe.IsProcessing = false;
            }
        }
 private void CompileElement(XmlSchemaElement xe)
 {
     if (xe.IsProcessing)
     {
         base.SendValidationEvent("Sch_ElementCircularRef", xe);
     }
     else if (xe.ElementDecl == null)
     {
         xe.IsProcessing = true;
         SchemaElementDecl decl = null;
         try
         {
             if (!xe.RefName.IsEmpty)
             {
                 XmlSchemaElement element = (XmlSchemaElement) this.schema.Elements[xe.RefName];
                 if (element == null)
                 {
                     throw new XmlSchemaException("Sch_UndeclaredElement", xe.RefName.ToString(), xe);
                 }
                 this.CompileElement(element);
                 if (element.ElementDecl == null)
                 {
                     throw new XmlSchemaException("Sch_RefInvalidElement", xe.RefName.ToString(), xe);
                 }
                 xe.SetElementType(element.ElementSchemaType);
                 decl = element.ElementDecl.Clone();
             }
             else
             {
                 if (xe.SchemaType != null)
                 {
                     xe.SetElementType(xe.SchemaType);
                 }
                 else if (!xe.SchemaTypeName.IsEmpty)
                 {
                     xe.SetElementType(this.GetAnySchemaType(xe.SchemaTypeName));
                     if (xe.ElementSchemaType == null)
                     {
                         throw new XmlSchemaException("Sch_UndeclaredType", xe.SchemaTypeName.ToString(), xe);
                     }
                 }
                 else if (!xe.SubstitutionGroup.IsEmpty)
                 {
                     XmlSchemaElement element2 = (XmlSchemaElement) this.schema.Elements[xe.SubstitutionGroup];
                     if (element2 == null)
                     {
                         throw new XmlSchemaException("Sch_UndeclaredEquivClass", xe.SubstitutionGroup.Name.ToString(CultureInfo.InvariantCulture), xe);
                     }
                     if (element2.IsProcessing)
                     {
                         return;
                     }
                     this.CompileElement(element2);
                     if (element2.ElementDecl == null)
                     {
                         xe.SetElementType(XmlSchemaComplexType.AnyType);
                         decl = XmlSchemaComplexType.AnyType.ElementDecl.Clone();
                     }
                     else
                     {
                         xe.SetElementType(element2.ElementSchemaType);
                         decl = element2.ElementDecl.Clone();
                     }
                 }
                 else
                 {
                     xe.SetElementType(XmlSchemaComplexType.AnyType);
                     decl = XmlSchemaComplexType.AnyType.ElementDecl.Clone();
                 }
                 if (decl == null)
                 {
                     if (xe.ElementSchemaType is XmlSchemaComplexType)
                     {
                         XmlSchemaComplexType complexType = (XmlSchemaComplexType) xe.ElementSchemaType;
                         this.CompileComplexType(complexType);
                         if (complexType.ElementDecl != null)
                         {
                             decl = complexType.ElementDecl.Clone();
                         }
                     }
                     else if (xe.ElementSchemaType is XmlSchemaSimpleType)
                     {
                         XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType) xe.ElementSchemaType;
                         this.CompileSimpleType(simpleType);
                         if (simpleType.ElementDecl != null)
                         {
                             decl = simpleType.ElementDecl.Clone();
                         }
                     }
                 }
                 decl.Name = xe.QualifiedName;
                 decl.IsAbstract = xe.IsAbstract;
                 XmlSchemaComplexType elementSchemaType = xe.ElementSchemaType as XmlSchemaComplexType;
                 if (elementSchemaType != null)
                 {
                     decl.IsAbstract |= elementSchemaType.IsAbstract;
                 }
                 decl.IsNillable = xe.IsNillable;
                 decl.Block |= xe.BlockResolved;
             }
             if (decl.Datatype != null)
             {
                 decl.Datatype.VerifySchemaValid(this.schema.Notations, xe);
             }
             if (((xe.DefaultValue != null) || (xe.FixedValue != null)) && (decl.ContentValidator != null))
             {
                 if (decl.ContentValidator.ContentType == XmlSchemaContentType.TextOnly)
                 {
                     if (xe.DefaultValue != null)
                     {
                         decl.Presence = SchemaDeclBase.Use.Default;
                         decl.DefaultValueRaw = xe.DefaultValue;
                     }
                     else
                     {
                         decl.Presence = SchemaDeclBase.Use.Fixed;
                         decl.DefaultValueRaw = xe.FixedValue;
                     }
                     if (decl.Datatype != null)
                     {
                         decl.DefaultValueTyped = decl.Datatype.ParseValue(decl.DefaultValueRaw, base.NameTable, new SchemaNamespaceManager(xe), true);
                     }
                 }
                 else if ((decl.ContentValidator.ContentType != XmlSchemaContentType.Mixed) || !decl.ContentValidator.IsEmptiable)
                 {
                     throw new XmlSchemaException("Sch_ElementCannotHaveValue", xe);
                 }
             }
             if (xe.HasConstraints)
             {
                 XmlSchemaObjectCollection constraints = xe.Constraints;
                 CompiledIdentityConstraint[] constraintArray = new CompiledIdentityConstraint[constraints.Count];
                 int num = 0;
                 for (int i = 0; i < constraints.Count; i++)
                 {
                     XmlSchemaIdentityConstraint xi = (XmlSchemaIdentityConstraint) constraints[i];
                     this.CompileIdentityConstraint(xi);
                     constraintArray[num++] = xi.CompiledConstraint;
                 }
                 decl.Constraints = constraintArray;
             }
             decl.SchemaElement = xe;
             xe.ElementDecl = decl;
         }
         catch (XmlSchemaException exception)
         {
             if (exception.SourceSchemaObject == null)
             {
                 exception.SetSource(xe);
             }
             base.SendValidationEvent(exception);
             xe.ElementDecl = SchemaElementDecl.Empty;
         }
         finally
         {
             xe.IsProcessing = false;
         }
     }
 }
示例#3
0
        private void CompileElement(XmlSchemaElement xe) {
            if (xe.Validating) {
                SendValidationEvent(Res.Sch_ElementCircularRef, xe);
                return;
            }
            if (xe.ElementDecl != null) {
                return;
            }
            xe.Validating = true;
            SchemaElementDecl decl = null;
            try {
                if (!xe.RefName.IsEmpty) {
                    XmlSchemaElement e = (XmlSchemaElement)this.schema.Elements[xe.RefName];
                    if (e == null) {
                        throw new XmlSchemaException(Res.Sch_UndeclaredElement, xe.RefName.ToString(), xe);
                    }  
                    CompileElement(e);
                    if (e.ElementDecl == null) {
                        throw new XmlSchemaException(Res.Sch_RefInvalidElement, xe.RefName.ToString(), xe);
                    }
                    xe.SetElementType(e.ElementType);
                    decl = e.ElementDecl.Clone();
                }
                else {
                    if (xe.SchemaType != null) {
                        xe.SetElementType(xe.SchemaType);
                    }
                    else if (!xe.SchemaTypeName.IsEmpty) {
                        XmlSchemaDatatype datatype;
                        xe.SetElementType(GetAnySchemaType(xe.SchemaTypeName, out datatype));
                        if (xe.ElementType == null) {
                            throw new XmlSchemaException(Res.Sch_UndeclaredType, xe.SchemaTypeName.ToString(), xe);   
                        }
                    }
                    else  if (!xe.SubstitutionGroup.IsEmpty) {
                        XmlSchemaElement examplar = (XmlSchemaElement)this.schema.Elements[xe.SubstitutionGroup];
                        if (examplar == null) {
                            throw new XmlSchemaException(Res.Sch_UndeclaredEquivClass, xe.SubstitutionGroup.Name.ToString(), xe);   
                        }
                        CompileElement(examplar);
                        xe.SetElementType(examplar.ElementType);
                        decl = examplar.ElementDecl.Clone();
                    }
                    else {
                        xe.SetElementType(XmlSchemaDatatype.AnyType);
                        decl = SchemaElementDecl.CreateAnyTypeElementDecl();
                    }
            
                    if (decl == null) {
                        Debug.Assert(xe.ElementType != null);
                        if (xe.ElementType is XmlSchemaComplexType) {
                            XmlSchemaComplexType complexType = (XmlSchemaComplexType)xe.ElementType;
                            CompileComplexType(complexType);
                            if (complexType.ElementDecl != null) {
                                decl = complexType.ElementDecl.Clone();
                                decl.LocalElements = complexType.LocalElementDecls;
                            }
                        } 
                        else if (xe.ElementType is XmlSchemaSimpleType) {
                            XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)xe.ElementType;
                            CompileSimpleType(simpleType);
                            if (simpleType.ElementDecl != null) {
                                decl = simpleType.ElementDecl.Clone();
                            }
                        } 
                        else {
                            decl = new SchemaElementDecl((XmlSchemaDatatype)xe.ElementType, this.schemaNames);
                        }
                    }
                    decl.Name = xe.QualifiedName;
                    decl.IsAbstract = xe.IsAbstract;
                    XmlSchemaComplexType ct = xe.ElementType as XmlSchemaComplexType;
                    if (ct != null) {
                        decl.IsAbstract |= ct.IsAbstract; 
                    }
                    decl.IsNillable = xe.IsNillable;
                    decl.Block |= xe.BlockResolved;
                }
                if (decl.Datatype != null) {
                    decl.Datatype.VerifySchemaValid(this.schema, xe);
                }

                if (xe.DefaultValue != null || xe.FixedValue != null) {
                    if (decl.Content.ContentType == CompiledContentModel.Type.Text) {
                        if (xe.DefaultValue != null) {
                            decl.Presence = SchemaDeclBase.Use.Default; 
                            decl.DefaultValueTyped = decl.Datatype.ParseValue(xe.DefaultValue, this.nameTable, this.namespaceManager);
                        }
                        else {
                            decl.Presence = SchemaDeclBase.Use.Fixed; 
                            decl.DefaultValueTyped = decl.Datatype.ParseValue(xe.FixedValue, this.nameTable, this.namespaceManager);
                        }
                    }
                    else if (decl.Content.ContentType != CompiledContentModel.Type.Mixed || !decl.Content.IsEmptiable) {
                        throw new XmlSchemaException(Res.Sch_ElementCannotHaveValue, xe);
                    }            
                }
                if (xe.HasConstraints) {
                    XmlSchemaObjectCollection constraints = xe.Constraints;
                    CompiledIdentityConstraint[] compiledConstraints = new CompiledIdentityConstraint[constraints.Count];
                    int idx = 0;
                    foreach(XmlSchemaIdentityConstraint constraint in constraints) {
                        CompileIdentityConstraint (constraint);
                        compiledConstraints[idx ++] = constraint.CompiledConstraint;
                    }
                    decl.Constraints = compiledConstraints;
                }
                xe.ElementDecl = decl;
            } 
            catch (XmlSchemaException e) {
                if (e.SourceSchemaObject == null) {
                    e.SetSource(xe);
                }
                SendValidationEvent(e);
                xe.ElementDecl = SchemaElementDecl.Empty;
            } 
            finally {
                xe.Validating = false;
            }
        }