XmlSchemaAttributeGroup.cs 8.6 KB

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