XmlSchemaComplexContentRestriction.cs 9.4 KB

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