XmlSchemaUnique.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.Xml;
  25. namespace System.Xml.Schema
  26. {
  27. /// <summary>
  28. /// Summary description for XmlSchemaUnique.
  29. /// </summary>
  30. public class XmlSchemaUnique : XmlSchemaIdentityConstraint
  31. {
  32. const string xmlname = "unique";
  33. public XmlSchemaUnique()
  34. {
  35. }
  36. /// <remarks>
  37. /// 1. name must be present
  38. /// 2. selector and field must be present
  39. /// </remarks>
  40. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  41. {
  42. return base.Compile(h,schema);
  43. }
  44. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  45. {
  46. return errorCount;
  47. }
  48. /*
  49. internal new void error(ValidationEventHandler handle, string message)
  50. {
  51. errorCount++;
  52. ValidationHandler.RaiseValidationError(handle, this, message);
  53. }
  54. */
  55. //<unique
  56. // id = ID
  57. // name = NCName
  58. // {any attributes with non-schema namespace . . .}>
  59. // Content: (annotation?, (selector, field+))
  60. //</unique>
  61. internal static XmlSchemaUnique Read(XmlSchemaReader reader, ValidationEventHandler h)
  62. {
  63. XmlSchemaUnique unique = new XmlSchemaUnique();
  64. reader.MoveToElement();
  65. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  66. {
  67. error(h,"Should not happen :1: XmlSchemaUnique.Read, name="+reader.Name,null);
  68. reader.Skip();
  69. return null;
  70. }
  71. unique.LineNumber = reader.LineNumber;
  72. unique.LinePosition = reader.LinePosition;
  73. unique.SourceUri = reader.BaseURI;
  74. while(reader.MoveToNextAttribute())
  75. {
  76. if(reader.Name == "id")
  77. {
  78. unique.Id = reader.Value;
  79. }
  80. else if(reader.Name == "name")
  81. {
  82. unique.Name = reader.Value;
  83. }
  84. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  85. {
  86. error(h,reader.Name + " is not a valid attribute for unique",null);
  87. }
  88. else
  89. {
  90. XmlSchemaUtil.ReadUnhandledAttribute(reader,unique);
  91. }
  92. }
  93. reader.MoveToElement();
  94. if(reader.IsEmptyElement)
  95. return unique;
  96. // Content: annotation?, selector, field+
  97. int level = 1;
  98. while(reader.ReadNextElement())
  99. {
  100. if(reader.NodeType == XmlNodeType.EndElement)
  101. {
  102. if(reader.LocalName != xmlname)
  103. error(h,"Should not happen :2: XmlSchemaUnion.Read, name="+reader.Name,null);
  104. break;
  105. }
  106. if(level <= 1 && reader.LocalName == "annotation")
  107. {
  108. level = 2; //Only one annotation
  109. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  110. if(annotation != null)
  111. unique.Annotation = annotation;
  112. continue;
  113. }
  114. if(level <= 2 && reader.LocalName == "selector")
  115. {
  116. level = 3;
  117. XmlSchemaXPath selector = XmlSchemaXPath.Read(reader,h,"selector");
  118. if(selector != null)
  119. unique.Selector = selector;
  120. continue;
  121. }
  122. if(level <= 3 && reader.LocalName == "field")
  123. {
  124. level = 3;
  125. if(unique.Selector == null)
  126. error(h,"selector must be defined before field declarations",null);
  127. XmlSchemaXPath field = XmlSchemaXPath.Read(reader, h, "field");
  128. if(field != null)
  129. unique.Fields.Add(field);
  130. continue;
  131. }
  132. reader.RaiseInvalidElementError();
  133. }
  134. return unique;
  135. }
  136. }
  137. }