XmlSchemaGroup.cs 5.2 KB

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