XmlSchemaComplexContentRestriction.cs 9.4 KB

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