XmlSchemaChoice.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. //
  2. // System.Xml.Schema.XmlSchemaChoice.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.Serialization;
  11. using System.Xml;
  12. namespace System.Xml.Schema
  13. {
  14. public class XmlSchemaChoice : XmlSchemaGroupBase
  15. {
  16. private XmlSchemaObjectCollection items;
  17. const string xmlname = "choice";
  18. private decimal minEffectiveTotalRange = -1;
  19. public XmlSchemaChoice ()
  20. {
  21. items = new XmlSchemaObjectCollection();
  22. }
  23. [XmlElement("element",typeof(XmlSchemaElement),Namespace=XmlSchema.Namespace)]
  24. [XmlElement("group",typeof(XmlSchemaGroupRef),Namespace=XmlSchema.Namespace)]
  25. [XmlElement("choice",typeof(XmlSchemaChoice),Namespace=XmlSchema.Namespace)]
  26. [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace=XmlSchema.Namespace)]
  27. [XmlElement("any",typeof(XmlSchemaAny),Namespace=XmlSchema.Namespace)]
  28. public override XmlSchemaObjectCollection Items
  29. {
  30. get{ return items; }
  31. }
  32. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  33. {
  34. // If this is already compiled this time, simply skip.
  35. if (this.IsComplied (schema.CompilationId))
  36. return 0;
  37. XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
  38. CompileOccurence (h, schema);
  39. if (Items.Count == 0)
  40. this.warn (h, "Empty choice is unsatisfiable if minOccurs not equals to 0");
  41. foreach(XmlSchemaObject obj in Items)
  42. {
  43. if(obj is XmlSchemaElement ||
  44. obj is XmlSchemaGroupRef ||
  45. obj is XmlSchemaChoice ||
  46. obj is XmlSchemaSequence ||
  47. obj is XmlSchemaAny)
  48. {
  49. errorCount += obj.Compile(h,schema);
  50. }
  51. else
  52. error(h, "Invalid schema object was specified in the particles of the choice model group.");
  53. }
  54. this.CompilationId = schema.CompilationId;
  55. return errorCount;
  56. }
  57. internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
  58. {
  59. if (OptimizedParticle != null)
  60. return OptimizedParticle;
  61. if (Items.Count == 0 || ValidatedMaxOccurs == 0)
  62. OptimizedParticle = XmlSchemaParticle.Empty;
  63. // LAMESPEC: Regardless of isTop, it should remove pointless particle. It seems ContentTypeParticle design bug.
  64. else if (!isTop && Items.Count == 1 && ValidatedMinOccurs == 1 && ValidatedMaxOccurs == 1)
  65. OptimizedParticle = ((XmlSchemaParticle) Items [0]).GetOptimizedParticle (false);
  66. else {
  67. XmlSchemaChoice c = new XmlSchemaChoice ();
  68. CopyInfo (c);
  69. for (int i = 0; i < Items.Count; i++) {
  70. XmlSchemaParticle p = Items [i] as XmlSchemaParticle;
  71. p = p.GetOptimizedParticle (false);
  72. if (p == XmlSchemaParticle.Empty)
  73. continue;
  74. else if (p is XmlSchemaChoice && p.ValidatedMinOccurs == 1 && p.ValidatedMaxOccurs == 1) {
  75. XmlSchemaChoice pc = p as XmlSchemaChoice;
  76. for (int ci = 0; ci < pc.Items.Count; ci++) {
  77. c.Items.Add (pc.Items [ci]);
  78. c.CompiledItems.Add (pc.Items [ci]);
  79. }
  80. }
  81. else {
  82. c.Items.Add (p);
  83. c.CompiledItems.Add (p);
  84. }
  85. }
  86. if (c.Items.Count == 0)
  87. OptimizedParticle = XmlSchemaParticle.Empty;
  88. else
  89. OptimizedParticle = c;
  90. }
  91. return OptimizedParticle;
  92. }
  93. internal override int Validate (ValidationEventHandler h, XmlSchema schema)
  94. {
  95. if (IsValidated (schema.CompilationId))
  96. return errorCount;
  97. CompiledItems.Clear ();
  98. foreach (XmlSchemaParticle p in Items) {
  99. errorCount += p.Validate (h, schema); // This is basically extraneous for pointless item, but needed to check validation error.
  100. CompiledItems.Add (p);
  101. }
  102. ValidationId = schema.ValidationId;
  103. return errorCount;
  104. }
  105. internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  106. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  107. {
  108. XmlSchemaAny any = baseParticle as XmlSchemaAny;
  109. if (any != null) {
  110. // NSRecurseCheckCardinality
  111. return ValidateNSRecurseCheckCardinality (any, h, schema, raiseError);
  112. }
  113. XmlSchemaChoice choice = baseParticle as XmlSchemaChoice;
  114. if (choice != null) {
  115. // RecurseLax
  116. if (!ValidateOccurenceRangeOK (choice, h, schema, raiseError))
  117. return false;
  118. // If it is totally optional, then ignore their contents.
  119. if (choice.ValidatedMinOccurs == 0 && choice.ValidatedMaxOccurs == 0 &&
  120. this.ValidatedMinOccurs == 0 && this.ValidatedMaxOccurs == 0)
  121. return true;
  122. // return ValidateRecurseLax (choice, h, schema, raiseError);
  123. return this.ValidateSeqRecurseMapSumCommon (choice, h, schema, true, false, raiseError);
  124. }
  125. if (raiseError)
  126. error (h, "Invalid choice derivation by restriction was found.");
  127. return false;
  128. }
  129. private bool ValidateRecurseLax (XmlSchemaGroupBase baseGroup,
  130. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  131. {
  132. int index = 0;
  133. for (int i = 0; i < baseGroup.CompiledItems.Count; i++) {
  134. XmlSchemaParticle pb = (XmlSchemaParticle) baseGroup.CompiledItems [i];
  135. pb = pb.GetOptimizedParticle (false);
  136. if (pb == XmlSchemaParticle.Empty)
  137. continue;
  138. XmlSchemaParticle pd = null;
  139. while (this.CompiledItems.Count > index) {
  140. pd = (XmlSchemaParticle) this.CompiledItems [index];
  141. pd = pd.GetOptimizedParticle (false);
  142. index++;
  143. if (pd != XmlSchemaParticle.Empty)
  144. break;
  145. }
  146. if (!ValidateParticleSection (ref index, pd, pb, h, schema, raiseError))
  147. continue;
  148. }
  149. if (this.CompiledItems.Count > 0 && index != this.CompiledItems.Count) {
  150. if (raiseError)
  151. error (h, "Invalid particle derivation by restriction was found. Extraneous derived particle was found.");
  152. return false;
  153. }
  154. return true;
  155. }
  156. private bool ValidateParticleSection (ref int index, XmlSchemaParticle pd, XmlSchemaParticle pb, ValidationEventHandler h, XmlSchema schema, bool raiseError)
  157. {
  158. if (pd == pb) // they are same particle
  159. return true;
  160. if (pd != null) {
  161. XmlSchemaElement el = pd as XmlSchemaElement;
  162. XmlSchemaParticle pdx = pd;
  163. // if (el != null && el.SubstitutingElements.Count > 0)
  164. // pdx = el.SubstitutingChoice;
  165. if (!pdx.ValidateDerivationByRestriction (pb, h, schema, false)) {
  166. if (!pb.ValidateIsEmptiable ()) {
  167. if (raiseError)
  168. error (h, "Invalid particle derivation by restriction was found. Invalid sub-particle derivation was found.");
  169. return false;
  170. }
  171. else {
  172. index--; // try the same derived particle and next base particle.
  173. return false;
  174. }
  175. }
  176. } else if (!pb.ValidateIsEmptiable ()) {
  177. if (raiseError)
  178. error (h, "Invalid particle derivation by restriction was found. Base schema particle has non-emptiable sub particle that is not mapped to the derived particle.");
  179. return false;
  180. }
  181. return true;
  182. }
  183. internal override decimal GetMinEffectiveTotalRange ()
  184. {
  185. if (minEffectiveTotalRange >= 0)
  186. return minEffectiveTotalRange;
  187. decimal product = 0; //this.ValidatedMinOccurs;
  188. if (Items.Count == 0)
  189. product = 0;
  190. else {
  191. foreach (XmlSchemaParticle p in this.Items) {
  192. decimal got = p.GetMinEffectiveTotalRange ();
  193. if (product > got)
  194. product= got;
  195. }
  196. }
  197. minEffectiveTotalRange = product;
  198. return product;
  199. }
  200. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  201. ValidationEventHandler h, XmlSchema schema)
  202. {
  203. foreach (XmlSchemaParticle p in this.Items)
  204. p.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
  205. }
  206. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  207. ValidationEventHandler h, XmlSchema schema)
  208. {
  209. foreach (XmlSchemaParticle p in this.Items)
  210. p.ValidateUniqueTypeAttribution (labels, h, schema);
  211. }
  212. //<choice
  213. // id = ID
  214. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  215. // minOccurs = nonNegativeInteger : 1
  216. // {any attributes with non-schema namespace . . .}>
  217. // Content: (annotation?, (element | group | choice | sequence | any)*)
  218. //</choice>
  219. internal static XmlSchemaChoice Read(XmlSchemaReader reader, ValidationEventHandler h)
  220. {
  221. XmlSchemaChoice choice = new XmlSchemaChoice();
  222. reader.MoveToElement();
  223. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  224. {
  225. error(h,"Should not happen :1: XmlSchemaChoice.Read, name="+reader.Name,null);
  226. reader.SkipToEnd();
  227. return null;
  228. }
  229. choice.LineNumber = reader.LineNumber;
  230. choice.LinePosition = reader.LinePosition;
  231. choice.SourceUri = reader.BaseURI;
  232. while(reader.MoveToNextAttribute())
  233. {
  234. if(reader.Name == "id")
  235. {
  236. choice.Id = reader.Value;
  237. }
  238. else if(reader.Name == "maxOccurs")
  239. {
  240. try
  241. {
  242. choice.MaxOccursString = reader.Value;
  243. }
  244. catch(Exception e)
  245. {
  246. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  247. }
  248. }
  249. else if(reader.Name == "minOccurs")
  250. {
  251. try
  252. {
  253. choice.MinOccursString = reader.Value;
  254. }
  255. catch(Exception e)
  256. {
  257. error(h,reader.Value + " is an invalid value for minOccurs",e);
  258. }
  259. }
  260. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  261. {
  262. error(h,reader.Name + " is not a valid attribute for choice",null);
  263. }
  264. else
  265. {
  266. XmlSchemaUtil.ReadUnhandledAttribute(reader,choice);
  267. }
  268. }
  269. reader.MoveToElement();
  270. if(reader.IsEmptyElement)
  271. return choice;
  272. // Content: (annotation?, (element | group | choice | sequence | any)*)
  273. int level = 1;
  274. while(reader.ReadNextElement())
  275. {
  276. if(reader.NodeType == XmlNodeType.EndElement)
  277. {
  278. if(reader.LocalName != xmlname)
  279. error(h,"Should not happen :2: XmlSchemaChoice.Read, name="+reader.Name,null);
  280. break;
  281. }
  282. if(level <= 1 && reader.LocalName == "annotation")
  283. {
  284. level = 2; //Only one annotation
  285. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  286. if(annotation != null)
  287. choice.Annotation = annotation;
  288. continue;
  289. }
  290. if(level <=2)
  291. {
  292. if(reader.LocalName == "element")
  293. {
  294. level = 2;
  295. XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
  296. if(element != null)
  297. choice.items.Add(element);
  298. continue;
  299. }
  300. if(reader.LocalName == "group")
  301. {
  302. level = 2;
  303. XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
  304. if(group != null)
  305. choice.items.Add(group);
  306. continue;
  307. }
  308. if(reader.LocalName == "choice")
  309. {
  310. level = 2;
  311. XmlSchemaChoice ch = XmlSchemaChoice.Read(reader,h);
  312. if(ch != null)
  313. choice.items.Add(ch);
  314. continue;
  315. }
  316. if(reader.LocalName == "sequence")
  317. {
  318. level = 2;
  319. XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
  320. if(sequence != null)
  321. choice.items.Add(sequence);
  322. continue;
  323. }
  324. if(reader.LocalName == "any")
  325. {
  326. level = 2;
  327. XmlSchemaAny any = XmlSchemaAny.Read(reader,h);
  328. if(any != null)
  329. choice.items.Add(any);
  330. continue;
  331. }
  332. }
  333. reader.RaiseInvalidElementError();
  334. }
  335. return choice;
  336. }
  337. }
  338. }