XmlSchemaComplexContentExtension.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // System.Xml.Schema.XmlSchemaComplexContentExtension.cs
  3. //
  4. // Author:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. using System;
  9. using System.Xml;
  10. using System.Xml.Serialization;
  11. namespace System.Xml.Schema
  12. {
  13. /// <summary>
  14. /// Summary description for XmlSchemaComplexContentExtension.
  15. /// </summary>
  16. public class XmlSchemaComplexContentExtension : XmlSchemaContent
  17. {
  18. private XmlSchemaAnyAttribute any;
  19. private XmlSchemaObjectCollection attributes;
  20. private XmlQualifiedName baseTypeName;
  21. private XmlSchemaParticle particle;
  22. const string xmlname = "extension";
  23. public XmlSchemaComplexContentExtension()
  24. {
  25. attributes = new XmlSchemaObjectCollection();
  26. baseTypeName = XmlQualifiedName.Empty;
  27. }
  28. [System.Xml.Serialization.XmlAttribute("base")]
  29. public XmlQualifiedName BaseTypeName
  30. {
  31. get{ return baseTypeName; }
  32. set{ baseTypeName = value; }
  33. }
  34. [XmlElement("group",typeof(XmlSchemaGroupRef),Namespace=XmlSchema.Namespace)]
  35. [XmlElement("all",typeof(XmlSchemaAll),Namespace=XmlSchema.Namespace)]
  36. [XmlElement("choice",typeof(XmlSchemaChoice),Namespace=XmlSchema.Namespace)]
  37. [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace=XmlSchema.Namespace)]
  38. public XmlSchemaParticle Particle
  39. {
  40. get{ return particle; }
  41. set{ particle = value; }
  42. }
  43. [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace=XmlSchema.Namespace)]
  44. [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace=XmlSchema.Namespace)]
  45. public XmlSchemaObjectCollection Attributes
  46. {
  47. get{ return attributes; }
  48. }
  49. [XmlElement("anyAttribute",Namespace=XmlSchema.Namespace)]
  50. public XmlSchemaAnyAttribute AnyAttribute
  51. {
  52. get{ return any; }
  53. set{ any = value;}
  54. }
  55. // internal properties
  56. internal override bool IsExtension {
  57. get { return true; }
  58. }
  59. /// <remarks>
  60. /// </remarks>
  61. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  62. {
  63. // If this is already compiled this time, simply skip.
  64. if (this.IsComplied (schema.CompilationId))
  65. return 0;
  66. if (this.isRedefinedComponent) {
  67. if (Annotation != null)
  68. Annotation.isRedefinedComponent = true;
  69. if (AnyAttribute != null)
  70. AnyAttribute.isRedefinedComponent = true;
  71. foreach (XmlSchemaObject obj in Attributes)
  72. obj.isRedefinedComponent = true;
  73. if (Particle != null)
  74. Particle.isRedefinedComponent = true;
  75. }
  76. if(BaseTypeName == null || BaseTypeName.IsEmpty)
  77. {
  78. error(h, "base must be present, as a QName");
  79. }
  80. else if(!XmlSchemaUtil.CheckQName(BaseTypeName))
  81. error(h,"BaseTypeName is not a valid XmlQualifiedName");
  82. if(this.AnyAttribute != null)
  83. {
  84. errorCount += AnyAttribute.Compile(h, schema);
  85. }
  86. foreach(XmlSchemaObject obj in Attributes)
  87. {
  88. if(obj is XmlSchemaAttribute)
  89. {
  90. XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
  91. errorCount += attr.Compile(h, schema);
  92. }
  93. else if(obj is XmlSchemaAttributeGroupRef)
  94. {
  95. XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;
  96. errorCount += atgrp.Compile(h, schema);
  97. }
  98. else
  99. error(h,obj.GetType() +" is not valid in this place::ComplexConetnetExtension");
  100. }
  101. if(Particle != null)
  102. {
  103. if(Particle is XmlSchemaGroupRef)
  104. {
  105. errorCount += ((XmlSchemaGroupRef)Particle).Compile(h, schema);
  106. }
  107. else if(Particle is XmlSchemaAll)
  108. {
  109. errorCount += ((XmlSchemaAll)Particle).Compile(h, schema);
  110. }
  111. else if(Particle is XmlSchemaChoice)
  112. {
  113. errorCount += ((XmlSchemaChoice)Particle).Compile(h, schema);
  114. }
  115. else if(Particle is XmlSchemaSequence)
  116. {
  117. errorCount += ((XmlSchemaSequence)Particle).Compile(h, schema);
  118. }
  119. else
  120. error (h, "Particle of a restriction is limited only to group, sequence, choice and all.");
  121. }
  122. XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
  123. this.CompilationId = schema.CompilationId;
  124. return errorCount;
  125. }
  126. internal override XmlQualifiedName GetBaseTypeName ()
  127. {
  128. return baseTypeName;
  129. }
  130. internal override XmlSchemaParticle GetParticle ()
  131. {
  132. return particle;
  133. }
  134. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  135. {
  136. if (IsValidated (schema.ValidationId))
  137. return errorCount;
  138. if (AnyAttribute != null)
  139. errorCount += AnyAttribute.Validate (h, schema);
  140. foreach (XmlSchemaObject attrObj in Attributes)
  141. errorCount += attrObj.Validate (h, schema);
  142. if (Particle != null)
  143. errorCount += Particle.Validate (h, schema);
  144. ValidationId = schema.ValidationId;
  145. return errorCount;
  146. }
  147. //<extension
  148. // base = QName
  149. // id = ID
  150. // {any attributes with non-schema namespace . . .}>
  151. // Content: (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))
  152. //</extension>
  153. internal static XmlSchemaComplexContentExtension Read(XmlSchemaReader reader, ValidationEventHandler h)
  154. {
  155. XmlSchemaComplexContentExtension extension = new XmlSchemaComplexContentExtension();
  156. reader.MoveToElement();
  157. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  158. {
  159. error(h,"Should not happen :1: XmlSchemaComplexContentExtension.Read, name="+reader.Name,null);
  160. reader.Skip();
  161. return null;
  162. }
  163. extension.LineNumber = reader.LineNumber;
  164. extension.LinePosition = reader.LinePosition;
  165. extension.SourceUri = reader.BaseURI;
  166. while(reader.MoveToNextAttribute())
  167. {
  168. if(reader.Name == "base")
  169. {
  170. Exception innerex;
  171. extension.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  172. if(innerex != null)
  173. error(h, reader.Value + " is not a valid value for base attribute",innerex);
  174. }
  175. else if(reader.Name == "id")
  176. {
  177. extension.Id = 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 extension",null);
  182. }
  183. else
  184. {
  185. XmlSchemaUtil.ReadUnhandledAttribute(reader,extension);
  186. }
  187. }
  188. reader.MoveToElement();
  189. if(reader.IsEmptyElement)
  190. return extension;
  191. //Content: 1. annotation?,
  192. // (2.(group | all | choice | sequence)?, (3.(attribute | attributeGroup)*, 4.anyAttribute?)))
  193. int level = 1;
  194. while(reader.ReadNextElement())
  195. {
  196. if(reader.NodeType == XmlNodeType.EndElement)
  197. {
  198. if(reader.LocalName != xmlname)
  199. error(h,"Should not happen :2: XmlSchemaComplexContentExtension.Read, name="+reader.Name,null);
  200. break;
  201. }
  202. if(level <= 1 && reader.LocalName == "annotation")
  203. {
  204. level = 2; //Only one annotation
  205. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  206. if(annotation != null)
  207. extension.Annotation = annotation;
  208. continue;
  209. }
  210. if(level <= 2)
  211. {
  212. if(reader.LocalName == "group")
  213. {
  214. level = 3;
  215. XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
  216. if(group != null)
  217. extension.particle = group;
  218. continue;
  219. }
  220. if(reader.LocalName == "all")
  221. {
  222. level = 3;
  223. XmlSchemaAll all = XmlSchemaAll.Read(reader,h);
  224. if(all != null)
  225. extension.particle = all;
  226. continue;
  227. }
  228. if(reader.LocalName == "choice")
  229. {
  230. level = 3;
  231. XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
  232. if(choice != null)
  233. extension.particle = choice;
  234. continue;
  235. }
  236. if(reader.LocalName == "sequence")
  237. {
  238. level = 3;
  239. XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
  240. if(sequence != null)
  241. extension.particle = sequence;
  242. continue;
  243. }
  244. }
  245. if(level <= 3)
  246. {
  247. if(reader.LocalName == "attribute")
  248. {
  249. level = 3;
  250. XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
  251. if(attr != null)
  252. extension.Attributes.Add(attr);
  253. continue;
  254. }
  255. if(reader.LocalName == "attributeGroup")
  256. {
  257. level = 3;
  258. XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
  259. if(attr != null)
  260. extension.attributes.Add(attr);
  261. continue;
  262. }
  263. }
  264. if(level <= 4 && reader.LocalName == "anyAttribute")
  265. {
  266. level = 5;
  267. XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
  268. if(anyattr != null)
  269. extension.AnyAttribute = anyattr;
  270. continue;
  271. }
  272. reader.RaiseInvalidElementError();
  273. }
  274. return extension;
  275. }
  276. }
  277. }