XmlSchemaElement.cs 38 KB

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