XmlSchemaGroup.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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="http://www.w3.org/2001/XMLSchema")]
  27. [XmlElement("choice",typeof(XmlSchemaChoice),Namespace="http://www.w3.org/2001/XMLSchema")]
  28. [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace="http://www.w3.org/2001/XMLSchema")]
  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, XmlSchemaInfo info)
  43. {
  44. if(Name == null)
  45. error(h,"Required attribute name must be present");
  46. else if(!XmlSchemaUtil.CheckNCName(this.name))
  47. error(h,"attribute name must be NCName");
  48. else
  49. qualifiedName = new XmlQualifiedName(Name,info.TargetNamespace);
  50. if(Particle == null)
  51. {
  52. error(h,"Particle is required");
  53. }
  54. else
  55. {
  56. if(Particle.MaxOccursString != null)
  57. Particle.error(h,"MaxOccurs must not be present when the Particle is a child of Group");
  58. if(Particle.MinOccursString != null)
  59. Particle.error(h,"MinOccurs must not be present when the Particle is a child of Group");
  60. if(Particle is XmlSchemaChoice)
  61. {
  62. errorCount += ((XmlSchemaChoice)Particle).Compile(h,info);
  63. }
  64. else if(Particle is XmlSchemaSequence)
  65. {
  66. errorCount += ((XmlSchemaSequence)Particle).Compile(h,info);
  67. }
  68. else if(Particle is XmlSchemaAll)
  69. {
  70. errorCount += ((XmlSchemaAll)Particle).Compile(h,info);
  71. }
  72. else
  73. {
  74. error(h,"only all,choice or sequence are allowed");
  75. }
  76. }
  77. XmlSchemaUtil.CompileID(Id,this,info.IDCollection,h);
  78. return errorCount;
  79. }
  80. [MonoTODO]
  81. internal int Validate(ValidationEventHandler h)
  82. {
  83. return errorCount;
  84. }
  85. //From the Errata
  86. //<group
  87. // id = ID
  88. // name = NCName
  89. // {any attributes with non-schema namespace . . .}>
  90. // Content: (annotation?, (all | choice | sequence)?)
  91. //</group>
  92. internal static XmlSchemaGroup Read(XmlSchemaReader reader, ValidationEventHandler h)
  93. {
  94. XmlSchemaGroup group = new XmlSchemaGroup();
  95. reader.MoveToElement();
  96. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  97. {
  98. error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
  99. reader.Skip();
  100. return null;
  101. }
  102. group.LineNumber = reader.LineNumber;
  103. group.LinePosition = reader.LinePosition;
  104. group.SourceUri = reader.BaseURI;
  105. while(reader.MoveToNextAttribute())
  106. {
  107. if(reader.Name == "id")
  108. {
  109. group.Id = reader.Value;
  110. }
  111. else if(reader.Name == "name")
  112. {
  113. group.name = reader.Value;
  114. }
  115. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  116. {
  117. error(h,reader.Name + " is not a valid attribute for group",null);
  118. }
  119. else
  120. {
  121. XmlSchemaUtil.ReadUnhandledAttribute(reader,group);
  122. }
  123. }
  124. reader.MoveToElement();
  125. if(reader.IsEmptyElement)
  126. return group;
  127. // Content: (annotation?, (all | choice | sequence)?)
  128. int level = 1;
  129. while(reader.ReadNextElement())
  130. {
  131. if(reader.NodeType == XmlNodeType.EndElement)
  132. {
  133. if(reader.LocalName != xmlname)
  134. error(h,"Should not happen :2: XmlSchemaGroup.Read, name="+reader.Name,null);
  135. break;
  136. }
  137. if(level <= 1 && reader.LocalName == "annotation")
  138. {
  139. level = 2; //Only one annotation
  140. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  141. if(annotation != null)
  142. group.Annotation = annotation;
  143. continue;
  144. }
  145. if(level <= 2)
  146. {
  147. if(reader.LocalName == "all")
  148. {
  149. level = 3;
  150. XmlSchemaAll all = XmlSchemaAll.Read(reader,h);
  151. if(all != null)
  152. group.Particle = all;
  153. continue;
  154. }
  155. if(reader.LocalName == "choice")
  156. {
  157. level = 3;
  158. XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
  159. if(choice != null)
  160. group.Particle = choice;
  161. continue;
  162. }
  163. if(reader.LocalName == "sequence")
  164. {
  165. level = 3;
  166. XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
  167. if(sequence != null)
  168. group.Particle = sequence;
  169. continue;
  170. }
  171. }
  172. reader.RaiseInvalidElementError();
  173. }
  174. return group;
  175. }
  176. }
  177. }