XmlSchemaChoice.cs 8.2 KB

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