XmlSchemaElement.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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. elementSchemaType = elementType as XmlSchemaType;
  674. if (elementSchemaType == null && elementType != null)
  675. elementSchemaType = XmlSchemaType.GetBuiltInType (((XmlSchemaDatatype) elementType).TypeCode);
  676. #endif
  677. ValidationId = schema.ValidationId;
  678. return errorCount;
  679. }
  680. internal override bool ParticleEquals (XmlSchemaParticle other)
  681. {
  682. XmlSchemaElement element = other as XmlSchemaElement;
  683. if (element == null)
  684. return false;
  685. if (this.ValidatedMaxOccurs != element.ValidatedMaxOccurs ||
  686. this.ValidatedMinOccurs != element.ValidatedMinOccurs)
  687. return false;
  688. if (this.QualifiedName != element.QualifiedName ||
  689. this.ElementType != element.ElementType ||
  690. this.Constraints.Count != element.Constraints.Count)
  691. return false;
  692. for (int i = 0; i < this.Constraints.Count; i++) {
  693. XmlSchemaIdentityConstraint c1 = Constraints [i] as XmlSchemaIdentityConstraint;
  694. XmlSchemaIdentityConstraint c2 = element.Constraints [i] as XmlSchemaIdentityConstraint;
  695. if (c1.QualifiedName != c2.QualifiedName ||
  696. c1.Selector.XPath != c2.Selector.XPath ||
  697. c1.Fields.Count != c2.Fields.Count)
  698. return false;
  699. for (int f = 0; f < c1.Fields.Count; f++) {
  700. XmlSchemaXPath f1 = c1.Fields [f] as XmlSchemaXPath;
  701. XmlSchemaXPath f2 = c2.Fields [f] as XmlSchemaXPath;
  702. if (f1.XPath != f2.XPath)
  703. return false;
  704. }
  705. }
  706. if (this.BlockResolved != element.BlockResolved ||
  707. this.FinalResolved != element.FinalResolved ||
  708. this.ValidatedDefaultValue != element.ValidatedDefaultValue ||
  709. this.ValidatedFixedValue != element.ValidatedFixedValue)
  710. return false;
  711. return true;
  712. }
  713. internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  714. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  715. {
  716. // element - NameAndTypeOK
  717. XmlSchemaElement baseElement = baseParticle as XmlSchemaElement;
  718. if (baseElement != null) {
  719. return ValidateDerivationByRestrictionNameAndTypeOK (baseElement, h, schema, raiseError);
  720. }
  721. // any - NSCompat
  722. XmlSchemaAny baseAny = baseParticle as XmlSchemaAny;
  723. if (baseAny != null) {
  724. // NSCompat
  725. if (!baseAny.ValidateWildcardAllowsNamespaceName (this.QualifiedName.Namespace, h, schema, raiseError))
  726. return false;
  727. return ValidateOccurenceRangeOK (baseAny, h, schema, raiseError);
  728. }
  729. //*
  730. // choice - RecurseAsIfGroup
  731. XmlSchemaGroupBase gb = null;
  732. if (baseParticle is XmlSchemaSequence)
  733. gb = new XmlSchemaSequence ();
  734. else if (baseParticle is XmlSchemaChoice)
  735. gb = new XmlSchemaChoice ();
  736. else if (baseParticle is XmlSchemaAll)
  737. gb = new XmlSchemaAll ();
  738. if (gb != null) {
  739. gb.Items.Add (this);
  740. gb.Compile (h, schema);
  741. gb.Validate (h, schema);
  742. // It looks weird, but here we never think about
  743. // _pointlessness_ of this groupbase particle.
  744. return gb.ValidateDerivationByRestriction (baseParticle, h, schema, raiseError);
  745. }
  746. //*/
  747. return true;
  748. }
  749. private bool ValidateDerivationByRestrictionNameAndTypeOK (XmlSchemaElement baseElement,
  750. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  751. {
  752. // 1.
  753. if (this.QualifiedName != baseElement.QualifiedName) {
  754. if (raiseError)
  755. error (h, "Invalid derivation by restriction of particle was found. Both elements must have the same name.");
  756. return false;
  757. }
  758. // 2.
  759. if (this.isNillable && !baseElement.isNillable) {
  760. if (raiseError)
  761. error (h, "Invalid element derivation by restriction of particle was found. Base element is not nillable and derived type is nillable.");
  762. return false;
  763. }
  764. // 3.
  765. if (!ValidateOccurenceRangeOK (baseElement, h, schema, raiseError))
  766. return false;
  767. // 4.
  768. if (baseElement.ValidatedFixedValue != null &&
  769. baseElement.ValidatedFixedValue != this.ValidatedFixedValue) {
  770. if (raiseError)
  771. error (h, "Invalid element derivation by restriction of particle was found. Both fixed value must be the same.");
  772. return false;
  773. }
  774. // 5. TODO: What is "identity constraints subset" ???
  775. // 6.
  776. if ((baseElement.BlockResolved | this.BlockResolved) != this.BlockResolved) {
  777. if (raiseError)
  778. error (h, "Invalid derivation by restriction of particle was found. Derived element must contain all of the base element's block value.");
  779. return false;
  780. }
  781. // 7.
  782. if (baseElement.ElementType != null) {
  783. XmlSchemaComplexType derivedCType = this.ElementType as XmlSchemaComplexType;
  784. if (derivedCType != null) {
  785. // FIXME: W3C REC says that it is Type Derivation OK to be check, but
  786. // in fact it should be DerivationValid (Restriction, Complex).
  787. derivedCType.ValidateDerivationValidRestriction (
  788. baseElement.ElementType as XmlSchemaComplexType, h, schema);
  789. derivedCType.ValidateTypeDerivationOK (baseElement.ElementType, h, schema);
  790. } else {
  791. XmlSchemaSimpleType derivedSType = this.ElementType as XmlSchemaSimpleType;
  792. if (derivedSType != null)
  793. derivedSType.ValidateTypeDerivationOK (baseElement.ElementType, h, schema, true);
  794. else if (baseElement.ElementType != XmlSchemaComplexType.AnyType && baseElement.ElementType != this.ElementType) {
  795. if (raiseError)
  796. error (h, "Invalid element derivation by restriction of particle was found. Both primitive types differ.");
  797. return false;
  798. }
  799. }
  800. }
  801. return true;
  802. }
  803. internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  804. {
  805. XmlSchemaComplexType ct = this.ElementType as XmlSchemaComplexType;
  806. if (ct == null || ct.Particle == null)
  807. return;
  808. ct.Particle.CheckRecursion (depth + 1, h, schema);
  809. }
  810. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  811. ValidationEventHandler h, XmlSchema schema)
  812. {
  813. if (qnames.Contains (this.QualifiedName))// && !this.ParticleEquals ((XmlSchemaParticle) qnames [this.QualifiedName]))
  814. error (h, "Ambiguous element label was detected: " + this.QualifiedName);
  815. else {
  816. foreach (XmlSchemaAny any in nsNames) {
  817. if (any.ValidatedMaxOccurs == 0)
  818. continue;
  819. if (any.HasValueAny ||
  820. any.HasValueLocal && this.QualifiedName.Namespace == "" ||
  821. any.HasValueOther && this.QualifiedName.Namespace != this.QualifiedName.Namespace ||
  822. any.HasValueTargetNamespace && this.QualifiedName.Namespace == this.QualifiedName.Namespace) {
  823. error (h, "Ambiguous element label which is contained by -any- particle was detected: " + this.QualifiedName);
  824. break;
  825. } else if (!any.HasValueOther) {
  826. bool bad = false;
  827. foreach (string ns in any.ResolvedNamespaces) {
  828. if (ns == this.QualifiedName.Namespace) {
  829. bad = true;
  830. break;
  831. }
  832. }
  833. if (bad) {
  834. error (h, "Ambiguous element label which is contained by -any- particle was detected: " + this.QualifiedName);
  835. break;
  836. }
  837. } else {
  838. if (any.TargetNamespace.Length == 0 ||
  839. any.TargetNamespace != this.QualifiedName.Namespace)
  840. error (h, "Ambiguous element label which is contained by -any- particle with ##other value was detected: " + this.QualifiedName);
  841. }
  842. }
  843. qnames.Add (this.QualifiedName, this);
  844. }
  845. }
  846. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  847. ValidationEventHandler h, XmlSchema schema)
  848. {
  849. XmlSchemaElement labeled = labels [this.QualifiedName] as XmlSchemaElement;
  850. if (labeled == null)
  851. labels.Add (this.QualifiedName, this);
  852. else if (labeled.ElementType != this.ElementType)
  853. error (h, "Different types are specified on the same named elements in the same sequence. Element name is " + QualifiedName);
  854. }
  855. // 3.3.6 Element Default Valid (Immediate)
  856. private void ValidateElementDefaultValidImmediate (ValidationEventHandler h, XmlSchema schema)
  857. {
  858. // This presumes that ElementType is already filled.
  859. XmlSchemaDatatype datatype = elementType as XmlSchemaDatatype;
  860. XmlSchemaSimpleType simpleType = elementType as XmlSchemaSimpleType;
  861. if (simpleType != null)
  862. datatype = simpleType.Datatype;
  863. if (datatype == null) {
  864. XmlSchemaComplexType complexType = elementType as XmlSchemaComplexType;
  865. switch (complexType.ContentType) {
  866. case XmlSchemaContentType.Empty:
  867. case XmlSchemaContentType.ElementOnly:
  868. error (h, "Element content type must be simple type or mixed.");
  869. break;
  870. }
  871. datatype = XmlSchemaSimpleType.AnySimpleType;
  872. }
  873. XmlNamespaceManager nsmgr = null;
  874. if (datatype.TokenizedType == XmlTokenizedType.QName) {
  875. if (this.Namespaces != null)
  876. foreach (XmlQualifiedName qname in Namespaces.ToArray ())
  877. nsmgr.AddNamespace (qname.Name, qname.Namespace);
  878. }
  879. try {
  880. if (defaultValue != null) {
  881. validatedDefaultValue = datatype.Normalize (defaultValue);
  882. datatype.ParseValue (validatedDefaultValue, null, nsmgr);
  883. }
  884. } catch (Exception ex) {
  885. // FIXME: This is not a good way to handle exception, but
  886. // I think there is no remedy for such Framework specification.
  887. error (h, "The Element's default value is invalid with respect to its type definition.", ex);
  888. }
  889. try {
  890. if (fixedValue != null) {
  891. validatedFixedValue = datatype.Normalize (fixedValue);
  892. datatype.ParseValue (validatedFixedValue, null, nsmgr);
  893. }
  894. } catch (Exception ex) {
  895. // FIXME: This is not a good way to handle exception.
  896. error (h, "The Element's fixed value is invalid with its type definition.", ex);
  897. }
  898. }
  899. //<element
  900. // abstract = boolean : false
  901. // block = (#all | List of (extension | restriction | substitution))
  902. // default = string
  903. // final = (#all | List of (extension | restriction))
  904. // fixed = string
  905. // form = (qualified | unqualified)
  906. // id = ID
  907. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  908. // minOccurs = nonNegativeInteger : 1
  909. // name = NCName
  910. // nillable = boolean : false
  911. // ref = QName
  912. // substitutionGroup = QName
  913. // type = QName
  914. // {any attributes with non-schema namespace . . .}>
  915. // Content: (annotation?, ((simpleType | complexType)?, (unique | key | keyref)*))
  916. //</element>
  917. internal static XmlSchemaElement Read(XmlSchemaReader reader, ValidationEventHandler h)
  918. {
  919. XmlSchemaElement element = new XmlSchemaElement();
  920. Exception innerex;
  921. reader.MoveToElement();
  922. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  923. {
  924. error(h,"Should not happen :1: XmlSchemaElement.Read, name="+reader.Name,null);
  925. reader.Skip();
  926. return null;
  927. }
  928. element.LineNumber = reader.LineNumber;
  929. element.LinePosition = reader.LinePosition;
  930. element.SourceUri = reader.BaseURI;
  931. while(reader.MoveToNextAttribute())
  932. {
  933. if(reader.Name == "abstract")
  934. {
  935. element.IsAbstract = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
  936. if(innerex != null)
  937. error(h,reader.Value + " is invalid value for abstract",innerex);
  938. }
  939. else if(reader.Name == "block")
  940. {
  941. element.block = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "block",
  942. XmlSchemaUtil.ElementBlockAllowed);
  943. if(innerex != null)
  944. error (h,"some invalid values for block attribute were found",innerex);
  945. }
  946. else if(reader.Name == "default")
  947. {
  948. element.defaultValue = reader.Value;
  949. }
  950. else if(reader.Name == "final")
  951. {
  952. element.Final = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "final",
  953. XmlSchemaUtil.FinalAllowed);
  954. if(innerex != null)
  955. error (h,"some invalid values for final attribute were found",innerex);
  956. }
  957. else if(reader.Name == "fixed")
  958. {
  959. element.fixedValue = reader.Value;
  960. }
  961. else if(reader.Name == "form")
  962. {
  963. element.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);
  964. if(innerex != null)
  965. error(h,reader.Value + " is an invalid value for form attribute",innerex);
  966. }
  967. else if(reader.Name == "id")
  968. {
  969. element.Id = reader.Value;
  970. }
  971. else if(reader.Name == "maxOccurs")
  972. {
  973. try
  974. {
  975. element.MaxOccursString = reader.Value;
  976. }
  977. catch(Exception e)
  978. {
  979. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  980. }
  981. }
  982. else if(reader.Name == "minOccurs")
  983. {
  984. try
  985. {
  986. element.MinOccursString = reader.Value;
  987. }
  988. catch(Exception e)
  989. {
  990. error(h,reader.Value + " is an invalid value for minOccurs",e);
  991. }
  992. }
  993. else if(reader.Name == "name")
  994. {
  995. element.Name = reader.Value;
  996. }
  997. else if(reader.Name == "nillable")
  998. {
  999. element.IsNillable = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
  1000. if(innerex != null)
  1001. error(h,reader.Value + "is not a valid value for nillable",innerex);
  1002. }
  1003. else if(reader.Name == "ref")
  1004. {
  1005. element.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  1006. if(innerex != null)
  1007. error(h, reader.Value + " is not a valid value for ref attribute",innerex);
  1008. }
  1009. else if(reader.Name == "substitutionGroup")
  1010. {
  1011. element.substitutionGroup = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  1012. if(innerex != null)
  1013. error(h, reader.Value + " is not a valid value for substitutionGroup attribute",innerex);
  1014. }
  1015. else if(reader.Name == "type")
  1016. {
  1017. element.SchemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  1018. if(innerex != null)
  1019. error(h, reader.Value + " is not a valid value for type attribute",innerex);
  1020. }
  1021. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  1022. {
  1023. error(h,reader.Name + " is not a valid attribute for element",null);
  1024. }
  1025. else
  1026. {
  1027. XmlSchemaUtil.ReadUnhandledAttribute(reader,element);
  1028. }
  1029. }
  1030. reader.MoveToElement();
  1031. if(reader.IsEmptyElement)
  1032. return element;
  1033. // Content: annotation?,
  1034. // (simpleType | complexType)?,
  1035. // (unique | key | keyref)*
  1036. int level = 1;
  1037. while(reader.ReadNextElement())
  1038. {
  1039. if(reader.NodeType == XmlNodeType.EndElement)
  1040. {
  1041. if(reader.LocalName != xmlname)
  1042. error(h,"Should not happen :2: XmlSchemaElement.Read, name="+reader.Name,null);
  1043. break;
  1044. }
  1045. if(level <= 1 && reader.LocalName == "annotation")
  1046. {
  1047. level = 2; //Only one annotation
  1048. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  1049. if(annotation != null)
  1050. element.Annotation = annotation;
  1051. continue;
  1052. }
  1053. if(level <= 2)
  1054. {
  1055. if(reader.LocalName == "simpleType")
  1056. {
  1057. level = 3;
  1058. XmlSchemaSimpleType simple = XmlSchemaSimpleType.Read(reader,h);
  1059. if(simple != null)
  1060. element.SchemaType = simple;
  1061. continue;
  1062. }
  1063. if(reader.LocalName == "complexType")
  1064. {
  1065. level = 3;
  1066. XmlSchemaComplexType complex = XmlSchemaComplexType.Read(reader,h);
  1067. if(complex != null)
  1068. {
  1069. element.SchemaType = complex;
  1070. }
  1071. continue;
  1072. }
  1073. }
  1074. if(level <= 3)
  1075. {
  1076. if(reader.LocalName == "unique")
  1077. {
  1078. level = 3;
  1079. XmlSchemaUnique unique = XmlSchemaUnique.Read(reader,h);
  1080. if(unique != null)
  1081. element.constraints.Add(unique);
  1082. continue;
  1083. }
  1084. else if(reader.LocalName == "key")
  1085. {
  1086. level = 3;
  1087. XmlSchemaKey key = XmlSchemaKey.Read(reader,h);
  1088. if(key != null)
  1089. element.constraints.Add(key);
  1090. continue;
  1091. }
  1092. else if(reader.LocalName == "keyref")
  1093. {
  1094. level = 3;
  1095. XmlSchemaKeyref keyref = XmlSchemaKeyref.Read(reader,h);
  1096. if(keyref != null)
  1097. element.constraints.Add(keyref);
  1098. continue;
  1099. }
  1100. }
  1101. reader.RaiseInvalidElementError();
  1102. }
  1103. return element;
  1104. }
  1105. }
  1106. }