XmlSchemaAttributeGroup.cs 7.5 KB

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