XmlSchemaSequence.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. private static 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 XmlSchemaParticle ActualParticle {
  35. get {
  36. if (CompiledItems.Count == 0)
  37. return XmlSchemaParticle.Empty;
  38. if (ValidatedMaxOccurs == 1 &&
  39. ValidatedMinOccurs == 1 &&
  40. CompiledItems.Count == 1)
  41. return ((XmlSchemaParticle) CompiledItems [0]).ActualParticle;
  42. else
  43. return this;
  44. }
  45. }
  46. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  47. {
  48. // If this is already compiled this time, simply skip.
  49. if (this.IsComplied (schema.CompilationId))
  50. return 0;
  51. XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
  52. CompileOccurence (h, schema);
  53. foreach(XmlSchemaObject obj in Items)
  54. {
  55. if(obj is XmlSchemaElement ||
  56. obj is XmlSchemaGroupRef ||
  57. obj is XmlSchemaChoice ||
  58. obj is XmlSchemaSequence ||
  59. obj is XmlSchemaAny)
  60. {
  61. errorCount += obj.Compile(h,schema);
  62. }
  63. else
  64. error(h, "Invalid schema object was specified in the particles of the sequence model group.");
  65. }
  66. this.CompilationId = schema.CompilationId;
  67. return errorCount;
  68. }
  69. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  70. {
  71. if (IsValidated (schema.CompilationId))
  72. return errorCount;
  73. CompiledItems.Clear ();
  74. foreach (XmlSchemaObject obj in Items) {
  75. errorCount += obj.Validate (h, schema);
  76. CompiledItems.Add (obj);
  77. }
  78. ValidationId = schema.ValidationId;
  79. return errorCount;
  80. }
  81. internal override void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  82. ValidationEventHandler h, XmlSchema schema)
  83. {
  84. if (this == baseParticle) // quick check
  85. return;
  86. XmlSchemaElement el = baseParticle as XmlSchemaElement;
  87. if (el != null) {
  88. // Forbidden
  89. error (h, "Invalid sequence paricle derivation.");
  90. return;
  91. }
  92. XmlSchemaSequence seq = baseParticle as XmlSchemaSequence;
  93. if (seq != null) {
  94. // Recurse
  95. ValidateOccurenceRangeOK (seq, h, schema);
  96. // If it is totally optional, then ignore their contents.
  97. if (seq.ValidatedMinOccurs == 0 && seq.ValidatedMaxOccurs == 0 &&
  98. this.ValidatedMinOccurs == 0 && this.ValidatedMaxOccurs == 0)
  99. return;
  100. this.ValidateRecurse (seq, h, schema);
  101. return;
  102. }
  103. XmlSchemaAll all = baseParticle as XmlSchemaAll;
  104. XmlSchemaAny any = baseParticle as XmlSchemaAny;
  105. XmlSchemaChoice choice = baseParticle as XmlSchemaChoice;
  106. if (all != null) {
  107. // RecurseUnordered
  108. XmlSchemaObjectCollection already = new XmlSchemaObjectCollection ();
  109. for (int i = 0; i < this.Items.Count; i++) {
  110. XmlSchemaElement de = this.Items [i] as XmlSchemaElement;
  111. if (de == null) {
  112. error (h, "Invalid sequence particle derivation by restriction from all.");
  113. continue;
  114. }
  115. foreach (XmlSchemaElement e in all.Items) {
  116. if (e.QualifiedName == de.QualifiedName) {
  117. if (already.Contains (e))
  118. error (h, "Base element particle is mapped to the derived element particle in a sequence two or more times.");
  119. else {
  120. already.Add (e);
  121. de.ValidateDerivationByRestriction (e, h, schema);
  122. }
  123. }
  124. }
  125. }
  126. foreach (XmlSchemaElement e in all.Items)
  127. if (!already.Contains (e))
  128. if (!e.ValidateIsEmptiable ())
  129. error (h, "In base -all- particle, mapping-skipped base element which is not emptiable was found.");
  130. } else if (any != null) {
  131. // NSRecurseCheckCardinality
  132. ValidateNSRecurseCheckCardinality (any, h, schema);
  133. return;
  134. } else if (choice != null) {
  135. // MapAndSum
  136. // In fact it is not Recurse, but it looks common.
  137. ValidateRecurse (choice, h, schema);
  138. }
  139. }
  140. internal override decimal GetMinEffectiveTotalRange ()
  141. {
  142. return GetMinEffectiveTotalRangeAllAndSequence ();
  143. }
  144. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  145. ValidationEventHandler h, XmlSchema schema)
  146. {
  147. foreach (XmlSchemaParticle p in this.Items) {
  148. p.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
  149. if (p.ValidatedMinOccurs == p.ValidatedMaxOccurs)
  150. break;
  151. }
  152. XmlSchemaObjectTable tmpTable = new XmlSchemaObjectTable ();
  153. ArrayList al = new ArrayList ();
  154. for (int i=0; i<Items.Count; i++) {
  155. XmlSchemaParticle p1 = Items [i] as XmlSchemaParticle;
  156. p1.ValidateUniqueParticleAttribution (tmpTable, al, h, schema);
  157. if (p1.ValidatedMinOccurs == p1.ValidatedMaxOccurs) {
  158. tmpTable.Clear ();
  159. al.Clear ();
  160. }
  161. }
  162. }
  163. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  164. ValidationEventHandler h, XmlSchema schema)
  165. {
  166. foreach (XmlSchemaParticle p in this.Items)
  167. p.ValidateUniqueTypeAttribution (labels, h, schema);
  168. }
  169. //<sequence
  170. // id = ID
  171. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  172. // minOccurs = nonNegativeInteger : 1
  173. // {any attributes with non-schema namespace . . .}>
  174. // Content: (annotation?, (element | group | choice | sequence | any)*)
  175. //</sequence>
  176. internal static XmlSchemaSequence Read(XmlSchemaReader reader, ValidationEventHandler h)
  177. {
  178. XmlSchemaSequence sequence = new XmlSchemaSequence();
  179. reader.MoveToElement();
  180. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  181. {
  182. error(h,"Should not happen :1: XmlSchemaSequence.Read, name="+reader.Name,null);
  183. reader.Skip();
  184. return null;
  185. }
  186. sequence.LineNumber = reader.LineNumber;
  187. sequence.LinePosition = reader.LinePosition;
  188. sequence.SourceUri = reader.BaseURI;
  189. while(reader.MoveToNextAttribute())
  190. {
  191. if(reader.Name == "id")
  192. {
  193. sequence.Id = reader.Value;
  194. }
  195. else if(reader.Name == "maxOccurs")
  196. {
  197. try
  198. {
  199. sequence.MaxOccursString = reader.Value;
  200. }
  201. catch(Exception e)
  202. {
  203. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  204. }
  205. }
  206. else if(reader.Name == "minOccurs")
  207. {
  208. try
  209. {
  210. sequence.MinOccursString = reader.Value;
  211. }
  212. catch(Exception e)
  213. {
  214. error(h,reader.Value + " is an invalid value for minOccurs",e);
  215. }
  216. }
  217. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  218. {
  219. error(h,reader.Name + " is not a valid attribute for sequence",null);
  220. }
  221. else
  222. {
  223. XmlSchemaUtil.ReadUnhandledAttribute(reader,sequence);
  224. }
  225. }
  226. reader.MoveToElement();
  227. if(reader.IsEmptyElement)
  228. return sequence;
  229. // Content: (annotation?, (element | group | choice | sequence | any)*)
  230. int level = 1;
  231. while(reader.ReadNextElement())
  232. {
  233. if(reader.NodeType == XmlNodeType.EndElement)
  234. {
  235. if(reader.LocalName != xmlname)
  236. error(h,"Should not happen :2: XmlSchemaSequence.Read, name="+reader.Name,null);
  237. break;
  238. }
  239. if(level <= 1 && reader.LocalName == "annotation")
  240. {
  241. level = 2; //Only one annotation
  242. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  243. if(annotation != null)
  244. sequence.Annotation = annotation;
  245. continue;
  246. }
  247. if(level <=2)
  248. {
  249. if(reader.LocalName == "element")
  250. {
  251. level = 2;
  252. XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
  253. if(element != null)
  254. sequence.items.Add(element);
  255. continue;
  256. }
  257. if(reader.LocalName == "group")
  258. {
  259. level = 2;
  260. XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
  261. if(group != null)
  262. sequence.items.Add(group);
  263. continue;
  264. }
  265. if(reader.LocalName == "choice")
  266. {
  267. level = 2;
  268. XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
  269. if(choice != null)
  270. sequence.items.Add(choice);
  271. continue;
  272. }
  273. if(reader.LocalName == "sequence")
  274. {
  275. level = 2;
  276. XmlSchemaSequence seq = XmlSchemaSequence.Read(reader,h);
  277. if(seq != null)
  278. sequence.items.Add(seq);
  279. continue;
  280. }
  281. if(reader.LocalName == "any")
  282. {
  283. level = 2;
  284. XmlSchemaAny any = XmlSchemaAny.Read(reader,h);
  285. if(any != null)
  286. sequence.items.Add(any);
  287. continue;
  288. }
  289. }
  290. reader.RaiseInvalidElementError();
  291. }
  292. return sequence;
  293. }
  294. }
  295. }