XmlSchemaGroupRef.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  98. ValidationEventHandler h, XmlSchema schema)
  99. {
  100. if (TargetGroup != null)
  101. TargetGroup.Particle.ValidateDerivationByRestriction (baseParticle, h, schema);
  102. }
  103. internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  104. {
  105. if (TargetGroup == null)
  106. return;
  107. if (this.recursionDepth == -1) {
  108. recursionDepth = depth;
  109. TargetGroup.Particle.CheckRecursion (depth, h, schema);
  110. recursionDepth = -2;
  111. } else if (depth == recursionDepth)
  112. throw new XmlSchemaException ("Circular group reference was found.", this, null);
  113. }
  114. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  115. ValidationEventHandler h, XmlSchema schema)
  116. {
  117. if (TargetGroup != null)
  118. TargetGroup.Particle.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
  119. }
  120. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  121. ValidationEventHandler h, XmlSchema schema)
  122. {
  123. if (TargetGroup != null)
  124. TargetGroup.Particle.ValidateUniqueTypeAttribution (labels, h, schema);
  125. }
  126. // <group
  127. // id = ID
  128. // ref = QName
  129. // minOccurs = ? : 1
  130. // maxOccurs = ? : 1>
  131. // Content: (annotation?)
  132. // </group>
  133. internal static XmlSchemaGroupRef Read(XmlSchemaReader reader, ValidationEventHandler h)
  134. {
  135. XmlSchemaGroupRef groupref = new XmlSchemaGroupRef();
  136. reader.MoveToElement();
  137. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  138. {
  139. error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
  140. reader.Skip();
  141. return null;
  142. }
  143. groupref.LineNumber = reader.LineNumber;
  144. groupref.LinePosition = reader.LinePosition;
  145. groupref.SourceUri = reader.BaseURI;
  146. while(reader.MoveToNextAttribute())
  147. {
  148. if(reader.Name == "id")
  149. {
  150. groupref.Id = reader.Value;
  151. }
  152. else if(reader.Name == "ref")
  153. {
  154. Exception innerex;
  155. groupref.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  156. if(innerex != null)
  157. error(h, reader.Value + " is not a valid value for ref attribute",innerex);
  158. }
  159. else if(reader.Name == "maxOccurs")
  160. {
  161. try
  162. {
  163. groupref.MaxOccursString = reader.Value;
  164. }
  165. catch(Exception e)
  166. {
  167. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  168. }
  169. }
  170. else if(reader.Name == "minOccurs")
  171. {
  172. try
  173. {
  174. groupref.MinOccursString = reader.Value;
  175. }
  176. catch(Exception e)
  177. {
  178. error(h,reader.Value + " is an invalid value for minOccurs", e);
  179. }
  180. }
  181. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  182. {
  183. error(h,reader.Name + " is not a valid attribute for group",null);
  184. }
  185. else
  186. {
  187. XmlSchemaUtil.ReadUnhandledAttribute(reader,groupref);
  188. }
  189. }
  190. reader.MoveToElement();
  191. if(reader.IsEmptyElement)
  192. return groupref;
  193. // Content: (annotation?)
  194. int level = 1;
  195. while(reader.ReadNextElement())
  196. {
  197. if(reader.NodeType == XmlNodeType.EndElement)
  198. {
  199. if(reader.LocalName != xmlname)
  200. error(h,"Should not happen :2: XmlSchemaGroupRef.Read, name="+reader.Name,null);
  201. break;
  202. }
  203. if(level <= 1 && reader.LocalName == "annotation")
  204. {
  205. level = 2; //Only one annotation
  206. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  207. if(annotation != null)
  208. groupref.Annotation = annotation;
  209. continue;
  210. }
  211. reader.RaiseInvalidElementError();
  212. }
  213. return groupref;
  214. }
  215. }
  216. }