XmlSchemaComplexContentRestriction.cs 8.4 KB

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