2
0

XmlSchemaAll.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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="http://www.w3.org/2001/XMLSchema")]
  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, XmlSchemaInfo info)
  30. {
  31. //FIXME: Should we reset the values on error??
  32. if(MaxOccurs != Decimal.One)
  33. error(h,"maxOccurs must be 1");
  34. if(MinOccurs != Decimal.One && MinOccurs != Decimal.Zero)
  35. error(h,"minOccurs must be 0 or 1");
  36. XmlSchemaUtil.CompileID(Id, this, info.IDCollection, h);
  37. foreach(XmlSchemaObject obj in Items)
  38. {
  39. if(obj is XmlSchemaElement)
  40. {
  41. XmlSchemaElement elem = (XmlSchemaElement)obj;
  42. if(elem.MaxOccurs != Decimal.One && elem.MaxOccurs != Decimal.Zero)
  43. {
  44. elem.error(h,"The {max occurs} of all the elements of 'all' must be 0 or 1. ");
  45. }
  46. errorCount += elem.Compile(h,info);
  47. }
  48. else
  49. {
  50. error(h,"XmlSchemaAll can only contain Items of type Element");
  51. }
  52. }
  53. return errorCount;
  54. }
  55. [MonoTODO]
  56. internal int Validate(ValidationEventHandler h)
  57. {
  58. return errorCount;
  59. }
  60. //<all
  61. // id = ID
  62. // maxOccurs = 1 : 1
  63. // minOccurs = (0 | 1) : 1
  64. // {any attributes with non-schema namespace . . .}>
  65. // Content: (annotation?, element*)
  66. //</all>
  67. internal static XmlSchemaAll Read(XmlSchemaReader reader, ValidationEventHandler h)
  68. {
  69. XmlSchemaAll all = new XmlSchemaAll();
  70. reader.MoveToElement();
  71. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  72. {
  73. error(h,"Should not happen :1: XmlSchemaAll.Read, name="+reader.Name,null);
  74. reader.SkipToEnd();
  75. return null;
  76. }
  77. all.LineNumber = reader.LineNumber;
  78. all.LinePosition = reader.LinePosition;
  79. all.SourceUri = reader.BaseURI;
  80. //Read Attributes
  81. while(reader.MoveToNextAttribute())
  82. {
  83. if(reader.Name == "id")
  84. {
  85. all.Id = reader.Value;
  86. }
  87. else if(reader.Name == "maxOccurs")
  88. {
  89. try
  90. {
  91. all.MaxOccursString = reader.Value;
  92. }
  93. catch(Exception e)
  94. {
  95. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  96. }
  97. }
  98. else if(reader.Name == "minOccurs")
  99. {
  100. try
  101. {
  102. all.MinOccursString = reader.Value;
  103. }
  104. catch(Exception e)
  105. {
  106. error(h,reader.Value + " is an invalid value for minOccurs",e);
  107. }
  108. }
  109. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  110. {
  111. error(h,reader.Name + " is not a valid attribute for all",null);
  112. }
  113. else
  114. {
  115. XmlSchemaUtil.ReadUnhandledAttribute(reader,all);
  116. }
  117. }
  118. reader.MoveToElement();
  119. if(reader.IsEmptyElement)
  120. return all;
  121. //Content: (annotation?, element*)
  122. int level = 1;
  123. while(reader.ReadNextElement())
  124. {
  125. if(reader.NodeType == XmlNodeType.EndElement)
  126. {
  127. if(reader.LocalName != xmlname)
  128. error(h,"Should not happen :2: XmlSchemaAll.Read, name="+reader.Name,null);
  129. break;
  130. }
  131. if(level <= 1 && reader.LocalName == "annotation")
  132. {
  133. level = 2; //Only one annotation
  134. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  135. if(annotation != null)
  136. all.Annotation = annotation;
  137. continue;
  138. }
  139. if(level <=2 && reader.LocalName == "element")
  140. {
  141. level = 2;
  142. XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
  143. if(element != null)
  144. all.items.Add(element);
  145. continue;
  146. }
  147. reader.RaiseInvalidElementError();
  148. }
  149. return all;
  150. }
  151. }
  152. }