XmlSchemaAll.cs 7.4 KB

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