XmlSchemaChoice.cs 12 KB

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