XmlSchemaAll.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. using System;
  4. using System.Xml;
  5. using System.Xml.Serialization;
  6. namespace System.Xml.Schema
  7. {
  8. /// <summary>
  9. /// Summary description for XmlSchemaAll.
  10. /// </summary>
  11. public class XmlSchemaAll : XmlSchemaGroupBase
  12. {
  13. private XmlSchemaObjectCollection items;
  14. private static string xmlname = "all";
  15. public XmlSchemaAll()
  16. {
  17. items = new XmlSchemaObjectCollection();
  18. }
  19. [XmlElement("element",typeof(XmlSchemaElement),Namespace=XmlSchema.Namespace)]
  20. public override XmlSchemaObjectCollection Items
  21. {
  22. get{ return items; }
  23. }
  24. /// <remarks>
  25. /// 1. MaxOccurs must be one. (default is also one)
  26. /// 2. MinOccurs must be zero or one.
  27. /// </remarks>
  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 on error??
  35. if(MaxOccurs != Decimal.One)
  36. error(h,"maxOccurs must be 1");
  37. if(MinOccurs != Decimal.One && MinOccurs != Decimal.Zero)
  38. error(h,"minOccurs must be 0 or 1");
  39. XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
  40. foreach(XmlSchemaObject obj in Items)
  41. {
  42. if(obj is XmlSchemaElement)
  43. {
  44. XmlSchemaElement elem = (XmlSchemaElement)obj;
  45. if(elem.MaxOccurs != Decimal.One && elem.MaxOccurs != Decimal.Zero)
  46. {
  47. elem.error(h,"The {max occurs} of all the elements of 'all' must be 0 or 1. ");
  48. }
  49. errorCount += elem.Compile(h, schema);
  50. }
  51. else
  52. {
  53. error(h,"XmlSchemaAll can only contain Items of type Element");
  54. }
  55. }
  56. this.CompilationId = schema.CompilationId;
  57. return errorCount;
  58. }
  59. [MonoTODO]
  60. internal int Validate(ValidationEventHandler h)
  61. {
  62. return errorCount;
  63. }
  64. //<all
  65. // id = ID
  66. // maxOccurs = 1 : 1
  67. // minOccurs = (0 | 1) : 1
  68. // {any attributes with non-schema namespace . . .}>
  69. // Content: (annotation?, element*)
  70. //</all>
  71. internal static XmlSchemaAll Read(XmlSchemaReader reader, ValidationEventHandler h)
  72. {
  73. XmlSchemaAll all = new XmlSchemaAll();
  74. reader.MoveToElement();
  75. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  76. {
  77. error(h,"Should not happen :1: XmlSchemaAll.Read, name="+reader.Name,null);
  78. reader.SkipToEnd();
  79. return null;
  80. }
  81. all.LineNumber = reader.LineNumber;
  82. all.LinePosition = reader.LinePosition;
  83. all.SourceUri = reader.BaseURI;
  84. //Read Attributes
  85. while(reader.MoveToNextAttribute())
  86. {
  87. if(reader.Name == "id")
  88. {
  89. all.Id = reader.Value;
  90. }
  91. else if(reader.Name == "maxOccurs")
  92. {
  93. try
  94. {
  95. all.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. all.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 all",null);
  116. }
  117. else
  118. {
  119. XmlSchemaUtil.ReadUnhandledAttribute(reader,all);
  120. }
  121. }
  122. reader.MoveToElement();
  123. if(reader.IsEmptyElement)
  124. return all;
  125. //Content: (annotation?, element*)
  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: XmlSchemaAll.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. all.Annotation = annotation;
  141. continue;
  142. }
  143. if(level <=2 && reader.LocalName == "element")
  144. {
  145. level = 2;
  146. XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
  147. if(element != null)
  148. all.items.Add(element);
  149. continue;
  150. }
  151. reader.RaiseInvalidElementError();
  152. }
  153. return all;
  154. }
  155. }
  156. }