XmlSchemaComplexContentRestriction.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. using System;
  4. using System.Xml;
  5. using System.Xml.Serialization;
  6. namespace System.Xml.Schema
  7. {
  8. /// <summary>
  9. /// Summary description for XmlSchemaComplexContentRestriction.
  10. /// </summary>
  11. public class XmlSchemaComplexContentRestriction : XmlSchemaContent
  12. {
  13. private XmlSchemaAnyAttribute any;
  14. private XmlSchemaObjectCollection attributes;
  15. private XmlQualifiedName baseTypeName;
  16. private XmlSchemaParticle particle;
  17. private static string xmlname = "restriction";
  18. public XmlSchemaComplexContentRestriction()
  19. {
  20. baseTypeName = XmlQualifiedName.Empty;
  21. attributes = new XmlSchemaObjectCollection();
  22. }
  23. [System.Xml.Serialization.XmlAttribute("base")]
  24. public XmlQualifiedName BaseTypeName
  25. {
  26. get{ return baseTypeName; }
  27. set{ baseTypeName = value; }
  28. }
  29. [XmlElement("group",typeof(XmlSchemaGroupRef),Namespace="http://www.w3.org/2001/XMLSchema")]
  30. [XmlElement("all",typeof(XmlSchemaAll),Namespace="http://www.w3.org/2001/XMLSchema")]
  31. [XmlElement("choice",typeof(XmlSchemaChoice),Namespace="http://www.w3.org/2001/XMLSchema")]
  32. [XmlElement("sequence",typeof(XmlSchemaSequence),Namespace="http://www.w3.org/2001/XMLSchema")]
  33. public XmlSchemaParticle Particle
  34. {
  35. get{ return particle; }
  36. set{ particle = value; }
  37. }
  38. [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace="http://www.w3.org/2001/XMLSchema")]
  39. [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace="http://www.w3.org/2001/XMLSchema")]
  40. public XmlSchemaObjectCollection Attributes
  41. {
  42. get{ return attributes; }
  43. }
  44. [XmlElement("anyAttribute",Namespace="http://www.w3.org/2001/XMLSchema")]
  45. public XmlSchemaAnyAttribute AnyAttribute
  46. {
  47. get{ return any; }
  48. set{ any = value; }
  49. }
  50. /// <remarks>
  51. /// 1. base must be present
  52. /// </remarks>
  53. [MonoTODO]
  54. internal int Compile(ValidationEventHandler h, XmlSchemaInfo info)
  55. {
  56. if(BaseTypeName == null || BaseTypeName.IsEmpty)
  57. {
  58. error(h, "base must be present and a QName");
  59. }
  60. else if(!XmlSchemaUtil.CheckQName(BaseTypeName))
  61. error(h,"BaseTypeName is not a valid XmlQualifiedName");
  62. if(this.AnyAttribute != null)
  63. {
  64. errorCount += AnyAttribute.Compile(h,info);
  65. }
  66. foreach(XmlSchemaObject obj in Attributes)
  67. {
  68. if(obj is XmlSchemaAttribute)
  69. {
  70. XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
  71. errorCount += attr.Compile(h,info);
  72. }
  73. else if(obj is XmlSchemaAttributeGroupRef)
  74. {
  75. XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;
  76. errorCount += atgrp.Compile(h,info);
  77. }
  78. else
  79. error(h,obj.GetType() +" is not valid in this place::ComplexContentRestriction");
  80. }
  81. if(Particle != null)
  82. {
  83. if(Particle is XmlSchemaGroupRef)
  84. {
  85. errorCount += ((XmlSchemaGroupRef)Particle).Compile(h,info);
  86. }
  87. else if(Particle is XmlSchemaAll)
  88. {
  89. errorCount += ((XmlSchemaAll)Particle).Compile(h,info);
  90. }
  91. else if(Particle is XmlSchemaChoice)
  92. {
  93. errorCount += ((XmlSchemaChoice)Particle).Compile(h,info);
  94. }
  95. else if(Particle is XmlSchemaSequence)
  96. {
  97. errorCount += ((XmlSchemaSequence)Particle).Compile(h,info);
  98. }
  99. }
  100. XmlSchemaUtil.CompileID(Id,this,info.IDCollection,h);
  101. return errorCount;
  102. }
  103. [MonoTODO]
  104. internal int Validate(ValidationEventHandler h)
  105. {
  106. return errorCount;
  107. }
  108. //<restriction
  109. // base = QName
  110. // id = ID
  111. // {any attributes with non-schema namespace . . .}>
  112. // Content: (annotation?, ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))
  113. //</restriction>
  114. internal static XmlSchemaComplexContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)
  115. {
  116. XmlSchemaComplexContentRestriction restriction = new XmlSchemaComplexContentRestriction();
  117. reader.MoveToElement();
  118. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  119. {
  120. error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);
  121. reader.Skip();
  122. return null;
  123. }
  124. restriction.LineNumber = reader.LineNumber;
  125. restriction.LinePosition = reader.LinePosition;
  126. restriction.SourceUri = reader.BaseURI;
  127. while(reader.MoveToNextAttribute())
  128. {
  129. if(reader.Name == "base")
  130. {
  131. Exception innerex;
  132. restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  133. if(innerex != null)
  134. error(h, reader.Value + " is not a valid value for base attribute",innerex);
  135. }
  136. else if(reader.Name == "id")
  137. {
  138. restriction.Id = reader.Value;
  139. }
  140. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  141. {
  142. error(h,reader.Name + " is not a valid attribute for restriction",null);
  143. }
  144. else
  145. {
  146. XmlSchemaUtil.ReadUnhandledAttribute(reader,restriction);
  147. }
  148. }
  149. reader.MoveToElement();
  150. if(reader.IsEmptyElement)
  151. return restriction;
  152. //Content: 1. annotation?,
  153. // (2.(group | all | choice | sequence)?, (3.(attribute | attributeGroup)*, 4.anyAttribute?)))
  154. int level = 1;
  155. while(reader.ReadNextElement())
  156. {
  157. if(reader.NodeType == XmlNodeType.EndElement)
  158. {
  159. if(reader.LocalName != xmlname)
  160. error(h,"Should not happen :2: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);
  161. break;
  162. }
  163. if(level <= 1 && reader.LocalName == "annotation")
  164. {
  165. level = 2; //Only one annotation
  166. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  167. if(annotation != null)
  168. restriction.Annotation = annotation;
  169. continue;
  170. }
  171. if(level <= 2)
  172. {
  173. if(reader.LocalName == "group")
  174. {
  175. level = 3;
  176. XmlSchemaGroupRef group = XmlSchemaGroupRef.Read(reader,h);
  177. if(group != null)
  178. restriction.particle = group;
  179. continue;
  180. }
  181. if(reader.LocalName == "all")
  182. {
  183. level = 3;
  184. XmlSchemaAll all = XmlSchemaAll.Read(reader,h);
  185. if(all != null)
  186. restriction.particle = all;
  187. continue;
  188. }
  189. if(reader.LocalName == "choice")
  190. {
  191. level = 3;
  192. XmlSchemaChoice choice = XmlSchemaChoice.Read(reader,h);
  193. if(choice != null)
  194. restriction.particle = choice;
  195. continue;
  196. }
  197. if(reader.LocalName == "sequence")
  198. {
  199. level = 3;
  200. XmlSchemaSequence sequence = XmlSchemaSequence.Read(reader,h);
  201. if(sequence != null)
  202. restriction.particle = sequence;
  203. continue;
  204. }
  205. }
  206. if(level <= 3)
  207. {
  208. if(reader.LocalName == "attribute")
  209. {
  210. level = 3;
  211. XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
  212. if(attr != null)
  213. restriction.Attributes.Add(attr);
  214. continue;
  215. }
  216. if(reader.LocalName == "attributeGroup")
  217. {
  218. level = 3;
  219. XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
  220. if(attr != null)
  221. restriction.attributes.Add(attr);
  222. continue;
  223. }
  224. }
  225. if(level <= 4 && reader.LocalName == "anyAttribute")
  226. {
  227. level = 5;
  228. XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
  229. if(anyattr != null)
  230. restriction.AnyAttribute = anyattr;
  231. continue;
  232. }
  233. reader.RaiseInvalidElementError();
  234. }
  235. return restriction;
  236. }
  237. }
  238. }