XmlSchemaSimpleContentRestriction.cs 12 KB

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