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