XmlSchemaGroup.cs 6.9 KB

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