XmlSchemaSimpleContentRestriction.cs 13 KB

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