XmlSchemaComplexContentExtension.cs 9.7 KB

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