XmlSchemaAttributeGroup.cs 7.6 KB

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