XmlSchemaElement.cs 34 KB

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