XmlSchemaGroupRef.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //
  2. // System.Xml.Schema.XmlSchemaGroupBase.cs
  3. //
  4. // Author:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. using System;
  9. using System.Collections;
  10. using System.Xml;
  11. using System.Xml.Serialization;
  12. namespace System.Xml.Schema
  13. {
  14. /// <summary>
  15. /// Summary description for XmlSchemaGroupRef.
  16. /// </summary>
  17. public class XmlSchemaGroupRef : XmlSchemaParticle
  18. {
  19. private XmlSchema schema;
  20. private XmlQualifiedName refName;
  21. const string xmlname = "group";
  22. private XmlSchemaGroup referencedGroup;
  23. public XmlSchemaGroupRef()
  24. {
  25. refName = XmlQualifiedName.Empty;
  26. }
  27. // Attribute
  28. [System.Xml.Serialization.XmlAttribute("ref")]
  29. public XmlQualifiedName RefName
  30. {
  31. get{ return refName; }
  32. set{ refName = value; }
  33. }
  34. // Post Compilation Schema Information
  35. [XmlIgnore]
  36. public XmlSchemaGroupBase Particle
  37. {
  38. get{
  39. if (TargetGroup != null)
  40. return TargetGroup.Particle;
  41. else
  42. return null;
  43. }
  44. }
  45. internal XmlSchemaGroup TargetGroup
  46. {
  47. get {
  48. if (referencedGroup != null && referencedGroup.IsCircularDefinition)
  49. return null;
  50. else
  51. return referencedGroup;
  52. }
  53. }
  54. /// <remarks>
  55. /// 1. RefName must be present
  56. /// </remarks>
  57. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  58. {
  59. // If this is already compiled this time, simply skip.
  60. if (this.IsComplied (schema.CompilationId))
  61. return 0;
  62. this.schema = schema;
  63. XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
  64. CompileOccurence (h, schema);
  65. if(refName == null || refName.IsEmpty)
  66. {
  67. error(h,"ref must be present");
  68. }
  69. else if(!XmlSchemaUtil.CheckQName(RefName))
  70. error(h, "RefName must be a valid XmlQualifiedName");
  71. this.CompilationId = schema.CompilationId;
  72. return errorCount;
  73. }
  74. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  75. {
  76. if (IsValidated (schema.ValidationId))
  77. return errorCount;
  78. referencedGroup = schema.Groups [RefName] as XmlSchemaGroup;
  79. // it might be missing sub components.
  80. if (referencedGroup == null) {
  81. if (!schema.IsNamespaceAbsent (RefName.Namespace))
  82. error (h, "Referenced group " + RefName + " was not found in the corresponding schema.");
  83. }
  84. // See Errata E1-26: minOccurs=0 is now allowed.
  85. else if (referencedGroup.Particle is XmlSchemaAll && ValidatedMaxOccurs != 1)
  86. error (h, "Group reference to -all- particle must have schema component {maxOccurs}=1.");
  87. if (TargetGroup != null)
  88. TargetGroup.Validate (h, schema);
  89. ValidationId = schema.ValidationId;
  90. return errorCount;
  91. }
  92. bool busy; // only for avoiding infinite loop on illegal recursion cases.
  93. internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
  94. {
  95. if (busy)
  96. return XmlSchemaParticle.Empty;
  97. if (OptimizedParticle != null)
  98. return OptimizedParticle;
  99. busy = true;
  100. XmlSchemaGroup g = referencedGroup != null ? referencedGroup : schema.Groups [RefName] as XmlSchemaGroup;
  101. if (g != null && g.Particle != null) {
  102. OptimizedParticle = g.Particle;
  103. OptimizedParticle = OptimizedParticle.GetOptimizedParticle (isTop);
  104. if (OptimizedParticle != XmlSchemaParticle.Empty && (ValidatedMinOccurs != 1 || ValidatedMaxOccurs != 1)) {
  105. OptimizedParticle = OptimizedParticle.GetShallowClone ();
  106. OptimizedParticle.MinOccurs = this.MinOccurs;
  107. OptimizedParticle.MaxOccurs = this.MaxOccurs;
  108. OptimizedParticle.CompileOccurence (null, null);
  109. }
  110. }
  111. else
  112. OptimizedParticle = XmlSchemaParticle.Empty;
  113. busy = false;
  114. return OptimizedParticle;
  115. }
  116. internal override bool ParticleEquals (XmlSchemaParticle other)
  117. {
  118. return this.GetOptimizedParticle (true).ParticleEquals (other);
  119. }
  120. internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  121. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  122. {
  123. if (TargetGroup != null)
  124. return TargetGroup.Particle.ValidateDerivationByRestriction (baseParticle, h, schema, raiseError);
  125. else
  126. return false; // should not occur
  127. }
  128. internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  129. {
  130. if (TargetGroup == null)
  131. return;
  132. if (this.recursionDepth == -1) {
  133. recursionDepth = depth;
  134. TargetGroup.Particle.CheckRecursion (depth, h, schema);
  135. recursionDepth = -2;
  136. } else if (depth == recursionDepth)
  137. throw new XmlSchemaException ("Circular group reference was found.", this, null);
  138. }
  139. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  140. ValidationEventHandler h, XmlSchema schema)
  141. {
  142. if (TargetGroup != null)
  143. TargetGroup.Particle.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
  144. }
  145. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  146. ValidationEventHandler h, XmlSchema schema)
  147. {
  148. if (TargetGroup != null)
  149. TargetGroup.Particle.ValidateUniqueTypeAttribution (labels, h, schema);
  150. }
  151. #region Read
  152. // <group
  153. // id = ID
  154. // ref = QName
  155. // minOccurs = ? : 1
  156. // maxOccurs = ? : 1>
  157. // Content: (annotation?)
  158. // </group>
  159. internal static XmlSchemaGroupRef Read(XmlSchemaReader reader, ValidationEventHandler h)
  160. {
  161. XmlSchemaGroupRef groupref = new XmlSchemaGroupRef();
  162. reader.MoveToElement();
  163. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  164. {
  165. error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
  166. reader.Skip();
  167. return null;
  168. }
  169. groupref.LineNumber = reader.LineNumber;
  170. groupref.LinePosition = reader.LinePosition;
  171. groupref.SourceUri = reader.BaseURI;
  172. while(reader.MoveToNextAttribute())
  173. {
  174. if(reader.Name == "id")
  175. {
  176. groupref.Id = reader.Value;
  177. }
  178. else if(reader.Name == "ref")
  179. {
  180. Exception innerex;
  181. groupref.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  182. if(innerex != null)
  183. error(h, reader.Value + " is not a valid value for ref attribute",innerex);
  184. }
  185. else if(reader.Name == "maxOccurs")
  186. {
  187. try
  188. {
  189. groupref.MaxOccursString = reader.Value;
  190. }
  191. catch(Exception e)
  192. {
  193. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  194. }
  195. }
  196. else if(reader.Name == "minOccurs")
  197. {
  198. try
  199. {
  200. groupref.MinOccursString = reader.Value;
  201. }
  202. catch(Exception e)
  203. {
  204. error(h,reader.Value + " is an invalid value for minOccurs", e);
  205. }
  206. }
  207. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  208. {
  209. error(h,reader.Name + " is not a valid attribute for group",null);
  210. }
  211. else
  212. {
  213. XmlSchemaUtil.ReadUnhandledAttribute(reader,groupref);
  214. }
  215. }
  216. reader.MoveToElement();
  217. if(reader.IsEmptyElement)
  218. return groupref;
  219. // Content: (annotation?)
  220. int level = 1;
  221. while(reader.ReadNextElement())
  222. {
  223. if(reader.NodeType == XmlNodeType.EndElement)
  224. {
  225. if(reader.LocalName != xmlname)
  226. error(h,"Should not happen :2: XmlSchemaGroupRef.Read, name="+reader.Name,null);
  227. break;
  228. }
  229. if(level <= 1 && reader.LocalName == "annotation")
  230. {
  231. level = 2; //Only one annotation
  232. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  233. if(annotation != null)
  234. groupref.Annotation = annotation;
  235. continue;
  236. }
  237. reader.RaiseInvalidElementError();
  238. }
  239. return groupref;
  240. }
  241. #endregion
  242. }
  243. }