XmlSchemaComplexContentRestriction.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. private static 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. /// <remarks>
  56. /// 1. base must be present
  57. /// </remarks>
  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::ComplexContentRestriction");
  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. internal override XmlQualifiedName GetBaseTypeName ()
  124. {
  125. return baseTypeName;
  126. }
  127. internal override XmlSchemaParticle GetParticle ()
  128. {
  129. return particle;
  130. }
  131. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  132. {
  133. return errorCount;
  134. }
  135. //<restriction
  136. // base = QName
  137. // id = ID
  138. // {any attributes with non-schema namespace . . .}>
  139. // Content: (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))
  140. //</restriction>
  141. internal static XmlSchemaComplexContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)
  142. {
  143. XmlSchemaComplexContentRestriction restriction = new XmlSchemaComplexContentRestriction();
  144. reader.MoveToElement();
  145. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  146. {
  147. error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);
  148. reader.Skip();
  149. return null;
  150. }
  151. restriction.LineNumber = reader.LineNumber;
  152. restriction.LinePosition = reader.LinePosition;
  153. restriction.SourceUri = reader.BaseURI;
  154. while(reader.MoveToNextAttribute())
  155. {
  156. if(reader.Name == "base")
  157. {
  158. Exception innerex;
  159. restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  160. if(innerex != null)
  161. error(h, reader.Value + " is not a valid value for base attribute",innerex);
  162. }
  163. else if(reader.Name == "id")
  164. {
  165. restriction.Id = reader.Value;
  166. }
  167. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  168. {
  169. error(h,reader.Name + " is not a valid attribute for restriction",null);
  170. }
  171. else
  172. {
  173. XmlSchemaUtil.ReadUnhandledAttribute(reader,restriction);
  174. }
  175. }
  176. reader.MoveToElement();
  177. if(reader.IsEmptyElement)
  178. return restriction;
  179. //Content: 1. annotation?,
  180. // (2.(group | all | choice | sequence)?, (3.(attribute | attributeGroup)*, 4.anyAttribute?)))
  181. int level = 1;
  182. while(reader.ReadNextElement())
  183. {
  184. if(reader.NodeType == XmlNodeType.EndElement)
  185. {
  186. if(reader.LocalName != xmlname)
  187. error(h,"Should not happen :2: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);
  188. break;
  189. }
  190. if(level <= 1 && reader.LocalName == "annotation")
  191. {
  192. level = 2; //Only one annotation
  193. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  194. if(annotation != null)
  195. restriction.Annotation = annotation;
  196. continue;
  197. }
  198. if(level <= 2)
  199. {
  200. if(reader.LocalName == "group")
  201. {
  202. level = 3;
  203. XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
  204. if(group != null)
  205. restriction.particle = group;
  206. continue;
  207. }
  208. if(reader.LocalName == "all")
  209. {
  210. level = 3;
  211. XmlSchemaAll all = XmlSchemaAll.Read(reader,h);
  212. if(all != null)
  213. restriction.particle = all;
  214. continue;
  215. }
  216. if(reader.LocalName == "choice")
  217. {
  218. level = 3;
  219. XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
  220. if(choice != null)
  221. restriction.particle = choice;
  222. continue;
  223. }
  224. if(reader.LocalName == "sequence")
  225. {
  226. level = 3;
  227. XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
  228. if(sequence != null)
  229. restriction.particle = sequence;
  230. continue;
  231. }
  232. }
  233. if(level <= 3)
  234. {
  235. if(reader.LocalName == "attribute")
  236. {
  237. level = 3;
  238. XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
  239. if(attr != null)
  240. restriction.Attributes.Add(attr);
  241. continue;
  242. }
  243. if(reader.LocalName == "attributeGroup")
  244. {
  245. level = 3;
  246. XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
  247. if(attr != null)
  248. restriction.attributes.Add(attr);
  249. continue;
  250. }
  251. }
  252. if(level <= 4 && reader.LocalName == "anyAttribute")
  253. {
  254. level = 5;
  255. XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
  256. if(anyattr != null)
  257. restriction.AnyAttribute = anyattr;
  258. continue;
  259. }
  260. reader.RaiseInvalidElementError();
  261. }
  262. return restriction;
  263. }
  264. }
  265. }