XmlSchemaSequence.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 XmlSchemaObjectCollection compiledItems;
  21. private static string xmlname = "sequence";
  22. public XmlSchemaSequence()
  23. {
  24. items = new XmlSchemaObjectCollection();
  25. }
  26. [XmlElement("element",typeof(XmlSchemaElement),Namespace="http://www.w3.org/2001/XMLSchema")]
  27. [XmlElement("group",typeof(XmlSchemaGroupRef),Namespace="http://www.w3.org/2001/XMLSchema")]
  28. [XmlElement("choice",typeof(XmlSchemaChoice),Namespace="http://www.w3.org/2001/XMLSchema")]
  29. [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace="http://www.w3.org/2001/XMLSchema")]
  30. [XmlElement("any",typeof(XmlSchemaAny),Namespace="http://www.w3.org/2001/XMLSchema")]
  31. public override XmlSchemaObjectCollection Items
  32. {
  33. get{ return items; }
  34. }
  35. internal XmlSchemaObjectCollection CompiledItems
  36. {
  37. get{ return compiledItems; }
  38. }
  39. [MonoTODO]
  40. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  41. {
  42. // If this is already compiled this time, simply skip.
  43. if (this.IsComplied (schema.CompilationId))
  44. return 0;
  45. XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
  46. CompileOccurence (h, schema);
  47. foreach(XmlSchemaObject obj in Items)
  48. {
  49. if(obj is XmlSchemaElement ||
  50. obj is XmlSchemaGroupRef ||
  51. obj is XmlSchemaChoice ||
  52. obj is XmlSchemaSequence ||
  53. obj is XmlSchemaAny)
  54. {
  55. errorCount += obj.Compile(h,schema);
  56. }
  57. else
  58. error(h, "Invalid schema object was specified in the particles of the sequence model group.");
  59. }
  60. this.CompilationId = schema.CompilationId;
  61. return errorCount;
  62. }
  63. [MonoTODO]
  64. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  65. {
  66. if (IsValidated (schema.CompilationId))
  67. return errorCount;
  68. compiledItems = new XmlSchemaObjectCollection ();
  69. foreach (XmlSchemaObject obj in Items) {
  70. errorCount += obj.Validate (h, schema);
  71. compiledItems.Add (obj);
  72. }
  73. ValidationId = schema.ValidationId;
  74. return errorCount;
  75. }
  76. internal override void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  77. ValidationEventHandler h, XmlSchema schema)
  78. {
  79. XmlSchemaElement el = baseParticle as XmlSchemaElement;
  80. if (el != null) {
  81. // Forbidden
  82. error (h, "Invalid sequence paricle derivation.");
  83. return;
  84. }
  85. XmlSchemaSequence seq = baseParticle as XmlSchemaSequence;
  86. if (seq != null) {
  87. // Recurse
  88. ValidateOccurenceRangeOK (seq, h, schema);
  89. // FIXME: What is the correct "order preserving" mapping?
  90. int baseIndex = 0;
  91. for (int i = 0; i < this.Items.Count; i++) {
  92. XmlSchemaParticle pd = Items [i] as XmlSchemaParticle;
  93. if (seq.Items.Count > baseIndex) {
  94. XmlSchemaParticle pb = seq.Items [baseIndex] as XmlSchemaParticle;
  95. pd.ActualParticle.ValidateDerivationByRestriction (pb.ActualParticle, h, schema);
  96. baseIndex++;
  97. }
  98. else
  99. error (h, "Invalid choice derivation by extension was found.");
  100. }
  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. }