XmlSchemaComplexContentExtension.cs 7.5 KB

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