XmlSchemaComplexContentExtension.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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))]
  55. [XmlElement("all",typeof(XmlSchemaAll))]
  56. [XmlElement("choice",typeof(XmlSchemaChoice))]
  57. [XmlElement("sequence",typeof(XmlSchemaSequence))]
  58. public XmlSchemaParticle Particle
  59. {
  60. get{ return particle; }
  61. set{ particle = value; }
  62. }
  63. [XmlElement("attribute",typeof(XmlSchemaAttribute))]
  64. [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef))]
  65. public XmlSchemaObjectCollection Attributes
  66. {
  67. get{ return attributes; }
  68. }
  69. [XmlElement("anyAttribute")]
  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 NET_2_0
  87. if (Particle != null)
  88. Particle.Parent = this;
  89. if (AnyAttribute != null)
  90. AnyAttribute.Parent = this;
  91. foreach (XmlSchemaObject obj in Attributes)
  92. obj.Parent = this;
  93. #endif
  94. if (this.isRedefinedComponent) {
  95. if (Annotation != null)
  96. Annotation.isRedefinedComponent = true;
  97. if (AnyAttribute != null)
  98. AnyAttribute.isRedefinedComponent = true;
  99. foreach (XmlSchemaObject obj in Attributes)
  100. obj.isRedefinedComponent = true;
  101. if (Particle != null)
  102. Particle.isRedefinedComponent = true;
  103. }
  104. if(BaseTypeName == null || BaseTypeName.IsEmpty)
  105. {
  106. error(h, "base must be present, as a QName");
  107. }
  108. else if(!XmlSchemaUtil.CheckQName(BaseTypeName))
  109. error(h,"BaseTypeName is not a valid XmlQualifiedName");
  110. if(this.AnyAttribute != null)
  111. {
  112. errorCount += AnyAttribute.Compile(h, schema);
  113. }
  114. foreach(XmlSchemaObject obj in Attributes)
  115. {
  116. if(obj is XmlSchemaAttribute)
  117. {
  118. XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
  119. errorCount += attr.Compile(h, schema);
  120. }
  121. else if(obj is XmlSchemaAttributeGroupRef)
  122. {
  123. XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;
  124. errorCount += atgrp.Compile(h, schema);
  125. }
  126. else
  127. error(h,obj.GetType() +" is not valid in this place::ComplexConetnetExtension");
  128. }
  129. if(Particle != null)
  130. {
  131. if(Particle is XmlSchemaGroupRef)
  132. {
  133. errorCount += ((XmlSchemaGroupRef)Particle).Compile(h, schema);
  134. }
  135. else if(Particle is XmlSchemaAll)
  136. {
  137. errorCount += ((XmlSchemaAll)Particle).Compile(h, schema);
  138. }
  139. else if(Particle is XmlSchemaChoice)
  140. {
  141. errorCount += ((XmlSchemaChoice)Particle).Compile(h, schema);
  142. }
  143. else if(Particle is XmlSchemaSequence)
  144. {
  145. errorCount += ((XmlSchemaSequence)Particle).Compile(h, schema);
  146. }
  147. else
  148. error (h, "Particle of a restriction is limited only to group, sequence, choice and all.");
  149. }
  150. XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
  151. this.CompilationId = schema.CompilationId;
  152. return errorCount;
  153. }
  154. internal override XmlQualifiedName GetBaseTypeName ()
  155. {
  156. return baseTypeName;
  157. }
  158. internal override XmlSchemaParticle GetParticle ()
  159. {
  160. return particle;
  161. }
  162. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  163. {
  164. if (IsValidated (schema.ValidationId))
  165. return errorCount;
  166. if (AnyAttribute != null)
  167. errorCount += AnyAttribute.Validate (h, schema);
  168. foreach (XmlSchemaObject attrObj in Attributes)
  169. errorCount += attrObj.Validate (h, schema);
  170. if (Particle != null)
  171. errorCount += Particle.Validate (h, schema);
  172. ValidationId = schema.ValidationId;
  173. return errorCount;
  174. }
  175. //<extension
  176. // base = QName
  177. // id = ID
  178. // {any attributes with non-schema namespace . . .}>
  179. // Content: (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))
  180. //</extension>
  181. internal static XmlSchemaComplexContentExtension Read(XmlSchemaReader reader, ValidationEventHandler h)
  182. {
  183. XmlSchemaComplexContentExtension extension = new XmlSchemaComplexContentExtension();
  184. reader.MoveToElement();
  185. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  186. {
  187. error(h,"Should not happen :1: XmlSchemaComplexContentExtension.Read, name="+reader.Name,null);
  188. reader.Skip();
  189. return null;
  190. }
  191. extension.LineNumber = reader.LineNumber;
  192. extension.LinePosition = reader.LinePosition;
  193. extension.SourceUri = reader.BaseURI;
  194. while(reader.MoveToNextAttribute())
  195. {
  196. if(reader.Name == "base")
  197. {
  198. Exception innerex;
  199. extension.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  200. if(innerex != null)
  201. error(h, reader.Value + " is not a valid value for base attribute",innerex);
  202. }
  203. else if(reader.Name == "id")
  204. {
  205. extension.Id = reader.Value;
  206. }
  207. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  208. {
  209. error(h,reader.Name + " is not a valid attribute for extension",null);
  210. }
  211. else
  212. {
  213. XmlSchemaUtil.ReadUnhandledAttribute(reader,extension);
  214. }
  215. }
  216. reader.MoveToElement();
  217. if(reader.IsEmptyElement)
  218. return extension;
  219. //Content: 1. annotation?,
  220. // (2.(group | all | choice | sequence)?, (3.(attribute | attributeGroup)*, 4.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: XmlSchemaComplexContentExtension.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. extension.Annotation = annotation;
  236. continue;
  237. }
  238. if(level <= 2)
  239. {
  240. if(reader.LocalName == "group")
  241. {
  242. level = 3;
  243. XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
  244. if(group != null)
  245. extension.particle = group;
  246. continue;
  247. }
  248. if(reader.LocalName == "all")
  249. {
  250. level = 3;
  251. XmlSchemaAll all = XmlSchemaAll.Read(reader,h);
  252. if(all != null)
  253. extension.particle = all;
  254. continue;
  255. }
  256. if(reader.LocalName == "choice")
  257. {
  258. level = 3;
  259. XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
  260. if(choice != null)
  261. extension.particle = choice;
  262. continue;
  263. }
  264. if(reader.LocalName == "sequence")
  265. {
  266. level = 3;
  267. XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
  268. if(sequence != null)
  269. extension.particle = sequence;
  270. continue;
  271. }
  272. }
  273. if(level <= 3)
  274. {
  275. if(reader.LocalName == "attribute")
  276. {
  277. level = 3;
  278. XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
  279. if(attr != null)
  280. extension.Attributes.Add(attr);
  281. continue;
  282. }
  283. if(reader.LocalName == "attributeGroup")
  284. {
  285. level = 3;
  286. XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
  287. if(attr != null)
  288. extension.attributes.Add(attr);
  289. continue;
  290. }
  291. }
  292. if(level <= 4 && reader.LocalName == "anyAttribute")
  293. {
  294. level = 5;
  295. XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
  296. if(anyattr != null)
  297. extension.AnyAttribute = anyattr;
  298. continue;
  299. }
  300. reader.RaiseInvalidElementError();
  301. }
  302. return extension;
  303. }
  304. }
  305. }