XmlSchemaKey.cs 4.2 KB

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