XmlSchemaAttributeGroup.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 XmlSchemaAttributeGroup.
  10. /// </summary>
  11. public class XmlSchemaAttributeGroup : XmlSchemaAnnotated
  12. {
  13. private XmlSchemaAnyAttribute any;
  14. private XmlSchemaObjectCollection attributes;
  15. private string name;
  16. private XmlSchemaAttributeGroup redefined;
  17. private XmlQualifiedName qualifiedName;
  18. private static string xmlname = "attributeGroup";
  19. public XmlSchemaAttributeGroup()
  20. {
  21. attributes = new XmlSchemaObjectCollection();
  22. }
  23. [System.Xml.Serialization.XmlAttribute("name")]
  24. public string Name
  25. {
  26. get{ return name;}
  27. set{ name = value;}
  28. }
  29. [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace=XmlSchema.Namespace)]
  30. [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace=XmlSchema.Namespace)]
  31. public XmlSchemaObjectCollection Attributes
  32. {
  33. get{ return attributes;}
  34. }
  35. [XmlElement("anyAttribute",Namespace=XmlSchema.Namespace)]
  36. public XmlSchemaAnyAttribute AnyAttribute
  37. {
  38. get{ return any;}
  39. set{ any = value;}
  40. }
  41. //Undocumented property
  42. [XmlIgnore]
  43. public XmlSchemaAttributeGroup RedefinedAttributeGroup
  44. {
  45. get{ return redefined;}
  46. }
  47. [XmlIgnore]
  48. internal XmlQualifiedName QualifiedName
  49. {
  50. get{ return qualifiedName;}
  51. }
  52. /// <remarks>
  53. /// An Attribute group can only be defined as a child of XmlSchema or in XmlSchemaRedefine.
  54. /// The other attributeGroup has type XmlSchemaAttributeGroupRef.
  55. /// 1. Name must be present
  56. /// </remarks>
  57. [MonoTODO]
  58. internal int Compile(ValidationEventHandler h, XmlSchema schema)
  59. {
  60. // If this is already compiled this time, simply skip.
  61. if (this.IsComplied (schema.CompilationId))
  62. return 0;
  63. errorCount = 0;
  64. XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
  65. if(this.Name == null) //1
  66. error(h,"Name is required in top level simpletype");
  67. else if(!XmlSchemaUtil.CheckNCName(this.Name)) // b.1.2
  68. error(h,"name attribute of a simpleType must be NCName");
  69. else
  70. this.qualifiedName = new XmlQualifiedName(this.Name, schema.TargetNamespace);
  71. if(this.AnyAttribute != null)
  72. {
  73. errorCount += this.AnyAttribute.Compile(h, schema);
  74. }
  75. foreach(XmlSchemaObject obj in Attributes)
  76. {
  77. if(obj is XmlSchemaAttribute)
  78. {
  79. XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
  80. errorCount += attr.Compile(h, schema);
  81. }
  82. else if(obj is XmlSchemaAttributeGroupRef)
  83. {
  84. XmlSchemaAttributeGroupRef gref = (XmlSchemaAttributeGroupRef) obj;
  85. errorCount += gref.Compile(h, schema);
  86. }
  87. else
  88. {
  89. error(h,"invalid type of object in Attributes property");
  90. }
  91. }
  92. this.CompilationId = schema.CompilationId;
  93. return errorCount;
  94. }
  95. [MonoTODO]
  96. internal int Validate(ValidationEventHandler h)
  97. {
  98. return errorCount;
  99. }
  100. //<attributeGroup
  101. // id = ID
  102. // name = NCName
  103. // ref = QName // Not present in this class.
  104. // {any attributes with non-schema namespace . . .}>
  105. // Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))
  106. //</attributeGroup>
  107. internal static XmlSchemaAttributeGroup Read(XmlSchemaReader reader, ValidationEventHandler h)
  108. {
  109. XmlSchemaAttributeGroup attrgrp = new XmlSchemaAttributeGroup();
  110. reader.MoveToElement();
  111. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  112. {
  113. error(h,"Should not happen :1: XmlSchemaAttributeGroup.Read, name="+reader.Name,null);
  114. reader.SkipToEnd();
  115. return null;
  116. }
  117. attrgrp.LineNumber = reader.LineNumber;
  118. attrgrp.LinePosition = reader.LinePosition;
  119. attrgrp.SourceUri = reader.BaseURI;
  120. while(reader.MoveToNextAttribute())
  121. {
  122. if(reader.Name == "id")
  123. {
  124. attrgrp.Id = reader.Value;
  125. }
  126. else if(reader.Name == "name")
  127. {
  128. attrgrp.name = reader.Value;
  129. }
  130. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  131. {
  132. error(h,reader.Name + " is not a valid attribute for attributeGroup in this context",null);
  133. }
  134. else
  135. {
  136. XmlSchemaUtil.ReadUnhandledAttribute(reader,attrgrp);
  137. }
  138. }
  139. reader.MoveToElement();
  140. if(reader.IsEmptyElement)
  141. return attrgrp;
  142. //Content: 1.annotation?, 2.(attribute | attributeGroup)*, 3.anyAttribute?
  143. int level = 1;
  144. while(reader.ReadNextElement())
  145. {
  146. if(reader.NodeType == XmlNodeType.EndElement)
  147. {
  148. if(reader.LocalName != xmlname)
  149. error(h,"Should not happen :2: XmlSchemaAttributeGroup.Read, name="+reader.Name,null);
  150. break;
  151. }
  152. if(level <= 1 && reader.LocalName == "annotation")
  153. {
  154. level = 2; //Only one annotation
  155. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  156. if(annotation != null)
  157. attrgrp.Annotation = annotation;
  158. continue;
  159. }
  160. if(level <= 2)
  161. {
  162. if(reader.LocalName == "attribute")
  163. {
  164. level = 2;
  165. XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
  166. if(attr != null)
  167. attrgrp.Attributes.Add(attr);
  168. continue;
  169. }
  170. if(reader.LocalName == "attributeGroup")
  171. {
  172. level = 2;
  173. XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
  174. if(attr != null)
  175. attrgrp.attributes.Add(attr);
  176. continue;
  177. }
  178. }
  179. if(level <= 3 && reader.LocalName == "anyAttribute")
  180. {
  181. level = 4;
  182. XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
  183. if(anyattr != null)
  184. attrgrp.AnyAttribute = anyattr;
  185. continue;
  186. }
  187. reader.RaiseInvalidElementError();
  188. }
  189. return attrgrp;
  190. }
  191. }
  192. }