XmlSchemaGroup.cs 7.0 KB

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