PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.XML/System.Xml.Schema/XmlSchemaSimpleType.cs

https://bitbucket.org/danipen/mono
C# | 661 lines | 477 code | 57 blank | 127 comment | 136 complexity | 87b6b40584e90a7a12117ac2f3966a4f MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // System.Xml.Schema.XmlSchemaSimpleType.cs
  3. //
  4. // Author:
  5. // Dwivedi, Ajay kumar Adwiv@Yahoo.com
  6. // Atsushi Enomoto ginga@kit.hi-ho.ne.jp
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Xml.Serialization;
  30. using System.Xml;
  31. using Mono.Xml.Schema;
  32. namespace System.Xml.Schema
  33. {
  34. /// <summary>
  35. /// Summary description for XmlSchemaSimpleType.
  36. /// </summary>
  37. public class XmlSchemaSimpleType : XmlSchemaType
  38. {
  39. const string xmlname = "simpleType";
  40. private static XmlSchemaSimpleType schemaLocationType;
  41. private XmlSchemaSimpleTypeContent content;
  42. //compilation vars
  43. internal bool islocal = true; // Assuming local means we have to specify islocal=false only in XmlSchema
  44. private bool recursed;
  45. private XmlSchemaDerivationMethod variety;
  46. #if NET_2_0
  47. // predefined simple types
  48. internal static readonly XmlSchemaSimpleType XsAnySimpleType;
  49. internal static readonly XmlSchemaSimpleType XsString;
  50. internal static readonly XmlSchemaSimpleType XsBoolean;
  51. internal static readonly XmlSchemaSimpleType XsDecimal;
  52. internal static readonly XmlSchemaSimpleType XsFloat;
  53. internal static readonly XmlSchemaSimpleType XsDouble;
  54. internal static readonly XmlSchemaSimpleType XsDuration;
  55. internal static readonly XmlSchemaSimpleType XsDateTime;
  56. internal static readonly XmlSchemaSimpleType XsTime;
  57. internal static readonly XmlSchemaSimpleType XsDate;
  58. internal static readonly XmlSchemaSimpleType XsGYearMonth;
  59. internal static readonly XmlSchemaSimpleType XsGYear;
  60. internal static readonly XmlSchemaSimpleType XsGMonthDay;
  61. internal static readonly XmlSchemaSimpleType XsGDay;
  62. internal static readonly XmlSchemaSimpleType XsGMonth;
  63. internal static readonly XmlSchemaSimpleType XsHexBinary;
  64. internal static readonly XmlSchemaSimpleType XsBase64Binary;
  65. internal static readonly XmlSchemaSimpleType XsAnyUri;
  66. internal static readonly XmlSchemaSimpleType XsQName;
  67. internal static readonly XmlSchemaSimpleType XsNotation;
  68. internal static readonly XmlSchemaSimpleType XsNormalizedString;
  69. internal static readonly XmlSchemaSimpleType XsToken;
  70. internal static readonly XmlSchemaSimpleType XsLanguage;
  71. internal static readonly XmlSchemaSimpleType XsNMToken;
  72. internal static readonly XmlSchemaSimpleType XsNMTokens;
  73. internal static readonly XmlSchemaSimpleType XsName;
  74. internal static readonly XmlSchemaSimpleType XsNCName;
  75. internal static readonly XmlSchemaSimpleType XsID;
  76. internal static readonly XmlSchemaSimpleType XsIDRef;
  77. internal static readonly XmlSchemaSimpleType XsIDRefs;
  78. internal static readonly XmlSchemaSimpleType XsEntity;
  79. internal static readonly XmlSchemaSimpleType XsEntities;
  80. internal static readonly XmlSchemaSimpleType XsInteger;
  81. internal static readonly XmlSchemaSimpleType XsNonPositiveInteger;
  82. internal static readonly XmlSchemaSimpleType XsNegativeInteger;
  83. internal static readonly XmlSchemaSimpleType XsLong;
  84. internal static readonly XmlSchemaSimpleType XsInt;
  85. internal static readonly XmlSchemaSimpleType XsShort;
  86. internal static readonly XmlSchemaSimpleType XsByte;
  87. internal static readonly XmlSchemaSimpleType XsNonNegativeInteger;
  88. internal static readonly XmlSchemaSimpleType XsUnsignedLong;
  89. internal static readonly XmlSchemaSimpleType XsUnsignedInt;
  90. internal static readonly XmlSchemaSimpleType XsUnsignedShort;
  91. internal static readonly XmlSchemaSimpleType XsUnsignedByte;
  92. internal static readonly XmlSchemaSimpleType XsPositiveInteger;
  93. // xdt:*
  94. internal static readonly XmlSchemaSimpleType XdtUntypedAtomic;
  95. internal static readonly XmlSchemaSimpleType XdtAnyAtomicType;
  96. internal static readonly XmlSchemaSimpleType XdtYearMonthDuration;
  97. internal static readonly XmlSchemaSimpleType XdtDayTimeDuration;
  98. #endif
  99. static XmlSchemaSimpleType ()
  100. {
  101. // This is not used in the meantime.
  102. XmlSchemaSimpleType st = new XmlSchemaSimpleType ();
  103. XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList ();
  104. list.ItemTypeName = new XmlQualifiedName ("anyURI", XmlSchema.Namespace);
  105. st.Content = list;
  106. st.BaseXmlSchemaTypeInternal = null;
  107. st.variety = XmlSchemaDerivationMethod.List;
  108. schemaLocationType = st;
  109. #if NET_2_0
  110. // Built-In schema types
  111. XsAnySimpleType = BuildSchemaType ("anySimpleType", null);
  112. XsString = BuildSchemaType ("string", "anySimpleType");
  113. XsBoolean = BuildSchemaType ("boolean", "anySimpleType");
  114. XsDecimal = BuildSchemaType ("decimal", "anySimpleType");
  115. XsFloat = BuildSchemaType ("float", "anySimpleType");
  116. XsDouble = BuildSchemaType ("double", "anySimpleType");
  117. XsDuration = BuildSchemaType ("duration", "anySimpleType");
  118. XsDateTime = BuildSchemaType ("dateTime", "anySimpleType");
  119. XsTime = BuildSchemaType ("time", "anySimpleType");
  120. XsDate = BuildSchemaType ("date", "anySimpleType");
  121. XsGYearMonth = BuildSchemaType ("gYearMonth", "anySimpleType");
  122. XsGYear = BuildSchemaType ("gYear", "anySimpleType");
  123. XsGMonthDay = BuildSchemaType ("gMonthDay", "anySimpleType");
  124. XsGDay = BuildSchemaType ("gDay", "anySimpleType");
  125. XsGMonth = BuildSchemaType ("gMonth", "anySimpleType");
  126. XsHexBinary = BuildSchemaType ("hexBinary", "anySimpleType");
  127. XsBase64Binary = BuildSchemaType ("base64Binary", "anySimpleType");
  128. XsAnyUri = BuildSchemaType ("anyURI", "anySimpleType");
  129. XsQName = BuildSchemaType ("QName", "anySimpleType");
  130. XsNotation = BuildSchemaType ("NOTATION", "anySimpleType");
  131. // derived types
  132. XsNormalizedString = BuildSchemaType ("normalizedString", "string");
  133. XsToken = BuildSchemaType ("token", "normalizedString");
  134. XsLanguage = BuildSchemaType ("language", "token");
  135. XsNMToken = BuildSchemaType ("NMTOKEN", "token");
  136. XsName = BuildSchemaType ("Name", "token");
  137. XsNCName = BuildSchemaType ("NCName", "Name");
  138. XsID = BuildSchemaType ("ID", "NCName");
  139. XsIDRef = BuildSchemaType ("IDREF", "NCName");
  140. XsEntity = BuildSchemaType ("ENTITY", "NCName");
  141. XsInteger = BuildSchemaType ("integer", "decimal");
  142. XsNonPositiveInteger = BuildSchemaType ("nonPositiveInteger", "integer");
  143. XsNegativeInteger = BuildSchemaType ("negativeInteger", "nonPositiveInteger");
  144. XsLong = BuildSchemaType ("long", "integer");
  145. XsInt = BuildSchemaType ("int", "long");
  146. XsShort = BuildSchemaType ("short", "int");
  147. XsByte = BuildSchemaType ("byte", "short");
  148. XsNonNegativeInteger = BuildSchemaType ("nonNegativeInteger", "integer");
  149. XsUnsignedLong = BuildSchemaType ("unsignedLong", "nonNegativeInteger");
  150. XsUnsignedInt = BuildSchemaType ("unsignedInt", "unsignedLong");
  151. XsUnsignedShort = BuildSchemaType ("unsignedShort", "unsignedInt");
  152. XsUnsignedByte = BuildSchemaType ("unsignedByte", "unsignedShort");
  153. XsPositiveInteger = BuildSchemaType ("positiveInteger", "nonNegativeInteger");
  154. // xdt:*
  155. XdtAnyAtomicType = BuildSchemaType ("anyAtomicType", "anySimpleType", true, false);
  156. XdtUntypedAtomic = BuildSchemaType ("untypedAtomic", "anyAtomicType", true, true);
  157. XdtDayTimeDuration = BuildSchemaType ("dayTimeDuration", "duration", true, false);
  158. XdtYearMonthDuration = BuildSchemaType ("yearMonthDuration", "duration", true, false);
  159. // NMTOKENS, IDREFS, ENTITIES - lists
  160. XsIDRefs = new XmlSchemaSimpleType ();
  161. XmlSchemaSimpleTypeList sl = new XmlSchemaSimpleTypeList ();
  162. sl.ItemType = XsIDRef;
  163. XsIDRefs.Content = sl;
  164. XsEntities = new XmlSchemaSimpleType ();
  165. sl = new XmlSchemaSimpleTypeList ();
  166. sl.ItemType = XsEntity;
  167. XsEntities.Content = sl;
  168. XsNMTokens = new XmlSchemaSimpleType ();
  169. sl = new XmlSchemaSimpleTypeList ();
  170. sl.ItemType = XsNMToken;
  171. XsNMTokens.Content = sl;
  172. #endif
  173. }
  174. #if NET_2_0
  175. private static XmlSchemaSimpleType BuildSchemaType (string name, string baseName)
  176. {
  177. return BuildSchemaType (name, baseName, false, false);
  178. }
  179. private static XmlSchemaSimpleType BuildSchemaType (string name, string baseName, bool xdt, bool baseXdt)
  180. {
  181. string ns = xdt ? "http://www.w3.org/2003/11/xpath-datatypes" : XmlSchema.Namespace;
  182. string ns2 = baseXdt ? "http://www.w3.org/2003/11/xpath-datatypes" : XmlSchema.Namespace;
  183. XmlSchemaSimpleType st = new XmlSchemaSimpleType ();
  184. st.QNameInternal = new XmlQualifiedName (name, ns);
  185. if (baseName != null)
  186. st.BaseXmlSchemaTypeInternal = XmlSchemaType. GetBuiltInSimpleType (new XmlQualifiedName (baseName, ns2));
  187. st.DatatypeInternal = XmlSchemaDatatype.FromName (st.QualifiedName);
  188. return st;
  189. }
  190. #endif
  191. internal static XsdAnySimpleType AnySimpleType {
  192. get { return XsdAnySimpleType.Instance; }
  193. }
  194. internal static XmlSchemaSimpleType SchemaLocationType {
  195. get { return schemaLocationType; }
  196. }
  197. public XmlSchemaSimpleType ()
  198. {
  199. }
  200. [XmlElement("restriction",typeof(XmlSchemaSimpleTypeRestriction))]
  201. [XmlElement("list",typeof(XmlSchemaSimpleTypeList))]
  202. [XmlElement("union",typeof(XmlSchemaSimpleTypeUnion))]
  203. public XmlSchemaSimpleTypeContent Content
  204. {
  205. get{ return content; }
  206. set{ content = value; }
  207. }
  208. internal XmlSchemaDerivationMethod Variety
  209. {
  210. get{ return variety; }
  211. }
  212. internal override void SetParent (XmlSchemaObject parent)
  213. {
  214. base.SetParent (parent);
  215. if (Content != null)
  216. Content.SetParent (this);
  217. }
  218. /// <remarks>
  219. /// For a simple Type:
  220. /// 1. Content must be present
  221. /// 2. id if present, must have be a valid ID
  222. /// a) If the simpletype is local
  223. /// 1- are from <xs:complexType name="simpleType"> and <xs:complexType name="localSimpleType">
  224. /// 1. name is prohibited
  225. /// 2. final is prohibited
  226. /// b) If the simpletype is toplevel
  227. /// 1- are from <xs:complexType name="simpleType"> and <xs:complexType name="topLevelSimpleType">
  228. /// 1. name is required, type must be NCName
  229. /// 2. Content is required
  230. /// 3. final can have values : #all | (list | union | restriction)
  231. /// 4. If final is set, finalResolved is same as final (but within the values of b.3)
  232. /// 5. If final is not set, the finalDefault of the schema (ie. only #all and restriction)
  233. /// 6. Base type is:
  234. /// 4.1 If restriction is chosen,the base type of restriction or elements
  235. /// 4.2 otherwise simple ur-type
  236. /// </remarks>
  237. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  238. {
  239. // If this is already compiled this time, simply skip.
  240. if (CompilationId == schema.CompilationId)
  241. return 0;
  242. errorCount = 0;
  243. if(this.islocal) // a
  244. {
  245. if(this.Name != null) // a.1
  246. error(h,"Name is prohibited in a local simpletype");
  247. else
  248. this.QNameInternal = new XmlQualifiedName(this.Name, AncestorSchema.TargetNamespace);
  249. if(this.Final != XmlSchemaDerivationMethod.None) //a.2
  250. error(h,"Final is prohibited in a local simpletype");
  251. }
  252. else //b
  253. {
  254. if(this.Name == null) //b.1
  255. error(h,"Name is required in top level simpletype");
  256. else if(!XmlSchemaUtil.CheckNCName(this.Name)) // b.1.2
  257. error(h,"name attribute of a simpleType must be NCName");
  258. else
  259. this.QNameInternal = new XmlQualifiedName(this.Name, AncestorSchema.TargetNamespace);
  260. //NOTE: Although the FinalResolved can be Empty, it is not a valid value for Final
  261. //DEVIATION: If an error occurs, the finaldefault is always consulted. This deviates
  262. // from the way MS implementation works.
  263. switch(this.Final) //b.3, b.4
  264. {
  265. case XmlSchemaDerivationMethod.All:
  266. this.finalResolved = XmlSchemaDerivationMethod.All;
  267. break;
  268. case XmlSchemaDerivationMethod.List:
  269. case XmlSchemaDerivationMethod.Union:
  270. case XmlSchemaDerivationMethod.Restriction:
  271. this.finalResolved = Final;
  272. break;
  273. default:
  274. error(h,"The value of final attribute is not valid for simpleType");
  275. goto case XmlSchemaDerivationMethod.None;
  276. // use assignment from finaldefault on schema.
  277. case XmlSchemaDerivationMethod.None: // b.5
  278. XmlSchemaDerivationMethod flags =
  279. (XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.List |
  280. XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Union );
  281. switch (schema.FinalDefault) {
  282. case XmlSchemaDerivationMethod.All:
  283. finalResolved = XmlSchemaDerivationMethod.All;
  284. break;
  285. case XmlSchemaDerivationMethod.None:
  286. finalResolved = XmlSchemaDerivationMethod.Empty;
  287. break;
  288. default:
  289. finalResolved = schema.FinalDefault & flags;
  290. break;
  291. }
  292. break;
  293. }
  294. }
  295. XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
  296. if (Content != null)
  297. Content.OwnerType = this;
  298. if(this.Content == null) //a.3,b.2
  299. error(h,"Content is required in a simpletype");
  300. else if(Content is XmlSchemaSimpleTypeRestriction)
  301. {
  302. this.resolvedDerivedBy = XmlSchemaDerivationMethod.Restriction;
  303. errorCount += ((XmlSchemaSimpleTypeRestriction)Content).Compile(h,schema);
  304. }
  305. else if(Content is XmlSchemaSimpleTypeList)
  306. {
  307. this.resolvedDerivedBy = XmlSchemaDerivationMethod.List;
  308. errorCount += ((XmlSchemaSimpleTypeList)Content).Compile(h,schema);
  309. }
  310. else if(Content is XmlSchemaSimpleTypeUnion)
  311. {
  312. this.resolvedDerivedBy = XmlSchemaDerivationMethod.Union;
  313. errorCount += ((XmlSchemaSimpleTypeUnion)Content).Compile(h,schema);
  314. }
  315. this.CompilationId = schema.CompilationId;
  316. return errorCount;
  317. }
  318. internal void CollectBaseType (ValidationEventHandler h, XmlSchema schema)
  319. {
  320. if (Content is XmlSchemaSimpleTypeRestriction) {
  321. object o = ((XmlSchemaSimpleTypeRestriction) Content).GetActualType (h, schema, false);
  322. BaseXmlSchemaTypeInternal = o as XmlSchemaSimpleType;
  323. if (BaseXmlSchemaTypeInternal != null)
  324. DatatypeInternal = BaseXmlSchemaTypeInternal.Datatype;
  325. else
  326. DatatypeInternal = o as XmlSchemaDatatype;
  327. }
  328. // otherwise, actualBaseSchemaType is null
  329. else
  330. DatatypeInternal = XmlSchemaSimpleType.AnySimpleType;
  331. }
  332. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  333. {
  334. // 3.14.6 Properties Correct.
  335. //
  336. // 1. Post Compilation Properties
  337. // {name}, {target namespace} => QNameInternal. Already Compile()d.
  338. // {base type definition} => baseSchemaTypeInternal
  339. // {final} => finalResolved. Already Compile()d.
  340. // {variety} => resolvedDerivedBy. Already Compile()d.
  341. //
  342. // 2. Should be checked by "recursed" field.
  343. if(IsValidated (schema.ValidationId))
  344. return errorCount;
  345. if (recursed) {
  346. error (h, "Circular type reference was found.");
  347. return errorCount;
  348. }
  349. recursed = true;
  350. CollectBaseType (h, schema);
  351. if (content != null)
  352. errorCount += content.Validate (h, schema);
  353. /*
  354. // BaseSchemaType property
  355. BaseXmlSchemaTypeInternal = content.ActualBaseSchemaType as XmlSchemaType;
  356. if (this.BaseXmlSchemaTypeInternal == null)
  357. this.DatatypeInternal = content.ActualBaseSchemaType as XmlSchemaDatatype;
  358. */
  359. // Datatype property
  360. XmlSchemaSimpleType simple = BaseXmlSchemaType as XmlSchemaSimpleType;
  361. if (simple != null)
  362. this.DatatypeInternal = simple.Datatype;
  363. // else
  364. // DatatypeInternal = BaseSchemaType as XmlSchemaDatatype;
  365. // 3.
  366. XmlSchemaSimpleType baseSType = BaseXmlSchemaType as XmlSchemaSimpleType;
  367. if (baseSType != null) {
  368. if ((baseSType.FinalResolved & this.resolvedDerivedBy) != 0)
  369. error (h, "Specified derivation is prohibited by the base simple type.");
  370. }
  371. // {variety}
  372. if (this.resolvedDerivedBy == XmlSchemaDerivationMethod.Restriction &&
  373. baseSType != null)
  374. this.variety = baseSType.Variety;
  375. else
  376. this.variety = this.resolvedDerivedBy;
  377. // 3.14.6 Derivation Valid (Restriction, Simple)
  378. XmlSchemaSimpleTypeRestriction r = Content as XmlSchemaSimpleTypeRestriction;
  379. object baseType = BaseXmlSchemaType != null ? (object) BaseXmlSchemaType : Datatype;
  380. if (r != null)
  381. ValidateDerivationValid (baseType, r.Facets, h, schema);
  382. // TODO: describe which validation term this belongs to.
  383. XmlSchemaSimpleTypeList l = Content as XmlSchemaSimpleTypeList;
  384. if (l != null) {
  385. XmlSchemaSimpleType itemSimpleType = l.ValidatedListItemType as XmlSchemaSimpleType;
  386. if (itemSimpleType != null && itemSimpleType.Content is XmlSchemaSimpleTypeList)
  387. error (h, "List type must not be derived from another list type.");
  388. }
  389. recursed = false;
  390. ValidationId = schema.ValidationId;
  391. return errorCount;
  392. }
  393. // 3.14.6 Derivation Valid (RestrictionSimple)
  394. internal void ValidateDerivationValid (object baseType, XmlSchemaObjectCollection facets,
  395. ValidationEventHandler h, XmlSchema schema)
  396. {
  397. // TODO
  398. XmlSchemaSimpleType baseSimpleType = baseType as XmlSchemaSimpleType;
  399. switch (this.Variety) {
  400. // 1. atomic type
  401. case XmlSchemaDerivationMethod.Restriction:
  402. // 1.1
  403. if (baseSimpleType != null && baseSimpleType.resolvedDerivedBy != XmlSchemaDerivationMethod.Restriction)
  404. error (h, "Base schema type is not either atomic type or primitive type.");
  405. // 1.2
  406. if (baseSimpleType != null &&
  407. (baseSimpleType.FinalResolved & XmlSchemaDerivationMethod.Restriction) != 0)
  408. error (h, "Derivation by restriction is prohibited by the base simple type.");
  409. // TODO: 1.3 facet restriction valid.
  410. break;
  411. case XmlSchemaDerivationMethod.List:
  412. /*
  413. XmlSchemaSimpleTypeList thisList = Content as XmlSchemaSimpleTypeList;
  414. // 2.1 item list type not allowed
  415. if (baseSimpleType != null && baseSimpleType.resolvedDerivedBy == XmlSchemaDerivationMethod.List)
  416. error (h, "Base list schema type is not allowed.");
  417. XmlSchemaSimpleTypeUnion baseUnion = baseSimpleType.Content as XmlSchemaSimpleTypeUnion;
  418. if (baseUnion != null) {
  419. bool errorFound = false;
  420. foreach (object memberType in baseUnion.ValidatedTypes) {
  421. XmlSchemaSimpleType memberST = memberType as XmlSchemaSimpleType;
  422. if (memberST != null && memberST.resolvedDerivedBy == XmlSchemaDerivationMethod.List)
  423. errorFound = true;
  424. }
  425. if (errorFound)
  426. error (h, "Base union schema type should not contain list types.");
  427. }
  428. */
  429. // 2.2 facets limited
  430. if (facets != null)
  431. foreach (XmlSchemaFacet facet in facets) {
  432. if (facet is XmlSchemaLengthFacet ||
  433. facet is XmlSchemaMaxLengthFacet ||
  434. facet is XmlSchemaMinLengthFacet ||
  435. facet is XmlSchemaEnumerationFacet ||
  436. facet is XmlSchemaPatternFacet)
  437. continue;
  438. else
  439. error (h, "Not allowed facet was found on this simple type which derives list type.");
  440. }
  441. break;
  442. case XmlSchemaDerivationMethod.Union:
  443. // 3.1
  444. // 3.2
  445. if (facets != null)
  446. foreach (XmlSchemaFacet facet in facets) {
  447. if (facet is XmlSchemaEnumerationFacet ||
  448. facet is XmlSchemaPatternFacet)
  449. continue;
  450. else
  451. error (h, "Not allowed facet was found on this simple type which derives list type.");
  452. }
  453. break;
  454. }
  455. }
  456. // 3.14.6 Type Derivation OK (Simple)
  457. internal bool ValidateTypeDerivationOK (object baseType,
  458. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  459. {
  460. // 1
  461. // Note that anyType should also be allowed as anySimpleType.
  462. if (this == baseType || baseType == XmlSchemaSimpleType.AnySimpleType ||
  463. baseType == XmlSchemaComplexType.AnyType)
  464. return true;
  465. // 2.1
  466. XmlSchemaSimpleType baseSimpleType = baseType as XmlSchemaSimpleType;
  467. if (baseSimpleType != null &&
  468. (baseSimpleType.FinalResolved & resolvedDerivedBy) != 0) {
  469. if (raiseError)
  470. error (h, "Specified derivation is prohibited by the base type.");
  471. return false;
  472. }
  473. // 2.2.1
  474. if (BaseXmlSchemaType == baseType || Datatype == baseType)
  475. return true;
  476. // 2.2.2
  477. XmlSchemaSimpleType thisBaseSimpleType = BaseXmlSchemaType as XmlSchemaSimpleType;
  478. if (thisBaseSimpleType != null) {
  479. if (thisBaseSimpleType.ValidateTypeDerivationOK (baseType, h, schema, false))
  480. return true;
  481. }
  482. // 2.2.3
  483. switch (Variety) {
  484. case XmlSchemaDerivationMethod.Union:
  485. case XmlSchemaDerivationMethod.List:
  486. if (baseType == XmlSchemaSimpleType.AnySimpleType)
  487. return true;
  488. break;
  489. }
  490. // 2.2.4 validly derived from one of the union member type.
  491. if (baseSimpleType != null && baseSimpleType.Variety == XmlSchemaDerivationMethod.Union) {
  492. foreach (object memberType in ((XmlSchemaSimpleTypeUnion) baseSimpleType.Content).ValidatedTypes)
  493. if (this.ValidateTypeDerivationOK (memberType, h, schema, false))
  494. return true;
  495. }
  496. if (raiseError)
  497. error(h, "Invalid simple type derivation was found.");
  498. return false;
  499. }
  500. internal string Normalize (string s, XmlNameTable nt, XmlNamespaceManager nsmgr)
  501. {
  502. return Content.Normalize (s, nt, nsmgr);
  503. }
  504. //<simpleType
  505. // final = (#all | (list | union | restriction))
  506. // id = ID
  507. // name = NCName
  508. // {any attributes with non-schema namespace . . .}>
  509. // Content: (annotation?, (restriction | list | union))
  510. //</simpleType>
  511. internal static XmlSchemaSimpleType Read(XmlSchemaReader reader, ValidationEventHandler h)
  512. {
  513. XmlSchemaSimpleType stype = new XmlSchemaSimpleType();
  514. reader.MoveToElement();
  515. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  516. {
  517. error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
  518. reader.Skip();
  519. return null;
  520. }
  521. stype.LineNumber = reader.LineNumber;
  522. stype.LinePosition = reader.LinePosition;
  523. stype.SourceUri = reader.BaseURI;
  524. while(reader.MoveToNextAttribute())
  525. {
  526. if(reader.Name == "final")
  527. {
  528. Exception innerex;
  529. stype.Final = XmlSchemaUtil.ReadDerivationAttribute(reader, out innerex, "final",
  530. XmlSchemaUtil.FinalAllowed);
  531. if(innerex != null)
  532. error(h, "some invalid values not a valid value for final", innerex);
  533. }
  534. else if(reader.Name == "id")
  535. {
  536. stype.Id = reader.Value;
  537. }
  538. else if(reader.Name == "name")
  539. {
  540. stype.Name = reader.Value;
  541. }
  542. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  543. {
  544. error(h,reader.Name + " is not a valid attribute for simpleType",null);
  545. }
  546. else
  547. {
  548. XmlSchemaUtil.ReadUnhandledAttribute(reader,stype);
  549. }
  550. }
  551. reader.MoveToElement();
  552. if(reader.IsEmptyElement)
  553. return stype;
  554. // Content: (annotation?, (restriction | list | union))
  555. int level = 1;
  556. while(reader.ReadNextElement())
  557. {
  558. if(reader.NodeType == XmlNodeType.EndElement)
  559. {
  560. if(reader.LocalName != xmlname)
  561. error(h,"Should not happen :2: XmlSchemaSimpleType.Read, name="+reader.Name,null);
  562. break;
  563. }
  564. if(level <= 1 && reader.LocalName == "annotation")
  565. {
  566. level = 2; //Only one annotation
  567. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  568. if(annotation != null)
  569. stype.Annotation = annotation;
  570. continue;
  571. }
  572. if(level <= 2)
  573. {
  574. if(reader.LocalName == "restriction")
  575. {
  576. level = 3;
  577. XmlSchemaSimpleTypeRestriction restriction = XmlSchemaSimpleTypeRestriction.Read(reader,h);
  578. if(restriction != null)
  579. stype.content = restriction;
  580. continue;
  581. }
  582. if(reader.LocalName == "list")
  583. {
  584. level = 3;
  585. XmlSchemaSimpleTypeList list = XmlSchemaSimpleTypeList.Read(reader,h);
  586. if(list != null)
  587. stype.content = list;
  588. continue;
  589. }
  590. if(reader.LocalName == "union")
  591. {
  592. level = 3;
  593. XmlSchemaSimpleTypeUnion union = XmlSchemaSimpleTypeUnion.Read(reader,h);
  594. if(union != null)
  595. stype.content = union;
  596. continue;
  597. }
  598. }
  599. reader.RaiseInvalidElementError();
  600. }
  601. return stype;
  602. }
  603. }
  604. }