XmlSchemaAll.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // System.Xml.Schema.XmlSchemaAll.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 XmlSchemaAll.
  16. /// </summary>
  17. public class XmlSchemaAll : XmlSchemaGroupBase
  18. {
  19. private XmlSchemaObjectCollection items;
  20. private static string xmlname = "all";
  21. private bool emptiable;
  22. public XmlSchemaAll()
  23. {
  24. items = new XmlSchemaObjectCollection();
  25. }
  26. [XmlElement("element",typeof(XmlSchemaElement),Namespace=XmlSchema.Namespace)]
  27. public override XmlSchemaObjectCollection Items
  28. {
  29. get{ return items; }
  30. }
  31. internal bool Emptiable
  32. {
  33. get { return emptiable; }
  34. }
  35. internal override XmlSchemaParticle ActualParticle {
  36. get {
  37. if (CompiledItems.Count == 0)
  38. return XmlSchemaParticle.Empty;
  39. else if (CompiledItems.Count == 1)
  40. return ((XmlSchemaParticle) CompiledItems [0]).ActualParticle;
  41. else
  42. return this;
  43. }
  44. }
  45. /// <remarks>
  46. /// 1. MaxOccurs must be one. (default is also one)
  47. /// 2. MinOccurs must be zero or one.
  48. /// </remarks>
  49. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  50. {
  51. // If this is already compiled this time, simply skip.
  52. if (this.IsComplied (schema.CompilationId))
  53. return 0;
  54. //FIXME: Should we reset the values on error??
  55. if(MaxOccurs != Decimal.One)
  56. error(h,"maxOccurs must be 1");
  57. if(MinOccurs != Decimal.One && MinOccurs != Decimal.Zero)
  58. error(h,"minOccurs must be 0 or 1");
  59. XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
  60. CompileOccurence (h, schema);
  61. foreach(XmlSchemaObject obj in Items)
  62. {
  63. XmlSchemaElement elem = obj as XmlSchemaElement;
  64. if(elem != null)
  65. {
  66. if(elem.MaxOccurs != Decimal.One && elem.MaxOccurs != Decimal.Zero)
  67. {
  68. elem.error(h,"The {max occurs} of all the elements of 'all' must be 0 or 1. ");
  69. }
  70. errorCount += elem.Compile(h, schema);
  71. }
  72. else
  73. {
  74. error(h,"XmlSchemaAll can only contain Items of type Element");
  75. }
  76. }
  77. this.CompilationId = schema.CompilationId;
  78. return errorCount;
  79. }
  80. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  81. {
  82. if (IsValidated (schema.CompilationId))
  83. return errorCount;
  84. this.CompileOccurence (h, schema);
  85. // 3.8.6 All Group Limited :: 1.
  86. // Beware that this section was corrected: E1-26 of http://www.w3.org/2001/05/xmlschema-errata#Errata1
  87. if (!this.parentIsGroupDefinition && ValidatedMaxOccurs != 1)
  88. error (h, "-all- group is limited to be content of a model group, or that of a complex type with maxOccurs to be 1.");
  89. emptiable = true;
  90. CompiledItems.Clear ();
  91. foreach (XmlSchemaElement obj in Items) {
  92. errorCount += obj.Validate (h, schema);
  93. if (obj.ValidatedMaxOccurs != 0 &&
  94. obj.ValidatedMaxOccurs != 1)
  95. error (h, "MaxOccurs of a particle inside -all- compositor must be either 0 or 1.");
  96. CompiledItems.Add (obj);
  97. if (obj.MinOccurs > 0)
  98. emptiable = false;
  99. }
  100. ValidationId = schema.ValidationId;
  101. return errorCount;
  102. }
  103. internal override void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  104. ValidationEventHandler h, XmlSchema schema)
  105. {
  106. XmlSchemaAny any = baseParticle as XmlSchemaAny;
  107. XmlSchemaAll derivedAll = baseParticle as XmlSchemaAll;
  108. if (any != null) {
  109. // NSRecurseCheckCardinality
  110. this.ValidateNSRecurseCheckCardinality (any, h, schema);
  111. return;
  112. } else if (derivedAll != null) {
  113. // Recurse
  114. ValidateOccurenceRangeOK (derivedAll, h, schema);
  115. this.ValidateRecurse (derivedAll, h, schema);
  116. return;
  117. }
  118. else
  119. error (h, "Invalid -all- particle derivation was found.");
  120. }
  121. internal override decimal GetMinEffectiveTotalRange ()
  122. {
  123. return GetMinEffectiveTotalRangeAllAndSequence ();
  124. }
  125. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  126. ValidationEventHandler h, XmlSchema schema)
  127. {
  128. foreach (XmlSchemaElement el in this.Items)
  129. el.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
  130. }
  131. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  132. ValidationEventHandler h, XmlSchema schema)
  133. {
  134. foreach (XmlSchemaElement el in this.Items)
  135. el.ValidateUniqueTypeAttribution (labels, h, schema);
  136. }
  137. //<all
  138. // id = ID
  139. // maxOccurs = 1 : 1
  140. // minOccurs = (0 | 1) : 1
  141. // {any attributes with non-schema namespace . . .}>
  142. // Content: (annotation?, element*)
  143. //</all>
  144. internal static XmlSchemaAll Read(XmlSchemaReader reader, ValidationEventHandler h)
  145. {
  146. XmlSchemaAll all = new XmlSchemaAll();
  147. reader.MoveToElement();
  148. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  149. {
  150. error(h,"Should not happen :1: XmlSchemaAll.Read, name="+reader.Name,null);
  151. reader.SkipToEnd();
  152. return null;
  153. }
  154. all.LineNumber = reader.LineNumber;
  155. all.LinePosition = reader.LinePosition;
  156. all.SourceUri = reader.BaseURI;
  157. //Read Attributes
  158. while(reader.MoveToNextAttribute())
  159. {
  160. if(reader.Name == "id")
  161. {
  162. all.Id = reader.Value;
  163. }
  164. else if(reader.Name == "maxOccurs")
  165. {
  166. try
  167. {
  168. all.MaxOccursString = reader.Value;
  169. }
  170. catch(Exception e)
  171. {
  172. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  173. }
  174. }
  175. else if(reader.Name == "minOccurs")
  176. {
  177. try
  178. {
  179. all.MinOccursString = reader.Value;
  180. }
  181. catch(Exception e)
  182. {
  183. error(h,reader.Value + " is an invalid value for minOccurs",e);
  184. }
  185. }
  186. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  187. {
  188. error(h,reader.Name + " is not a valid attribute for all",null);
  189. }
  190. else
  191. {
  192. XmlSchemaUtil.ReadUnhandledAttribute(reader,all);
  193. }
  194. }
  195. reader.MoveToElement();
  196. if(reader.IsEmptyElement)
  197. return all;
  198. //Content: (annotation?, element*)
  199. int level = 1;
  200. while(reader.ReadNextElement())
  201. {
  202. if(reader.NodeType == XmlNodeType.EndElement)
  203. {
  204. if(reader.LocalName != xmlname)
  205. error(h,"Should not happen :2: XmlSchemaAll.Read, name="+reader.Name,null);
  206. break;
  207. }
  208. if(level <= 1 && reader.LocalName == "annotation")
  209. {
  210. level = 2; //Only one annotation
  211. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  212. if(annotation != null)
  213. all.Annotation = annotation;
  214. continue;
  215. }
  216. if(level <=2 && reader.LocalName == "element")
  217. {
  218. level = 2;
  219. XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
  220. if(element != null)
  221. all.items.Add(element);
  222. continue;
  223. }
  224. reader.RaiseInvalidElementError();
  225. }
  226. return all;
  227. }
  228. }
  229. }