XmlSchemaElement.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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 != this.QualifiedName.Namespace)
  839. error (h, "Ambiguous element label which is contained by -any- particle with ##other value was detected: " + this.QualifiedName);
  840. }
  841. }
  842. qnames.Add (this.QualifiedName, this);
  843. }
  844. }
  845. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  846. ValidationEventHandler h, XmlSchema schema)
  847. {
  848. XmlSchemaElement labeled = labels [this.QualifiedName] as XmlSchemaElement;
  849. if (labeled == null)
  850. labels.Add (this.QualifiedName, this);
  851. else if (labeled.ElementType != this.ElementType)
  852. error (h, "Different types are specified on the same named elements in the same sequence. Element name is " + QualifiedName);
  853. }
  854. // 3.3.6 Element Default Valid (Immediate)
  855. private void ValidateElementDefaultValidImmediate (ValidationEventHandler h, XmlSchema schema)
  856. {
  857. // This presumes that ElementType is already filled.
  858. XmlSchemaDatatype datatype = elementType as XmlSchemaDatatype;
  859. XmlSchemaSimpleType simpleType = elementType as XmlSchemaSimpleType;
  860. if (simpleType != null)
  861. datatype = simpleType.Datatype;
  862. if (datatype == null) {
  863. XmlSchemaComplexType complexType = elementType as XmlSchemaComplexType;
  864. switch (complexType.ContentType) {
  865. case XmlSchemaContentType.Empty:
  866. case XmlSchemaContentType.ElementOnly:
  867. error (h, "Element content type must be simple type or mixed.");
  868. break;
  869. }
  870. datatype = XmlSchemaSimpleType.AnySimpleType;
  871. }
  872. XmlNamespaceManager nsmgr = null;
  873. if (datatype.TokenizedType == XmlTokenizedType.QName) {
  874. if (this.Namespaces != null)
  875. foreach (XmlQualifiedName qname in Namespaces.ToArray ())
  876. nsmgr.AddNamespace (qname.Name, qname.Namespace);
  877. }
  878. try {
  879. if (defaultValue != null) {
  880. validatedDefaultValue = datatype.Normalize (defaultValue);
  881. datatype.ParseValue (validatedDefaultValue, null, nsmgr);
  882. }
  883. } catch (Exception ex) {
  884. // FIXME: This is not a good way to handle exception, but
  885. // I think there is no remedy for such Framework specification.
  886. error (h, "The Element's default value is invalid with respect to its type definition.", ex);
  887. }
  888. try {
  889. if (fixedValue != null) {
  890. validatedFixedValue = datatype.Normalize (fixedValue);
  891. datatype.ParseValue (validatedFixedValue, null, nsmgr);
  892. }
  893. } catch (Exception ex) {
  894. // FIXME: This is not a good way to handle exception.
  895. error (h, "The Element's fixed value is invalid with its type definition.", ex);
  896. }
  897. }
  898. //<element
  899. // abstract = boolean : false
  900. // block = (#all | List of (extension | restriction | substitution))
  901. // default = string
  902. // final = (#all | List of (extension | restriction))
  903. // fixed = string
  904. // form = (qualified | unqualified)
  905. // id = ID
  906. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  907. // minOccurs = nonNegativeInteger : 1
  908. // name = NCName
  909. // nillable = boolean : false
  910. // ref = QName
  911. // substitutionGroup = QName
  912. // type = QName
  913. // {any attributes with non-schema namespace . . .}>
  914. // Content: (annotation?, ((simpleType | complexType)?, (unique | key | keyref)*))
  915. //</element>
  916. internal static XmlSchemaElement Read(XmlSchemaReader reader, ValidationEventHandler h)
  917. {
  918. XmlSchemaElement element = new XmlSchemaElement();
  919. Exception innerex;
  920. reader.MoveToElement();
  921. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  922. {
  923. error(h,"Should not happen :1: XmlSchemaElement.Read, name="+reader.Name,null);
  924. reader.Skip();
  925. return null;
  926. }
  927. element.LineNumber = reader.LineNumber;
  928. element.LinePosition = reader.LinePosition;
  929. element.SourceUri = reader.BaseURI;
  930. while(reader.MoveToNextAttribute())
  931. {
  932. if(reader.Name == "abstract")
  933. {
  934. element.IsAbstract = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
  935. if(innerex != null)
  936. error(h,reader.Value + " is invalid value for abstract",innerex);
  937. }
  938. else if(reader.Name == "block")
  939. {
  940. element.block = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "block",
  941. XmlSchemaUtil.ElementBlockAllowed);
  942. if(innerex != null)
  943. error (h,"some invalid values for block attribute were found",innerex);
  944. }
  945. else if(reader.Name == "default")
  946. {
  947. element.defaultValue = reader.Value;
  948. }
  949. else if(reader.Name == "final")
  950. {
  951. element.Final = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "final",
  952. XmlSchemaUtil.FinalAllowed);
  953. if(innerex != null)
  954. error (h,"some invalid values for final attribute were found",innerex);
  955. }
  956. else if(reader.Name == "fixed")
  957. {
  958. element.fixedValue = reader.Value;
  959. }
  960. else if(reader.Name == "form")
  961. {
  962. element.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);
  963. if(innerex != null)
  964. error(h,reader.Value + " is an invalid value for form attribute",innerex);
  965. }
  966. else if(reader.Name == "id")
  967. {
  968. element.Id = reader.Value;
  969. }
  970. else if(reader.Name == "maxOccurs")
  971. {
  972. try
  973. {
  974. element.MaxOccursString = reader.Value;
  975. }
  976. catch(Exception e)
  977. {
  978. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  979. }
  980. }
  981. else if(reader.Name == "minOccurs")
  982. {
  983. try
  984. {
  985. element.MinOccursString = reader.Value;
  986. }
  987. catch(Exception e)
  988. {
  989. error(h,reader.Value + " is an invalid value for minOccurs",e);
  990. }
  991. }
  992. else if(reader.Name == "name")
  993. {
  994. element.Name = reader.Value;
  995. }
  996. else if(reader.Name == "nillable")
  997. {
  998. element.IsNillable = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
  999. if(innerex != null)
  1000. error(h,reader.Value + "is not a valid value for nillable",innerex);
  1001. }
  1002. else if(reader.Name == "ref")
  1003. {
  1004. element.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  1005. if(innerex != null)
  1006. error(h, reader.Value + " is not a valid value for ref attribute",innerex);
  1007. }
  1008. else if(reader.Name == "substitutionGroup")
  1009. {
  1010. element.substitutionGroup = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  1011. if(innerex != null)
  1012. error(h, reader.Value + " is not a valid value for substitutionGroup attribute",innerex);
  1013. }
  1014. else if(reader.Name == "type")
  1015. {
  1016. element.SchemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  1017. if(innerex != null)
  1018. error(h, reader.Value + " is not a valid value for type attribute",innerex);
  1019. }
  1020. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  1021. {
  1022. error(h,reader.Name + " is not a valid attribute for element",null);
  1023. }
  1024. else
  1025. {
  1026. XmlSchemaUtil.ReadUnhandledAttribute(reader,element);
  1027. }
  1028. }
  1029. reader.MoveToElement();
  1030. if(reader.IsEmptyElement)
  1031. return element;
  1032. // Content: annotation?,
  1033. // (simpleType | complexType)?,
  1034. // (unique | key | keyref)*
  1035. int level = 1;
  1036. while(reader.ReadNextElement())
  1037. {
  1038. if(reader.NodeType == XmlNodeType.EndElement)
  1039. {
  1040. if(reader.LocalName != xmlname)
  1041. error(h,"Should not happen :2: XmlSchemaElement.Read, name="+reader.Name,null);
  1042. break;
  1043. }
  1044. if(level <= 1 && reader.LocalName == "annotation")
  1045. {
  1046. level = 2; //Only one annotation
  1047. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  1048. if(annotation != null)
  1049. element.Annotation = annotation;
  1050. continue;
  1051. }
  1052. if(level <= 2)
  1053. {
  1054. if(reader.LocalName == "simpleType")
  1055. {
  1056. level = 3;
  1057. XmlSchemaSimpleType simple = XmlSchemaSimpleType.Read(reader,h);
  1058. if(simple != null)
  1059. element.SchemaType = simple;
  1060. continue;
  1061. }
  1062. if(reader.LocalName == "complexType")
  1063. {
  1064. level = 3;
  1065. XmlSchemaComplexType complex = XmlSchemaComplexType.Read(reader,h);
  1066. if(complex != null)
  1067. {
  1068. element.SchemaType = complex;
  1069. }
  1070. continue;
  1071. }
  1072. }
  1073. if(level <= 3)
  1074. {
  1075. if(reader.LocalName == "unique")
  1076. {
  1077. level = 3;
  1078. XmlSchemaUnique unique = XmlSchemaUnique.Read(reader,h);
  1079. if(unique != null)
  1080. element.constraints.Add(unique);
  1081. continue;
  1082. }
  1083. else if(reader.LocalName == "key")
  1084. {
  1085. level = 3;
  1086. XmlSchemaKey key = XmlSchemaKey.Read(reader,h);
  1087. if(key != null)
  1088. element.constraints.Add(key);
  1089. continue;
  1090. }
  1091. else if(reader.LocalName == "keyref")
  1092. {
  1093. level = 3;
  1094. XmlSchemaKeyref keyref = XmlSchemaKeyref.Read(reader,h);
  1095. if(keyref != null)
  1096. element.constraints.Add(keyref);
  1097. continue;
  1098. }
  1099. }
  1100. reader.RaiseInvalidElementError();
  1101. }
  1102. return element;
  1103. }
  1104. }
  1105. }