XmlSchemaAll.cs 7.2 KB

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