XmlSchemaSequence.cs 9.4 KB

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