XmlSchemaAttributeGroup.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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))]
  60. [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef))]
  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")]
  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. #if NET_2_0
  120. this.AnyAttribute.Parent = this;
  121. #endif
  122. errorCount += this.AnyAttribute.Compile(h, schema);
  123. }
  124. foreach(XmlSchemaObject obj in Attributes)
  125. {
  126. #if NET_2_0
  127. obj.Parent = this;
  128. #endif
  129. if(obj is XmlSchemaAttribute)
  130. {
  131. XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
  132. errorCount += attr.Compile(h, schema);
  133. }
  134. else if(obj is XmlSchemaAttributeGroupRef)
  135. {
  136. XmlSchemaAttributeGroupRef gref = (XmlSchemaAttributeGroupRef) obj;
  137. errorCount += gref.Compile(h, schema);
  138. }
  139. else
  140. {
  141. error(h,"invalid type of object in Attributes property");
  142. }
  143. }
  144. this.CompilationId = schema.CompilationId;
  145. return errorCount;
  146. }
  147. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  148. {
  149. if (IsValidated (schema.CompilationId))
  150. return errorCount;
  151. if (redefined == null && redefinedObject != null) {
  152. redefinedObject.Compile (h, schema);
  153. redefined = (XmlSchemaAttributeGroup) redefinedObject;
  154. redefined.Validate (h, schema);
  155. }
  156. XmlSchemaObjectCollection actualAttributes = null;
  157. /*
  158. if (this.redefined != null) {
  159. actualAttributes = new XmlSchemaObjectCollection ();
  160. foreach (XmlSchemaObject obj in Attributes) {
  161. XmlSchemaAttributeGroupRef grp = obj as XmlSchemaAttributeGroupRef;
  162. if (grp != null && grp.QualifiedName == this.QualifiedName)
  163. actualAttributes.Add (redefined);
  164. else
  165. actualAttributes.Add (obj);
  166. }
  167. }
  168. else
  169. */
  170. actualAttributes = Attributes;
  171. attributeUses = new XmlSchemaObjectTable ();
  172. errorCount += XmlSchemaUtil.ValidateAttributesResolved (attributeUses,
  173. h, schema, actualAttributes, AnyAttribute,
  174. ref anyAttributeUse, redefined, false);
  175. ValidationId = schema.ValidationId;
  176. return errorCount;
  177. }
  178. //<attributeGroup
  179. // id = ID
  180. // name = NCName
  181. // ref = QName // Not present in this class.
  182. // {any attributes with non-schema namespace . . .}>
  183. // Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))
  184. //</attributeGroup>
  185. internal static XmlSchemaAttributeGroup Read(XmlSchemaReader reader, ValidationEventHandler h)
  186. {
  187. XmlSchemaAttributeGroup attrgrp = new XmlSchemaAttributeGroup();
  188. reader.MoveToElement();
  189. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  190. {
  191. error(h,"Should not happen :1: XmlSchemaAttributeGroup.Read, name="+reader.Name,null);
  192. reader.SkipToEnd();
  193. return null;
  194. }
  195. attrgrp.LineNumber = reader.LineNumber;
  196. attrgrp.LinePosition = reader.LinePosition;
  197. attrgrp.SourceUri = reader.BaseURI;
  198. while(reader.MoveToNextAttribute())
  199. {
  200. if(reader.Name == "id")
  201. {
  202. attrgrp.Id = reader.Value;
  203. }
  204. else if(reader.Name == "name")
  205. {
  206. attrgrp.name = reader.Value;
  207. }
  208. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  209. {
  210. error(h,reader.Name + " is not a valid attribute for attributeGroup in this context",null);
  211. }
  212. else
  213. {
  214. XmlSchemaUtil.ReadUnhandledAttribute(reader,attrgrp);
  215. }
  216. }
  217. reader.MoveToElement();
  218. if(reader.IsEmptyElement)
  219. return attrgrp;
  220. //Content: 1.annotation?, 2.(attribute | attributeGroup)*, 3.anyAttribute?
  221. int level = 1;
  222. while(reader.ReadNextElement())
  223. {
  224. if(reader.NodeType == XmlNodeType.EndElement)
  225. {
  226. if(reader.LocalName != xmlname)
  227. error(h,"Should not happen :2: XmlSchemaAttributeGroup.Read, name="+reader.Name,null);
  228. break;
  229. }
  230. if(level <= 1 && reader.LocalName == "annotation")
  231. {
  232. level = 2; //Only one annotation
  233. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  234. if(annotation != null)
  235. attrgrp.Annotation = annotation;
  236. continue;
  237. }
  238. if(level <= 2)
  239. {
  240. if(reader.LocalName == "attribute")
  241. {
  242. level = 2;
  243. XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
  244. if(attr != null)
  245. attrgrp.Attributes.Add(attr);
  246. continue;
  247. }
  248. if(reader.LocalName == "attributeGroup")
  249. {
  250. level = 2;
  251. XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
  252. if(attr != null)
  253. attrgrp.attributes.Add(attr);
  254. continue;
  255. }
  256. }
  257. if(level <= 3 && reader.LocalName == "anyAttribute")
  258. {
  259. level = 4;
  260. XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
  261. if(anyattr != null)
  262. attrgrp.AnyAttribute = anyattr;
  263. continue;
  264. }
  265. reader.RaiseInvalidElementError();
  266. }
  267. return attrgrp;
  268. }
  269. }
  270. }