XmlSchemaChoice.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. using System;
  4. using System.Xml.Serialization;
  5. using System.Xml;
  6. namespace System.Xml.Schema
  7. {
  8. /// <summary>
  9. /// Summary description for XmlSchemaAll.
  10. /// </summary>
  11. public class XmlSchemaChoice : XmlSchemaGroupBase
  12. {
  13. private XmlSchemaObjectCollection items;
  14. private static string xmlname = "choice";
  15. public XmlSchemaChoice()
  16. {
  17. items = new XmlSchemaObjectCollection();
  18. }
  19. [XmlElement("element",typeof(XmlSchemaElement),Namespace="http://www.w3.org/2001/XMLSchema")]
  20. [XmlElement("group",typeof(XmlSchemaGroupRef),Namespace="http://www.w3.org/2001/XMLSchema")]
  21. [XmlElement("choice",typeof(XmlSchemaChoice),Namespace="http://www.w3.org/2001/XMLSchema")]
  22. [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace="http://www.w3.org/2001/XMLSchema")]
  23. [XmlElement("any",typeof(XmlSchemaAny),Namespace="http://www.w3.org/2001/XMLSchema")]
  24. public override XmlSchemaObjectCollection Items
  25. {
  26. get{ return items; }
  27. }
  28. [MonoTODO]
  29. internal int Compile(ValidationEventHandler h, XmlSchemaInfo info)
  30. {
  31. //FIXME: Should we reset the values
  32. if(MinOccurs > MaxOccurs)
  33. error(h,"minOccurs must be less than or equal to maxOccurs");
  34. XmlSchemaUtil.CompileID(Id, this, info.IDCollection, h);
  35. foreach(XmlSchemaObject obj in Items)
  36. {
  37. if(obj is XmlSchemaElement)
  38. {
  39. errorCount += ((XmlSchemaElement)obj).Compile(h,info);
  40. }
  41. else if(obj is XmlSchemaGroupRef)
  42. {
  43. errorCount += ((XmlSchemaGroupRef)obj).Compile(h,info);
  44. }
  45. else if(obj is XmlSchemaChoice)
  46. {
  47. errorCount += ((XmlSchemaChoice)obj).Compile(h,info);
  48. }
  49. else if(obj is XmlSchemaSequence)
  50. {
  51. errorCount += ((XmlSchemaSequence)obj).Compile(h,info);
  52. }
  53. else if(obj is XmlSchemaAny)
  54. {
  55. errorCount += ((XmlSchemaAny)obj).Compile(h,info);
  56. }
  57. }
  58. return errorCount;
  59. }
  60. [MonoTODO]
  61. internal int Validate(ValidationEventHandler h)
  62. {
  63. return errorCount;
  64. }
  65. //<choice
  66. // id = ID
  67. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  68. // minOccurs = nonNegativeInteger : 1
  69. // {any attributes with non-schema namespace . . .}>
  70. // Content: (annotation?, (element | group | choice | sequence | any)*)
  71. //</choice>
  72. internal static XmlSchemaChoice Read(XmlSchemaReader reader, ValidationEventHandler h)
  73. {
  74. XmlSchemaChoice choice = new XmlSchemaChoice();
  75. reader.MoveToElement();
  76. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  77. {
  78. error(h,"Should not happen :1: XmlSchemaChoice.Read, name="+reader.Name,null);
  79. reader.SkipToEnd();
  80. return null;
  81. }
  82. choice.LineNumber = reader.LineNumber;
  83. choice.LinePosition = reader.LinePosition;
  84. choice.SourceUri = reader.BaseURI;
  85. while(reader.MoveToNextAttribute())
  86. {
  87. if(reader.Name == "id")
  88. {
  89. choice.Id = reader.Value;
  90. }
  91. else if(reader.Name == "maxOccurs")
  92. {
  93. try
  94. {
  95. choice.MaxOccursString = reader.Value;
  96. }
  97. catch(Exception e)
  98. {
  99. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  100. }
  101. }
  102. else if(reader.Name == "minOccurs")
  103. {
  104. try
  105. {
  106. choice.MinOccursString = reader.Value;
  107. }
  108. catch(Exception e)
  109. {
  110. error(h,reader.Value + " is an invalid value for minOccurs",e);
  111. }
  112. }
  113. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  114. {
  115. error(h,reader.Name + " is not a valid attribute for choice",null);
  116. }
  117. else
  118. {
  119. XmlSchemaUtil.ReadUnhandledAttribute(reader,choice);
  120. }
  121. }
  122. reader.MoveToElement();
  123. if(reader.IsEmptyElement)
  124. return choice;
  125. // Content: (annotation?, (element | group | choice | sequence | any)*)
  126. int level = 1;
  127. while(reader.ReadNextElement())
  128. {
  129. if(reader.NodeType == XmlNodeType.EndElement)
  130. {
  131. if(reader.LocalName != xmlname)
  132. error(h,"Should not happen :2: XmlSchemaChoice.Read, name="+reader.Name,null);
  133. break;
  134. }
  135. if(level <= 1 && reader.LocalName == "annotation")
  136. {
  137. level = 2; //Only one annotation
  138. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  139. if(annotation != null)
  140. choice.Annotation = annotation;
  141. continue;
  142. }
  143. if(level <=2)
  144. {
  145. if(reader.LocalName == "element")
  146. {
  147. level = 2;
  148. XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
  149. if(element != null)
  150. choice.items.Add(element);
  151. continue;
  152. }
  153. if(reader.LocalName == "group")
  154. {
  155. level = 2;
  156. XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
  157. if(group != null)
  158. choice.items.Add(group);
  159. continue;
  160. }
  161. if(reader.LocalName == "choice")
  162. {
  163. level = 2;
  164. XmlSchemaChoice ch = XmlSchemaChoice.Read(reader,h);
  165. if(ch != null)
  166. choice.items.Add(ch);
  167. continue;
  168. }
  169. if(reader.LocalName == "sequence")
  170. {
  171. level = 2;
  172. XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
  173. if(sequence != null)
  174. choice.items.Add(sequence);
  175. continue;
  176. }
  177. if(reader.LocalName == "any")
  178. {
  179. level = 2;
  180. XmlSchemaAny any = XmlSchemaAny.Read(reader,h);
  181. if(any != null)
  182. choice.items.Add(any);
  183. continue;
  184. }
  185. }
  186. reader.RaiseInvalidElementError();
  187. }
  188. return choice;
  189. }
  190. }
  191. }