XmlSchemaGroupRef.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. private static 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 && !schema.IsNamespaceAbsent (RefName.Namespace))
  81. error (h, "Referenced group " + RefName + " was not found in the corresponding schema.");
  82. // See Errata E1-26: minOccurs=0 is now allowed.
  83. else if (referencedGroup.Particle is XmlSchemaAll && ValidatedMaxOccurs != 1)
  84. error (h, "Group reference to -all- particle must have schema component {maxOccurs}=1.");
  85. if (TargetGroup != null)
  86. TargetGroup.Validate (h, schema);
  87. ValidationId = schema.ValidationId;
  88. return errorCount;
  89. }
  90. bool busy; // only for avoiding infinite loop on illegal recursion cases.
  91. internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
  92. {
  93. if (busy)
  94. return XmlSchemaParticle.Empty;
  95. if (OptimizedParticle != null)
  96. return OptimizedParticle;
  97. busy = true;
  98. XmlSchemaGroup g = referencedGroup != null ? referencedGroup : schema.Groups [RefName] as XmlSchemaGroup;
  99. if (g != null && g.Particle != null) {
  100. OptimizedParticle = g.Particle;
  101. if (OptimizedParticle != XmlSchemaParticle.Empty && (ValidatedMinOccurs != 1 || ValidatedMaxOccurs != 1)) {
  102. OptimizedParticle = OptimizedParticle.GetShallowClone ();
  103. OptimizedParticle.MinOccurs = this.MinOccurs;
  104. OptimizedParticle.MaxOccurs = this.MaxOccurs;
  105. OptimizedParticle.CompileOccurence (null, null);
  106. }
  107. OptimizedParticle = OptimizedParticle.GetOptimizedParticle (isTop);
  108. }
  109. else
  110. OptimizedParticle = XmlSchemaParticle.Empty;
  111. busy = false;
  112. return OptimizedParticle;
  113. }
  114. internal override bool ParticleEquals (XmlSchemaParticle other)
  115. {
  116. return this.GetOptimizedParticle (true).ParticleEquals (other);
  117. }
  118. internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  119. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  120. {
  121. if (TargetGroup != null)
  122. return TargetGroup.Particle.ValidateDerivationByRestriction (baseParticle, h, schema, raiseError);
  123. else
  124. return false; // should not occur
  125. }
  126. internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  127. {
  128. if (TargetGroup == null)
  129. return;
  130. if (this.recursionDepth == -1) {
  131. recursionDepth = depth;
  132. TargetGroup.Particle.CheckRecursion (depth, h, schema);
  133. recursionDepth = -2;
  134. } else if (depth == recursionDepth)
  135. throw new XmlSchemaException ("Circular group reference was found.", this, null);
  136. }
  137. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  138. ValidationEventHandler h, XmlSchema schema)
  139. {
  140. if (TargetGroup != null)
  141. TargetGroup.Particle.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
  142. }
  143. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  144. ValidationEventHandler h, XmlSchema schema)
  145. {
  146. if (TargetGroup != null)
  147. TargetGroup.Particle.ValidateUniqueTypeAttribution (labels, h, schema);
  148. }
  149. #region Read
  150. // <group
  151. // id = ID
  152. // ref = QName
  153. // minOccurs = ? : 1
  154. // maxOccurs = ? : 1>
  155. // Content: (annotation?)
  156. // </group>
  157. internal static XmlSchemaGroupRef Read(XmlSchemaReader reader, ValidationEventHandler h)
  158. {
  159. XmlSchemaGroupRef groupref = new XmlSchemaGroupRef();
  160. reader.MoveToElement();
  161. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  162. {
  163. error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
  164. reader.Skip();
  165. return null;
  166. }
  167. groupref.LineNumber = reader.LineNumber;
  168. groupref.LinePosition = reader.LinePosition;
  169. groupref.SourceUri = reader.BaseURI;
  170. while(reader.MoveToNextAttribute())
  171. {
  172. if(reader.Name == "id")
  173. {
  174. groupref.Id = reader.Value;
  175. }
  176. else if(reader.Name == "ref")
  177. {
  178. Exception innerex;
  179. groupref.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  180. if(innerex != null)
  181. error(h, reader.Value + " is not a valid value for ref attribute",innerex);
  182. }
  183. else if(reader.Name == "maxOccurs")
  184. {
  185. try
  186. {
  187. groupref.MaxOccursString = reader.Value;
  188. }
  189. catch(Exception e)
  190. {
  191. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  192. }
  193. }
  194. else if(reader.Name == "minOccurs")
  195. {
  196. try
  197. {
  198. groupref.MinOccursString = reader.Value;
  199. }
  200. catch(Exception e)
  201. {
  202. error(h,reader.Value + " is an invalid value for minOccurs", e);
  203. }
  204. }
  205. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  206. {
  207. error(h,reader.Name + " is not a valid attribute for group",null);
  208. }
  209. else
  210. {
  211. XmlSchemaUtil.ReadUnhandledAttribute(reader,groupref);
  212. }
  213. }
  214. reader.MoveToElement();
  215. if(reader.IsEmptyElement)
  216. return groupref;
  217. // Content: (annotation?)
  218. int level = 1;
  219. while(reader.ReadNextElement())
  220. {
  221. if(reader.NodeType == XmlNodeType.EndElement)
  222. {
  223. if(reader.LocalName != xmlname)
  224. error(h,"Should not happen :2: XmlSchemaGroupRef.Read, name="+reader.Name,null);
  225. break;
  226. }
  227. if(level <= 1 && reader.LocalName == "annotation")
  228. {
  229. level = 2; //Only one annotation
  230. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  231. if(annotation != null)
  232. groupref.Annotation = annotation;
  233. continue;
  234. }
  235. reader.RaiseInvalidElementError();
  236. }
  237. return groupref;
  238. }
  239. #endregion
  240. }
  241. }