XmlSchemaAll.cs 7.6 KB

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