XmlSchemaSequence.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. //
  2. // System.Xml.Schema.XmlSchemaSequence.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. /// <summary>
  35. /// Summary description for XmlSchemaSequence.
  36. /// </summary>
  37. public class XmlSchemaSequence : XmlSchemaGroupBase
  38. {
  39. private XmlSchemaObjectCollection items;
  40. const string xmlname = "sequence";
  41. public XmlSchemaSequence()
  42. {
  43. items = new XmlSchemaObjectCollection();
  44. }
  45. [XmlElement("element",typeof(XmlSchemaElement))]
  46. [XmlElement("group",typeof(XmlSchemaGroupRef))]
  47. [XmlElement("choice",typeof(XmlSchemaChoice))]
  48. [XmlElement("sequence",typeof(XmlSchemaSequence))]
  49. [XmlElement("any",typeof(XmlSchemaAny))]
  50. public override XmlSchemaObjectCollection Items
  51. {
  52. get{ return items; }
  53. }
  54. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  55. {
  56. // If this is already compiled this time, simply skip.
  57. if (this.IsComplied (schema.CompilationId))
  58. return 0;
  59. XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
  60. CompileOccurence (h, schema);
  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 sequence 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. return OptimizedParticle;
  87. }
  88. if (!isTop && ValidatedMinOccurs == 1 && ValidatedMaxOccurs == 1) {
  89. if (Items.Count == 1)
  90. return ((XmlSchemaParticle) Items [0]).GetOptimizedParticle (false);
  91. }
  92. XmlSchemaSequence seq = new XmlSchemaSequence ();
  93. CopyInfo (seq);
  94. for (int i = 0; i < Items.Count; i++) {
  95. XmlSchemaParticle p = Items [i] as XmlSchemaParticle;
  96. p = p.GetOptimizedParticle (false);
  97. if (p == XmlSchemaParticle.Empty)
  98. continue;
  99. else if (p is XmlSchemaSequence && p.ValidatedMinOccurs == 1 && p.ValidatedMaxOccurs == 1) {
  100. XmlSchemaSequence ps = p as XmlSchemaSequence;
  101. for (int pi = 0; pi < ps.Items.Count; pi++) {
  102. seq.Items.Add (ps.Items [pi]);
  103. seq.CompiledItems.Add (ps.Items [pi]);
  104. }
  105. }
  106. else {
  107. seq.Items.Add (p);
  108. seq.CompiledItems.Add (p);
  109. }
  110. }
  111. if (seq.Items.Count == 0)
  112. OptimizedParticle = XmlSchemaParticle.Empty;
  113. else
  114. OptimizedParticle = seq;
  115. return OptimizedParticle;
  116. }
  117. internal override int Validate (ValidationEventHandler h, XmlSchema schema)
  118. {
  119. if (IsValidated (schema.CompilationId))
  120. return errorCount;
  121. CompiledItems.Clear ();
  122. foreach (XmlSchemaParticle p in Items) {
  123. errorCount += p.Validate (h, schema); // This is basically extraneous for pointless item, but needed to check validation error.
  124. // XmlSchemaParticle particleInPoint = p.GetParticleWithoutPointless ();
  125. // if (particleInPoint != XmlSchemaParticle.Empty)
  126. // CompiledItems.Add (particleInPoint);
  127. CompiledItems.Add (p);
  128. }
  129. ValidationId = schema.ValidationId;
  130. return errorCount;
  131. }
  132. internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  133. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  134. {
  135. if (this == baseParticle) // quick check
  136. return true;
  137. XmlSchemaElement el = baseParticle as XmlSchemaElement;
  138. if (el != null) {
  139. // Forbidden
  140. if (raiseError)
  141. error (h, "Invalid sequence paricle derivation.");
  142. return false;
  143. }
  144. XmlSchemaSequence seq = baseParticle as XmlSchemaSequence;
  145. if (seq != null) {
  146. // Recurse
  147. if (!ValidateOccurenceRangeOK (seq, h, schema, raiseError))
  148. return false;
  149. // If it is totally optional, then ignore their contents.
  150. if (seq.ValidatedMinOccurs == 0 && seq.ValidatedMaxOccurs == 0 &&
  151. this.ValidatedMinOccurs == 0 && this.ValidatedMaxOccurs == 0)
  152. return true;
  153. return ValidateRecurse (seq, h, schema, raiseError);
  154. }
  155. XmlSchemaAll all = baseParticle as XmlSchemaAll;
  156. if (all != null) {
  157. // RecurseUnordered
  158. XmlSchemaObjectCollection already = new XmlSchemaObjectCollection ();
  159. for (int i = 0; i < this.Items.Count; i++) {
  160. XmlSchemaElement de = this.Items [i] as XmlSchemaElement;
  161. if (de == null) {
  162. if (raiseError)
  163. error (h, "Invalid sequence particle derivation by restriction from all.");
  164. return false;
  165. }
  166. foreach (XmlSchemaElement e in all.Items) {
  167. if (e.QualifiedName == de.QualifiedName) {
  168. if (already.Contains (e)) {
  169. if (raiseError)
  170. error (h, "Base element particle is mapped to the derived element particle in a sequence two or more times.");
  171. return false;
  172. } else {
  173. already.Add (e);
  174. if (!de.ValidateDerivationByRestriction (e, h, schema, raiseError))
  175. return false;
  176. }
  177. }
  178. }
  179. }
  180. foreach (XmlSchemaElement e in all.Items)
  181. if (!already.Contains (e))
  182. if (!e.ValidateIsEmptiable ()) {
  183. if (raiseError)
  184. error (h, "In base -all- particle, mapping-skipped base element which is not emptiable was found.");
  185. return false;
  186. }
  187. return true;
  188. }
  189. XmlSchemaAny any = baseParticle as XmlSchemaAny;
  190. if (any != null) {
  191. // NSRecurseCheckCardinality
  192. return ValidateNSRecurseCheckCardinality (any, h, schema, raiseError);
  193. }
  194. XmlSchemaChoice choice = baseParticle as XmlSchemaChoice;
  195. if (choice != null) {
  196. // MapAndSum
  197. // In fact it is not Recurse, but it looks almost common.
  198. return ValidateSeqRecurseMapSumCommon (choice, h, schema, false, true, raiseError);
  199. }
  200. return true;
  201. }
  202. internal override decimal GetMinEffectiveTotalRange ()
  203. {
  204. return GetMinEffectiveTotalRangeAllAndSequence ();
  205. }
  206. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  207. ValidationEventHandler h, XmlSchema schema)
  208. {
  209. ValidateUPAOnHeadingOptionalComponents (qnames, nsNames, h, schema);
  210. ValidateUPAOnItems (qnames, nsNames, h, schema);
  211. }
  212. void ValidateUPAOnHeadingOptionalComponents (XmlSchemaObjectTable qnames, ArrayList nsNames,
  213. ValidationEventHandler h, XmlSchema schema)
  214. {
  215. // heading optional components
  216. foreach (XmlSchemaParticle p in this.Items) {
  217. p.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
  218. if (p.ValidatedMinOccurs != 0)
  219. break;
  220. }
  221. }
  222. void ValidateUPAOnItems (XmlSchemaObjectTable qnames, ArrayList nsNames,
  223. ValidationEventHandler h, XmlSchema schema)
  224. {
  225. // non-optional components
  226. XmlSchemaObjectTable elems = new XmlSchemaObjectTable ();
  227. ArrayList wildcards = new ArrayList ();
  228. XmlSchemaObjectTable tmpElems = new XmlSchemaObjectTable ();
  229. ArrayList tmpWildcards = new ArrayList ();
  230. for (int i=0; i<Items.Count; i++) {
  231. XmlSchemaParticle p1 = Items [i] as XmlSchemaParticle;
  232. p1.ValidateUniqueParticleAttribution (elems, wildcards, h, schema);
  233. if (p1.ValidatedMinOccurs == p1.ValidatedMaxOccurs) {
  234. elems.Clear ();
  235. wildcards.Clear ();
  236. }
  237. else {
  238. if (p1.ValidatedMinOccurs != 0) {
  239. foreach (XmlQualifiedName n in tmpElems.Names)
  240. elems.Set (n, null); // remove
  241. foreach (object o in tmpWildcards)
  242. wildcards.Remove (o);
  243. }
  244. foreach (XmlQualifiedName n in elems.Names)
  245. tmpElems.Set (n, elems [n]);
  246. tmpWildcards.Clear ();
  247. tmpWildcards.AddRange (wildcards);
  248. }
  249. }
  250. }
  251. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  252. ValidationEventHandler h, XmlSchema schema)
  253. {
  254. foreach (XmlSchemaParticle p in this.Items)
  255. p.ValidateUniqueTypeAttribution (labels, h, schema);
  256. }
  257. //<sequence
  258. // id = ID
  259. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  260. // minOccurs = nonNegativeInteger : 1
  261. // {any attributes with non-schema namespace . . .}>
  262. // Content: (annotation?, (element | group | choice | sequence | any)*)
  263. //</sequence>
  264. internal static XmlSchemaSequence Read(XmlSchemaReader reader, ValidationEventHandler h)
  265. {
  266. XmlSchemaSequence sequence = new XmlSchemaSequence();
  267. reader.MoveToElement();
  268. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  269. {
  270. error(h,"Should not happen :1: XmlSchemaSequence.Read, name="+reader.Name,null);
  271. reader.Skip();
  272. return null;
  273. }
  274. sequence.LineNumber = reader.LineNumber;
  275. sequence.LinePosition = reader.LinePosition;
  276. sequence.SourceUri = reader.BaseURI;
  277. while(reader.MoveToNextAttribute())
  278. {
  279. if(reader.Name == "id")
  280. {
  281. sequence.Id = reader.Value;
  282. }
  283. else if(reader.Name == "maxOccurs")
  284. {
  285. try
  286. {
  287. sequence.MaxOccursString = reader.Value;
  288. }
  289. catch(Exception e)
  290. {
  291. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  292. }
  293. }
  294. else if(reader.Name == "minOccurs")
  295. {
  296. try
  297. {
  298. sequence.MinOccursString = reader.Value;
  299. }
  300. catch(Exception e)
  301. {
  302. error(h,reader.Value + " is an invalid value for minOccurs",e);
  303. }
  304. }
  305. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  306. {
  307. error(h,reader.Name + " is not a valid attribute for sequence",null);
  308. }
  309. else
  310. {
  311. XmlSchemaUtil.ReadUnhandledAttribute(reader,sequence);
  312. }
  313. }
  314. reader.MoveToElement();
  315. if(reader.IsEmptyElement)
  316. return sequence;
  317. // Content: (annotation?, (element | group | choice | sequence | any)*)
  318. int level = 1;
  319. while(reader.ReadNextElement())
  320. {
  321. if(reader.NodeType == XmlNodeType.EndElement)
  322. {
  323. if(reader.LocalName != xmlname)
  324. error(h,"Should not happen :2: XmlSchemaSequence.Read, name="+reader.Name,null);
  325. break;
  326. }
  327. if(level <= 1 && reader.LocalName == "annotation")
  328. {
  329. level = 2; //Only one annotation
  330. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  331. if(annotation != null)
  332. sequence.Annotation = annotation;
  333. continue;
  334. }
  335. if(level <=2)
  336. {
  337. if(reader.LocalName == "element")
  338. {
  339. level = 2;
  340. XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
  341. if(element != null)
  342. sequence.items.Add(element);
  343. continue;
  344. }
  345. if(reader.LocalName == "group")
  346. {
  347. level = 2;
  348. XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
  349. if(group != null)
  350. sequence.items.Add(group);
  351. continue;
  352. }
  353. if(reader.LocalName == "choice")
  354. {
  355. level = 2;
  356. XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
  357. if(choice != null)
  358. sequence.items.Add(choice);
  359. continue;
  360. }
  361. if(reader.LocalName == "sequence")
  362. {
  363. level = 2;
  364. XmlSchemaSequence seq = XmlSchemaSequence.Read(reader,h);
  365. if(seq != null)
  366. sequence.items.Add(seq);
  367. continue;
  368. }
  369. if(reader.LocalName == "any")
  370. {
  371. level = 2;
  372. XmlSchemaAny any = XmlSchemaAny.Read(reader,h);
  373. if(any != null)
  374. sequence.items.Add(any);
  375. continue;
  376. }
  377. }
  378. reader.RaiseInvalidElementError();
  379. }
  380. return sequence;
  381. }
  382. }
  383. }