XmlSchemaGroupRef.cs 8.7 KB

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