XmlSchemaSequence.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. // TODO: MapAndSum
  136. }
  137. }
  138. internal override decimal GetMinEffectiveTotalRange ()
  139. {
  140. return GetMinEffectiveTotalRangeAllAndSequence ();
  141. }
  142. internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  143. {
  144. foreach (XmlSchemaParticle p in this.Items)
  145. p.CheckRecursion (depth, h, schema);
  146. }
  147. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  148. ValidationEventHandler h, XmlSchema schema)
  149. {
  150. foreach (XmlSchemaParticle p in this.Items) {
  151. p.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
  152. if (p.ValidatedMinOccurs == p.ValidatedMaxOccurs)
  153. break;
  154. }
  155. XmlSchemaObjectTable tmpTable = new XmlSchemaObjectTable ();
  156. ArrayList al = new ArrayList ();
  157. for (int i=0; i<Items.Count; i++) {
  158. XmlSchemaParticle p1 = Items [i] as XmlSchemaParticle;
  159. p1.ValidateUniqueParticleAttribution (tmpTable, al, h, schema);
  160. if (p1.ValidatedMinOccurs == p1.ValidatedMaxOccurs) {
  161. tmpTable.Clear ();
  162. al.Clear ();
  163. }
  164. }
  165. }
  166. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  167. ValidationEventHandler h, XmlSchema schema)
  168. {
  169. foreach (XmlSchemaParticle p in this.Items)
  170. p.ValidateUniqueTypeAttribution (labels, h, schema);
  171. }
  172. //<sequence
  173. // id = ID
  174. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  175. // minOccurs = nonNegativeInteger : 1
  176. // {any attributes with non-schema namespace . . .}>
  177. // Content: (annotation?, (element | group | choice | sequence | any)*)
  178. //</sequence>
  179. internal static XmlSchemaSequence Read(XmlSchemaReader reader, ValidationEventHandler h)
  180. {
  181. XmlSchemaSequence sequence = new XmlSchemaSequence();
  182. reader.MoveToElement();
  183. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  184. {
  185. error(h,"Should not happen :1: XmlSchemaSequence.Read, name="+reader.Name,null);
  186. reader.Skip();
  187. return null;
  188. }
  189. sequence.LineNumber = reader.LineNumber;
  190. sequence.LinePosition = reader.LinePosition;
  191. sequence.SourceUri = reader.BaseURI;
  192. while(reader.MoveToNextAttribute())
  193. {
  194. if(reader.Name == "id")
  195. {
  196. sequence.Id = reader.Value;
  197. }
  198. else if(reader.Name == "maxOccurs")
  199. {
  200. try
  201. {
  202. sequence.MaxOccursString = reader.Value;
  203. }
  204. catch(Exception e)
  205. {
  206. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  207. }
  208. }
  209. else if(reader.Name == "minOccurs")
  210. {
  211. try
  212. {
  213. sequence.MinOccursString = reader.Value;
  214. }
  215. catch(Exception e)
  216. {
  217. error(h,reader.Value + " is an invalid value for minOccurs",e);
  218. }
  219. }
  220. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  221. {
  222. error(h,reader.Name + " is not a valid attribute for sequence",null);
  223. }
  224. else
  225. {
  226. XmlSchemaUtil.ReadUnhandledAttribute(reader,sequence);
  227. }
  228. }
  229. reader.MoveToElement();
  230. if(reader.IsEmptyElement)
  231. return sequence;
  232. // Content: (annotation?, (element | group | choice | sequence | any)*)
  233. int level = 1;
  234. while(reader.ReadNextElement())
  235. {
  236. if(reader.NodeType == XmlNodeType.EndElement)
  237. {
  238. if(reader.LocalName != xmlname)
  239. error(h,"Should not happen :2: XmlSchemaSequence.Read, name="+reader.Name,null);
  240. break;
  241. }
  242. if(level <= 1 && reader.LocalName == "annotation")
  243. {
  244. level = 2; //Only one annotation
  245. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  246. if(annotation != null)
  247. sequence.Annotation = annotation;
  248. continue;
  249. }
  250. if(level <=2)
  251. {
  252. if(reader.LocalName == "element")
  253. {
  254. level = 2;
  255. XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
  256. if(element != null)
  257. sequence.items.Add(element);
  258. continue;
  259. }
  260. if(reader.LocalName == "group")
  261. {
  262. level = 2;
  263. XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
  264. if(group != null)
  265. sequence.items.Add(group);
  266. continue;
  267. }
  268. if(reader.LocalName == "choice")
  269. {
  270. level = 2;
  271. XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
  272. if(choice != null)
  273. sequence.items.Add(choice);
  274. continue;
  275. }
  276. if(reader.LocalName == "sequence")
  277. {
  278. level = 2;
  279. XmlSchemaSequence seq = XmlSchemaSequence.Read(reader,h);
  280. if(seq != null)
  281. sequence.items.Add(seq);
  282. continue;
  283. }
  284. if(reader.LocalName == "any")
  285. {
  286. level = 2;
  287. XmlSchemaAny any = XmlSchemaAny.Read(reader,h);
  288. if(any != null)
  289. sequence.items.Add(any);
  290. continue;
  291. }
  292. }
  293. reader.RaiseInvalidElementError();
  294. }
  295. return sequence;
  296. }
  297. }
  298. }