XmlSchemaChoice.cs 8.0 KB

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