XmlSchemaSimpleContentRestriction.cs 12 KB

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