XmlSchemaChoice.cs 12 KB

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