XmlSchemaComplexContentExtension.cs 8.6 KB

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