XmlSchemaElement.cs 39 KB

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