XmlSchemaSequence.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //
  2. // System.Xml.Schema.XmlSchemaSequence.cs
  3. //
  4. // Author:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. using System;
  9. using System.Collections;
  10. using System.Xml.Serialization;
  11. using System.Xml;
  12. namespace System.Xml.Schema
  13. {
  14. /// <summary>
  15. /// Summary description for XmlSchemaSequence.
  16. /// </summary>
  17. public class XmlSchemaSequence : XmlSchemaGroupBase
  18. {
  19. private XmlSchemaObjectCollection items;
  20. const string xmlname = "sequence";
  21. public XmlSchemaSequence()
  22. {
  23. items = new XmlSchemaObjectCollection();
  24. }
  25. [XmlElement("element",typeof(XmlSchemaElement),Namespace="http://www.w3.org/2001/XMLSchema")]
  26. [XmlElement("group",typeof(XmlSchemaGroupRef),Namespace="http://www.w3.org/2001/XMLSchema")]
  27. [XmlElement("choice",typeof(XmlSchemaChoice),Namespace="http://www.w3.org/2001/XMLSchema")]
  28. [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace="http://www.w3.org/2001/XMLSchema")]
  29. [XmlElement("any",typeof(XmlSchemaAny),Namespace="http://www.w3.org/2001/XMLSchema")]
  30. public override XmlSchemaObjectCollection Items
  31. {
  32. get{ return items; }
  33. }
  34. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  35. {
  36. // If this is already compiled this time, simply skip.
  37. if (this.IsComplied (schema.CompilationId))
  38. return 0;
  39. XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
  40. CompileOccurence (h, schema);
  41. foreach(XmlSchemaObject obj in Items)
  42. {
  43. if(obj is XmlSchemaElement ||
  44. obj is XmlSchemaGroupRef ||
  45. obj is XmlSchemaChoice ||
  46. obj is XmlSchemaSequence ||
  47. obj is XmlSchemaAny)
  48. {
  49. errorCount += obj.Compile(h,schema);
  50. }
  51. else
  52. error(h, "Invalid schema object was specified in the particles of the sequence model group.");
  53. }
  54. this.CompilationId = schema.CompilationId;
  55. return errorCount;
  56. }
  57. internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
  58. {
  59. if (OptimizedParticle != null)
  60. return OptimizedParticle;
  61. if (Items.Count == 0 || ValidatedMaxOccurs == 0) {
  62. OptimizedParticle = XmlSchemaParticle.Empty;
  63. return OptimizedParticle;
  64. }
  65. if (!isTop && ValidatedMinOccurs == 1 && ValidatedMaxOccurs == 1) {
  66. if (Items.Count == 1)
  67. return ((XmlSchemaParticle) Items [0]).GetOptimizedParticle (false);
  68. }
  69. XmlSchemaSequence seq = new XmlSchemaSequence ();
  70. CopyInfo (seq);
  71. for (int i = 0; i < Items.Count; i++) {
  72. XmlSchemaParticle p = Items [i] as XmlSchemaParticle;
  73. p = p.GetOptimizedParticle (false);
  74. if (p == XmlSchemaParticle.Empty)
  75. continue;
  76. else if (p is XmlSchemaSequence && p.ValidatedMinOccurs == 1 && p.ValidatedMaxOccurs == 1) {
  77. XmlSchemaSequence ps = p as XmlSchemaSequence;
  78. for (int pi = 0; pi < ps.Items.Count; pi++) {
  79. seq.Items.Add (ps.Items [pi]);
  80. seq.CompiledItems.Add (ps.Items [pi]);
  81. }
  82. }
  83. else {
  84. seq.Items.Add (p);
  85. seq.CompiledItems.Add (p);
  86. }
  87. }
  88. if (seq.Items.Count == 0)
  89. OptimizedParticle = XmlSchemaParticle.Empty;
  90. else
  91. OptimizedParticle = seq;
  92. return OptimizedParticle;
  93. }
  94. internal override int Validate (ValidationEventHandler h, XmlSchema schema)
  95. {
  96. if (IsValidated (schema.CompilationId))
  97. return errorCount;
  98. CompiledItems.Clear ();
  99. foreach (XmlSchemaParticle p in Items) {
  100. errorCount += p.Validate (h, schema); // This is basically extraneous for pointless item, but needed to check validation error.
  101. // XmlSchemaParticle particleInPoint = p.GetParticleWithoutPointless ();
  102. // if (particleInPoint != XmlSchemaParticle.Empty)
  103. // CompiledItems.Add (particleInPoint);
  104. CompiledItems.Add (p);
  105. }
  106. ValidationId = schema.ValidationId;
  107. return errorCount;
  108. }
  109. internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  110. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  111. {
  112. if (this == baseParticle) // quick check
  113. return true;
  114. XmlSchemaElement el = baseParticle as XmlSchemaElement;
  115. if (el != null) {
  116. // Forbidden
  117. if (raiseError)
  118. error (h, "Invalid sequence paricle derivation.");
  119. return false;
  120. }
  121. XmlSchemaSequence seq = baseParticle as XmlSchemaSequence;
  122. if (seq != null) {
  123. // Recurse
  124. if (!ValidateOccurenceRangeOK (seq, h, schema, raiseError))
  125. return false;
  126. // If it is totally optional, then ignore their contents.
  127. if (seq.ValidatedMinOccurs == 0 && seq.ValidatedMaxOccurs == 0 &&
  128. this.ValidatedMinOccurs == 0 && this.ValidatedMaxOccurs == 0)
  129. return true;
  130. return ValidateRecurse (seq, h, schema, raiseError);
  131. }
  132. XmlSchemaAll all = baseParticle as XmlSchemaAll;
  133. if (all != null) {
  134. // RecurseUnordered
  135. XmlSchemaObjectCollection already = new XmlSchemaObjectCollection ();
  136. for (int i = 0; i < this.Items.Count; i++) {
  137. XmlSchemaElement de = this.Items [i] as XmlSchemaElement;
  138. if (de == null) {
  139. if (raiseError)
  140. error (h, "Invalid sequence particle derivation by restriction from all.");
  141. return false;
  142. }
  143. foreach (XmlSchemaElement e in all.Items) {
  144. if (e.QualifiedName == de.QualifiedName) {
  145. if (already.Contains (e)) {
  146. if (raiseError)
  147. error (h, "Base element particle is mapped to the derived element particle in a sequence two or more times.");
  148. return false;
  149. } else {
  150. already.Add (e);
  151. if (!de.ValidateDerivationByRestriction (e, h, schema, raiseError))
  152. return false;
  153. }
  154. }
  155. }
  156. }
  157. foreach (XmlSchemaElement e in all.Items)
  158. if (!already.Contains (e))
  159. if (!e.ValidateIsEmptiable ()) {
  160. if (raiseError)
  161. error (h, "In base -all- particle, mapping-skipped base element which is not emptiable was found.");
  162. return false;
  163. }
  164. return true;
  165. }
  166. XmlSchemaAny any = baseParticle as XmlSchemaAny;
  167. if (any != null) {
  168. // NSRecurseCheckCardinality
  169. return ValidateNSRecurseCheckCardinality (any, h, schema, raiseError);
  170. }
  171. XmlSchemaChoice choice = baseParticle as XmlSchemaChoice;
  172. if (choice != null) {
  173. // MapAndSum
  174. // In fact it is not Recurse, but it looks almost common.
  175. return ValidateSeqRecurseMapSumCommon (choice, h, schema, false, true, raiseError);
  176. }
  177. return true;
  178. }
  179. internal override decimal GetMinEffectiveTotalRange ()
  180. {
  181. return GetMinEffectiveTotalRangeAllAndSequence ();
  182. }
  183. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  184. ValidationEventHandler h, XmlSchema schema)
  185. {
  186. foreach (XmlSchemaParticle p in this.Items) {
  187. p.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
  188. if (p.ValidatedMinOccurs == p.ValidatedMaxOccurs)
  189. break;
  190. }
  191. XmlSchemaObjectTable tmpTable = new XmlSchemaObjectTable ();
  192. ArrayList al = new ArrayList ();
  193. for (int i=0; i<Items.Count; i++) {
  194. XmlSchemaParticle p1 = Items [i] as XmlSchemaParticle;
  195. p1.ValidateUniqueParticleAttribution (tmpTable, al, h, schema);
  196. if (p1.ValidatedMinOccurs == p1.ValidatedMaxOccurs) {
  197. tmpTable.Clear ();
  198. al.Clear ();
  199. }
  200. }
  201. }
  202. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  203. ValidationEventHandler h, XmlSchema schema)
  204. {
  205. foreach (XmlSchemaParticle p in this.Items)
  206. p.ValidateUniqueTypeAttribution (labels, h, schema);
  207. }
  208. //<sequence
  209. // id = ID
  210. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  211. // minOccurs = nonNegativeInteger : 1
  212. // {any attributes with non-schema namespace . . .}>
  213. // Content: (annotation?, (element | group | choice | sequence | any)*)
  214. //</sequence>
  215. internal static XmlSchemaSequence Read(XmlSchemaReader reader, ValidationEventHandler h)
  216. {
  217. XmlSchemaSequence sequence = new XmlSchemaSequence();
  218. reader.MoveToElement();
  219. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  220. {
  221. error(h,"Should not happen :1: XmlSchemaSequence.Read, name="+reader.Name,null);
  222. reader.Skip();
  223. return null;
  224. }
  225. sequence.LineNumber = reader.LineNumber;
  226. sequence.LinePosition = reader.LinePosition;
  227. sequence.SourceUri = reader.BaseURI;
  228. while(reader.MoveToNextAttribute())
  229. {
  230. if(reader.Name == "id")
  231. {
  232. sequence.Id = reader.Value;
  233. }
  234. else if(reader.Name == "maxOccurs")
  235. {
  236. try
  237. {
  238. sequence.MaxOccursString = reader.Value;
  239. }
  240. catch(Exception e)
  241. {
  242. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  243. }
  244. }
  245. else if(reader.Name == "minOccurs")
  246. {
  247. try
  248. {
  249. sequence.MinOccursString = reader.Value;
  250. }
  251. catch(Exception e)
  252. {
  253. error(h,reader.Value + " is an invalid value for minOccurs",e);
  254. }
  255. }
  256. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  257. {
  258. error(h,reader.Name + " is not a valid attribute for sequence",null);
  259. }
  260. else
  261. {
  262. XmlSchemaUtil.ReadUnhandledAttribute(reader,sequence);
  263. }
  264. }
  265. reader.MoveToElement();
  266. if(reader.IsEmptyElement)
  267. return sequence;
  268. // Content: (annotation?, (element | group | choice | sequence | any)*)
  269. int level = 1;
  270. while(reader.ReadNextElement())
  271. {
  272. if(reader.NodeType == XmlNodeType.EndElement)
  273. {
  274. if(reader.LocalName != xmlname)
  275. error(h,"Should not happen :2: XmlSchemaSequence.Read, name="+reader.Name,null);
  276. break;
  277. }
  278. if(level <= 1 && reader.LocalName == "annotation")
  279. {
  280. level = 2; //Only one annotation
  281. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  282. if(annotation != null)
  283. sequence.Annotation = annotation;
  284. continue;
  285. }
  286. if(level <=2)
  287. {
  288. if(reader.LocalName == "element")
  289. {
  290. level = 2;
  291. XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
  292. if(element != null)
  293. sequence.items.Add(element);
  294. continue;
  295. }
  296. if(reader.LocalName == "group")
  297. {
  298. level = 2;
  299. XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
  300. if(group != null)
  301. sequence.items.Add(group);
  302. continue;
  303. }
  304. if(reader.LocalName == "choice")
  305. {
  306. level = 2;
  307. XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
  308. if(choice != null)
  309. sequence.items.Add(choice);
  310. continue;
  311. }
  312. if(reader.LocalName == "sequence")
  313. {
  314. level = 2;
  315. XmlSchemaSequence seq = XmlSchemaSequence.Read(reader,h);
  316. if(seq != null)
  317. sequence.items.Add(seq);
  318. continue;
  319. }
  320. if(reader.LocalName == "any")
  321. {
  322. level = 2;
  323. XmlSchemaAny any = XmlSchemaAny.Read(reader,h);
  324. if(any != null)
  325. sequence.items.Add(any);
  326. continue;
  327. }
  328. }
  329. reader.RaiseInvalidElementError();
  330. }
  331. return sequence;
  332. }
  333. }
  334. }