XmlSchemaIdentityConstraint.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Collections;
  25. using System.Xml;
  26. using System.Xml.Serialization;
  27. using Mono.Xml.Schema;
  28. namespace System.Xml.Schema
  29. {
  30. /// <summary>
  31. /// Summary description for XmlSchemaIdentityConstraint.
  32. /// </summary>
  33. public class XmlSchemaIdentityConstraint : XmlSchemaAnnotated
  34. {
  35. private XmlSchemaObjectCollection fields;
  36. private string name;
  37. private XmlQualifiedName qName;
  38. private XmlSchemaXPath selector;
  39. private XsdIdentitySelector compiledSelector;
  40. // ArrayList compiledFields;
  41. public XmlSchemaIdentityConstraint()
  42. {
  43. fields = new XmlSchemaObjectCollection();
  44. qName = XmlQualifiedName.Empty;
  45. }
  46. [System.Xml.Serialization.XmlAttribute("name")]
  47. public string Name
  48. {
  49. get{ return name; }
  50. set{ name = value; }
  51. }
  52. [XmlElement("selector",typeof(XmlSchemaXPath),Namespace=XmlSchema.Namespace)]
  53. public XmlSchemaXPath Selector
  54. {
  55. get{ return selector; }
  56. set{ selector = value; }
  57. }
  58. [XmlElement("field",typeof(XmlSchemaXPath),Namespace=XmlSchema.Namespace)]
  59. public XmlSchemaObjectCollection Fields
  60. {
  61. get{ return fields; }
  62. }
  63. [XmlIgnore]
  64. public XmlQualifiedName QualifiedName
  65. {
  66. get{ return qName; }
  67. }
  68. internal XsdIdentitySelector CompiledSelector {
  69. get { return compiledSelector; }
  70. }
  71. /// <remarks>
  72. /// 1. name must be present
  73. /// 2. selector and field must be present
  74. /// </remarks>
  75. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  76. {
  77. // If this is already compiled this time, simply skip.
  78. if (this.IsComplied (schema.CompilationId))
  79. return 0;
  80. #if NET_2_0
  81. if (Selector != null)
  82. Selector.Parent = this;
  83. foreach (XmlSchemaObject obj in Fields)
  84. obj.Parent = this;
  85. #endif
  86. if(Name == null)
  87. error(h,"Required attribute name must be present");
  88. else if(!XmlSchemaUtil.CheckNCName(this.name))
  89. error(h,"attribute name must be NCName");
  90. else {
  91. this.qName = new XmlQualifiedName(Name,schema.TargetNamespace);
  92. if (schema.NamedIdentities.Contains (qName))
  93. error(h,"There is already same named identity constraint in this namespace.");
  94. else
  95. schema.NamedIdentities.Add (qName, this);
  96. }
  97. if(Selector == null)
  98. error(h,"selector must be present");
  99. else
  100. {
  101. Selector.isSelector = true;
  102. errorCount += Selector.Compile(h,schema);
  103. if (selector.errorCount == 0)
  104. compiledSelector = new XsdIdentitySelector (Selector);
  105. }
  106. if (errorCount > 0)
  107. return errorCount; // fatal
  108. if(Fields.Count == 0)
  109. error(h,"atleast one field value must be present");
  110. else
  111. {
  112. for (int i = 0; i < Fields.Count; i++)
  113. {
  114. XmlSchemaXPath field = Fields [i] as XmlSchemaXPath;
  115. if(field != null)
  116. {
  117. errorCount += field.Compile(h,schema);
  118. if (field.errorCount == 0)
  119. this.compiledSelector.AddField (new XsdIdentityField (field, i));
  120. }
  121. else
  122. error (h, "Object of type " + Fields [i].GetType() + " is invalid in the Fields Collection");
  123. }
  124. }
  125. XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
  126. this.CompilationId = schema.CompilationId;
  127. return errorCount;
  128. }
  129. }
  130. }