XmlSchemaComplexContentRestriction.cs 9.3 KB

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