XmlSchemaAttributeGroup.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // System.Xml.Schema.XmlSchemaAttributeGroup.cs
  3. //
  4. // Authors:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Enomoto, Atsushi [email protected]
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Xml.Serialization;
  31. using System.Xml;
  32. namespace System.Xml.Schema
  33. {
  34. /// <summary>
  35. /// Summary description for XmlSchemaAttributeGroup.
  36. /// </summary>
  37. public class XmlSchemaAttributeGroup : XmlSchemaAnnotated
  38. {
  39. private XmlSchemaAnyAttribute anyAttribute;
  40. private XmlSchemaObjectCollection attributes;
  41. private string name;
  42. private XmlSchemaAttributeGroup redefined;
  43. private XmlQualifiedName qualifiedName;
  44. const string xmlname = "attributeGroup";
  45. private XmlSchemaObjectTable attributeUses;
  46. private XmlSchemaAnyAttribute anyAttributeUse;
  47. internal bool AttributeGroupRecursionCheck;
  48. public XmlSchemaAttributeGroup()
  49. {
  50. attributes = new XmlSchemaObjectCollection();
  51. qualifiedName = XmlQualifiedName.Empty;
  52. }
  53. [System.Xml.Serialization.XmlAttribute("name")]
  54. public string Name
  55. {
  56. get{ return name;}
  57. set{ name = value;}
  58. }
  59. [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace=XmlSchema.Namespace)]
  60. [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace=XmlSchema.Namespace)]
  61. public XmlSchemaObjectCollection Attributes
  62. {
  63. get{ return attributes;}
  64. }
  65. internal XmlSchemaObjectTable AttributeUses
  66. {
  67. get { return attributeUses; }
  68. }
  69. internal XmlSchemaAnyAttribute AnyAttributeUse
  70. {
  71. get { return anyAttributeUse; }
  72. }
  73. [XmlElement("anyAttribute",Namespace=XmlSchema.Namespace)]
  74. public XmlSchemaAnyAttribute AnyAttribute
  75. {
  76. get{ return anyAttribute;}
  77. set{ anyAttribute = value;}
  78. }
  79. //Undocumented property
  80. [XmlIgnore]
  81. public XmlSchemaAttributeGroup RedefinedAttributeGroup
  82. {
  83. get{ return redefined;}
  84. }
  85. [XmlIgnore]
  86. #if NET_2_0
  87. public XmlQualifiedName QualifiedName
  88. #else
  89. internal XmlQualifiedName QualifiedName
  90. #endif
  91. {
  92. get{ return qualifiedName;}
  93. }
  94. /// <remarks>
  95. /// An Attribute group can only be defined as a child of XmlSchema or in XmlSchemaRedefine.
  96. /// The other attributeGroup has type XmlSchemaAttributeGroupRef.
  97. /// 1. Name must be present
  98. /// </remarks>
  99. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  100. {
  101. // If this is already compiled this time, simply skip.
  102. if (this.IsComplied (schema.CompilationId))
  103. return errorCount;
  104. errorCount = 0;
  105. if (redefinedObject != null) {
  106. errorCount += redefined.Compile (h, schema);
  107. if (errorCount == 0)
  108. redefined = (XmlSchemaAttributeGroup) redefinedObject;
  109. }
  110. XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
  111. if(this.Name == null || this.Name == String.Empty) //1
  112. error(h,"Name is required in top level simpletype");
  113. else if(!XmlSchemaUtil.CheckNCName(this.Name)) // b.1.2
  114. error(h,"name attribute of a simpleType must be NCName");
  115. else
  116. this.qualifiedName = new XmlQualifiedName(this.Name, schema.TargetNamespace);
  117. if(this.AnyAttribute != null)
  118. {
  119. errorCount += this.AnyAttribute.Compile(h, schema);
  120. }
  121. foreach(XmlSchemaObject obj in Attributes)
  122. {
  123. if(obj is XmlSchemaAttribute)
  124. {
  125. XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
  126. errorCount += attr.Compile(h, schema);
  127. }
  128. else if(obj is XmlSchemaAttributeGroupRef)
  129. {
  130. XmlSchemaAttributeGroupRef gref = (XmlSchemaAttributeGroupRef) obj;
  131. errorCount += gref.Compile(h, schema);
  132. }
  133. else
  134. {
  135. error(h,"invalid type of object in Attributes property");
  136. }
  137. }
  138. this.CompilationId = schema.CompilationId;
  139. return errorCount;
  140. }
  141. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  142. {
  143. if (IsValidated (schema.CompilationId))
  144. return errorCount;
  145. if (redefined == null && redefinedObject != null) {
  146. redefinedObject.Compile (h, schema);
  147. redefined = (XmlSchemaAttributeGroup) redefinedObject;
  148. redefined.Validate (h, schema);
  149. }
  150. XmlSchemaObjectCollection actualAttributes = null;
  151. /*
  152. if (this.redefined != null) {
  153. actualAttributes = new XmlSchemaObjectCollection ();
  154. foreach (XmlSchemaObject obj in Attributes) {
  155. XmlSchemaAttributeGroupRef grp = obj as XmlSchemaAttributeGroupRef;
  156. if (grp != null && grp.QualifiedName == this.QualifiedName)
  157. actualAttributes.Add (redefined);
  158. else
  159. actualAttributes.Add (obj);
  160. }
  161. }
  162. else
  163. */
  164. actualAttributes = Attributes;
  165. attributeUses = new XmlSchemaObjectTable ();
  166. errorCount += XmlSchemaUtil.ValidateAttributesResolved (attributeUses,
  167. h, schema, actualAttributes, AnyAttribute,
  168. ref anyAttributeUse, redefined);
  169. ValidationId = schema.ValidationId;
  170. return errorCount;
  171. }
  172. //<attributeGroup
  173. // id = ID
  174. // name = NCName
  175. // ref = QName // Not present in this class.
  176. // {any attributes with non-schema namespace . . .}>
  177. // Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))
  178. //</attributeGroup>
  179. internal static XmlSchemaAttributeGroup Read(XmlSchemaReader reader, ValidationEventHandler h)
  180. {
  181. XmlSchemaAttributeGroup attrgrp = new XmlSchemaAttributeGroup();
  182. reader.MoveToElement();
  183. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  184. {
  185. error(h,"Should not happen :1: XmlSchemaAttributeGroup.Read, name="+reader.Name,null);
  186. reader.SkipToEnd();
  187. return null;
  188. }
  189. attrgrp.LineNumber = reader.LineNumber;
  190. attrgrp.LinePosition = reader.LinePosition;
  191. attrgrp.SourceUri = reader.BaseURI;
  192. while(reader.MoveToNextAttribute())
  193. {
  194. if(reader.Name == "id")
  195. {
  196. attrgrp.Id = reader.Value;
  197. }
  198. else if(reader.Name == "name")
  199. {
  200. attrgrp.name = reader.Value;
  201. }
  202. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  203. {
  204. error(h,reader.Name + " is not a valid attribute for attributeGroup in this context",null);
  205. }
  206. else
  207. {
  208. XmlSchemaUtil.ReadUnhandledAttribute(reader,attrgrp);
  209. }
  210. }
  211. reader.MoveToElement();
  212. if(reader.IsEmptyElement)
  213. return attrgrp;
  214. //Content: 1.annotation?, 2.(attribute | attributeGroup)*, 3.anyAttribute?
  215. int level = 1;
  216. while(reader.ReadNextElement())
  217. {
  218. if(reader.NodeType == XmlNodeType.EndElement)
  219. {
  220. if(reader.LocalName != xmlname)
  221. error(h,"Should not happen :2: XmlSchemaAttributeGroup.Read, name="+reader.Name,null);
  222. break;
  223. }
  224. if(level <= 1 && reader.LocalName == "annotation")
  225. {
  226. level = 2; //Only one annotation
  227. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  228. if(annotation != null)
  229. attrgrp.Annotation = annotation;
  230. continue;
  231. }
  232. if(level <= 2)
  233. {
  234. if(reader.LocalName == "attribute")
  235. {
  236. level = 2;
  237. XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
  238. if(attr != null)
  239. attrgrp.Attributes.Add(attr);
  240. continue;
  241. }
  242. if(reader.LocalName == "attributeGroup")
  243. {
  244. level = 2;
  245. XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
  246. if(attr != null)
  247. attrgrp.attributes.Add(attr);
  248. continue;
  249. }
  250. }
  251. if(level <= 3 && reader.LocalName == "anyAttribute")
  252. {
  253. level = 4;
  254. XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
  255. if(anyattr != null)
  256. attrgrp.AnyAttribute = anyattr;
  257. continue;
  258. }
  259. reader.RaiseInvalidElementError();
  260. }
  261. return attrgrp;
  262. }
  263. }
  264. }