XmlSchemaElement.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. //
  2. // System.Xml.Schema.XmlSchemaElement.cs
  3. //
  4. // Authors:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Enomoto, Atsushi [email protected]
  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.Collections;
  30. using System.Xml;
  31. using System.Xml.Serialization;
  32. using System.ComponentModel;
  33. namespace System.Xml.Schema
  34. {
  35. /// <summary>
  36. /// Summary description for XmlSchemaElement.
  37. /// </summary>
  38. public class XmlSchemaElement : XmlSchemaParticle
  39. {
  40. private XmlSchemaDerivationMethod block;
  41. private XmlSchemaObjectCollection constraints;
  42. private string defaultValue;
  43. private object elementType;
  44. #if NET_2_0
  45. private XmlSchemaType elementSchemaType;
  46. #endif
  47. private XmlSchemaDerivationMethod final;
  48. private string fixedValue;
  49. private XmlSchemaForm form;
  50. private bool isAbstract;
  51. private bool isNillable;
  52. private string name;
  53. private XmlQualifiedName refName;
  54. private XmlSchemaType schemaType;
  55. private XmlQualifiedName schemaTypeName;
  56. private XmlQualifiedName substitutionGroup;
  57. // Post compilation items.
  58. XmlSchema schema;
  59. internal bool parentIsSchema = false;
  60. private XmlQualifiedName qName;
  61. private XmlSchemaDerivationMethod blockResolved;
  62. private XmlSchemaDerivationMethod finalResolved;
  63. // private XmlSchemaParticle substChoice;
  64. private XmlSchemaElement referencedElement;
  65. private ArrayList substitutingElements = new ArrayList ();
  66. private XmlSchemaElement substitutionGroupElement;
  67. private bool actualIsAbstract;
  68. private bool actualIsNillable;
  69. private string validatedDefaultValue;
  70. private string validatedFixedValue;
  71. const string xmlname = "element";
  72. public XmlSchemaElement()
  73. {
  74. block = XmlSchemaDerivationMethod.None;
  75. final = XmlSchemaDerivationMethod.None;
  76. constraints = new XmlSchemaObjectCollection();
  77. refName = XmlQualifiedName.Empty;
  78. schemaTypeName = XmlQualifiedName.Empty;
  79. substitutionGroup = XmlQualifiedName.Empty;
  80. InitPostCompileInformations ();
  81. }
  82. private void InitPostCompileInformations ()
  83. {
  84. qName = XmlQualifiedName.Empty;
  85. schema = null;
  86. // parentIsSchema = false; ... it is set in Schema's Compile()
  87. blockResolved = XmlSchemaDerivationMethod.None;
  88. finalResolved = XmlSchemaDerivationMethod.None;
  89. // substChoice = null;
  90. referencedElement = null;
  91. substitutingElements.Clear ();
  92. substitutionGroupElement = null;
  93. actualIsAbstract = false;
  94. actualIsNillable = false;
  95. validatedDefaultValue = null;
  96. validatedFixedValue = null;
  97. }
  98. #region Attributes
  99. [DefaultValue(false)]
  100. [System.Xml.Serialization.XmlAttribute("abstract")]
  101. public bool IsAbstract
  102. {
  103. get{ return isAbstract; }
  104. set{ isAbstract = value; }
  105. }
  106. [DefaultValue(XmlSchemaDerivationMethod.None)]
  107. [System.Xml.Serialization.XmlAttribute("block")]
  108. public XmlSchemaDerivationMethod Block
  109. {
  110. get{ return block; }
  111. set{ block = value; }
  112. }
  113. [DefaultValue(null)]
  114. [System.Xml.Serialization.XmlAttribute("default")]
  115. public string DefaultValue
  116. {
  117. get{ return defaultValue; }
  118. set{ defaultValue = value; }
  119. }
  120. [DefaultValue(XmlSchemaDerivationMethod.None)]
  121. [System.Xml.Serialization.XmlAttribute("final")]
  122. public XmlSchemaDerivationMethod Final
  123. {
  124. get{ return final; }
  125. set{ final = value; }
  126. }
  127. [DefaultValue(null)]
  128. [System.Xml.Serialization.XmlAttribute("fixed")]
  129. public string FixedValue
  130. {
  131. get{ return fixedValue; }
  132. set{ fixedValue = value; }
  133. }
  134. [DefaultValue(XmlSchemaForm.None)]
  135. [System.Xml.Serialization.XmlAttribute("form")]
  136. public XmlSchemaForm Form
  137. {
  138. get{ return form; }
  139. set{ form = value; }
  140. }
  141. [DefaultValue("")]
  142. [System.Xml.Serialization.XmlAttribute("name")]
  143. public string Name
  144. {
  145. get{ return name; }
  146. set{ name = value; }
  147. }
  148. [DefaultValue(false)]
  149. [System.Xml.Serialization.XmlAttribute("nillable")]
  150. public bool IsNillable
  151. {
  152. get{ return isNillable; }
  153. set{ isNillable = value; }
  154. }
  155. [System.Xml.Serialization.XmlAttribute("ref")]
  156. public XmlQualifiedName RefName
  157. {
  158. get{ return refName; }
  159. set{ refName = value;}
  160. }
  161. [System.Xml.Serialization.XmlAttribute("substitutionGroup")]
  162. public XmlQualifiedName SubstitutionGroup
  163. {
  164. get{ return substitutionGroup; }
  165. set{ substitutionGroup = value; }
  166. }
  167. [System.Xml.Serialization.XmlAttribute("type")]
  168. public XmlQualifiedName SchemaTypeName
  169. {
  170. get{ return schemaTypeName; }
  171. set{ schemaTypeName = value; }
  172. }
  173. #endregion
  174. #region Elements
  175. [XmlElement("simpleType",typeof(XmlSchemaSimpleType))]
  176. [XmlElement("complexType",typeof(XmlSchemaComplexType))]
  177. public XmlSchemaType SchemaType
  178. {
  179. get{ return schemaType; }
  180. set{ schemaType = value; }
  181. }
  182. [XmlElement("unique",typeof(XmlSchemaUnique))]
  183. [XmlElement("key",typeof(XmlSchemaKey))]
  184. [XmlElement("keyref",typeof(XmlSchemaKeyref))]
  185. public XmlSchemaObjectCollection Constraints
  186. {
  187. get{ return constraints; }
  188. }
  189. #endregion
  190. #region Post Compilation Schema Info
  191. [XmlIgnore]
  192. public XmlQualifiedName QualifiedName
  193. {
  194. get{ return qName; }
  195. }
  196. [XmlIgnore]
  197. #if NET_2_0
  198. [Obsolete]
  199. #endif
  200. public object ElementType
  201. {
  202. get {
  203. if (referencedElement != null)
  204. return referencedElement.ElementType;
  205. else
  206. return elementType;
  207. }
  208. }
  209. #if NET_2_0
  210. [XmlIgnore]
  211. public XmlSchemaType ElementSchemaType
  212. {
  213. get {
  214. if (referencedElement != null)
  215. return referencedElement.ElementSchemaType;
  216. else
  217. return elementSchemaType;
  218. }
  219. }
  220. #endif
  221. [XmlIgnore]
  222. public XmlSchemaDerivationMethod BlockResolved
  223. {
  224. get{
  225. if (referencedElement != null)
  226. return referencedElement.BlockResolved;
  227. else
  228. return blockResolved;
  229. }
  230. }
  231. [XmlIgnore]
  232. public XmlSchemaDerivationMethod FinalResolved
  233. {
  234. get{
  235. if (referencedElement != null)
  236. return referencedElement.FinalResolved;
  237. else
  238. return finalResolved;
  239. }
  240. }
  241. internal bool ActualIsNillable {
  242. get {
  243. if (referencedElement != null)
  244. return referencedElement.ActualIsNillable;
  245. else
  246. return actualIsNillable;
  247. }
  248. }
  249. internal bool ActualIsAbstract {
  250. get {
  251. if (referencedElement != null)
  252. return referencedElement.ActualIsAbstract;
  253. else
  254. return actualIsAbstract;
  255. }
  256. }
  257. // Post compilation default value (normalized)
  258. internal string ValidatedDefaultValue {
  259. get{
  260. if (referencedElement != null)
  261. return referencedElement.ValidatedDefaultValue;
  262. else
  263. return validatedDefaultValue;
  264. }
  265. }
  266. // Post compilation fixed value (normalized)
  267. internal string ValidatedFixedValue {
  268. get{
  269. if (referencedElement != null)
  270. return referencedElement.ValidatedFixedValue;
  271. else
  272. return validatedFixedValue;
  273. }
  274. }
  275. internal ArrayList SubstitutingElements {
  276. get {
  277. if (referencedElement != null)
  278. return referencedElement.SubstitutingElements;
  279. else
  280. return this.substitutingElements;
  281. }
  282. }
  283. internal XmlSchemaElement SubstitutionGroupElement {
  284. get {
  285. if (referencedElement != null)
  286. return referencedElement.SubstitutionGroupElement;
  287. else
  288. return substitutionGroupElement;
  289. }
  290. }
  291. #endregion
  292. /// <remarks>
  293. /// a) If Element has parent as schema:
  294. /// 1. name must be present and of type NCName.
  295. /// 2. ref must be absent
  296. /// 3. form must be absent
  297. /// 4. minOccurs must be absent
  298. /// 5. maxOccurs must be absent
  299. /// b) If Element has parent is not schema and ref is absent
  300. /// 1. name must be present and of type NCName.
  301. /// 2. if form equals qualified or form is absent and schema's formdefault is qualifed,
  302. /// targetNamespace is schema's targetnamespace else empty.
  303. /// 3. type and either <simpleType> or <complexType> are mutually exclusive
  304. /// 4. default and fixed must not both be present.
  305. /// 5. substitutiongroup must be absent
  306. /// 6. final must be absent
  307. /// 7. abstract must be absent
  308. /// c) if the parent is not schema and ref is set
  309. /// 1. name must not be present
  310. /// 2. all of <simpleType>,<complexType>, <key>, <keyref>, <unique>, nillable,
  311. /// default, fixed, form, block and type, must be absent.
  312. /// 3. substitutiongroup is prohibited
  313. /// 4. final is prohibited
  314. /// 5. abstract is prohibited
  315. /// 6. default and fixed must not both be present.(Actually both are absent)
  316. /// </remarks>
  317. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  318. {
  319. // If this is already compiled this time, simply skip.
  320. if (this.IsComplied (schema.CompilationId))
  321. return 0;
  322. InitPostCompileInformations ();
  323. this.schema = schema;
  324. #if NET_2_0
  325. if (SchemaType != null)
  326. SchemaType.Parent = this;
  327. foreach (XmlSchemaObject obj in Constraints)
  328. obj.Parent = this;
  329. #endif
  330. if(this.defaultValue != null && this.fixedValue != null)
  331. error(h,"both default and fixed can't be present");
  332. if(parentIsSchema || isRedefineChild)
  333. {
  334. if(this.refName != null && !RefName.IsEmpty)
  335. error(h,"ref must be absent");
  336. if(this.name == null) //b1
  337. error(h,"Required attribute name must be present");
  338. else if(!XmlSchemaUtil.CheckNCName(this.name)) // b1.2
  339. error(h,"attribute name must be NCName");
  340. else
  341. this.qName = new XmlQualifiedName (this.name, schema.TargetNamespace);
  342. if(form != XmlSchemaForm.None)
  343. error(h,"form must be absent");
  344. if(MinOccursString != null)
  345. error(h,"minOccurs must be absent");
  346. if(MaxOccursString != null)
  347. error(h,"maxOccurs must be absent");
  348. XmlSchemaDerivationMethod allfinal = (XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Restriction);
  349. if(final == XmlSchemaDerivationMethod.All)
  350. finalResolved = allfinal;
  351. else if(final == XmlSchemaDerivationMethod.None)
  352. finalResolved = XmlSchemaDerivationMethod.Empty;
  353. else
  354. {
  355. // if((final & ~allfinal) != 0)
  356. if ((final | XmlSchemaUtil.FinalAllowed) != XmlSchemaUtil.FinalAllowed)
  357. error (h,"some values for final are invalid in this context");
  358. finalResolved = final & allfinal;
  359. }
  360. if(schemaType != null && schemaTypeName != null && !schemaTypeName.IsEmpty)
  361. {
  362. error(h,"both schemaType and content can't be present");
  363. }
  364. //Even if both are present, read both of them.
  365. if(schemaType != null)
  366. {
  367. if(schemaType is XmlSchemaSimpleType)
  368. {
  369. errorCount += ((XmlSchemaSimpleType)schemaType).Compile(h,schema);
  370. }
  371. else if(schemaType is XmlSchemaComplexType)
  372. {
  373. errorCount += ((XmlSchemaComplexType)schemaType).Compile(h,schema);
  374. }
  375. else
  376. error(h,"only simpletype or complextype is allowed");
  377. }
  378. if(schemaTypeName != null && !schemaTypeName.IsEmpty)
  379. {
  380. if(!XmlSchemaUtil.CheckQName(SchemaTypeName))
  381. error(h,"SchemaTypeName must be an XmlQualifiedName");
  382. }
  383. if(SubstitutionGroup != null && !SubstitutionGroup.IsEmpty)
  384. {
  385. if(!XmlSchemaUtil.CheckQName(SubstitutionGroup))
  386. error(h,"SubstitutionGroup must be a valid XmlQualifiedName");
  387. }
  388. foreach(XmlSchemaObject obj in constraints)
  389. {
  390. if(obj is XmlSchemaUnique)
  391. errorCount += ((XmlSchemaUnique)obj).Compile(h,schema);
  392. else if(obj is XmlSchemaKey)
  393. errorCount += ((XmlSchemaKey)obj).Compile(h,schema);
  394. else if(obj is XmlSchemaKeyref)
  395. errorCount += ((XmlSchemaKeyref)obj).Compile(h,schema);
  396. }
  397. }
  398. else
  399. {
  400. if(substitutionGroup != null && !substitutionGroup.IsEmpty)
  401. error(h,"substitutionGroup must be absent");
  402. if(final != XmlSchemaDerivationMethod.None)
  403. error(h,"final must be absent");
  404. CompileOccurence (h, schema);
  405. if(refName == null || RefName.IsEmpty)
  406. {
  407. string targetNamespace = String.Empty;
  408. if(form == XmlSchemaForm.Qualified || (form == XmlSchemaForm.None && schema.ElementFormDefault == XmlSchemaForm.Qualified))
  409. targetNamespace = schema.TargetNamespace;
  410. if(this.name == null) //b1
  411. error(h,"Required attribute name must be present");
  412. else if(!XmlSchemaUtil.CheckNCName(this.name)) // b1.2
  413. error(h,"attribute name must be NCName");
  414. else
  415. this.qName = new XmlQualifiedName(this.name, targetNamespace);
  416. if(schemaType != null && schemaTypeName != null && !schemaTypeName.IsEmpty)
  417. {
  418. error(h,"both schemaType and content can't be present");
  419. }
  420. //Even if both are present, read both of them.
  421. if(schemaType != null)
  422. {
  423. if(schemaType is XmlSchemaSimpleType)
  424. {
  425. errorCount += ((XmlSchemaSimpleType)schemaType).Compile(h,schema);
  426. }
  427. else if(schemaType is XmlSchemaComplexType)
  428. {
  429. errorCount += ((XmlSchemaComplexType)schemaType).Compile(h,schema);
  430. }
  431. else
  432. error(h,"only simpletype or complextype is allowed");
  433. }
  434. if(schemaTypeName != null && !schemaTypeName.IsEmpty)
  435. {
  436. if(!XmlSchemaUtil.CheckQName(SchemaTypeName))
  437. error(h,"SchemaTypeName must be an XmlQualifiedName");
  438. }
  439. if(SubstitutionGroup != null && !SubstitutionGroup.IsEmpty)
  440. {
  441. if(!XmlSchemaUtil.CheckQName(SubstitutionGroup))
  442. error(h,"SubstitutionGroup must be a valid XmlQualifiedName");
  443. }
  444. foreach(XmlSchemaObject obj in constraints)
  445. {
  446. if(obj is XmlSchemaUnique)
  447. errorCount += ((XmlSchemaUnique)obj).Compile(h,schema);
  448. else if(obj is XmlSchemaKey)
  449. errorCount += ((XmlSchemaKey)obj).Compile(h,schema);
  450. else if(obj is XmlSchemaKeyref)
  451. errorCount += ((XmlSchemaKeyref)obj).Compile(h,schema);
  452. }
  453. }
  454. else
  455. {
  456. if(!XmlSchemaUtil.CheckQName(RefName))
  457. error(h,"RefName must be a XmlQualifiedName");
  458. if(name != null)
  459. error(h,"name must not be present when ref is present");
  460. if(Constraints.Count != 0)
  461. error(h,"key, keyref and unique must be absent");
  462. if(isNillable)
  463. error(h,"nillable must be absent");
  464. if(defaultValue != null)
  465. error(h,"default must be absent");
  466. if(fixedValue != null)
  467. error(h,"fixed must be null");
  468. if(form != XmlSchemaForm.None)
  469. error(h,"form must be absent");
  470. if(block != XmlSchemaDerivationMethod.None)
  471. error(h,"block must be absent");
  472. if(schemaTypeName != null && !schemaTypeName.IsEmpty)
  473. error(h,"type must be absent");
  474. if(SchemaType != null)
  475. error(h,"simpleType or complexType must be absent");
  476. qName = RefName;
  477. }
  478. }
  479. switch (block) {
  480. case XmlSchemaDerivationMethod.All:
  481. blockResolved = XmlSchemaDerivationMethod.All;
  482. break;
  483. case XmlSchemaDerivationMethod.None:
  484. blockResolved = XmlSchemaDerivationMethod.Empty;
  485. break;
  486. default:
  487. if ((block | XmlSchemaUtil.ElementBlockAllowed) != XmlSchemaUtil.ElementBlockAllowed)
  488. error (h,"Some of the values for block are invalid in this context");
  489. blockResolved = block;
  490. break;
  491. }
  492. if (Constraints != null) {
  493. XmlSchemaObjectTable table = new XmlSchemaObjectTable ();
  494. foreach (XmlSchemaIdentityConstraint c in Constraints) {
  495. XmlSchemaUtil.AddToTable (table, c, c.QualifiedName, h);
  496. }
  497. }
  498. XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
  499. this.CompilationId = schema.CompilationId;
  500. return errorCount;
  501. }
  502. [MonoTODO ("Return clone in case when it returns itself")]
  503. internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
  504. {
  505. if (OptimizedParticle != null)
  506. return OptimizedParticle;
  507. if (RefName != null && RefName != XmlQualifiedName.Empty) {
  508. referencedElement = schema.Elements [RefName] as XmlSchemaElement;
  509. }
  510. // if (this.referencedElement != null)
  511. // OptimizedParticle = referencedElement.GetOptimizedParticle (isTop);
  512. // else
  513. if (ValidatedMaxOccurs == 0)
  514. OptimizedParticle = XmlSchemaParticle.Empty;
  515. // Substitution Group
  516. else if (SubstitutingElements != null && SubstitutingElements.Count > 0) {
  517. XmlSchemaChoice choice = new XmlSchemaChoice ();
  518. choice.MinOccurs = MinOccurs;
  519. choice.MaxOccurs = MaxOccurs;
  520. // substChoice = choice;
  521. choice.Compile (null, schema); // compute Validated Min/Max Occurs.
  522. XmlSchemaElement item = this.MemberwiseClone () as XmlSchemaElement;
  523. item.MinOccurs = 1;
  524. item.MaxOccurs = 1;
  525. item.substitutionGroupElement = null;
  526. item.substitutingElements = null;
  527. for (int i = 0; i < SubstitutingElements.Count; i++) {
  528. XmlSchemaElement se = SubstitutingElements [i] as XmlSchemaElement;
  529. // choice.Items.Add (se);
  530. // choice.CompiledItems.Add (se);
  531. this.AddSubstElementRecursively (choice.Items, se);
  532. this.AddSubstElementRecursively (choice.CompiledItems, se);
  533. }
  534. if (!choice.Items.Contains (item)) {
  535. choice.Items.Add (item);
  536. choice.CompiledItems.Add (item);
  537. }
  538. OptimizedParticle = choice;
  539. }
  540. else
  541. OptimizedParticle = this;//.MemberwiseClone () as XmlSchemaElement;
  542. return OptimizedParticle;
  543. }
  544. private void AddSubstElementRecursively (XmlSchemaObjectCollection col, XmlSchemaElement el)
  545. {
  546. if (el.SubstitutingElements != null)
  547. for (int i = 0; i < el.SubstitutingElements.Count; i++)
  548. this.AddSubstElementRecursively (col, el.SubstitutingElements [i] as XmlSchemaElement);
  549. if (!col.Contains (el))
  550. col.Add (el);
  551. }
  552. internal void FillSubstitutionElementInfo ()
  553. {
  554. if (this.substitutionGroupElement != null)
  555. return;
  556. if (this.SubstitutionGroup != XmlQualifiedName.Empty) {
  557. XmlSchemaElement substElem = schema.Elements [SubstitutionGroup] as XmlSchemaElement;
  558. this.substitutionGroupElement = substElem;
  559. if (substElem != null)
  560. substElem.substitutingElements.Add (this);
  561. }
  562. }
  563. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  564. {
  565. if (IsValidated (schema.CompilationId))
  566. return errorCount;
  567. // See XML Schema Structures 3.6 for the complete description.
  568. // Element Declaration Properties Correct
  569. // 1. = 3.3.1 (modulo 5.3)
  570. // 3.3.1:
  571. // {annotation} is as is.
  572. // {name}, {target namespace}, {scope}, {disallowed substitution},
  573. // {substitution group exclusions} (handled the same as 'disallowed substitution')
  574. // and {identity-constraint-definitions} are Compile()d.
  575. // {value constraint} is going to be filled in step 2.
  576. // actual {nillable}, {abstract}
  577. this.actualIsNillable = IsNillable;
  578. this.actualIsAbstract = IsAbstract;
  579. // Before determining element type, we need to validate substituting element
  580. if (this.SubstitutionGroup != XmlQualifiedName.Empty) {
  581. XmlSchemaElement substElem = substitutionGroupElement;
  582. if (substElem != null)
  583. substElem.Validate (h, schema);
  584. }
  585. // {type} from here
  586. XmlSchemaDatatype datatype = null;
  587. if (schemaType != null)
  588. elementType = schemaType;
  589. else if (SchemaTypeName != XmlQualifiedName.Empty) {
  590. XmlSchemaType type = schema.SchemaTypes [SchemaTypeName] as XmlSchemaType;
  591. if (type != null) {
  592. type.Validate (h, schema);
  593. elementType = type;
  594. }
  595. else if (SchemaTypeName == XmlSchemaComplexType.AnyTypeName)
  596. elementType = XmlSchemaComplexType.AnyType;
  597. else if (XmlSchemaUtil.IsBuiltInDatatypeName (SchemaTypeName)) {
  598. datatype = XmlSchemaDatatype.FromName (SchemaTypeName);
  599. if (datatype == null)
  600. error (h, "Invalid schema datatype was specified.");
  601. else
  602. elementType = datatype;
  603. }
  604. // otherwise, it might be missing sub components.
  605. else if (!schema.IsNamespaceAbsent (SchemaTypeName.Namespace))
  606. error (h, "Referenced element schema type " + SchemaTypeName + " was not found in the corresponding schema.");
  607. }
  608. else if (RefName != XmlQualifiedName.Empty)
  609. {
  610. XmlSchemaElement refElem = schema.Elements [RefName] as XmlSchemaElement;
  611. // If el is null, then it is missing sub components .
  612. if (refElem != null) {
  613. this.referencedElement = refElem;
  614. errorCount += refElem.Validate (h, schema);
  615. }
  616. // otherwise, it might be missing sub components.
  617. else if (!schema.IsNamespaceAbsent (RefName.Namespace))
  618. error (h, "Referenced element " + RefName + " was not found in the corresponding schema.");
  619. }
  620. // Otherwise if there are substitution group, then the type of the substitution group element.
  621. if (referencedElement == null) {
  622. if (elementType == null && this.substitutionGroupElement != null)
  623. elementType = substitutionGroupElement.ElementType;
  624. // Otherwise, the -ur type- definition.
  625. if (elementType == null)
  626. elementType = XmlSchemaComplexType.AnyType;
  627. }
  628. XmlSchemaType xsType = elementType as XmlSchemaType;
  629. if (xsType != null) {
  630. errorCount += xsType.Validate (h, schema);
  631. datatype = xsType.Datatype;
  632. }
  633. // basic {type} is now filled, except for derivation by {substitution group}.
  634. // {substitution group affiliation}
  635. // 3. subsitution group's type derivation check.
  636. if (this.SubstitutionGroup != XmlQualifiedName.Empty) {
  637. XmlSchemaElement substElem = schema.Elements [SubstitutionGroup] as XmlSchemaElement;
  638. // If el is null, then it is missing sub components .
  639. if (substElem != null) {
  640. XmlSchemaType substSchemaType = substElem.ElementType as XmlSchemaType;
  641. if (substSchemaType != null) {
  642. // 3.3.6 Properties Correct 3.
  643. if ((substElem.FinalResolved & XmlSchemaDerivationMethod.Substitution) != 0)
  644. error (h, "Substituted element blocks substitution.");
  645. if (xsType != null && (substElem.FinalResolved & xsType.DerivedBy) != 0)
  646. error (h, "Invalid derivation was found. Substituted element prohibits this derivation method: " + xsType.DerivedBy + ".");
  647. }
  648. XmlSchemaComplexType xsComplexType = xsType as XmlSchemaComplexType;
  649. if (xsComplexType != null)
  650. xsComplexType.ValidateTypeDerivationOK (substElem.ElementType, h, schema);
  651. else {
  652. XmlSchemaSimpleType xsSimpleType = xsType as XmlSchemaSimpleType;
  653. if (xsSimpleType != null)
  654. xsSimpleType.ValidateTypeDerivationOK (substElem.ElementType, h, schema, true);
  655. }
  656. }
  657. // otherwise, it might be missing sub components.
  658. else if (!schema.IsNamespaceAbsent (SubstitutionGroup.Namespace))
  659. error (h, "Referenced element type " + SubstitutionGroup + " was not found in the corresponding schema.");
  660. }
  661. // 2. ElementDefaultValid
  662. // 4. ID with {value constraint} is prohibited.
  663. if (defaultValue != null || fixedValue != null) {
  664. ValidateElementDefaultValidImmediate (h, schema);
  665. if (datatype != null && // Such situation is basically an error. For ValidationEventHandler.
  666. datatype.TokenizedType == XmlTokenizedType.ID)
  667. error (h, "Element type is ID, which does not allows default or fixed values.");
  668. }
  669. // Identity constraints (3.11.3 / 3.11.6)
  670. foreach (XmlSchemaIdentityConstraint ident in Constraints)
  671. ident.Validate (h, schema);
  672. #if NET_2_0
  673. if (elementType != null) {
  674. elementSchemaType = elementType as XmlSchemaType;
  675. if (elementType == XmlSchemaSimpleType.AnySimpleType)
  676. elementSchemaType = XmlSchemaSimpleType.XsAnySimpleType;
  677. if (elementSchemaType == null)
  678. elementSchemaType = XmlSchemaType.GetBuiltInSimpleType (SchemaTypeName);
  679. }
  680. #endif
  681. ValidationId = schema.ValidationId;
  682. return errorCount;
  683. }
  684. internal override bool ParticleEquals (XmlSchemaParticle other)
  685. {
  686. XmlSchemaElement element = other as XmlSchemaElement;
  687. if (element == null)
  688. return false;
  689. if (this.ValidatedMaxOccurs != element.ValidatedMaxOccurs ||
  690. this.ValidatedMinOccurs != element.ValidatedMinOccurs)
  691. return false;
  692. if (this.QualifiedName != element.QualifiedName ||
  693. this.ElementType != element.ElementType ||
  694. this.Constraints.Count != element.Constraints.Count)
  695. return false;
  696. for (int i = 0; i < this.Constraints.Count; i++) {
  697. XmlSchemaIdentityConstraint c1 = Constraints [i] as XmlSchemaIdentityConstraint;
  698. XmlSchemaIdentityConstraint c2 = element.Constraints [i] as XmlSchemaIdentityConstraint;
  699. if (c1.QualifiedName != c2.QualifiedName ||
  700. c1.Selector.XPath != c2.Selector.XPath ||
  701. c1.Fields.Count != c2.Fields.Count)
  702. return false;
  703. for (int f = 0; f < c1.Fields.Count; f++) {
  704. XmlSchemaXPath f1 = c1.Fields [f] as XmlSchemaXPath;
  705. XmlSchemaXPath f2 = c2.Fields [f] as XmlSchemaXPath;
  706. if (f1.XPath != f2.XPath)
  707. return false;
  708. }
  709. }
  710. if (this.BlockResolved != element.BlockResolved ||
  711. this.FinalResolved != element.FinalResolved ||
  712. this.ValidatedDefaultValue != element.ValidatedDefaultValue ||
  713. this.ValidatedFixedValue != element.ValidatedFixedValue)
  714. return false;
  715. return true;
  716. }
  717. internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  718. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  719. {
  720. // element - NameAndTypeOK
  721. XmlSchemaElement baseElement = baseParticle as XmlSchemaElement;
  722. if (baseElement != null) {
  723. return ValidateDerivationByRestrictionNameAndTypeOK (baseElement, h, schema, raiseError);
  724. }
  725. // any - NSCompat
  726. XmlSchemaAny baseAny = baseParticle as XmlSchemaAny;
  727. if (baseAny != null) {
  728. // NSCompat
  729. if (!baseAny.ValidateWildcardAllowsNamespaceName (this.QualifiedName.Namespace, h, schema, raiseError))
  730. return false;
  731. return ValidateOccurenceRangeOK (baseAny, h, schema, raiseError);
  732. }
  733. //*
  734. // choice - RecurseAsIfGroup
  735. XmlSchemaGroupBase gb = null;
  736. if (baseParticle is XmlSchemaSequence)
  737. gb = new XmlSchemaSequence ();
  738. else if (baseParticle is XmlSchemaChoice)
  739. gb = new XmlSchemaChoice ();
  740. else if (baseParticle is XmlSchemaAll)
  741. gb = new XmlSchemaAll ();
  742. if (gb != null) {
  743. gb.Items.Add (this);
  744. gb.Compile (h, schema);
  745. gb.Validate (h, schema);
  746. // It looks weird, but here we never think about
  747. // _pointlessness_ of this groupbase particle.
  748. return gb.ValidateDerivationByRestriction (baseParticle, h, schema, raiseError);
  749. }
  750. //*/
  751. return true;
  752. }
  753. private bool ValidateDerivationByRestrictionNameAndTypeOK (XmlSchemaElement baseElement,
  754. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  755. {
  756. // 1.
  757. if (this.QualifiedName != baseElement.QualifiedName) {
  758. if (raiseError)
  759. error (h, "Invalid derivation by restriction of particle was found. Both elements must have the same name.");
  760. return false;
  761. }
  762. // 2.
  763. if (this.isNillable && !baseElement.isNillable) {
  764. if (raiseError)
  765. error (h, "Invalid element derivation by restriction of particle was found. Base element is not nillable and derived type is nillable.");
  766. return false;
  767. }
  768. // 3.
  769. if (!ValidateOccurenceRangeOK (baseElement, h, schema, raiseError))
  770. return false;
  771. // 4.
  772. if (baseElement.ValidatedFixedValue != null &&
  773. baseElement.ValidatedFixedValue != this.ValidatedFixedValue) {
  774. if (raiseError)
  775. error (h, "Invalid element derivation by restriction of particle was found. Both fixed value must be the same.");
  776. return false;
  777. }
  778. // 5. TODO: What is "identity constraints subset" ???
  779. // 6.
  780. if ((baseElement.BlockResolved | this.BlockResolved) != this.BlockResolved) {
  781. if (raiseError)
  782. error (h, "Invalid derivation by restriction of particle was found. Derived element must contain all of the base element's block value.");
  783. return false;
  784. }
  785. // 7.
  786. if (baseElement.ElementType != null) {
  787. XmlSchemaComplexType derivedCType = this.ElementType as XmlSchemaComplexType;
  788. if (derivedCType != null) {
  789. // FIXME: W3C REC says that it is Type Derivation OK to be check, but
  790. // in fact it should be DerivationValid (Restriction, Complex).
  791. derivedCType.ValidateDerivationValidRestriction (
  792. baseElement.ElementType as XmlSchemaComplexType, h, schema);
  793. derivedCType.ValidateTypeDerivationOK (baseElement.ElementType, h, schema);
  794. } else {
  795. XmlSchemaSimpleType derivedSType = this.ElementType as XmlSchemaSimpleType;
  796. if (derivedSType != null)
  797. derivedSType.ValidateTypeDerivationOK (baseElement.ElementType, h, schema, true);
  798. else if (baseElement.ElementType != XmlSchemaComplexType.AnyType && baseElement.ElementType != this.ElementType) {
  799. if (raiseError)
  800. error (h, "Invalid element derivation by restriction of particle was found. Both primitive types differ.");
  801. return false;
  802. }
  803. }
  804. }
  805. return true;
  806. }
  807. internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  808. {
  809. XmlSchemaComplexType ct = this.ElementType as XmlSchemaComplexType;
  810. if (ct == null || ct.Particle == null)
  811. return;
  812. ct.Particle.CheckRecursion (depth + 1, h, schema);
  813. }
  814. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  815. ValidationEventHandler h, XmlSchema schema)
  816. {
  817. if (qnames.Contains (this.QualifiedName))// && !this.ParticleEquals ((XmlSchemaParticle) qnames [this.QualifiedName]))
  818. error (h, "Ambiguous element label was detected: " + this.QualifiedName);
  819. else {
  820. foreach (XmlSchemaAny any in nsNames) {
  821. if (any.ValidatedMaxOccurs == 0)
  822. continue;
  823. if (any.HasValueAny ||
  824. any.HasValueLocal && this.QualifiedName.Namespace == "" ||
  825. any.HasValueOther && this.QualifiedName.Namespace != this.QualifiedName.Namespace ||
  826. any.HasValueTargetNamespace && this.QualifiedName.Namespace == this.QualifiedName.Namespace) {
  827. error (h, "Ambiguous element label which is contained by -any- particle was detected: " + this.QualifiedName);
  828. break;
  829. } else if (!any.HasValueOther) {
  830. bool bad = false;
  831. foreach (string ns in any.ResolvedNamespaces) {
  832. if (ns == this.QualifiedName.Namespace) {
  833. bad = true;
  834. break;
  835. }
  836. }
  837. if (bad) {
  838. error (h, "Ambiguous element label which is contained by -any- particle was detected: " + this.QualifiedName);
  839. break;
  840. }
  841. } else {
  842. if (any.TargetNamespace != this.QualifiedName.Namespace)
  843. error (h, "Ambiguous element label which is contained by -any- particle with ##other value was detected: " + this.QualifiedName);
  844. }
  845. }
  846. qnames.Add (this.QualifiedName, this);
  847. }
  848. }
  849. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  850. ValidationEventHandler h, XmlSchema schema)
  851. {
  852. XmlSchemaElement labeled = labels [this.QualifiedName] as XmlSchemaElement;
  853. if (labeled == null)
  854. labels.Add (this.QualifiedName, this);
  855. else if (labeled.ElementType != this.ElementType)
  856. error (h, "Different types are specified on the same named elements in the same sequence. Element name is " + QualifiedName);
  857. }
  858. // 3.3.6 Element Default Valid (Immediate)
  859. private void ValidateElementDefaultValidImmediate (ValidationEventHandler h, XmlSchema schema)
  860. {
  861. // This presumes that ElementType is already filled.
  862. XmlSchemaDatatype datatype = elementType as XmlSchemaDatatype;
  863. XmlSchemaSimpleType simpleType = elementType as XmlSchemaSimpleType;
  864. if (simpleType != null)
  865. datatype = simpleType.Datatype;
  866. if (datatype == null) {
  867. XmlSchemaComplexType complexType = elementType as XmlSchemaComplexType;
  868. switch (complexType.ContentType) {
  869. case XmlSchemaContentType.Empty:
  870. case XmlSchemaContentType.ElementOnly:
  871. error (h, "Element content type must be simple type or mixed.");
  872. break;
  873. }
  874. datatype = XmlSchemaSimpleType.AnySimpleType;
  875. }
  876. XmlNamespaceManager nsmgr = null;
  877. if (datatype.TokenizedType == XmlTokenizedType.QName) {
  878. if (this.Namespaces != null)
  879. foreach (XmlQualifiedName qname in Namespaces.ToArray ())
  880. nsmgr.AddNamespace (qname.Name, qname.Namespace);
  881. }
  882. try {
  883. if (defaultValue != null) {
  884. validatedDefaultValue = datatype.Normalize (defaultValue);
  885. datatype.ParseValue (validatedDefaultValue, null, nsmgr);
  886. }
  887. } catch (Exception ex) {
  888. // FIXME: This is not a good way to handle exception, but
  889. // I think there is no remedy for such Framework specification.
  890. error (h, "The Element's default value is invalid with respect to its type definition.", ex);
  891. }
  892. try {
  893. if (fixedValue != null) {
  894. validatedFixedValue = datatype.Normalize (fixedValue);
  895. datatype.ParseValue (validatedFixedValue, null, nsmgr);
  896. }
  897. } catch (Exception ex) {
  898. // FIXME: This is not a good way to handle exception.
  899. error (h, "The Element's fixed value is invalid with its type definition.", ex);
  900. }
  901. }
  902. //<element
  903. // abstract = boolean : false
  904. // block = (#all | List of (extension | restriction | substitution))
  905. // default = string
  906. // final = (#all | List of (extension | restriction))
  907. // fixed = string
  908. // form = (qualified | unqualified)
  909. // id = ID
  910. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  911. // minOccurs = nonNegativeInteger : 1
  912. // name = NCName
  913. // nillable = boolean : false
  914. // ref = QName
  915. // substitutionGroup = QName
  916. // type = QName
  917. // {any attributes with non-schema namespace . . .}>
  918. // Content: (annotation?, ((simpleType | complexType)?, (unique | key | keyref)*))
  919. //</element>
  920. internal static XmlSchemaElement Read(XmlSchemaReader reader, ValidationEventHandler h)
  921. {
  922. XmlSchemaElement element = new XmlSchemaElement();
  923. Exception innerex;
  924. reader.MoveToElement();
  925. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  926. {
  927. error(h,"Should not happen :1: XmlSchemaElement.Read, name="+reader.Name,null);
  928. reader.Skip();
  929. return null;
  930. }
  931. element.LineNumber = reader.LineNumber;
  932. element.LinePosition = reader.LinePosition;
  933. element.SourceUri = reader.BaseURI;
  934. while(reader.MoveToNextAttribute())
  935. {
  936. if(reader.Name == "abstract")
  937. {
  938. element.IsAbstract = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
  939. if(innerex != null)
  940. error(h,reader.Value + " is invalid value for abstract",innerex);
  941. }
  942. else if(reader.Name == "block")
  943. {
  944. element.block = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "block",
  945. XmlSchemaUtil.ElementBlockAllowed);
  946. if(innerex != null)
  947. error (h,"some invalid values for block attribute were found",innerex);
  948. }
  949. else if(reader.Name == "default")
  950. {
  951. element.defaultValue = reader.Value;
  952. }
  953. else if(reader.Name == "final")
  954. {
  955. element.Final = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "final",
  956. XmlSchemaUtil.FinalAllowed);
  957. if(innerex != null)
  958. error (h,"some invalid values for final attribute were found",innerex);
  959. }
  960. else if(reader.Name == "fixed")
  961. {
  962. element.fixedValue = reader.Value;
  963. }
  964. else if(reader.Name == "form")
  965. {
  966. element.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);
  967. if(innerex != null)
  968. error(h,reader.Value + " is an invalid value for form attribute",innerex);
  969. }
  970. else if(reader.Name == "id")
  971. {
  972. element.Id = reader.Value;
  973. }
  974. else if(reader.Name == "maxOccurs")
  975. {
  976. try
  977. {
  978. element.MaxOccursString = reader.Value;
  979. }
  980. catch(Exception e)
  981. {
  982. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  983. }
  984. }
  985. else if(reader.Name == "minOccurs")
  986. {
  987. try
  988. {
  989. element.MinOccursString = reader.Value;
  990. }
  991. catch(Exception e)
  992. {
  993. error(h,reader.Value + " is an invalid value for minOccurs",e);
  994. }
  995. }
  996. else if(reader.Name == "name")
  997. {
  998. element.Name = reader.Value;
  999. }
  1000. else if(reader.Name == "nillable")
  1001. {
  1002. element.IsNillable = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
  1003. if(innerex != null)
  1004. error(h,reader.Value + "is not a valid value for nillable",innerex);
  1005. }
  1006. else if(reader.Name == "ref")
  1007. {
  1008. element.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  1009. if(innerex != null)
  1010. error(h, reader.Value + " is not a valid value for ref attribute",innerex);
  1011. }
  1012. else if(reader.Name == "substitutionGroup")
  1013. {
  1014. element.substitutionGroup = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  1015. if(innerex != null)
  1016. error(h, reader.Value + " is not a valid value for substitutionGroup attribute",innerex);
  1017. }
  1018. else if(reader.Name == "type")
  1019. {
  1020. element.SchemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  1021. if(innerex != null)
  1022. error(h, reader.Value + " is not a valid value for type attribute",innerex);
  1023. }
  1024. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  1025. {
  1026. error(h,reader.Name + " is not a valid attribute for element",null);
  1027. }
  1028. else
  1029. {
  1030. XmlSchemaUtil.ReadUnhandledAttribute(reader,element);
  1031. }
  1032. }
  1033. reader.MoveToElement();
  1034. if(reader.IsEmptyElement)
  1035. return element;
  1036. // Content: annotation?,
  1037. // (simpleType | complexType)?,
  1038. // (unique | key | keyref)*
  1039. int level = 1;
  1040. while(reader.ReadNextElement())
  1041. {
  1042. if(reader.NodeType == XmlNodeType.EndElement)
  1043. {
  1044. if(reader.LocalName != xmlname)
  1045. error(h,"Should not happen :2: XmlSchemaElement.Read, name="+reader.Name,null);
  1046. break;
  1047. }
  1048. if(level <= 1 && reader.LocalName == "annotation")
  1049. {
  1050. level = 2; //Only one annotation
  1051. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  1052. if(annotation != null)
  1053. element.Annotation = annotation;
  1054. continue;
  1055. }
  1056. if(level <= 2)
  1057. {
  1058. if(reader.LocalName == "simpleType")
  1059. {
  1060. level = 3;
  1061. XmlSchemaSimpleType simple = XmlSchemaSimpleType.Read(reader,h);
  1062. if(simple != null)
  1063. element.SchemaType = simple;
  1064. continue;
  1065. }
  1066. if(reader.LocalName == "complexType")
  1067. {
  1068. level = 3;
  1069. XmlSchemaComplexType complex = XmlSchemaComplexType.Read(reader,h);
  1070. if(complex != null)
  1071. {
  1072. element.SchemaType = complex;
  1073. }
  1074. continue;
  1075. }
  1076. }
  1077. if(level <= 3)
  1078. {
  1079. if(reader.LocalName == "unique")
  1080. {
  1081. level = 3;
  1082. XmlSchemaUnique unique = XmlSchemaUnique.Read(reader,h);
  1083. if(unique != null)
  1084. element.constraints.Add(unique);
  1085. continue;
  1086. }
  1087. else if(reader.LocalName == "key")
  1088. {
  1089. level = 3;
  1090. XmlSchemaKey key = XmlSchemaKey.Read(reader,h);
  1091. if(key != null)
  1092. element.constraints.Add(key);
  1093. continue;
  1094. }
  1095. else if(reader.LocalName == "keyref")
  1096. {
  1097. level = 3;
  1098. XmlSchemaKeyref keyref = XmlSchemaKeyref.Read(reader,h);
  1099. if(keyref != null)
  1100. element.constraints.Add(keyref);
  1101. continue;
  1102. }
  1103. }
  1104. reader.RaiseInvalidElementError();
  1105. }
  1106. return element;
  1107. }
  1108. }
  1109. }