XmlSchemaSimpleContentRestriction.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 override 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 (this.isRedefinedComponent) {
  75. if (Annotation != null)
  76. Annotation.isRedefinedComponent = true;
  77. if (AnyAttribute != null)
  78. AnyAttribute.isRedefinedComponent = true;
  79. foreach (XmlSchemaObject obj in Attributes)
  80. obj.isRedefinedComponent = true;
  81. }
  82. if(BaseTypeName == null || BaseTypeName.IsEmpty)
  83. {
  84. error(h, "base must be present, as a QName");
  85. }
  86. else if(!XmlSchemaUtil.CheckQName(BaseTypeName))
  87. error(h,"BaseTypeName must be a QName");
  88. if(BaseType != null)
  89. {
  90. errorCount += BaseType.Compile(h,schema);
  91. }
  92. if(this.AnyAttribute != null)
  93. {
  94. errorCount += AnyAttribute.Compile(h,schema);
  95. }
  96. foreach(XmlSchemaObject obj in Attributes)
  97. {
  98. if(obj is XmlSchemaAttribute)
  99. {
  100. XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
  101. errorCount += attr.Compile(h,schema);
  102. }
  103. else if(obj is XmlSchemaAttributeGroupRef)
  104. {
  105. XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;
  106. errorCount += atgrp.Compile(h,schema);
  107. }
  108. else
  109. error(h,obj.GetType() +" is not valid in this place::SimpleContentRestriction");
  110. }
  111. //TODO: Compile Facets: Looks like they are a part of datatypes. So we'll do them with the datatypes
  112. XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
  113. this.CompilationId = schema.CompilationId;
  114. return errorCount;
  115. }
  116. [MonoTODO]
  117. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  118. {
  119. if (IsValidated (schema.ValidationId))
  120. return errorCount;
  121. if (baseType != null) {
  122. baseType.Validate (h, schema);
  123. actualBaseSchemaType = baseType;
  124. }
  125. else if (baseTypeName != XmlQualifiedName.Empty) {
  126. XmlSchemaType st = schema.SchemaTypes [baseTypeName] as XmlSchemaType;
  127. if (st != null) {
  128. st.Validate (h, schema);
  129. actualBaseSchemaType = st;
  130. } else if (baseTypeName == XmlSchemaComplexType.AnyTypeName) {
  131. actualBaseSchemaType = XmlSchemaComplexType.AnyType;
  132. } else if (baseTypeName.Namespace == XmlSchema.Namespace) {
  133. actualBaseSchemaType = XmlSchemaDatatype.FromName (baseTypeName);
  134. if (actualBaseSchemaType == null)
  135. error (h, "Invalid schema datatype name is specified.");
  136. }
  137. // otherwise, it might be missing sub components.
  138. else if (!schema.IsNamespaceAbsent (baseTypeName.Namespace))
  139. error (h, "Referenced base schema type " + baseTypeName + " was not found in the corresponding schema.");
  140. }
  141. ValidationId = schema.ValidationId;
  142. return errorCount;
  143. }
  144. //<restriction
  145. //base = QName
  146. //id = ID
  147. //{any attributes with non-schema namespace . . .}>
  148. //Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))
  149. //</restriction>
  150. internal static XmlSchemaSimpleContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)
  151. {
  152. XmlSchemaSimpleContentRestriction restriction = new XmlSchemaSimpleContentRestriction();
  153. reader.MoveToElement();
  154. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  155. {
  156. error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);
  157. reader.SkipToEnd();
  158. return null;
  159. }
  160. restriction.LineNumber = reader.LineNumber;
  161. restriction.LinePosition = reader.LinePosition;
  162. restriction.SourceUri = reader.BaseURI;
  163. while(reader.MoveToNextAttribute())
  164. {
  165. if(reader.Name == "base")
  166. {
  167. Exception innerex;
  168. restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  169. if(innerex != null)
  170. error(h, reader.Value + " is not a valid value for base attribute",innerex);
  171. }
  172. else if(reader.Name == "id")
  173. {
  174. restriction.Id = reader.Value;
  175. }
  176. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  177. {
  178. error(h,reader.Name + " is not a valid attribute for restriction",null);
  179. }
  180. else
  181. {
  182. XmlSchemaUtil.ReadUnhandledAttribute(reader,restriction);
  183. }
  184. }
  185. reader.MoveToElement();
  186. if(reader.IsEmptyElement)
  187. return restriction;
  188. //Content: 1.annotation?,
  189. // 2.simpleType?,
  190. // 3.(minExclusive |...| enumeration | whiteSpace | pattern)*,
  191. // 4.(attribute | attributeGroup)*,
  192. // 5.anyAttribute?
  193. int level = 1;
  194. while(reader.ReadNextElement())
  195. {
  196. if(reader.NodeType == XmlNodeType.EndElement)
  197. {
  198. if(reader.LocalName != xmlname)
  199. error(h,"Should not happen :2: XmlSchemaSimpleContentRestriction.Read, name="+reader.Name,null);
  200. break;
  201. }
  202. if(level <= 1 && reader.LocalName == "annotation")
  203. {
  204. level = 2; //Only one annotation
  205. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  206. if(annotation != null)
  207. restriction.Annotation = annotation;
  208. continue;
  209. }
  210. if(level <=2 && reader.LocalName == "simpleType")
  211. {
  212. level = 3;
  213. XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
  214. if(stype != null)
  215. restriction.baseType = stype;
  216. continue;
  217. }
  218. if(level <= 3)
  219. {
  220. if(reader.LocalName == "minExclusive")
  221. {
  222. level = 3;
  223. XmlSchemaMinExclusiveFacet minex = XmlSchemaMinExclusiveFacet.Read(reader,h);
  224. if(minex != null)
  225. restriction.facets.Add(minex);
  226. continue;
  227. }
  228. else if(reader.LocalName == "minInclusive")
  229. {
  230. level = 3;
  231. XmlSchemaMinInclusiveFacet mini = XmlSchemaMinInclusiveFacet.Read(reader,h);
  232. if(mini != null)
  233. restriction.facets.Add(mini);
  234. continue;
  235. }
  236. else if(reader.LocalName == "maxExclusive")
  237. {
  238. level = 3;
  239. XmlSchemaMaxExclusiveFacet maxex = XmlSchemaMaxExclusiveFacet.Read(reader,h);
  240. if(maxex != null)
  241. restriction.facets.Add(maxex);
  242. continue;
  243. }
  244. else if(reader.LocalName == "maxInclusive")
  245. {
  246. level = 3;
  247. XmlSchemaMaxInclusiveFacet maxi = XmlSchemaMaxInclusiveFacet.Read(reader,h);
  248. if(maxi != null)
  249. restriction.facets.Add(maxi);
  250. continue;
  251. }
  252. else if(reader.LocalName == "totalDigits")
  253. {
  254. level = 3;
  255. XmlSchemaTotalDigitsFacet total = XmlSchemaTotalDigitsFacet.Read(reader,h);
  256. if(total != null)
  257. restriction.facets.Add(total);
  258. continue;
  259. }
  260. else if(reader.LocalName == "fractionDigits")
  261. {
  262. level = 3;
  263. XmlSchemaFractionDigitsFacet fraction = XmlSchemaFractionDigitsFacet.Read(reader,h);
  264. if(fraction != null)
  265. restriction.facets.Add(fraction);
  266. continue;
  267. }
  268. else if(reader.LocalName == "length")
  269. {
  270. level = 3;
  271. XmlSchemaLengthFacet length = XmlSchemaLengthFacet.Read(reader,h);
  272. if(length != null)
  273. restriction.facets.Add(length);
  274. continue;
  275. }
  276. else if(reader.LocalName == "minLength")
  277. {
  278. level = 3;
  279. XmlSchemaMinLengthFacet minlen = XmlSchemaMinLengthFacet.Read(reader,h);
  280. if(minlen != null)
  281. restriction.facets.Add(minlen);
  282. continue;
  283. }
  284. else if(reader.LocalName == "maxLength")
  285. {
  286. level = 3;
  287. XmlSchemaMaxLengthFacet maxlen = XmlSchemaMaxLengthFacet.Read(reader,h);
  288. if(maxlen != null)
  289. restriction.facets.Add(maxlen);
  290. continue;
  291. }
  292. else if(reader.LocalName == "enumeration")
  293. {
  294. level = 3;
  295. XmlSchemaEnumerationFacet enumeration = XmlSchemaEnumerationFacet.Read(reader,h);
  296. if(enumeration != null)
  297. restriction.facets.Add(enumeration);
  298. continue;
  299. }
  300. else if(reader.LocalName == "whiteSpace")
  301. {
  302. level = 3;
  303. XmlSchemaWhiteSpaceFacet ws = XmlSchemaWhiteSpaceFacet.Read(reader,h);
  304. if(ws != null)
  305. restriction.facets.Add(ws);
  306. continue;
  307. }
  308. else if(reader.LocalName == "pattern")
  309. {
  310. level = 3;
  311. XmlSchemaPatternFacet pattern = XmlSchemaPatternFacet.Read(reader,h);
  312. if(pattern != null)
  313. restriction.facets.Add(pattern);
  314. continue;
  315. }
  316. }
  317. if(level <= 4)
  318. {
  319. if(reader.LocalName == "attribute")
  320. {
  321. level = 4;
  322. XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
  323. if(attr != null)
  324. restriction.Attributes.Add(attr);
  325. continue;
  326. }
  327. if(reader.LocalName == "attributeGroup")
  328. {
  329. level = 4;
  330. XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
  331. if(attr != null)
  332. restriction.attributes.Add(attr);
  333. continue;
  334. }
  335. }
  336. if(level <= 5 && reader.LocalName == "anyAttribute")
  337. {
  338. level = 6;
  339. XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
  340. if(anyattr != null)
  341. restriction.AnyAttribute = anyattr;
  342. continue;
  343. }
  344. reader.RaiseInvalidElementError();
  345. }
  346. return restriction;
  347. }
  348. }
  349. }