XmlSchemaGroup.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // System.Xml.Schema.XmlSchemaGroup.cs
  3. //
  4. // Author:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Xml.Serialization;
  31. using System.Xml;
  32. namespace System.Xml.Schema
  33. {
  34. /// <summary>
  35. /// refers to the named group
  36. /// </summary>
  37. public class XmlSchemaGroup : XmlSchemaAnnotated
  38. {
  39. private string name;
  40. private XmlSchemaGroupBase particle;
  41. private XmlQualifiedName qualifiedName;
  42. private bool isCircularDefinition;
  43. const string xmlname = "group";
  44. public XmlSchemaGroup()
  45. {
  46. }
  47. [System.Xml.Serialization.XmlAttribute("name")]
  48. public string Name
  49. {
  50. get{ return name; }
  51. set{ name = value; }
  52. }
  53. [XmlElement("all",typeof(XmlSchemaAll),Namespace=XmlSchema.Namespace)]
  54. [XmlElement("choice",typeof(XmlSchemaChoice),Namespace=XmlSchema.Namespace)]
  55. [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace=XmlSchema.Namespace)]
  56. public XmlSchemaGroupBase Particle
  57. {
  58. get{ return particle; }
  59. set{ particle = value; }
  60. }
  61. [XmlIgnore]
  62. internal XmlQualifiedName QualifiedName
  63. {
  64. get{ return qualifiedName;}
  65. }
  66. internal bool IsCircularDefinition
  67. {
  68. get { return isCircularDefinition; }
  69. }
  70. // 1. name must be present
  71. // 2. MinOccurs & MaxOccurs of the Particle must be absent
  72. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  73. {
  74. // If this is already compiled this time, simply skip.
  75. if (this.IsComplied (schema.CompilationId))
  76. return 0;
  77. if(Name == null)
  78. error(h,"Required attribute name must be present");
  79. else if(!XmlSchemaUtil.CheckNCName(this.name))
  80. error(h,"attribute name must be NCName");
  81. else
  82. qualifiedName = new XmlQualifiedName(Name,schema.TargetNamespace);
  83. if(Particle == null)
  84. {
  85. error(h,"Particle is required");
  86. }
  87. else
  88. {
  89. if(Particle.MaxOccursString != null)
  90. Particle.error(h,"MaxOccurs must not be present when the Particle is a child of Group");
  91. if(Particle.MinOccursString != null)
  92. Particle.error(h,"MinOccurs must not be present when the Particle is a child of Group");
  93. Particle.Compile (h, schema);
  94. }
  95. XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
  96. this.CompilationId = schema.CompilationId;
  97. return errorCount;
  98. }
  99. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  100. {
  101. if (this.IsValidated (schema.ValidationId))
  102. return errorCount;
  103. // 3.8.6 Model Group Correct :: 2. Circular group disallowed.
  104. if (Particle != null) { // in case of invalid schema.
  105. Particle.parentIsGroupDefinition = true;
  106. try {
  107. Particle.CheckRecursion (0, h, schema);
  108. } catch (XmlSchemaException ex) {
  109. error (h, ex.Message, ex);
  110. this.isCircularDefinition = true;
  111. return errorCount;
  112. }
  113. errorCount += Particle.Validate (h,schema);
  114. Particle.ValidateUniqueParticleAttribution (new XmlSchemaObjectTable (),
  115. new ArrayList (), h, schema);
  116. Particle.ValidateUniqueTypeAttribution (
  117. new XmlSchemaObjectTable (), h, schema);
  118. }
  119. this.ValidationId = schema.ValidationId;
  120. return errorCount;
  121. }
  122. //From the Errata
  123. //<group
  124. // id = ID
  125. // name = NCName
  126. // {any attributes with non-schema namespace . . .}>
  127. // Content: (annotation?, (all | choice | sequence)?)
  128. //</group>
  129. internal static XmlSchemaGroup Read(XmlSchemaReader reader, ValidationEventHandler h)
  130. {
  131. XmlSchemaGroup group = new XmlSchemaGroup();
  132. reader.MoveToElement();
  133. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  134. {
  135. error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
  136. reader.Skip();
  137. return null;
  138. }
  139. group.LineNumber = reader.LineNumber;
  140. group.LinePosition = reader.LinePosition;
  141. group.SourceUri = reader.BaseURI;
  142. while(reader.MoveToNextAttribute())
  143. {
  144. if(reader.Name == "id")
  145. {
  146. group.Id = reader.Value;
  147. }
  148. else if(reader.Name == "name")
  149. {
  150. group.name = reader.Value;
  151. }
  152. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  153. {
  154. error(h,reader.Name + " is not a valid attribute for group",null);
  155. }
  156. else
  157. {
  158. XmlSchemaUtil.ReadUnhandledAttribute(reader,group);
  159. }
  160. }
  161. reader.MoveToElement();
  162. if(reader.IsEmptyElement)
  163. return group;
  164. // Content: (annotation?, (all | choice | sequence)?)
  165. int level = 1;
  166. while(reader.ReadNextElement())
  167. {
  168. if(reader.NodeType == XmlNodeType.EndElement)
  169. {
  170. if(reader.LocalName != xmlname)
  171. error(h,"Should not happen :2: XmlSchemaGroup.Read, name="+reader.Name,null);
  172. break;
  173. }
  174. if(level <= 1 && reader.LocalName == "annotation")
  175. {
  176. level = 2; //Only one annotation
  177. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  178. if(annotation != null)
  179. group.Annotation = annotation;
  180. continue;
  181. }
  182. if(level <= 2)
  183. {
  184. if(reader.LocalName == "all")
  185. {
  186. level = 3;
  187. XmlSchemaAll all = XmlSchemaAll.Read(reader,h);
  188. if(all != null)
  189. group.Particle = all;
  190. continue;
  191. }
  192. if(reader.LocalName == "choice")
  193. {
  194. level = 3;
  195. XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
  196. if(choice != null)
  197. group.Particle = choice;
  198. continue;
  199. }
  200. if(reader.LocalName == "sequence")
  201. {
  202. level = 3;
  203. XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
  204. if(sequence != null)
  205. group.Particle = sequence;
  206. continue;
  207. }
  208. }
  209. reader.RaiseInvalidElementError();
  210. }
  211. return group;
  212. }
  213. }
  214. }