XmlSchemaKey.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. using System;
  4. using System.Xml;
  5. namespace System.Xml.Schema
  6. {
  7. /// <summary>
  8. /// Summary description for XmlSchemaKey.
  9. /// </summary>
  10. public class XmlSchemaKey : XmlSchemaIdentityConstraint
  11. {
  12. private static string xmlname = "key";
  13. public XmlSchemaKey()
  14. {
  15. }
  16. /// <remarks>
  17. /// 1. name must be present
  18. /// 2. selector and field must be present
  19. /// </remarks>
  20. [MonoTODO]
  21. internal new int Compile(ValidationEventHandler h, XmlSchema schema)
  22. {
  23. // If this is already compiled this time, simply skip.
  24. if (this.IsComplied (schema.CompilationId))
  25. return 0;
  26. return base.Compile(h, schema);
  27. }
  28. [MonoTODO]
  29. internal int Validate(ValidationEventHandler h)
  30. {
  31. return errorCount;
  32. }
  33. internal new void error(ValidationEventHandler handle, string message)
  34. {
  35. errorCount++;
  36. ValidationHandler.RaiseValidationError(handle, this, message);
  37. }
  38. //<key
  39. // id = ID
  40. // name = NCName
  41. // {any attributes with non-schema namespace . . .}>
  42. // Content: (annotation?, (selector, field+))
  43. //</key>
  44. internal static XmlSchemaKey Read(XmlSchemaReader reader, ValidationEventHandler h)
  45. {
  46. XmlSchemaKey key = new XmlSchemaKey();
  47. reader.MoveToElement();
  48. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  49. {
  50. error(h,"Should not happen :1: XmlSchemaKey.Read, name="+reader.Name,null);
  51. reader.Skip();
  52. return null;
  53. }
  54. key.LineNumber = reader.LineNumber;
  55. key.LinePosition = reader.LinePosition;
  56. key.SourceUri = reader.BaseURI;
  57. while(reader.MoveToNextAttribute())
  58. {
  59. if(reader.Name == "id")
  60. {
  61. key.Id = reader.Value;
  62. }
  63. else if(reader.Name == "name")
  64. {
  65. key.Name = reader.Value;
  66. }
  67. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  68. {
  69. error(h,reader.Name + " is not a valid attribute for key",null);
  70. }
  71. else
  72. {
  73. XmlSchemaUtil.ReadUnhandledAttribute(reader,key);
  74. }
  75. }
  76. reader.MoveToElement();
  77. if(reader.IsEmptyElement)
  78. return key;
  79. // Content: annotation?, selector, field+
  80. int level = 1;
  81. while(reader.ReadNextElement())
  82. {
  83. if(reader.NodeType == XmlNodeType.EndElement)
  84. {
  85. if(reader.LocalName != xmlname)
  86. error(h,"Should not happen :2: XmlSchemaKey.Read, name="+reader.Name,null);
  87. break;
  88. }
  89. if(level <= 1 && reader.LocalName == "annotation")
  90. {
  91. level = 2; //Only one annotation
  92. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  93. if(annotation != null)
  94. key.Annotation = annotation;
  95. continue;
  96. }
  97. if(level <= 2 && reader.LocalName == "selector")
  98. {
  99. level = 3;
  100. XmlSchemaXPath selector = XmlSchemaXPath.Read(reader,h,"selector");
  101. if(selector != null)
  102. key.Selector = selector;
  103. continue;
  104. }
  105. if(level <= 3 && reader.LocalName == "field")
  106. {
  107. level = 3;
  108. if(key.Selector == null)
  109. error(h,"selector must be defined before field declarations",null);
  110. XmlSchemaXPath field = XmlSchemaXPath.Read(reader,h,"field");
  111. if(field != null)
  112. key.Fields.Add(field);
  113. continue;
  114. }
  115. reader.RaiseInvalidElementError();
  116. }
  117. return key;
  118. }
  119. }
  120. }