XmlSchemaChoice.cs 7.8 KB

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