XmlSchemaGroup.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. private static 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. [MonoTODO]
  53. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  54. {
  55. // If this is already compiled this time, simply skip.
  56. if (this.IsComplied (schema.CompilationId))
  57. return 0;
  58. if(Name == null)
  59. error(h,"Required attribute name must be present");
  60. else if(!XmlSchemaUtil.CheckNCName(this.name))
  61. error(h,"attribute name must be NCName");
  62. else
  63. qualifiedName = new XmlQualifiedName(Name,schema.TargetNamespace);
  64. if(Particle == null)
  65. {
  66. error(h,"Particle is required");
  67. }
  68. else
  69. {
  70. if(Particle.MaxOccursString != null)
  71. Particle.error(h,"MaxOccurs must not be present when the Particle is a child of Group");
  72. if(Particle.MinOccursString != null)
  73. Particle.error(h,"MinOccurs must not be present when the Particle is a child of Group");
  74. if(Particle is XmlSchemaChoice)
  75. {
  76. errorCount += ((XmlSchemaChoice)Particle).Compile(h,schema);
  77. }
  78. else if(Particle is XmlSchemaSequence)
  79. {
  80. errorCount += ((XmlSchemaSequence)Particle).Compile(h,schema);
  81. }
  82. else if(Particle is XmlSchemaAll)
  83. {
  84. errorCount += ((XmlSchemaAll)Particle).Compile(h,schema);
  85. }
  86. else
  87. {
  88. error(h,"only all,choice or sequence are allowed");
  89. }
  90. }
  91. XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
  92. this.CompilationId = schema.CompilationId;
  93. return errorCount;
  94. }
  95. [MonoTODO]
  96. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  97. {
  98. if (this.IsValidated (schema.ValidationId))
  99. return errorCount;
  100. // 3.8.6 Model Group Correct :: 2. Circular group disallowed.
  101. if (Particle != null) { // in case of invalid schema.
  102. Particle.parentIsGroupDefinition = true;
  103. try {
  104. Particle.CheckRecursion (0, h, schema);
  105. } catch (XmlSchemaException ex) {
  106. error (h, ex.Message, ex);
  107. this.isCircularDefinition = true;
  108. return errorCount;
  109. }
  110. errorCount += Particle.Validate (h,schema);
  111. Particle.ValidateUniqueParticleAttribution (new XmlSchemaObjectTable (),
  112. new ArrayList (), h, schema);
  113. Particle.ValidateUniqueTypeAttribution (
  114. new XmlSchemaObjectTable (), h, schema);
  115. }
  116. this.ValidationId = schema.ValidationId;
  117. return errorCount;
  118. }
  119. //From the Errata
  120. //<group
  121. // id = ID
  122. // name = NCName
  123. // {any attributes with non-schema namespace . . .}>
  124. // Content: (annotation?, (all | choice | sequence)?)
  125. //</group>
  126. internal static XmlSchemaGroup Read(XmlSchemaReader reader, ValidationEventHandler h)
  127. {
  128. XmlSchemaGroup group = new XmlSchemaGroup();
  129. reader.MoveToElement();
  130. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  131. {
  132. error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
  133. reader.Skip();
  134. return null;
  135. }
  136. group.LineNumber = reader.LineNumber;
  137. group.LinePosition = reader.LinePosition;
  138. group.SourceUri = reader.BaseURI;
  139. while(reader.MoveToNextAttribute())
  140. {
  141. if(reader.Name == "id")
  142. {
  143. group.Id = reader.Value;
  144. }
  145. else if(reader.Name == "name")
  146. {
  147. group.name = reader.Value;
  148. }
  149. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  150. {
  151. error(h,reader.Name + " is not a valid attribute for group",null);
  152. }
  153. else
  154. {
  155. XmlSchemaUtil.ReadUnhandledAttribute(reader,group);
  156. }
  157. }
  158. reader.MoveToElement();
  159. if(reader.IsEmptyElement)
  160. return group;
  161. // Content: (annotation?, (all | choice | sequence)?)
  162. int level = 1;
  163. while(reader.ReadNextElement())
  164. {
  165. if(reader.NodeType == XmlNodeType.EndElement)
  166. {
  167. if(reader.LocalName != xmlname)
  168. error(h,"Should not happen :2: XmlSchemaGroup.Read, name="+reader.Name,null);
  169. break;
  170. }
  171. if(level <= 1 && reader.LocalName == "annotation")
  172. {
  173. level = 2; //Only one annotation
  174. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  175. if(annotation != null)
  176. group.Annotation = annotation;
  177. continue;
  178. }
  179. if(level <= 2)
  180. {
  181. if(reader.LocalName == "all")
  182. {
  183. level = 3;
  184. XmlSchemaAll all = XmlSchemaAll.Read(reader,h);
  185. if(all != null)
  186. group.Particle = all;
  187. continue;
  188. }
  189. if(reader.LocalName == "choice")
  190. {
  191. level = 3;
  192. XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
  193. if(choice != null)
  194. group.Particle = choice;
  195. continue;
  196. }
  197. if(reader.LocalName == "sequence")
  198. {
  199. level = 3;
  200. XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
  201. if(sequence != null)
  202. group.Particle = sequence;
  203. continue;
  204. }
  205. }
  206. reader.RaiseInvalidElementError();
  207. }
  208. return group;
  209. }
  210. }
  211. }