2
0

XmlSchemaComplexContentRestriction.cs 7.5 KB

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