XmlSchemaGroupRef.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 XmlSchemaGroupBase particle;
  20. private XmlQualifiedName refName;
  21. private XmlQualifiedName resolvedRefName;
  22. private static string xmlname = "group";
  23. private XmlSchemaGroup referencedGroup;
  24. public XmlSchemaGroupRef()
  25. {
  26. refName = XmlQualifiedName.Empty;
  27. }
  28. [System.Xml.Serialization.XmlAttribute("ref")]
  29. public XmlQualifiedName RefName
  30. {
  31. get{ return refName; }
  32. set{ refName = value; }
  33. }
  34. [XmlIgnore]
  35. public XmlSchemaGroupBase Particle
  36. {
  37. get{
  38. if (TargetGroup != null)
  39. return TargetGroup.Particle;
  40. else
  41. return null;
  42. }
  43. }
  44. internal XmlSchemaGroup TargetGroup
  45. {
  46. get {
  47. if (referencedGroup != null && referencedGroup.IsCircularDefinition)
  48. return null;
  49. else
  50. return referencedGroup;
  51. }
  52. }
  53. internal override XmlSchemaParticle ActualParticle
  54. {
  55. get {
  56. if (TargetGroup != null)
  57. return TargetGroup.Particle.ActualParticle;
  58. else
  59. // For ValidationEventHandler and missing sub components.
  60. return XmlSchemaParticle.Empty;
  61. }
  62. }
  63. /// <remarks>
  64. /// 1. RefName must be present
  65. /// </remarks>
  66. [MonoTODO]
  67. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  68. {
  69. // If this is already compiled this time, simply skip.
  70. if (this.IsComplied (schema.CompilationId))
  71. return 0;
  72. XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
  73. CompileOccurence (h, schema);
  74. if(refName == null || refName.IsEmpty)
  75. {
  76. error(h,"ref must be present");
  77. }
  78. else if(!XmlSchemaUtil.CheckQName(RefName))
  79. error(h, "RefName must be a valid XmlQualifiedName");
  80. this.CompilationId = schema.CompilationId;
  81. return errorCount;
  82. }
  83. [MonoTODO]
  84. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  85. {
  86. if (IsValidated (schema.ValidationId))
  87. return errorCount;
  88. referencedGroup = schema.Groups [RefName] as XmlSchemaGroup;
  89. // it might be missing sub components.
  90. if (referencedGroup == null && !schema.IsNamespaceAbsent (RefName.Namespace))
  91. error (h, "Referenced group " + RefName + " was not found in the corresponding schema.");
  92. else if (TargetGroup != null)
  93. TargetGroup.Validate (h, schema);
  94. ValidationId = schema.ValidationId;
  95. return errorCount;
  96. }
  97. internal override bool ParticleEquals (XmlSchemaParticle other)
  98. {
  99. return ActualParticle.ParticleEquals (other.ActualParticle);
  100. }
  101. internal override void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  102. ValidationEventHandler h, XmlSchema schema)
  103. {
  104. if (TargetGroup != null)
  105. TargetGroup.Particle.ValidateDerivationByRestriction (baseParticle, h, schema);
  106. }
  107. internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  108. {
  109. if (TargetGroup == null)
  110. return;
  111. if (this.recursionDepth == -1) {
  112. recursionDepth = depth;
  113. TargetGroup.Particle.CheckRecursion (depth, h, schema);
  114. recursionDepth = -2;
  115. } else if (depth == recursionDepth)
  116. throw new XmlSchemaException ("Circular group reference was found.", this, null);
  117. }
  118. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  119. ValidationEventHandler h, XmlSchema schema)
  120. {
  121. if (TargetGroup != null)
  122. TargetGroup.Particle.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
  123. }
  124. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  125. ValidationEventHandler h, XmlSchema schema)
  126. {
  127. if (TargetGroup != null)
  128. TargetGroup.Particle.ValidateUniqueTypeAttribution (labels, h, schema);
  129. }
  130. // <group
  131. // id = ID
  132. // ref = QName
  133. // minOccurs = ? : 1
  134. // maxOccurs = ? : 1>
  135. // Content: (annotation?)
  136. // </group>
  137. internal static XmlSchemaGroupRef Read(XmlSchemaReader reader, ValidationEventHandler h)
  138. {
  139. XmlSchemaGroupRef groupref = new XmlSchemaGroupRef();
  140. reader.MoveToElement();
  141. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  142. {
  143. error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
  144. reader.Skip();
  145. return null;
  146. }
  147. groupref.LineNumber = reader.LineNumber;
  148. groupref.LinePosition = reader.LinePosition;
  149. groupref.SourceUri = reader.BaseURI;
  150. while(reader.MoveToNextAttribute())
  151. {
  152. if(reader.Name == "id")
  153. {
  154. groupref.Id = reader.Value;
  155. }
  156. else if(reader.Name == "ref")
  157. {
  158. Exception innerex;
  159. groupref.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  160. if(innerex != null)
  161. error(h, reader.Value + " is not a valid value for ref attribute",innerex);
  162. }
  163. else if(reader.Name == "maxOccurs")
  164. {
  165. try
  166. {
  167. groupref.MaxOccursString = reader.Value;
  168. }
  169. catch(Exception e)
  170. {
  171. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  172. }
  173. }
  174. else if(reader.Name == "minOccurs")
  175. {
  176. try
  177. {
  178. groupref.MinOccursString = reader.Value;
  179. }
  180. catch(Exception e)
  181. {
  182. error(h,reader.Value + " is an invalid value for minOccurs", e);
  183. }
  184. }
  185. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  186. {
  187. error(h,reader.Name + " is not a valid attribute for group",null);
  188. }
  189. else
  190. {
  191. XmlSchemaUtil.ReadUnhandledAttribute(reader,groupref);
  192. }
  193. }
  194. reader.MoveToElement();
  195. if(reader.IsEmptyElement)
  196. return groupref;
  197. // Content: (annotation?)
  198. int level = 1;
  199. while(reader.ReadNextElement())
  200. {
  201. if(reader.NodeType == XmlNodeType.EndElement)
  202. {
  203. if(reader.LocalName != xmlname)
  204. error(h,"Should not happen :2: XmlSchemaGroupRef.Read, name="+reader.Name,null);
  205. break;
  206. }
  207. if(level <= 1 && reader.LocalName == "annotation")
  208. {
  209. level = 2; //Only one annotation
  210. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  211. if(annotation != null)
  212. groupref.Annotation = annotation;
  213. continue;
  214. }
  215. reader.RaiseInvalidElementError();
  216. }
  217. return groupref;
  218. }
  219. }
  220. }