XmlSchemaComplexContentExtension.cs 8.4 KB

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