XmlSchemaElement.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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 void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  543. ValidationEventHandler h, XmlSchema schema)
  544. {
  545. XmlSchemaElement baseElement = baseParticle as XmlSchemaElement;
  546. if (baseElement != null) {
  547. ValidateDerivationByRestrictionNameAndTypeOK (baseElement, h, schema);
  548. return;
  549. }
  550. XmlSchemaAny baseAny = baseParticle as XmlSchemaAny;
  551. if (baseAny != null) {
  552. // NSCompat
  553. baseAny.ValidateWildcardAllowsNamespaceName (this.QualifiedName.Namespace, h, schema, true);
  554. ValidateOccurenceRangeOK (baseAny, h, schema);
  555. return;
  556. }
  557. }
  558. private void ValidateDerivationByRestrictionNameAndTypeOK (XmlSchemaElement baseElement,
  559. ValidationEventHandler h, XmlSchema schema)
  560. {
  561. // 1.
  562. if (this.QualifiedName != baseElement.QualifiedName)
  563. error (h, "Invalid derivation by restriction of particle was found. Both elements must have the same name.");
  564. // 2.
  565. if (this.isNillable && !baseElement.isNillable)
  566. error (h, "Invalid element derivation by restriction of particle was found. Base element is not nillable and derived type is nillable.");
  567. // 3.
  568. ValidateOccurenceRangeOK (baseElement, h, schema);
  569. // 4.
  570. if (baseElement.ValidatedFixedValue != null &&
  571. baseElement.ValidatedFixedValue != this.ValidatedFixedValue)
  572. error (h, "Invalid element derivation by restriction of particle was found. Both fixed value must be the same.");
  573. // 5. TODO: What is "identity constraints subset" ???
  574. // 6.
  575. if ((baseElement.BlockResolved | this.BlockResolved) != this.BlockResolved)
  576. error (h, "Invalid derivation by restriction of particle was found. Derived element must contain all of the base element's block value.");
  577. // 7.
  578. if (baseElement.ElementType != null) {
  579. XmlSchemaComplexType derivedCType = this.ElementType as XmlSchemaComplexType;
  580. if (derivedCType != null)
  581. // W3C REC says that it is Type Derivation OK to be check, but
  582. // in fact it should be DerivationValid (Restriction, Complex).
  583. derivedCType.ValidateDerivationValidRestriction (
  584. baseElement.ElementType as XmlSchemaComplexType, h, schema);
  585. // derivedCType.ValidateTypeDerivationOK (baseElement.ElementType, h, schema);
  586. else {
  587. XmlSchemaSimpleType derivedSType = this.ElementType as XmlSchemaSimpleType;
  588. if (derivedSType != null)
  589. derivedSType.ValidateTypeDerivationOK (baseElement.ElementType, h, schema, true);
  590. else if (baseElement.ElementType != this.ElementType)
  591. error (h, "Invalid element derivation by restriction of particle was found. Both primitive types differ.");
  592. }
  593. }
  594. }
  595. internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  596. {
  597. XmlSchemaComplexType ct = this.ElementType as XmlSchemaComplexType;
  598. if (ct == null || ct.ContentTypeParticle == null)
  599. return;
  600. ct.ContentTypeParticle.CheckRecursion (depth + 1, h, schema);
  601. }
  602. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  603. ValidationEventHandler h, XmlSchema schema)
  604. {
  605. if (qnames.Contains (this.QualifiedName))
  606. error (h, "Ambiguous element label was detected: " + this.QualifiedName);
  607. else {
  608. foreach (XmlSchemaAny any in nsNames) {
  609. if (any.ValidatedMaxOccurs == 0)
  610. continue;
  611. if (any.HasValueAny ||
  612. any.HasValueLocal && this.QualifiedName.Namespace == "" ||
  613. any.HasValueOther && this.QualifiedName.Namespace != this.targetNamespace ||
  614. any.HasValueTargetNamespace && this.QualifiedName.Namespace == this.targetNamespace) {
  615. error (h, "Ambiguous element label which is contained by -any- particle was detected: " + this.QualifiedName);
  616. break;
  617. } else {
  618. bool bad = false;
  619. foreach (string ns in any.ResolvedNamespaces) {
  620. if (ns == this.QualifiedName.Namespace) {
  621. bad = true;
  622. break;
  623. }
  624. }
  625. if (bad) {
  626. error (h, "Ambiguous element label which is contained by -any- particle was detected: " + this.QualifiedName);
  627. break;
  628. }
  629. }
  630. }
  631. qnames.Add (this.QualifiedName, this);
  632. }
  633. }
  634. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  635. ValidationEventHandler h, XmlSchema schema)
  636. {
  637. XmlSchemaElement labeled = labels [this.QualifiedName] as XmlSchemaElement;
  638. if (labeled == null)
  639. labels.Add (this.QualifiedName, this);
  640. else if (labeled.ElementType != this.ElementType)
  641. error (h, "Different types are specified on the same named elements in the same sequence. Element name is " + QualifiedName);
  642. }
  643. // 3.3.6 Element Default Valid (Immediate)
  644. private void ValidateElementDefaultValidImmediate (ValidationEventHandler h, XmlSchema schema)
  645. {
  646. // This presumes that ElementType is already filled.
  647. XmlSchemaDatatype datatype = elementType as XmlSchemaDatatype;
  648. XmlSchemaSimpleType simpleType = elementType as XmlSchemaSimpleType;
  649. if (simpleType != null)
  650. datatype = simpleType.Datatype;
  651. if (datatype == null) {
  652. XmlSchemaComplexType complexType = elementType as XmlSchemaComplexType;
  653. switch (complexType.ContentType) {
  654. case XmlSchemaContentType.Empty:
  655. case XmlSchemaContentType.ElementOnly:
  656. error (h, "Element content type must be simple type or mixed.");
  657. break;
  658. }
  659. datatype = XmlSchemaSimpleType.AnySimpleType;
  660. }
  661. XmlNamespaceManager nsmgr = null;
  662. if (datatype.TokenizedType == XmlTokenizedType.QName) {
  663. if (this.Namespaces != null)
  664. foreach (XmlQualifiedName qname in Namespaces.ToArray ())
  665. nsmgr.AddNamespace (qname.Name, qname.Namespace);
  666. }
  667. try {
  668. if (defaultValue != null) {
  669. validatedDefaultValue = datatype.Normalize (defaultValue);
  670. datatype.ParseValue (validatedDefaultValue, null, nsmgr);
  671. }
  672. } catch (Exception ex) {
  673. // FIXME: This is not a good way to handle exception, but
  674. // I think there is no remedy for such Framework specification.
  675. error (h, "The Element's default value is invalid with respect to its type definition.", ex);
  676. }
  677. try {
  678. if (fixedValue != null) {
  679. validatedFixedValue = datatype.Normalize (fixedValue);
  680. datatype.ParseValue (validatedFixedValue, null, nsmgr);
  681. }
  682. } catch (Exception ex) {
  683. // FIXME: This is not a good way to handle exception.
  684. error (h, "The Element's fixed value is invalid with its type definition.", ex);
  685. }
  686. }
  687. //<element
  688. // abstract = boolean : false
  689. // block = (#all | List of (extension | restriction | substitution))
  690. // default = string
  691. // final = (#all | List of (extension | restriction))
  692. // fixed = string
  693. // form = (qualified | unqualified)
  694. // id = ID
  695. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  696. // minOccurs = nonNegativeInteger : 1
  697. // name = NCName
  698. // nillable = boolean : false
  699. // ref = QName
  700. // substitutionGroup = QName
  701. // type = QName
  702. // {any attributes with non-schema namespace . . .}>
  703. // Content: (annotation?, ((simpleType | complexType)?, (unique | key | keyref)*))
  704. //</element>
  705. internal static XmlSchemaElement Read(XmlSchemaReader reader, ValidationEventHandler h)
  706. {
  707. XmlSchemaElement element = new XmlSchemaElement();
  708. Exception innerex;
  709. reader.MoveToElement();
  710. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  711. {
  712. error(h,"Should not happen :1: XmlSchemaElement.Read, name="+reader.Name,null);
  713. reader.Skip();
  714. return null;
  715. }
  716. element.LineNumber = reader.LineNumber;
  717. element.LinePosition = reader.LinePosition;
  718. element.SourceUri = reader.BaseURI;
  719. while(reader.MoveToNextAttribute())
  720. {
  721. if(reader.Name == "abstract")
  722. {
  723. element.IsAbstract = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
  724. if(innerex != null)
  725. error(h,reader.Value + " is invalid value for abstract",innerex);
  726. }
  727. else if(reader.Name == "block")
  728. {
  729. element.block = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "block",
  730. XmlSchemaUtil.ElementBlockAllowed);
  731. if(innerex != null)
  732. warn(h,"some invalid values for block attribute were found",innerex);
  733. }
  734. else if(reader.Name == "default")
  735. {
  736. element.defaultValue = reader.Value;
  737. }
  738. else if(reader.Name == "final")
  739. {
  740. element.Final = XmlSchemaUtil.ReadDerivationAttribute(reader,out innerex, "final",
  741. XmlSchemaUtil.FinalAllowed);
  742. if(innerex != null)
  743. warn(h,"some invalid values for final attribute were found",innerex);
  744. }
  745. else if(reader.Name == "fixed")
  746. {
  747. element.fixedValue = reader.Value;
  748. }
  749. else if(reader.Name == "form")
  750. {
  751. element.form = XmlSchemaUtil.ReadFormAttribute(reader,out innerex);
  752. if(innerex != null)
  753. error(h,reader.Value + " is an invalid value for form attribute",innerex);
  754. }
  755. else if(reader.Name == "id")
  756. {
  757. element.Id = reader.Value;
  758. }
  759. else if(reader.Name == "maxOccurs")
  760. {
  761. try
  762. {
  763. element.MaxOccursString = reader.Value;
  764. }
  765. catch(Exception e)
  766. {
  767. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  768. }
  769. }
  770. else if(reader.Name == "minOccurs")
  771. {
  772. try
  773. {
  774. element.MinOccursString = reader.Value;
  775. }
  776. catch(Exception e)
  777. {
  778. error(h,reader.Value + " is an invalid value for minOccurs",e);
  779. }
  780. }
  781. else if(reader.Name == "name")
  782. {
  783. element.Name = reader.Value;
  784. }
  785. else if(reader.Name == "nillable")
  786. {
  787. element.IsNillable = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
  788. if(innerex != null)
  789. error(h,reader.Value + "is not a valid value for nillable",innerex);
  790. }
  791. else if(reader.Name == "ref")
  792. {
  793. element.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  794. if(innerex != null)
  795. error(h, reader.Value + " is not a valid value for ref attribute",innerex);
  796. }
  797. else if(reader.Name == "substitutionGroup")
  798. {
  799. element.substitutionGroup = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  800. if(innerex != null)
  801. error(h, reader.Value + " is not a valid value for substitutionGroup attribute",innerex);
  802. }
  803. else if(reader.Name == "type")
  804. {
  805. element.SchemaTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  806. if(innerex != null)
  807. error(h, reader.Value + " is not a valid value for type attribute",innerex);
  808. }
  809. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  810. {
  811. error(h,reader.Name + " is not a valid attribute for element",null);
  812. }
  813. else
  814. {
  815. XmlSchemaUtil.ReadUnhandledAttribute(reader,element);
  816. }
  817. }
  818. reader.MoveToElement();
  819. if(reader.IsEmptyElement)
  820. return element;
  821. // Content: annotation?,
  822. // (simpleType | complexType)?,
  823. // (unique | key | keyref)*
  824. int level = 1;
  825. while(reader.ReadNextElement())
  826. {
  827. if(reader.NodeType == XmlNodeType.EndElement)
  828. {
  829. if(reader.LocalName != xmlname)
  830. error(h,"Should not happen :2: XmlSchemaElement.Read, name="+reader.Name,null);
  831. break;
  832. }
  833. if(level <= 1 && reader.LocalName == "annotation")
  834. {
  835. level = 2; //Only one annotation
  836. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  837. if(annotation != null)
  838. element.Annotation = annotation;
  839. continue;
  840. }
  841. if(level <= 2)
  842. {
  843. if(reader.LocalName == "simpleType")
  844. {
  845. level = 3;
  846. XmlSchemaSimpleType simple = XmlSchemaSimpleType.Read(reader,h);
  847. if(simple != null)
  848. element.SchemaType = simple;
  849. continue;
  850. }
  851. if(reader.LocalName == "complexType")
  852. {
  853. level = 3;
  854. XmlSchemaComplexType complex = XmlSchemaComplexType.Read(reader,h);
  855. if(complex != null)
  856. {
  857. element.SchemaType = complex;
  858. }
  859. continue;
  860. }
  861. }
  862. if(level <= 3)
  863. {
  864. if(reader.LocalName == "unique")
  865. {
  866. level = 3;
  867. XmlSchemaUnique unique = XmlSchemaUnique.Read(reader,h);
  868. if(unique != null)
  869. element.constraints.Add(unique);
  870. continue;
  871. }
  872. else if(reader.LocalName == "key")
  873. {
  874. level = 3;
  875. XmlSchemaKey key = XmlSchemaKey.Read(reader,h);
  876. if(key != null)
  877. element.constraints.Add(key);
  878. continue;
  879. }
  880. else if(reader.LocalName == "keyref")
  881. {
  882. level = 3;
  883. XmlSchemaKeyref keyref = XmlSchemaKeyref.Read(reader,h);
  884. if(keyref != null)
  885. element.constraints.Add(keyref);
  886. continue;
  887. }
  888. }
  889. reader.RaiseInvalidElementError();
  890. }
  891. return element;
  892. }
  893. }
  894. }