XmlSchemaComplexContentRestriction.cs 8.1 KB

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