XmlSchemaIdentityConstraint.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. using System;
  4. using System.Collections;
  5. using System.Xml;
  6. using System.Xml.Serialization;
  7. using Mono.Xml.Schema;
  8. namespace System.Xml.Schema
  9. {
  10. /// <summary>
  11. /// Summary description for XmlSchemaIdentityConstraint.
  12. /// </summary>
  13. public class XmlSchemaIdentityConstraint : XmlSchemaAnnotated
  14. {
  15. private XmlSchemaObjectCollection fields;
  16. private string name;
  17. private XmlQualifiedName qName;
  18. private XmlSchemaXPath selector;
  19. private XsdIdentitySelector compiledSelector;
  20. // ArrayList compiledFields;
  21. public XmlSchemaIdentityConstraint()
  22. {
  23. fields = new XmlSchemaObjectCollection();
  24. qName = XmlQualifiedName.Empty;
  25. }
  26. [System.Xml.Serialization.XmlAttribute("name")]
  27. public string Name
  28. {
  29. get{ return name; }
  30. set{ name = value; }
  31. }
  32. [XmlElement("selector",typeof(XmlSchemaXPath),Namespace=XmlSchema.Namespace)]
  33. public XmlSchemaXPath Selector
  34. {
  35. get{ return selector; }
  36. set{ selector = value; }
  37. }
  38. [XmlElement("field",typeof(XmlSchemaXPath),Namespace=XmlSchema.Namespace)]
  39. public XmlSchemaObjectCollection Fields
  40. {
  41. get{ return fields; }
  42. }
  43. [XmlIgnore]
  44. public XmlQualifiedName QualifiedName
  45. {
  46. get{ return qName; }
  47. }
  48. internal XsdIdentitySelector CompiledSelector {
  49. get { return compiledSelector; }
  50. }
  51. /// <remarks>
  52. /// 1. name must be present
  53. /// 2. selector and field must be present
  54. /// </remarks>
  55. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  56. {
  57. // If this is already compiled this time, simply skip.
  58. if (this.IsComplied (schema.CompilationId))
  59. return 0;
  60. if(Name == null)
  61. error(h,"Required attribute name must be present");
  62. else if(!XmlSchemaUtil.CheckNCName(this.name))
  63. error(h,"attribute name must be NCName");
  64. else {
  65. this.qName = new XmlQualifiedName(Name,schema.TargetNamespace);
  66. if (schema.NamedIdentities.Contains (qName))
  67. error(h,"There is already same named identity constraint in this namespace.");
  68. else
  69. schema.NamedIdentities.Add (qName, this);
  70. }
  71. if(Selector == null)
  72. error(h,"selector must be present");
  73. else
  74. {
  75. Selector.isSelector = true;
  76. errorCount += Selector.Compile(h,schema);
  77. if (selector.errorCount == 0)
  78. compiledSelector = new XsdIdentitySelector (Selector);
  79. }
  80. if (errorCount > 0)
  81. return errorCount; // fatal
  82. if(Fields.Count == 0)
  83. error(h,"atleast one field value must be present");
  84. else
  85. {
  86. for (int i = 0; i < Fields.Count; i++)
  87. {
  88. XmlSchemaXPath field = Fields [i] as XmlSchemaXPath;
  89. if(field != null)
  90. {
  91. errorCount += field.Compile(h,schema);
  92. if (field.errorCount == 0)
  93. this.compiledSelector.AddField (new XsdIdentityField (field, i));
  94. }
  95. else
  96. error (h, "Object of type " + Fields [i].GetType() + " is invalid in the Fields Collection");
  97. }
  98. }
  99. XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
  100. this.CompilationId = schema.CompilationId;
  101. return errorCount;
  102. }
  103. }
  104. }