XmlSchemaSimpleContentRestriction.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Xml;
  25. using System.Xml.Serialization;
  26. namespace System.Xml.Schema
  27. {
  28. /// <summary>
  29. /// Summary description for XmlSchemaSimpleContentRestriction.
  30. /// </summary>
  31. public class XmlSchemaSimpleContentRestriction : XmlSchemaContent
  32. {
  33. private XmlSchemaAnyAttribute any;
  34. private XmlSchemaObjectCollection attributes;
  35. private XmlSchemaSimpleType baseType;
  36. private XmlQualifiedName baseTypeName;
  37. private XmlSchemaObjectCollection facets;
  38. const string xmlname = "restriction";
  39. public XmlSchemaSimpleContentRestriction()
  40. {
  41. baseTypeName = XmlQualifiedName.Empty;
  42. attributes = new XmlSchemaObjectCollection();
  43. facets = new XmlSchemaObjectCollection();
  44. }
  45. [System.Xml.Serialization.XmlAttribute("base")]
  46. public XmlQualifiedName BaseTypeName
  47. {
  48. get{ return baseTypeName; }
  49. set{ baseTypeName = value; }
  50. }
  51. [XmlElement("simpleType",Namespace=XmlSchema.Namespace)]
  52. public XmlSchemaSimpleType BaseType
  53. {
  54. get{ return baseType; }
  55. set{ baseType = value; }
  56. }
  57. [XmlElement("minExclusive",typeof(XmlSchemaMinExclusiveFacet),Namespace=XmlSchema.Namespace)]
  58. [XmlElement("minInclusive",typeof(XmlSchemaMinInclusiveFacet),Namespace=XmlSchema.Namespace)]
  59. [XmlElement("maxExclusive",typeof(XmlSchemaMaxExclusiveFacet),Namespace=XmlSchema.Namespace)]
  60. [XmlElement("maxInclusive",typeof(XmlSchemaMaxInclusiveFacet),Namespace=XmlSchema.Namespace)]
  61. [XmlElement("totalDigits",typeof(XmlSchemaTotalDigitsFacet),Namespace=XmlSchema.Namespace)]
  62. [XmlElement("fractionDigits",typeof(XmlSchemaFractionDigitsFacet),Namespace=XmlSchema.Namespace)]
  63. [XmlElement("length",typeof(XmlSchemaLengthFacet),Namespace=XmlSchema.Namespace)]
  64. [XmlElement("minLength",typeof(XmlSchemaMinLengthFacet),Namespace=XmlSchema.Namespace)]
  65. [XmlElement("maxLength",typeof(XmlSchemaMaxLengthFacet),Namespace=XmlSchema.Namespace)]
  66. [XmlElement("enumeration",typeof(XmlSchemaEnumerationFacet),Namespace=XmlSchema.Namespace)]
  67. [XmlElement("whiteSpace",typeof(XmlSchemaWhiteSpaceFacet),Namespace=XmlSchema.Namespace)]
  68. [XmlElement("pattern",typeof(XmlSchemaPatternFacet),Namespace=XmlSchema.Namespace)]
  69. public XmlSchemaObjectCollection Facets
  70. {
  71. get{ return facets; }
  72. }
  73. [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace=XmlSchema.Namespace)]
  74. [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace=XmlSchema.Namespace)]
  75. public XmlSchemaObjectCollection Attributes
  76. {
  77. get{ return attributes; }
  78. }
  79. [XmlElement("anyAttribute",Namespace=XmlSchema.Namespace)]
  80. public XmlSchemaAnyAttribute AnyAttribute
  81. {
  82. get{ return any; }
  83. set{ any = value; }
  84. }
  85. // internal properties
  86. internal override bool IsExtension {
  87. get { return false; }
  88. }
  89. ///<remarks>
  90. /// 1. Base must be present and a QName
  91. ///</remarks>
  92. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  93. {
  94. // If this is already compiled this time, simply skip.
  95. if (this.IsComplied (schema.CompilationId))
  96. return 0;
  97. if (this.isRedefinedComponent) {
  98. if (Annotation != null)
  99. Annotation.isRedefinedComponent = true;
  100. if (AnyAttribute != null)
  101. AnyAttribute.isRedefinedComponent = true;
  102. foreach (XmlSchemaObject obj in Attributes)
  103. obj.isRedefinedComponent = true;
  104. }
  105. if(BaseTypeName == null || BaseTypeName.IsEmpty)
  106. {
  107. error(h, "base must be present, as a QName");
  108. }
  109. else if(!XmlSchemaUtil.CheckQName(BaseTypeName))
  110. error(h,"BaseTypeName must be a QName");
  111. if(BaseType != null)
  112. {
  113. errorCount += BaseType.Compile(h,schema);
  114. }
  115. if(this.AnyAttribute != null)
  116. {
  117. errorCount += AnyAttribute.Compile(h,schema);
  118. }
  119. foreach(XmlSchemaObject obj in Attributes)
  120. {
  121. if(obj is XmlSchemaAttribute)
  122. {
  123. XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
  124. errorCount += attr.Compile(h,schema);
  125. }
  126. else if(obj is XmlSchemaAttributeGroupRef)
  127. {
  128. XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;
  129. errorCount += atgrp.Compile(h,schema);
  130. }
  131. else
  132. error(h,obj.GetType() +" is not valid in this place::SimpleContentRestriction");
  133. }
  134. //TODO: Compile Facets: Looks like they are a part of datatypes. So we'll do them with the datatypes
  135. XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
  136. this.CompilationId = schema.CompilationId;
  137. return errorCount;
  138. }
  139. internal override XmlQualifiedName GetBaseTypeName ()
  140. {
  141. return baseTypeName;
  142. }
  143. internal override XmlSchemaParticle GetParticle ()
  144. {
  145. return null;
  146. }
  147. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  148. {
  149. if (IsValidated (schema.ValidationId))
  150. return errorCount;
  151. if (baseType != null) {
  152. baseType.Validate (h, schema);
  153. actualBaseSchemaType = baseType;
  154. }
  155. else if (baseTypeName != XmlQualifiedName.Empty) {
  156. XmlSchemaType st = schema.SchemaTypes [baseTypeName] as XmlSchemaType;
  157. if (st != null) {
  158. st.Validate (h, schema);
  159. actualBaseSchemaType = st;
  160. } else if (baseTypeName == XmlSchemaComplexType.AnyTypeName) {
  161. actualBaseSchemaType = XmlSchemaComplexType.AnyType;
  162. } else if (XmlSchemaUtil.IsBuiltInDatatypeName (baseTypeName)) {
  163. actualBaseSchemaType = XmlSchemaDatatype.FromName (baseTypeName);
  164. if (actualBaseSchemaType == null)
  165. error (h, "Invalid schema datatype name is specified.");
  166. }
  167. // otherwise, it might be missing sub components.
  168. else if (!schema.IsNamespaceAbsent (baseTypeName.Namespace))
  169. error (h, "Referenced base schema type " + baseTypeName + " was not found in the corresponding schema.");
  170. }
  171. ValidationId = schema.ValidationId;
  172. return errorCount;
  173. }
  174. //<restriction
  175. //base = QName
  176. //id = ID
  177. //{any attributes with non-schema namespace . . .}>
  178. //Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))
  179. //</restriction>
  180. internal static XmlSchemaSimpleContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)
  181. {
  182. XmlSchemaSimpleContentRestriction restriction = new XmlSchemaSimpleContentRestriction();
  183. reader.MoveToElement();
  184. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  185. {
  186. error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);
  187. reader.SkipToEnd();
  188. return null;
  189. }
  190. restriction.LineNumber = reader.LineNumber;
  191. restriction.LinePosition = reader.LinePosition;
  192. restriction.SourceUri = reader.BaseURI;
  193. while(reader.MoveToNextAttribute())
  194. {
  195. if(reader.Name == "base")
  196. {
  197. Exception innerex;
  198. restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  199. if(innerex != null)
  200. error(h, reader.Value + " is not a valid value for base attribute",innerex);
  201. }
  202. else if(reader.Name == "id")
  203. {
  204. restriction.Id = reader.Value;
  205. }
  206. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  207. {
  208. error(h,reader.Name + " is not a valid attribute for restriction",null);
  209. }
  210. else
  211. {
  212. XmlSchemaUtil.ReadUnhandledAttribute(reader,restriction);
  213. }
  214. }
  215. reader.MoveToElement();
  216. if(reader.IsEmptyElement)
  217. return restriction;
  218. //Content: 1.annotation?,
  219. // 2.simpleType?,
  220. // 3.(minExclusive |...| enumeration | whiteSpace | pattern)*,
  221. // 4.(attribute | attributeGroup)*,
  222. // 5.anyAttribute?
  223. int level = 1;
  224. while(reader.ReadNextElement())
  225. {
  226. if(reader.NodeType == XmlNodeType.EndElement)
  227. {
  228. if(reader.LocalName != xmlname)
  229. error(h,"Should not happen :2: XmlSchemaSimpleContentRestriction.Read, name="+reader.Name,null);
  230. break;
  231. }
  232. if(level <= 1 && reader.LocalName == "annotation")
  233. {
  234. level = 2; //Only one annotation
  235. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  236. if(annotation != null)
  237. restriction.Annotation = annotation;
  238. continue;
  239. }
  240. if(level <=2 && reader.LocalName == "simpleType")
  241. {
  242. level = 3;
  243. XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
  244. if(stype != null)
  245. restriction.baseType = stype;
  246. continue;
  247. }
  248. if(level <= 3)
  249. {
  250. if(reader.LocalName == "minExclusive")
  251. {
  252. level = 3;
  253. XmlSchemaMinExclusiveFacet minex = XmlSchemaMinExclusiveFacet.Read(reader,h);
  254. if(minex != null)
  255. restriction.facets.Add(minex);
  256. continue;
  257. }
  258. else if(reader.LocalName == "minInclusive")
  259. {
  260. level = 3;
  261. XmlSchemaMinInclusiveFacet mini = XmlSchemaMinInclusiveFacet.Read(reader,h);
  262. if(mini != null)
  263. restriction.facets.Add(mini);
  264. continue;
  265. }
  266. else if(reader.LocalName == "maxExclusive")
  267. {
  268. level = 3;
  269. XmlSchemaMaxExclusiveFacet maxex = XmlSchemaMaxExclusiveFacet.Read(reader,h);
  270. if(maxex != null)
  271. restriction.facets.Add(maxex);
  272. continue;
  273. }
  274. else if(reader.LocalName == "maxInclusive")
  275. {
  276. level = 3;
  277. XmlSchemaMaxInclusiveFacet maxi = XmlSchemaMaxInclusiveFacet.Read(reader,h);
  278. if(maxi != null)
  279. restriction.facets.Add(maxi);
  280. continue;
  281. }
  282. else if(reader.LocalName == "totalDigits")
  283. {
  284. level = 3;
  285. XmlSchemaTotalDigitsFacet total = XmlSchemaTotalDigitsFacet.Read(reader,h);
  286. if(total != null)
  287. restriction.facets.Add(total);
  288. continue;
  289. }
  290. else if(reader.LocalName == "fractionDigits")
  291. {
  292. level = 3;
  293. XmlSchemaFractionDigitsFacet fraction = XmlSchemaFractionDigitsFacet.Read(reader,h);
  294. if(fraction != null)
  295. restriction.facets.Add(fraction);
  296. continue;
  297. }
  298. else if(reader.LocalName == "length")
  299. {
  300. level = 3;
  301. XmlSchemaLengthFacet length = XmlSchemaLengthFacet.Read(reader,h);
  302. if(length != null)
  303. restriction.facets.Add(length);
  304. continue;
  305. }
  306. else if(reader.LocalName == "minLength")
  307. {
  308. level = 3;
  309. XmlSchemaMinLengthFacet minlen = XmlSchemaMinLengthFacet.Read(reader,h);
  310. if(minlen != null)
  311. restriction.facets.Add(minlen);
  312. continue;
  313. }
  314. else if(reader.LocalName == "maxLength")
  315. {
  316. level = 3;
  317. XmlSchemaMaxLengthFacet maxlen = XmlSchemaMaxLengthFacet.Read(reader,h);
  318. if(maxlen != null)
  319. restriction.facets.Add(maxlen);
  320. continue;
  321. }
  322. else if(reader.LocalName == "enumeration")
  323. {
  324. level = 3;
  325. XmlSchemaEnumerationFacet enumeration = XmlSchemaEnumerationFacet.Read(reader,h);
  326. if(enumeration != null)
  327. restriction.facets.Add(enumeration);
  328. continue;
  329. }
  330. else if(reader.LocalName == "whiteSpace")
  331. {
  332. level = 3;
  333. XmlSchemaWhiteSpaceFacet ws = XmlSchemaWhiteSpaceFacet.Read(reader,h);
  334. if(ws != null)
  335. restriction.facets.Add(ws);
  336. continue;
  337. }
  338. else if(reader.LocalName == "pattern")
  339. {
  340. level = 3;
  341. XmlSchemaPatternFacet pattern = XmlSchemaPatternFacet.Read(reader,h);
  342. if(pattern != null)
  343. restriction.facets.Add(pattern);
  344. continue;
  345. }
  346. }
  347. if(level <= 4)
  348. {
  349. if(reader.LocalName == "attribute")
  350. {
  351. level = 4;
  352. XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
  353. if(attr != null)
  354. restriction.Attributes.Add(attr);
  355. continue;
  356. }
  357. if(reader.LocalName == "attributeGroup")
  358. {
  359. level = 4;
  360. XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
  361. if(attr != null)
  362. restriction.attributes.Add(attr);
  363. continue;
  364. }
  365. }
  366. if(level <= 5 && reader.LocalName == "anyAttribute")
  367. {
  368. level = 6;
  369. XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
  370. if(anyattr != null)
  371. restriction.AnyAttribute = anyattr;
  372. continue;
  373. }
  374. reader.RaiseInvalidElementError();
  375. }
  376. return restriction;
  377. }
  378. }
  379. }