XmlSchemaElement.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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(null)]
  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),Namespace="http://www.w3.org/2001/XMLSchema")]
  176. [XmlElement("complexType",typeof(XmlSchemaComplexType),Namespace="http://www.w3.org/2001/XMLSchema")]
  177. public XmlSchemaType SchemaType
  178. {
  179. get{ return schemaType; }
  180. set{ schemaType = value; }
  181. }
  182. [XmlElement("unique",typeof(XmlSchemaUnique),Namespace="http://www.w3.org/2001/XMLSchema")]
  183. [XmlElement("key",typeof(XmlSchemaKey),Namespace="http://www.w3.org/2001/XMLSchema")]
  184. [XmlElement("keyref",typeof(XmlSchemaKeyref),Namespace="http://www.w3.org/2001/XMLSchema")]
  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(this.defaultValue != null && this.fixedValue != null)
  325. error(h,"both default and fixed can't be present");
  326. if(parentIsSchema || isRedefineChild)
  327. {
  328. if(this.refName != null && !RefName.IsEmpty)
  329. error(h,"ref must be absent");
  330. if(this.name == null) //b1
  331. error(h,"Required attribute name must be present");
  332. else if(!XmlSchemaUtil.CheckNCName(this.name)) // b1.2
  333. error(h,"attribute name must be NCName");
  334. else
  335. this.qName = new XmlQualifiedName (this.name, schema.TargetNamespace);
  336. if(form != XmlSchemaForm.None)
  337. error(h,"form must be absent");
  338. if(MinOccursString != null)
  339. error(h,"minOccurs must be absent");
  340. if(MaxOccursString != null)
  341. error(h,"maxOccurs must be absent");
  342. XmlSchemaDerivationMethod allfinal = (XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Restriction);
  343. if(final == XmlSchemaDerivationMethod.All)
  344. finalResolved = allfinal;
  345. else if(final == XmlSchemaDerivationMethod.None)
  346. finalResolved = XmlSchemaDerivationMethod.Empty;
  347. else
  348. {
  349. // if((final & ~allfinal) != 0)
  350. if ((final | XmlSchemaUtil.FinalAllowed) != XmlSchemaUtil.FinalAllowed)
  351. error (h,"some values for final are invalid in this context");
  352. finalResolved = final & allfinal;
  353. }
  354. if(schemaType != null && schemaTypeName != null && !schemaTypeName.IsEmpty)
  355. {
  356. error(h,"both schemaType and content can't be present");
  357. }
  358. //Even if both are present, read both of them.
  359. if(schemaType != null)
  360. {
  361. if(schemaType is XmlSchemaSimpleType)
  362. {
  363. errorCount += ((XmlSchemaSimpleType)schemaType).Compile(h,schema);
  364. }
  365. else if(schemaType is XmlSchemaComplexType)
  366. {
  367. errorCount += ((XmlSchemaComplexType)schemaType).Compile(h,schema);
  368. }
  369. else
  370. error(h,"only simpletype or complextype is allowed");
  371. }
  372. if(schemaTypeName != null && !schemaTypeName.IsEmpty)
  373. {
  374. if(!XmlSchemaUtil.CheckQName(SchemaTypeName))
  375. error(h,"SchemaTypeName must be an XmlQualifiedName");
  376. }
  377. if(SubstitutionGroup != null && !SubstitutionGroup.IsEmpty)
  378. {
  379. if(!XmlSchemaUtil.CheckQName(SubstitutionGroup))
  380. error(h,"SubstitutionGroup must be a valid XmlQualifiedName");
  381. }
  382. foreach(XmlSchemaObject obj in constraints)
  383. {
  384. if(obj is XmlSchemaUnique)
  385. errorCount += ((XmlSchemaUnique)obj).Compile(h,schema);
  386. else if(obj is XmlSchemaKey)
  387. errorCount += ((XmlSchemaKey)obj).Compile(h,schema);
  388. else if(obj is XmlSchemaKeyref)
  389. errorCount += ((XmlSchemaKeyref)obj).Compile(h,schema);
  390. }
  391. }
  392. else
  393. {
  394. if(substitutionGroup != null && !substitutionGroup.IsEmpty)
  395. error(h,"substitutionGroup must be absent");
  396. if(final != XmlSchemaDerivationMethod.None)
  397. error(h,"final must be absent");
  398. CompileOccurence (h, schema);
  399. if(refName == null || RefName.IsEmpty)
  400. {
  401. string targetNamespace = String.Empty;
  402. if(form == XmlSchemaForm.Qualified || (form == XmlSchemaForm.None && schema.ElementFormDefault == XmlSchemaForm.Qualified))
  403. targetNamespace = schema.TargetNamespace;
  404. if(this.name == null) //b1
  405. error(h,"Required attribute name must be present");
  406. else if(!XmlSchemaUtil.CheckNCName(this.name)) // b1.2
  407. error(h,"attribute name must be NCName");
  408. else
  409. this.qName = new XmlQualifiedName(this.name, targetNamespace);
  410. if(schemaType != null && schemaTypeName != null && !schemaTypeName.IsEmpty)
  411. {
  412. error(h,"both schemaType and content can't be present");
  413. }
  414. //Even if both are present, read both of them.
  415. if(schemaType != null)
  416. {
  417. if(schemaType is XmlSchemaSimpleType)
  418. {
  419. errorCount += ((XmlSchemaSimpleType)schemaType).Compile(h,schema);
  420. }
  421. else if(schemaType is XmlSchemaComplexType)
  422. {
  423. errorCount += ((XmlSchemaComplexType)schemaType).Compile(h,schema);
  424. }
  425. else
  426. error(h,"only simpletype or complextype is allowed");
  427. }
  428. if(schemaTypeName != null && !schemaTypeName.IsEmpty)
  429. {
  430. if(!XmlSchemaUtil.CheckQName(SchemaTypeName))
  431. error(h,"SchemaTypeName must be an XmlQualifiedName");
  432. }
  433. if(SubstitutionGroup != null && !SubstitutionGroup.IsEmpty)
  434. {
  435. if(!XmlSchemaUtil.CheckQName(SubstitutionGroup))
  436. error(h,"SubstitutionGroup must be a valid XmlQualifiedName");
  437. }
  438. foreach(XmlSchemaObject obj in constraints)
  439. {
  440. if(obj is XmlSchemaUnique)
  441. errorCount += ((XmlSchemaUnique)obj).Compile(h,schema);
  442. else if(obj is XmlSchemaKey)
  443. errorCount += ((XmlSchemaKey)obj).Compile(h,schema);
  444. else if(obj is XmlSchemaKeyref)
  445. errorCount += ((XmlSchemaKeyref)obj).Compile(h,schema);
  446. }
  447. }
  448. else
  449. {
  450. if(!XmlSchemaUtil.CheckQName(RefName))
  451. error(h,"RefName must be a XmlQualifiedName");
  452. if(name != null)
  453. error(h,"name must not be present when ref is present");
  454. if(Constraints.Count != 0)
  455. error(h,"key, keyref and unique must be absent");
  456. if(isNillable)
  457. error(h,"nillable must be absent");
  458. if(defaultValue != null)
  459. error(h,"default must be absent");
  460. if(fixedValue != null)
  461. error(h,"fixed must be null");
  462. if(form != XmlSchemaForm.None)
  463. error(h,"form must be absent");
  464. if(block != XmlSchemaDerivationMethod.None)
  465. error(h,"block must be absent");
  466. if(schemaTypeName != null && !schemaTypeName.IsEmpty)
  467. error(h,"type must be absent");
  468. if(SchemaType != null)
  469. error(h,"simpleType or complexType must be absent");
  470. qName = RefName;
  471. }
  472. }
  473. switch (block) {
  474. case XmlSchemaDerivationMethod.All:
  475. blockResolved = XmlSchemaDerivationMethod.All;
  476. break;
  477. case XmlSchemaDerivationMethod.None:
  478. blockResolved = XmlSchemaDerivationMethod.Empty;
  479. break;
  480. default:
  481. if ((block | XmlSchemaUtil.ElementBlockAllowed) != XmlSchemaUtil.ElementBlockAllowed)
  482. error (h,"Some of the values for block are invalid in this context");
  483. blockResolved = block;
  484. break;
  485. }
  486. if (Constraints != null) {
  487. XmlSchemaObjectTable table = new XmlSchemaObjectTable ();
  488. foreach (XmlSchemaIdentityConstraint c in Constraints) {
  489. XmlSchemaUtil.AddToTable (table, c, c.QualifiedName, h);
  490. }
  491. }
  492. XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
  493. this.CompilationId = schema.CompilationId;
  494. return errorCount;
  495. }
  496. [MonoTODO ("Return clone in case when it returns itself")]
  497. internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
  498. {
  499. if (OptimizedParticle != null)
  500. return OptimizedParticle;
  501. if (RefName != null && RefName != XmlQualifiedName.Empty) {
  502. referencedElement = schema.Elements [RefName] as XmlSchemaElement;
  503. }
  504. // if (this.referencedElement != null)
  505. // OptimizedParticle = referencedElement.GetOptimizedParticle (isTop);
  506. // else
  507. if (ValidatedMaxOccurs == 0)
  508. OptimizedParticle = XmlSchemaParticle.Empty;
  509. // Substitution Group
  510. else if (SubstitutingElements != null && SubstitutingElements.Count > 0) {
  511. XmlSchemaChoice choice = new XmlSchemaChoice ();
  512. choice.MinOccurs = MinOccurs;
  513. choice.MaxOccurs = MaxOccurs;
  514. substChoice = choice;
  515. choice.Compile (null, schema); // compute Validated Min/Max Occurs.
  516. XmlSchemaElement item = this.MemberwiseClone () as XmlSchemaElement;
  517. item.MinOccurs = 1;
  518. item.MaxOccurs = 1;
  519. item.substitutionGroupElement = null;
  520. item.substitutingElements = null;
  521. for (int i = 0; i < SubstitutingElements.Count; i++) {
  522. XmlSchemaElement se = SubstitutingElements [i] as XmlSchemaElement;
  523. // choice.Items.Add (se);
  524. // choice.CompiledItems.Add (se);
  525. this.AddSubstElementRecursively (choice.Items, se);
  526. this.AddSubstElementRecursively (choice.CompiledItems, se);
  527. }
  528. if (!choice.Items.Contains (item)) {
  529. choice.Items.Add (item);
  530. choice.CompiledItems.Add (item);
  531. }
  532. OptimizedParticle = choice;
  533. }
  534. else
  535. OptimizedParticle = this;//.MemberwiseClone () as XmlSchemaElement;
  536. return OptimizedParticle;
  537. }
  538. private void AddSubstElementRecursively (XmlSchemaObjectCollection col, XmlSchemaElement el)
  539. {
  540. if (el.SubstitutingElements != null)
  541. for (int i = 0; i < el.SubstitutingElements.Count; i++)
  542. this.AddSubstElementRecursively (col, el.SubstitutingElements [i] as XmlSchemaElement);
  543. if (!col.Contains (el))
  544. col.Add (el);
  545. }
  546. internal void FillSubstitutionElementInfo ()
  547. {
  548. if (this.substitutionGroupElement != null)
  549. return;
  550. if (this.SubstitutionGroup != XmlQualifiedName.Empty) {
  551. XmlSchemaElement substElem = schema.Elements [SubstitutionGroup] as XmlSchemaElement;
  552. this.substitutionGroupElement = substElem;
  553. if (substElem != null)
  554. substElem.substitutingElements.Add (this);
  555. }
  556. }
  557. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  558. {
  559. if (IsValidated (schema.CompilationId))
  560. return errorCount;
  561. // See XML Schema Structures 3.6 for the complete description.
  562. // Element Declaration Properties Correct
  563. // 1. = 3.3.1 (modulo 5.3)
  564. // 3.3.1:
  565. // {annotation} is as is.
  566. // {name}, {target namespace}, {scope}, {disallowed substitution},
  567. // {substitution group exclusions} (handled the same as 'disallowed substitution')
  568. // and {identity-constraint-definitions} are Compile()d.
  569. // {value constraint} is going to be filled in step 2.
  570. // actual {nillable}, {abstract}
  571. this.actualIsNillable = IsNillable;
  572. this.actualIsAbstract = IsAbstract;
  573. // Before determining element type, we need to validate substituting element
  574. if (this.SubstitutionGroup != XmlQualifiedName.Empty) {
  575. XmlSchemaElement substElem = substitutionGroupElement;
  576. if (substElem != null)
  577. substElem.Validate (h, schema);
  578. }
  579. // {type} from here
  580. XmlSchemaDatatype datatype = null;
  581. if (schemaType != null)
  582. elementType = schemaType;
  583. else if (SchemaTypeName != XmlQualifiedName.Empty) {
  584. XmlSchemaType type = schema.SchemaTypes [SchemaTypeName] as XmlSchemaType;
  585. if (type != null) {
  586. type.Validate (h, schema);
  587. elementType = type;
  588. }
  589. else if (SchemaTypeName == XmlSchemaComplexType.AnyTypeName)
  590. elementType = XmlSchemaComplexType.AnyType;
  591. else if (XmlSchemaUtil.IsBuiltInDatatypeName (SchemaTypeName)) {
  592. datatype = XmlSchemaDatatype.FromName (SchemaTypeName);
  593. if (datatype == null)
  594. error (h, "Invalid schema datatype was specified.");
  595. else
  596. elementType = datatype;
  597. }
  598. // otherwise, it might be missing sub components.
  599. else if (!schema.IsNamespaceAbsent (SchemaTypeName.Namespace))
  600. error (h, "Referenced element schema type " + SchemaTypeName + " was not found in the corresponding schema.");
  601. }
  602. else if (RefName != XmlQualifiedName.Empty)
  603. {
  604. XmlSchemaElement refElem = schema.Elements [RefName] as XmlSchemaElement;
  605. // If el is null, then it is missing sub components .
  606. if (refElem != null) {
  607. this.referencedElement = refElem;
  608. errorCount += refElem.Validate (h, schema);
  609. }
  610. // otherwise, it might be missing sub components.
  611. else if (!schema.IsNamespaceAbsent (RefName.Namespace))
  612. error (h, "Referenced element " + RefName + " was not found in the corresponding schema.");
  613. }
  614. // Otherwise if there are substitution group, then the type of the substitution group element.
  615. if (referencedElement == null) {
  616. if (elementType == null && this.substitutionGroupElement != null)
  617. elementType = substitutionGroupElement.ElementType;
  618. // Otherwise, the -ur type- definition.
  619. if (elementType == null)
  620. elementType = XmlSchemaComplexType.AnyType;
  621. }
  622. XmlSchemaType xsType = elementType as XmlSchemaType;
  623. if (xsType != null) {
  624. errorCount += xsType.Validate (h, schema);
  625. datatype = xsType.Datatype;
  626. }
  627. // basic {type} is now filled, except for derivation by {substitution group}.
  628. // {substitution group affiliation}
  629. // 3. subsitution group's type derivation check.
  630. if (this.SubstitutionGroup != XmlQualifiedName.Empty) {
  631. XmlSchemaElement substElem = schema.Elements [SubstitutionGroup] as XmlSchemaElement;
  632. // If el is null, then it is missing sub components .
  633. if (substElem != null) {
  634. XmlSchemaType substSchemaType = substElem.ElementType as XmlSchemaType;
  635. if (substSchemaType != null) {
  636. // 3.3.6 Properties Correct 3.
  637. if ((substElem.FinalResolved & XmlSchemaDerivationMethod.Substitution) != 0)
  638. error (h, "Substituted element blocks substitution.");
  639. if (xsType != null && (substElem.FinalResolved & xsType.DerivedBy) != 0)
  640. error (h, "Invalid derivation was found. Substituted element prohibits this derivation method: " + xsType.DerivedBy + ".");
  641. }
  642. XmlSchemaComplexType xsComplexType = xsType as XmlSchemaComplexType;
  643. if (xsComplexType != null)
  644. xsComplexType.ValidateTypeDerivationOK (substElem.ElementType, h, schema);
  645. else {
  646. XmlSchemaSimpleType xsSimpleType = xsType as XmlSchemaSimpleType;
  647. if (xsSimpleType != null)
  648. xsSimpleType.ValidateTypeDerivationOK (substElem.ElementType, h, schema, true);
  649. }
  650. }
  651. // otherwise, it might be missing sub components.
  652. else if (!schema.IsNamespaceAbsent (SubstitutionGroup.Namespace))
  653. error (h, "Referenced element type " + SubstitutionGroup + " was not found in the corresponding schema.");
  654. }
  655. // 2. ElementDefaultValid
  656. // 4. ID with {value constraint} is prohibited.
  657. if (defaultValue != null || fixedValue != null) {
  658. ValidateElementDefaultValidImmediate (h, schema);
  659. if (datatype != null && // Such situation is basically an error. For ValidationEventHandler.
  660. datatype.TokenizedType == XmlTokenizedType.ID)
  661. error (h, "Element type is ID, which does not allows default or fixed values.");
  662. }
  663. // Identity constraints (3.11.3 / 3.11.6)
  664. foreach (XmlSchemaIdentityConstraint ident in Constraints)
  665. ident.Validate (h, schema);
  666. #if NET_2_0
  667. elementSchemaType = elementType as XmlSchemaType;
  668. if (elementSchemaType == null && elementType != null)
  669. elementSchemaType = XmlSchemaType.GetBuiltInType (((XmlSchemaDatatype) elementType).TypeCode);
  670. #endif
  671. ValidationId = schema.ValidationId;
  672. return errorCount;
  673. }
  674. internal override bool ParticleEquals (XmlSchemaParticle other)
  675. {
  676. XmlSchemaElement element = other as XmlSchemaElement;
  677. if (element == null)
  678. return false;
  679. if (this.ValidatedMaxOccurs != element.ValidatedMaxOccurs ||
  680. this.ValidatedMinOccurs != element.ValidatedMinOccurs)
  681. return false;
  682. if (this.QualifiedName != element.QualifiedName ||
  683. this.ElementType != element.ElementType ||
  684. this.Constraints.Count != element.Constraints.Count)
  685. return false;
  686. for (int i = 0; i < this.Constraints.Count; i++) {
  687. XmlSchemaIdentityConstraint c1 = Constraints [i] as XmlSchemaIdentityConstraint;
  688. XmlSchemaIdentityConstraint c2 = element.Constraints [i] as XmlSchemaIdentityConstraint;
  689. if (c1.QualifiedName != c2.QualifiedName ||
  690. c1.Selector.XPath != c2.Selector.XPath ||
  691. c1.Fields.Count != c2.Fields.Count)
  692. return false;
  693. for (int f = 0; f < c1.Fields.Count; f++) {
  694. XmlSchemaXPath f1 = c1.Fields [f] as XmlSchemaXPath;
  695. XmlSchemaXPath f2 = c2.Fields [f] as XmlSchemaXPath;
  696. if (f1.XPath != f2.XPath)
  697. return false;
  698. }
  699. }
  700. if (this.BlockResolved != element.BlockResolved ||
  701. this.FinalResolved != element.FinalResolved ||
  702. this.ValidatedDefaultValue != element.ValidatedDefaultValue ||
  703. this.ValidatedFixedValue != element.ValidatedFixedValue)
  704. return false;
  705. return true;
  706. }
  707. internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  708. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  709. {
  710. // element - NameAndTypeOK
  711. XmlSchemaElement baseElement = baseParticle as XmlSchemaElement;
  712. if (baseElement != null) {
  713. return ValidateDerivationByRestrictionNameAndTypeOK (baseElement, h, schema, raiseError);
  714. }
  715. // any - NSCompat
  716. XmlSchemaAny baseAny = baseParticle as XmlSchemaAny;
  717. if (baseAny != null) {
  718. // NSCompat
  719. if (!baseAny.ValidateWildcardAllowsNamespaceName (this.QualifiedName.Namespace, h, schema, raiseError))
  720. return false;
  721. return ValidateOccurenceRangeOK (baseAny, h, schema, raiseError);
  722. }
  723. //*
  724. // choice - RecurseAsIfGroup
  725. XmlSchemaGroupBase gb = null;
  726. if (baseParticle is XmlSchemaSequence)
  727. gb = new XmlSchemaSequence ();
  728. else if (baseParticle is XmlSchemaChoice)
  729. gb = new XmlSchemaChoice ();
  730. else if (baseParticle is XmlSchemaAll)
  731. gb = new XmlSchemaAll ();
  732. if (gb != null) {
  733. gb.Items.Add (this);
  734. gb.Compile (h, schema);
  735. gb.Validate (h, schema);
  736. // It looks weird, but here we never think about
  737. // _pointlessness_ of this groupbase particle.
  738. return gb.ValidateDerivationByRestriction (baseParticle, h, schema, raiseError);
  739. }
  740. //*/
  741. return true;
  742. }
  743. private bool ValidateDerivationByRestrictionNameAndTypeOK (XmlSchemaElement baseElement,
  744. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  745. {
  746. // 1.
  747. if (this.QualifiedName != baseElement.QualifiedName) {
  748. if (raiseError)
  749. error (h, "Invalid derivation by restriction of particle was found. Both elements must have the same name.");
  750. return false;
  751. }
  752. // 2.
  753. if (this.isNillable && !baseElement.isNillable) {
  754. if (raiseError)
  755. error (h, "Invalid element derivation by restriction of particle was found. Base element is not nillable and derived type is nillable.");
  756. return false;
  757. }
  758. // 3.
  759. if (!ValidateOccurenceRangeOK (baseElement, h, schema, raiseError))
  760. return false;
  761. // 4.
  762. if (baseElement.ValidatedFixedValue != null &&
  763. baseElement.ValidatedFixedValue != this.ValidatedFixedValue) {
  764. if (raiseError)
  765. error (h, "Invalid element derivation by restriction of particle was found. Both fixed value must be the same.");
  766. return false;
  767. }
  768. // 5. TODO: What is "identity constraints subset" ???
  769. // 6.
  770. if ((baseElement.BlockResolved | this.BlockResolved) != this.BlockResolved) {
  771. if (raiseError)
  772. error (h, "Invalid derivation by restriction of particle was found. Derived element must contain all of the base element's block value.");
  773. return false;
  774. }
  775. // 7.
  776. if (baseElement.ElementType != null) {
  777. XmlSchemaComplexType derivedCType = this.ElementType as XmlSchemaComplexType;
  778. if (derivedCType != null) {
  779. // FIXME: W3C REC says that it is Type Derivation OK to be check, but
  780. // in fact it should be DerivationValid (Restriction, Complex).
  781. derivedCType.ValidateDerivationValidRestriction (
  782. baseElement.ElementType as XmlSchemaComplexType, h, schema);
  783. derivedCType.ValidateTypeDerivationOK (baseElement.ElementType, h, schema);
  784. } else {
  785. XmlSchemaSimpleType derivedSType = this.ElementType as XmlSchemaSimpleType;
  786. if (derivedSType != null)
  787. derivedSType.ValidateTypeDerivationOK (baseElement.ElementType, h, schema, true);
  788. else if (baseElement.ElementType != XmlSchemaComplexType.AnyType && baseElement.ElementType != this.ElementType) {
  789. if (raiseError)
  790. error (h, "Invalid element derivation by restriction of particle was found. Both primitive types differ.");
  791. return false;
  792. }
  793. }
  794. }
  795. return true;
  796. }
  797. internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  798. {
  799. XmlSchemaComplexType ct = this.ElementType as XmlSchemaComplexType;
  800. if (ct == null || ct.Particle == null)
  801. return;
  802. ct.Particle.CheckRecursion (depth + 1, h, schema);
  803. }
  804. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  805. ValidationEventHandler h, XmlSchema schema)
  806. {
  807. if (qnames.Contains (this.QualifiedName))// && !this.ParticleEquals ((XmlSchemaParticle) qnames [this.QualifiedName]))
  808. error (h, "Ambiguous element label was detected: " + this.QualifiedName);
  809. else {
  810. foreach (XmlSchemaAny any in nsNames) {
  811. if (any.ValidatedMaxOccurs == 0)
  812. continue;
  813. if (any.HasValueAny ||
  814. any.HasValueLocal && this.QualifiedName.Namespace == "" ||
  815. any.HasValueOther && this.QualifiedName.Namespace != this.QualifiedName.Namespace ||
  816. any.HasValueTargetNamespace && this.QualifiedName.Namespace == this.QualifiedName.Namespace) {
  817. error (h, "Ambiguous element label which is contained by -any- particle was detected: " + this.QualifiedName);
  818. break;
  819. } else if (!any.HasValueOther) {
  820. bool bad = false;
  821. foreach (string ns in any.ResolvedNamespaces) {
  822. if (ns == this.QualifiedName.Namespace) {
  823. bad = true;
  824. break;
  825. }
  826. }
  827. if (bad) {
  828. error (h, "Ambiguous element label which is contained by -any- particle was detected: " + this.QualifiedName);
  829. break;
  830. }
  831. } else {
  832. if (any.TargetNamespace.Length == 0 ||
  833. any.TargetNamespace != this.QualifiedName.Namespace)
  834. error (h, "Ambiguous element label which is contained by -any- particle with ##other value was detected: " + this.QualifiedName);
  835. }
  836. }
  837. qnames.Add (this.QualifiedName, this);
  838. }
  839. }
  840. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  841. ValidationEventHandler h, XmlSchema schema)
  842. {
  843. XmlSchemaElement labeled = labels [this.QualifiedName] as XmlSchemaElement;
  844. if (labeled == null)
  845. labels.Add (this.QualifiedName, this);
  846. else if (labeled.ElementType != this.ElementType)
  847. error (h, "Different types are specified on the same named elements in the same sequence. Element name is " + QualifiedName);
  848. }
  849. // 3.3.6 Element Default Valid (Immediate)
  850. private void ValidateElementDefaultValidImmediate (ValidationEventHandler h, XmlSchema schema)
  851. {
  852. // This presumes that ElementType is already filled.
  853. XmlSchemaDatatype datatype = elementType as XmlSchemaDatatype;
  854. XmlSchemaSimpleType simpleType = elementType as XmlSchemaSimpleType;
  855. if (simpleType != null)
  856. datatype = simpleType.Datatype;
  857. if (datatype == null) {
  858. XmlSchemaComplexType complexType = elementType as XmlSchemaComplexType;
  859. switch (complexType.ContentType) {
  860. case XmlSchemaContentType.Empty:
  861. case XmlSchemaContentType.ElementOnly:
  862. error (h, "Element content type must be simple type or mixed.");
  863. break;
  864. }
  865. datatype = XmlSchemaSimpleType.AnySimpleType;
  866. }
  867. XmlNamespaceManager nsmgr = null;
  868. if (datatype.TokenizedType == XmlTokenizedType.QName) {
  869. if (this.Namespaces != null)
  870. foreach (XmlQualifiedName qname in Namespaces.ToArray ())
  871. nsmgr.AddNamespace (qname.Name, qname.Namespace);
  872. }
  873. try {
  874. if (defaultValue != null) {
  875. validatedDefaultValue = datatype.Normalize (defaultValue);
  876. datatype.ParseValue (validatedDefaultValue, null, nsmgr);
  877. }
  878. } catch (Exception ex) {
  879. // FIXME: This is not a good way to handle exception, but
  880. // I think there is no remedy for such Framework specification.
  881. error (h, "The Element's default value is invalid with respect to its type definition.", ex);
  882. }
  883. try {
  884. if (fixedValue != null) {
  885. validatedFixedValue = datatype.Normalize (fixedValue);
  886. datatype.ParseValue (validatedFixedValue, null, nsmgr);
  887. }
  888. } catch (Exception ex) {
  889. // FIXME: This is not a good way to handle exception.
  890. error (h, "The Element's fixed value is invalid with its type definition.", ex);
  891. }
  892. }
  893. //<element
  894. // abstract = boolean : false
  895. // block = (#all | List of (extension | restriction | substitution))
  896. // default = string
  897. // final = (#all | List of (extension | restriction))
  898. // fixed = string
  899. // form = (qualified | unqualified)
  900. // id = ID
  901. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  902. // minOccurs = nonNegativeInteger : 1
  903. // name = NCName
  904. // nillable = boolean : false
  905. // ref = QName
  906. // substitutionGroup = QName
  907. // type = QName
  908. // {any attributes with non-schema namespace . . .}>
  909. // Content: (annotation?, ((simpleType | complexType)?, (unique | key | keyref)*))
  910. //</element>
  911. internal static XmlSchemaElement Read(XmlSchemaReader reader, ValidationEventHandler h)
  912. {
  913. XmlSchemaElement element = new XmlSchemaElement();
  914. Exception innerex;
  915. reader.MoveToElement();
  916. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  917. {
  918. error(h,"Should not happen :1: XmlSchemaElement.Read, name="+reader.Name,null);
  919. reader.Skip();
  920. return null;
  921. }
  922. element.LineNumber = reader.LineNumber;
  923. element.LinePosition = reader.LinePosition;
  924. element.SourceUri = reader.BaseURI;
  925. while(reader.MoveToNextAttribute())
  926. {
  927. if(reader.Name == "abstract")
  928. {
  929. element.IsAbstract = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
  930. if(innerex != null)
  931. error(h,reader.Value + " is invalid value for abstract",innerex);
  932. }
  933. else if(reader.Name == "block")
  934. {
  935. element.block = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "block",
  936. XmlSchemaUtil.ElementBlockAllowed);
  937. if(innerex != null)
  938. error (h,"some invalid values for block attribute were found",innerex);
  939. }
  940. else if(reader.Name == "default")
  941. {
  942. element.defaultValue = reader.Value;
  943. }
  944. else if(reader.Name == "final")
  945. {
  946. element.Final = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "final",
  947. XmlSchemaUtil.FinalAllowed);
  948. if(innerex != null)
  949. error (h,"some invalid values for final attribute were found",innerex);
  950. }
  951. else if(reader.Name == "fixed")
  952. {
  953. element.fixedValue = reader.Value;
  954. }
  955. else if(reader.Name == "form")
  956. {
  957. element.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);
  958. if(innerex != null)
  959. error(h,reader.Value + " is an invalid value for form attribute",innerex);
  960. }
  961. else if(reader.Name == "id")
  962. {
  963. element.Id = reader.Value;
  964. }
  965. else if(reader.Name == "maxOccurs")
  966. {
  967. try
  968. {
  969. element.MaxOccursString = reader.Value;
  970. }
  971. catch(Exception e)
  972. {
  973. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  974. }
  975. }
  976. else if(reader.Name == "minOccurs")
  977. {
  978. try
  979. {
  980. element.MinOccursString = reader.Value;
  981. }
  982. catch(Exception e)
  983. {
  984. error(h,reader.Value + " is an invalid value for minOccurs",e);
  985. }
  986. }
  987. else if(reader.Name == "name")
  988. {
  989. element.Name = reader.Value;
  990. }
  991. else if(reader.Name == "nillable")
  992. {
  993. element.IsNillable = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
  994. if(innerex != null)
  995. error(h,reader.Value + "is not a valid value for nillable",innerex);
  996. }
  997. else if(reader.Name == "ref")
  998. {
  999. element.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  1000. if(innerex != null)
  1001. error(h, reader.Value + " is not a valid value for ref attribute",innerex);
  1002. }
  1003. else if(reader.Name == "substitutionGroup")
  1004. {
  1005. element.substitutionGroup = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  1006. if(innerex != null)
  1007. error(h, reader.Value + " is not a valid value for substitutionGroup attribute",innerex);
  1008. }
  1009. else if(reader.Name == "type")
  1010. {
  1011. element.SchemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  1012. if(innerex != null)
  1013. error(h, reader.Value + " is not a valid value for type attribute",innerex);
  1014. }
  1015. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  1016. {
  1017. error(h,reader.Name + " is not a valid attribute for element",null);
  1018. }
  1019. else
  1020. {
  1021. XmlSchemaUtil.ReadUnhandledAttribute(reader,element);
  1022. }
  1023. }
  1024. reader.MoveToElement();
  1025. if(reader.IsEmptyElement)
  1026. return element;
  1027. // Content: annotation?,
  1028. // (simpleType | complexType)?,
  1029. // (unique | key | keyref)*
  1030. int level = 1;
  1031. while(reader.ReadNextElement())
  1032. {
  1033. if(reader.NodeType == XmlNodeType.EndElement)
  1034. {
  1035. if(reader.LocalName != xmlname)
  1036. error(h,"Should not happen :2: XmlSchemaElement.Read, name="+reader.Name,null);
  1037. break;
  1038. }
  1039. if(level <= 1 && reader.LocalName == "annotation")
  1040. {
  1041. level = 2; //Only one annotation
  1042. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  1043. if(annotation != null)
  1044. element.Annotation = annotation;
  1045. continue;
  1046. }
  1047. if(level <= 2)
  1048. {
  1049. if(reader.LocalName == "simpleType")
  1050. {
  1051. level = 3;
  1052. XmlSchemaSimpleType simple = XmlSchemaSimpleType.Read(reader,h);
  1053. if(simple != null)
  1054. element.SchemaType = simple;
  1055. continue;
  1056. }
  1057. if(reader.LocalName == "complexType")
  1058. {
  1059. level = 3;
  1060. XmlSchemaComplexType complex = XmlSchemaComplexType.Read(reader,h);
  1061. if(complex != null)
  1062. {
  1063. element.SchemaType = complex;
  1064. }
  1065. continue;
  1066. }
  1067. }
  1068. if(level <= 3)
  1069. {
  1070. if(reader.LocalName == "unique")
  1071. {
  1072. level = 3;
  1073. XmlSchemaUnique unique = XmlSchemaUnique.Read(reader,h);
  1074. if(unique != null)
  1075. element.constraints.Add(unique);
  1076. continue;
  1077. }
  1078. else if(reader.LocalName == "key")
  1079. {
  1080. level = 3;
  1081. XmlSchemaKey key = XmlSchemaKey.Read(reader,h);
  1082. if(key != null)
  1083. element.constraints.Add(key);
  1084. continue;
  1085. }
  1086. else if(reader.LocalName == "keyref")
  1087. {
  1088. level = 3;
  1089. XmlSchemaKeyref keyref = XmlSchemaKeyref.Read(reader,h);
  1090. if(keyref != null)
  1091. element.constraints.Add(keyref);
  1092. continue;
  1093. }
  1094. }
  1095. reader.RaiseInvalidElementError();
  1096. }
  1097. return element;
  1098. }
  1099. }
  1100. }