XmlSchemaSequence.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. [MonoTODO]
  47. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  48. {
  49. // If this is already compiled this time, simply skip.
  50. if (this.IsComplied (schema.CompilationId))
  51. return 0;
  52. XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
  53. CompileOccurence (h, schema);
  54. foreach(XmlSchemaObject obj in Items)
  55. {
  56. if(obj is XmlSchemaElement ||
  57. obj is XmlSchemaGroupRef ||
  58. obj is XmlSchemaChoice ||
  59. obj is XmlSchemaSequence ||
  60. obj is XmlSchemaAny)
  61. {
  62. errorCount += obj.Compile(h,schema);
  63. }
  64. else
  65. error(h, "Invalid schema object was specified in the particles of the sequence model group.");
  66. }
  67. this.CompilationId = schema.CompilationId;
  68. return errorCount;
  69. }
  70. [MonoTODO]
  71. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  72. {
  73. if (IsValidated (schema.CompilationId))
  74. return errorCount;
  75. CompiledItems.Clear ();
  76. foreach (XmlSchemaObject obj in Items) {
  77. errorCount += obj.Validate (h, schema);
  78. CompiledItems.Add (obj);
  79. }
  80. ValidationId = schema.ValidationId;
  81. return errorCount;
  82. }
  83. internal override void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  84. ValidationEventHandler h, XmlSchema schema)
  85. {
  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 CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  145. {
  146. foreach (XmlSchemaParticle p in this.Items)
  147. p.CheckRecursion (depth, h, schema);
  148. }
  149. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  150. ValidationEventHandler h, XmlSchema schema)
  151. {
  152. foreach (XmlSchemaParticle p in this.Items) {
  153. p.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
  154. if (p.ValidatedMinOccurs == p.ValidatedMaxOccurs)
  155. break;
  156. }
  157. XmlSchemaObjectTable tmpTable = new XmlSchemaObjectTable ();
  158. ArrayList al = new ArrayList ();
  159. for (int i=0; i<Items.Count; i++) {
  160. XmlSchemaParticle p1 = Items [i] as XmlSchemaParticle;
  161. p1.ValidateUniqueParticleAttribution (tmpTable, al, h, schema);
  162. if (p1.ValidatedMinOccurs == p1.ValidatedMaxOccurs) {
  163. tmpTable.Clear ();
  164. al.Clear ();
  165. }
  166. }
  167. }
  168. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  169. ValidationEventHandler h, XmlSchema schema)
  170. {
  171. foreach (XmlSchemaParticle p in this.Items)
  172. p.ValidateUniqueTypeAttribution (labels, h, schema);
  173. }
  174. //<sequence
  175. // id = ID
  176. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  177. // minOccurs = nonNegativeInteger : 1
  178. // {any attributes with non-schema namespace . . .}>
  179. // Content: (annotation?, (element | group | choice | sequence | any)*)
  180. //</sequence>
  181. internal static XmlSchemaSequence Read(XmlSchemaReader reader, ValidationEventHandler h)
  182. {
  183. XmlSchemaSequence sequence = new XmlSchemaSequence();
  184. reader.MoveToElement();
  185. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  186. {
  187. error(h,"Should not happen :1: XmlSchemaSequence.Read, name="+reader.Name,null);
  188. reader.Skip();
  189. return null;
  190. }
  191. sequence.LineNumber = reader.LineNumber;
  192. sequence.LinePosition = reader.LinePosition;
  193. sequence.SourceUri = reader.BaseURI;
  194. while(reader.MoveToNextAttribute())
  195. {
  196. if(reader.Name == "id")
  197. {
  198. sequence.Id = reader.Value;
  199. }
  200. else if(reader.Name == "maxOccurs")
  201. {
  202. try
  203. {
  204. sequence.MaxOccursString = reader.Value;
  205. }
  206. catch(Exception e)
  207. {
  208. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  209. }
  210. }
  211. else if(reader.Name == "minOccurs")
  212. {
  213. try
  214. {
  215. sequence.MinOccursString = reader.Value;
  216. }
  217. catch(Exception e)
  218. {
  219. error(h,reader.Value + " is an invalid value for minOccurs",e);
  220. }
  221. }
  222. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  223. {
  224. error(h,reader.Name + " is not a valid attribute for sequence",null);
  225. }
  226. else
  227. {
  228. XmlSchemaUtil.ReadUnhandledAttribute(reader,sequence);
  229. }
  230. }
  231. reader.MoveToElement();
  232. if(reader.IsEmptyElement)
  233. return sequence;
  234. // Content: (annotation?, (element | group | choice | sequence | any)*)
  235. int level = 1;
  236. while(reader.ReadNextElement())
  237. {
  238. if(reader.NodeType == XmlNodeType.EndElement)
  239. {
  240. if(reader.LocalName != xmlname)
  241. error(h,"Should not happen :2: XmlSchemaSequence.Read, name="+reader.Name,null);
  242. break;
  243. }
  244. if(level <= 1 && reader.LocalName == "annotation")
  245. {
  246. level = 2; //Only one annotation
  247. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  248. if(annotation != null)
  249. sequence.Annotation = annotation;
  250. continue;
  251. }
  252. if(level <=2)
  253. {
  254. if(reader.LocalName == "element")
  255. {
  256. level = 2;
  257. XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
  258. if(element != null)
  259. sequence.items.Add(element);
  260. continue;
  261. }
  262. if(reader.LocalName == "group")
  263. {
  264. level = 2;
  265. XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
  266. if(group != null)
  267. sequence.items.Add(group);
  268. continue;
  269. }
  270. if(reader.LocalName == "choice")
  271. {
  272. level = 2;
  273. XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
  274. if(choice != null)
  275. sequence.items.Add(choice);
  276. continue;
  277. }
  278. if(reader.LocalName == "sequence")
  279. {
  280. level = 2;
  281. XmlSchemaSequence seq = XmlSchemaSequence.Read(reader,h);
  282. if(seq != null)
  283. sequence.items.Add(seq);
  284. continue;
  285. }
  286. if(reader.LocalName == "any")
  287. {
  288. level = 2;
  289. XmlSchemaAny any = XmlSchemaAny.Read(reader,h);
  290. if(any != null)
  291. sequence.items.Add(any);
  292. continue;
  293. }
  294. }
  295. reader.RaiseInvalidElementError();
  296. }
  297. return sequence;
  298. }
  299. }
  300. }