XmlSchemaSequence.cs 5.7 KB

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