XmlSchemaGroup.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // System.Xml.Schema.XmlSchemaGroup.cs
  3. //
  4. // Author:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. using System;
  9. using System.Collections;
  10. using System.Xml.Serialization;
  11. using System.Xml;
  12. namespace System.Xml.Schema
  13. {
  14. /// <summary>
  15. /// refers to the named group
  16. /// </summary>
  17. public class XmlSchemaGroup : XmlSchemaAnnotated
  18. {
  19. private string name;
  20. private XmlSchemaGroupBase particle;
  21. private XmlQualifiedName qualifiedName;
  22. private bool isCircularDefinition;
  23. const string xmlname = "group";
  24. public XmlSchemaGroup()
  25. {
  26. }
  27. [System.Xml.Serialization.XmlAttribute("name")]
  28. public string Name
  29. {
  30. get{ return name; }
  31. set{ name = value; }
  32. }
  33. [XmlElement("all",typeof(XmlSchemaAll),Namespace=XmlSchema.Namespace)]
  34. [XmlElement("choice",typeof(XmlSchemaChoice),Namespace=XmlSchema.Namespace)]
  35. [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace=XmlSchema.Namespace)]
  36. public XmlSchemaGroupBase Particle
  37. {
  38. get{ return particle; }
  39. set{ particle = value; }
  40. }
  41. [XmlIgnore]
  42. internal XmlQualifiedName QualifiedName
  43. {
  44. get{ return qualifiedName;}
  45. }
  46. internal bool IsCircularDefinition
  47. {
  48. get { return isCircularDefinition; }
  49. }
  50. // 1. name must be present
  51. // 2. MinOccurs & MaxOccurs of the Particle must be absent
  52. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  53. {
  54. // If this is already compiled this time, simply skip.
  55. if (this.IsComplied (schema.CompilationId))
  56. return 0;
  57. if(Name == null)
  58. error(h,"Required attribute name must be present");
  59. else if(!XmlSchemaUtil.CheckNCName(this.name))
  60. error(h,"attribute name must be NCName");
  61. else
  62. qualifiedName = new XmlQualifiedName(Name,schema.TargetNamespace);
  63. if(Particle == null)
  64. {
  65. error(h,"Particle is required");
  66. }
  67. else
  68. {
  69. if(Particle.MaxOccursString != null)
  70. Particle.error(h,"MaxOccurs must not be present when the Particle is a child of Group");
  71. if(Particle.MinOccursString != null)
  72. Particle.error(h,"MinOccurs must not be present when the Particle is a child of Group");
  73. Particle.Compile (h, schema);
  74. }
  75. XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
  76. this.CompilationId = schema.CompilationId;
  77. return errorCount;
  78. }
  79. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  80. {
  81. if (this.IsValidated (schema.ValidationId))
  82. return errorCount;
  83. // 3.8.6 Model Group Correct :: 2. Circular group disallowed.
  84. if (Particle != null) { // in case of invalid schema.
  85. Particle.parentIsGroupDefinition = true;
  86. try {
  87. Particle.CheckRecursion (0, h, schema);
  88. } catch (XmlSchemaException ex) {
  89. error (h, ex.Message, ex);
  90. this.isCircularDefinition = true;
  91. return errorCount;
  92. }
  93. errorCount += Particle.Validate (h,schema);
  94. Particle.ValidateUniqueParticleAttribution (new XmlSchemaObjectTable (),
  95. new ArrayList (), h, schema);
  96. Particle.ValidateUniqueTypeAttribution (
  97. new XmlSchemaObjectTable (), h, schema);
  98. }
  99. this.ValidationId = schema.ValidationId;
  100. return errorCount;
  101. }
  102. //From the Errata
  103. //<group
  104. // id = ID
  105. // name = NCName
  106. // {any attributes with non-schema namespace . . .}>
  107. // Content: (annotation?, (all | choice | sequence)?)
  108. //</group>
  109. internal static XmlSchemaGroup Read(XmlSchemaReader reader, ValidationEventHandler h)
  110. {
  111. XmlSchemaGroup group = new XmlSchemaGroup();
  112. reader.MoveToElement();
  113. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  114. {
  115. error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
  116. reader.Skip();
  117. return null;
  118. }
  119. group.LineNumber = reader.LineNumber;
  120. group.LinePosition = reader.LinePosition;
  121. group.SourceUri = reader.BaseURI;
  122. while(reader.MoveToNextAttribute())
  123. {
  124. if(reader.Name == "id")
  125. {
  126. group.Id = reader.Value;
  127. }
  128. else if(reader.Name == "name")
  129. {
  130. group.name = reader.Value;
  131. }
  132. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  133. {
  134. error(h,reader.Name + " is not a valid attribute for group",null);
  135. }
  136. else
  137. {
  138. XmlSchemaUtil.ReadUnhandledAttribute(reader,group);
  139. }
  140. }
  141. reader.MoveToElement();
  142. if(reader.IsEmptyElement)
  143. return group;
  144. // Content: (annotation?, (all | choice | sequence)?)
  145. int level = 1;
  146. while(reader.ReadNextElement())
  147. {
  148. if(reader.NodeType == XmlNodeType.EndElement)
  149. {
  150. if(reader.LocalName != xmlname)
  151. error(h,"Should not happen :2: XmlSchemaGroup.Read, name="+reader.Name,null);
  152. break;
  153. }
  154. if(level <= 1 && reader.LocalName == "annotation")
  155. {
  156. level = 2; //Only one annotation
  157. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  158. if(annotation != null)
  159. group.Annotation = annotation;
  160. continue;
  161. }
  162. if(level <= 2)
  163. {
  164. if(reader.LocalName == "all")
  165. {
  166. level = 3;
  167. XmlSchemaAll all = XmlSchemaAll.Read(reader,h);
  168. if(all != null)
  169. group.Particle = all;
  170. continue;
  171. }
  172. if(reader.LocalName == "choice")
  173. {
  174. level = 3;
  175. XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
  176. if(choice != null)
  177. group.Particle = choice;
  178. continue;
  179. }
  180. if(reader.LocalName == "sequence")
  181. {
  182. level = 3;
  183. XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
  184. if(sequence != null)
  185. group.Particle = sequence;
  186. continue;
  187. }
  188. }
  189. reader.RaiseInvalidElementError();
  190. }
  191. return group;
  192. }
  193. }
  194. }