XmlSchemaSequence.cs 12 KB

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