XmlSchemaSequence.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 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. foreach (XmlSchemaParticle p in this.Items) {
  210. p.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
  211. if (p.ValidatedMinOccurs == p.ValidatedMaxOccurs)
  212. break;
  213. }
  214. XmlSchemaObjectTable tmpTable = new XmlSchemaObjectTable ();
  215. ArrayList al = new ArrayList ();
  216. for (int i=0; i<Items.Count; i++) {
  217. XmlSchemaParticle p1 = Items [i] as XmlSchemaParticle;
  218. p1.ValidateUniqueParticleAttribution (tmpTable, al, h, schema);
  219. if (p1.ValidatedMinOccurs == p1.ValidatedMaxOccurs) {
  220. tmpTable.Clear ();
  221. al.Clear ();
  222. }
  223. }
  224. }
  225. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  226. ValidationEventHandler h, XmlSchema schema)
  227. {
  228. foreach (XmlSchemaParticle p in this.Items)
  229. p.ValidateUniqueTypeAttribution (labels, h, schema);
  230. }
  231. //<sequence
  232. // id = ID
  233. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  234. // minOccurs = nonNegativeInteger : 1
  235. // {any attributes with non-schema namespace . . .}>
  236. // Content: (annotation?, (element | group | choice | sequence | any)*)
  237. //</sequence>
  238. internal static XmlSchemaSequence Read(XmlSchemaReader reader, ValidationEventHandler h)
  239. {
  240. XmlSchemaSequence sequence = new XmlSchemaSequence();
  241. reader.MoveToElement();
  242. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  243. {
  244. error(h,"Should not happen :1: XmlSchemaSequence.Read, name="+reader.Name,null);
  245. reader.Skip();
  246. return null;
  247. }
  248. sequence.LineNumber = reader.LineNumber;
  249. sequence.LinePosition = reader.LinePosition;
  250. sequence.SourceUri = reader.BaseURI;
  251. while(reader.MoveToNextAttribute())
  252. {
  253. if(reader.Name == "id")
  254. {
  255. sequence.Id = reader.Value;
  256. }
  257. else if(reader.Name == "maxOccurs")
  258. {
  259. try
  260. {
  261. sequence.MaxOccursString = reader.Value;
  262. }
  263. catch(Exception e)
  264. {
  265. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  266. }
  267. }
  268. else if(reader.Name == "minOccurs")
  269. {
  270. try
  271. {
  272. sequence.MinOccursString = reader.Value;
  273. }
  274. catch(Exception e)
  275. {
  276. error(h,reader.Value + " is an invalid value for minOccurs",e);
  277. }
  278. }
  279. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  280. {
  281. error(h,reader.Name + " is not a valid attribute for sequence",null);
  282. }
  283. else
  284. {
  285. XmlSchemaUtil.ReadUnhandledAttribute(reader,sequence);
  286. }
  287. }
  288. reader.MoveToElement();
  289. if(reader.IsEmptyElement)
  290. return sequence;
  291. // Content: (annotation?, (element | group | choice | sequence | any)*)
  292. int level = 1;
  293. while(reader.ReadNextElement())
  294. {
  295. if(reader.NodeType == XmlNodeType.EndElement)
  296. {
  297. if(reader.LocalName != xmlname)
  298. error(h,"Should not happen :2: XmlSchemaSequence.Read, name="+reader.Name,null);
  299. break;
  300. }
  301. if(level <= 1 && reader.LocalName == "annotation")
  302. {
  303. level = 2; //Only one annotation
  304. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  305. if(annotation != null)
  306. sequence.Annotation = annotation;
  307. continue;
  308. }
  309. if(level <=2)
  310. {
  311. if(reader.LocalName == "element")
  312. {
  313. level = 2;
  314. XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
  315. if(element != null)
  316. sequence.items.Add(element);
  317. continue;
  318. }
  319. if(reader.LocalName == "group")
  320. {
  321. level = 2;
  322. XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
  323. if(group != null)
  324. sequence.items.Add(group);
  325. continue;
  326. }
  327. if(reader.LocalName == "choice")
  328. {
  329. level = 2;
  330. XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
  331. if(choice != null)
  332. sequence.items.Add(choice);
  333. continue;
  334. }
  335. if(reader.LocalName == "sequence")
  336. {
  337. level = 2;
  338. XmlSchemaSequence seq = XmlSchemaSequence.Read(reader,h);
  339. if(seq != null)
  340. sequence.items.Add(seq);
  341. continue;
  342. }
  343. if(reader.LocalName == "any")
  344. {
  345. level = 2;
  346. XmlSchemaAny any = XmlSchemaAny.Read(reader,h);
  347. if(any != null)
  348. sequence.items.Add(any);
  349. continue;
  350. }
  351. }
  352. reader.RaiseInvalidElementError();
  353. }
  354. return sequence;
  355. }
  356. }
  357. }