XmlSchemaAll.cs 8.0 KB

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