XmlSchemaSimpleContentRestriction.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 XmlSchemaSimpleContentRestriction.
  10. /// </summary>
  11. public class XmlSchemaSimpleContentRestriction : XmlSchemaContent
  12. {
  13. private XmlSchemaAnyAttribute any;
  14. private XmlSchemaObjectCollection attributes;
  15. private XmlSchemaSimpleType baseType;
  16. private XmlQualifiedName baseTypeName;
  17. private XmlSchemaObjectCollection facets;
  18. private static string xmlname = "restriction";
  19. public XmlSchemaSimpleContentRestriction()
  20. {
  21. baseTypeName = XmlQualifiedName.Empty;
  22. attributes = new XmlSchemaObjectCollection();
  23. facets = new XmlSchemaObjectCollection();
  24. }
  25. [System.Xml.Serialization.XmlAttribute("base")]
  26. public XmlQualifiedName BaseTypeName
  27. {
  28. get{ return baseTypeName; }
  29. set{ baseTypeName = value; }
  30. }
  31. [XmlElement("simpleType",Namespace=XmlSchema.Namespace)]
  32. public XmlSchemaSimpleType BaseType
  33. {
  34. get{ return baseType; }
  35. set{ baseType = value; }
  36. }
  37. [XmlElement("minExclusive",typeof(XmlSchemaMinExclusiveFacet),Namespace=XmlSchema.Namespace)]
  38. [XmlElement("minInclusive",typeof(XmlSchemaMinInclusiveFacet),Namespace=XmlSchema.Namespace)]
  39. [XmlElement("maxExclusive",typeof(XmlSchemaMaxExclusiveFacet),Namespace=XmlSchema.Namespace)]
  40. [XmlElement("maxInclusive",typeof(XmlSchemaMaxInclusiveFacet),Namespace=XmlSchema.Namespace)]
  41. [XmlElement("totalDigits",typeof(XmlSchemaTotalDigitsFacet),Namespace=XmlSchema.Namespace)]
  42. [XmlElement("fractionDigits",typeof(XmlSchemaFractionDigitsFacet),Namespace=XmlSchema.Namespace)]
  43. [XmlElement("length",typeof(XmlSchemaLengthFacet),Namespace=XmlSchema.Namespace)]
  44. [XmlElement("minLength",typeof(XmlSchemaMinLengthFacet),Namespace=XmlSchema.Namespace)]
  45. [XmlElement("maxLength",typeof(XmlSchemaMaxLengthFacet),Namespace=XmlSchema.Namespace)]
  46. [XmlElement("enumeration",typeof(XmlSchemaEnumerationFacet),Namespace=XmlSchema.Namespace)]
  47. [XmlElement("whiteSpace",typeof(XmlSchemaWhiteSpaceFacet),Namespace=XmlSchema.Namespace)]
  48. [XmlElement("pattern",typeof(XmlSchemaPatternFacet),Namespace=XmlSchema.Namespace)]
  49. public XmlSchemaObjectCollection Facets
  50. {
  51. get{ return facets; }
  52. }
  53. [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace=XmlSchema.Namespace)]
  54. [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace=XmlSchema.Namespace)]
  55. public XmlSchemaObjectCollection Attributes
  56. {
  57. get{ return attributes; }
  58. }
  59. [XmlElement("anyAttribute",Namespace=XmlSchema.Namespace)]
  60. public XmlSchemaAnyAttribute AnyAttribute
  61. {
  62. get{ return any; }
  63. set{ any = value; }
  64. }
  65. ///<remarks>
  66. /// 1. Base must be present and a QName
  67. ///</remarks>
  68. [MonoTODO]
  69. internal int Compile(ValidationEventHandler h, XmlSchema schema)
  70. {
  71. // If this is already compiled this time, simply skip.
  72. if (this.IsComplied (schema.CompilationId))
  73. return 0;
  74. if(BaseTypeName == null || BaseTypeName.IsEmpty)
  75. {
  76. error(h, "base must be present and a QName");
  77. }
  78. else if(!XmlSchemaUtil.CheckQName(BaseTypeName))
  79. error(h,"BaseTypeName must be a QName");
  80. if(BaseType != null)
  81. {
  82. errorCount += BaseType.Compile(h,schema);
  83. }
  84. if(this.AnyAttribute != null)
  85. {
  86. errorCount += AnyAttribute.Compile(h,schema);
  87. }
  88. foreach(XmlSchemaObject obj in Attributes)
  89. {
  90. if(obj is XmlSchemaAttribute)
  91. {
  92. XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
  93. errorCount += attr.Compile(h,schema);
  94. }
  95. else if(obj is XmlSchemaAttributeGroupRef)
  96. {
  97. XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;
  98. errorCount += atgrp.Compile(h,schema);
  99. }
  100. else
  101. error(h,obj.GetType() +" is not valid in this place::SimpleContentRestriction");
  102. }
  103. //TODO: Compile Facets: Looks like they are a part of datatypes. So we'll do them with the datatypes
  104. XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
  105. this.CompilationId = schema.CompilationId;
  106. return errorCount;
  107. }
  108. [MonoTODO]
  109. internal int Validate(ValidationEventHandler h)
  110. {
  111. return errorCount;
  112. }
  113. //<restriction
  114. //base = QName
  115. //id = ID
  116. //{any attributes with non-schema namespace . . .}>
  117. //Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))
  118. //</restriction>
  119. internal static XmlSchemaSimpleContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)
  120. {
  121. XmlSchemaSimpleContentRestriction restriction = new XmlSchemaSimpleContentRestriction();
  122. reader.MoveToElement();
  123. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  124. {
  125. error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);
  126. reader.SkipToEnd();
  127. return null;
  128. }
  129. restriction.LineNumber = reader.LineNumber;
  130. restriction.LinePosition = reader.LinePosition;
  131. restriction.SourceUri = reader.BaseURI;
  132. while(reader.MoveToNextAttribute())
  133. {
  134. if(reader.Name == "base")
  135. {
  136. Exception innerex;
  137. restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  138. if(innerex != null)
  139. error(h, reader.Value + " is not a valid value for base attribute",innerex);
  140. }
  141. else if(reader.Name == "id")
  142. {
  143. restriction.Id = reader.Value;
  144. }
  145. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  146. {
  147. error(h,reader.Name + " is not a valid attribute for restriction",null);
  148. }
  149. else
  150. {
  151. XmlSchemaUtil.ReadUnhandledAttribute(reader,restriction);
  152. }
  153. }
  154. reader.MoveToElement();
  155. if(reader.IsEmptyElement)
  156. return restriction;
  157. //Content: 1.annotation?,
  158. // 2.simpleType?,
  159. // 3.(minExclusive |...| enumeration | whiteSpace | pattern)*,
  160. // 4.(attribute | attributeGroup)*,
  161. // 5.anyAttribute?
  162. int level = 1;
  163. while(reader.ReadNextElement())
  164. {
  165. if(reader.NodeType == XmlNodeType.EndElement)
  166. {
  167. if(reader.LocalName != xmlname)
  168. error(h,"Should not happen :2: XmlSchemaSimpleContentRestriction.Read, name="+reader.Name,null);
  169. break;
  170. }
  171. if(level <= 1 && reader.LocalName == "annotation")
  172. {
  173. level = 2; //Only one annotation
  174. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  175. if(annotation != null)
  176. restriction.Annotation = annotation;
  177. continue;
  178. }
  179. if(level <=2 && reader.LocalName == "simpleType")
  180. {
  181. level = 3;
  182. XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
  183. if(stype != null)
  184. restriction.baseType = stype;
  185. continue;
  186. }
  187. if(level <= 3)
  188. {
  189. if(reader.LocalName == "minExclusive")
  190. {
  191. level = 3;
  192. XmlSchemaMinExclusiveFacet minex = XmlSchemaMinExclusiveFacet.Read(reader,h);
  193. if(minex != null)
  194. restriction.facets.Add(minex);
  195. continue;
  196. }
  197. else if(reader.LocalName == "minInclusive")
  198. {
  199. level = 3;
  200. XmlSchemaMinInclusiveFacet mini = XmlSchemaMinInclusiveFacet.Read(reader,h);
  201. if(mini != null)
  202. restriction.facets.Add(mini);
  203. continue;
  204. }
  205. else if(reader.LocalName == "maxExclusive")
  206. {
  207. level = 3;
  208. XmlSchemaMaxExclusiveFacet maxex = XmlSchemaMaxExclusiveFacet.Read(reader,h);
  209. if(maxex != null)
  210. restriction.facets.Add(maxex);
  211. continue;
  212. }
  213. else if(reader.LocalName == "maxInclusive")
  214. {
  215. level = 3;
  216. XmlSchemaMaxInclusiveFacet maxi = XmlSchemaMaxInclusiveFacet.Read(reader,h);
  217. if(maxi != null)
  218. restriction.facets.Add(maxi);
  219. continue;
  220. }
  221. else if(reader.LocalName == "totalDigits")
  222. {
  223. level = 3;
  224. XmlSchemaTotalDigitsFacet total = XmlSchemaTotalDigitsFacet.Read(reader,h);
  225. if(total != null)
  226. restriction.facets.Add(total);
  227. continue;
  228. }
  229. else if(reader.LocalName == "fractionDigits")
  230. {
  231. level = 3;
  232. XmlSchemaFractionDigitsFacet fraction = XmlSchemaFractionDigitsFacet.Read(reader,h);
  233. if(fraction != null)
  234. restriction.facets.Add(fraction);
  235. continue;
  236. }
  237. else if(reader.LocalName == "length")
  238. {
  239. level = 3;
  240. XmlSchemaLengthFacet length = XmlSchemaLengthFacet.Read(reader,h);
  241. if(length != null)
  242. restriction.facets.Add(length);
  243. continue;
  244. }
  245. else if(reader.LocalName == "minLength")
  246. {
  247. level = 3;
  248. XmlSchemaMinLengthFacet minlen = XmlSchemaMinLengthFacet.Read(reader,h);
  249. if(minlen != null)
  250. restriction.facets.Add(minlen);
  251. continue;
  252. }
  253. else if(reader.LocalName == "maxLength")
  254. {
  255. level = 3;
  256. XmlSchemaMaxLengthFacet maxlen = XmlSchemaMaxLengthFacet.Read(reader,h);
  257. if(maxlen != null)
  258. restriction.facets.Add(maxlen);
  259. continue;
  260. }
  261. else if(reader.LocalName == "enumeration")
  262. {
  263. level = 3;
  264. XmlSchemaEnumerationFacet enumeration = XmlSchemaEnumerationFacet.Read(reader,h);
  265. if(enumeration != null)
  266. restriction.facets.Add(enumeration);
  267. continue;
  268. }
  269. else if(reader.LocalName == "whiteSpace")
  270. {
  271. level = 3;
  272. XmlSchemaWhiteSpaceFacet ws = XmlSchemaWhiteSpaceFacet.Read(reader,h);
  273. if(ws != null)
  274. restriction.facets.Add(ws);
  275. continue;
  276. }
  277. else if(reader.LocalName == "pattern")
  278. {
  279. level = 3;
  280. XmlSchemaPatternFacet pattern = XmlSchemaPatternFacet.Read(reader,h);
  281. if(pattern != null)
  282. restriction.facets.Add(pattern);
  283. continue;
  284. }
  285. }
  286. if(level <= 4)
  287. {
  288. if(reader.LocalName == "attribute")
  289. {
  290. level = 4;
  291. XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
  292. if(attr != null)
  293. restriction.Attributes.Add(attr);
  294. continue;
  295. }
  296. if(reader.LocalName == "attributeGroup")
  297. {
  298. level = 4;
  299. XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
  300. if(attr != null)
  301. restriction.attributes.Add(attr);
  302. continue;
  303. }
  304. }
  305. if(level <= 5 && reader.LocalName == "anyAttribute")
  306. {
  307. level = 6;
  308. XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
  309. if(anyattr != null)
  310. restriction.AnyAttribute = anyattr;
  311. continue;
  312. }
  313. reader.RaiseInvalidElementError();
  314. }
  315. return restriction;
  316. }
  317. }
  318. }