XmlSchemaKeyref.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. using System;
  4. using System.Xml;
  5. using System.Xml.Serialization;
  6. namespace System.Xml.Schema
  7. {
  8. /// <summary>
  9. /// Summary description for XmlSchemaKeyref.
  10. /// </summary>
  11. public class XmlSchemaKeyref : XmlSchemaIdentityConstraint
  12. {
  13. private XmlQualifiedName refer;
  14. private static string xmlname = "keyref";
  15. private XmlSchemaIdentityConstraint target;
  16. public XmlSchemaKeyref()
  17. {
  18. refer = XmlQualifiedName.Empty;
  19. }
  20. [System.Xml.Serialization.XmlAttribute("refer")]
  21. public XmlQualifiedName Refer
  22. {
  23. get{ return refer; }
  24. set{ refer = value; }
  25. }
  26. internal XmlSchemaIdentityConstraint Target
  27. {
  28. get { return target; }
  29. }
  30. /// <remarks>
  31. /// 1. name must be present
  32. /// 2. selector and field must be present
  33. /// 3. refer must be present
  34. /// </remarks>
  35. [MonoTODO]
  36. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  37. {
  38. base.Compile(h, schema);
  39. if(refer == null || refer.IsEmpty)
  40. error(h,"refer must be present");
  41. else if(!XmlSchemaUtil.CheckQName(refer))
  42. error(h,"Refer is not a valid XmlQualifiedName");
  43. return errorCount;
  44. }
  45. [MonoTODO]
  46. internal override int Validate (ValidationEventHandler h, XmlSchema schema)
  47. {
  48. // Find target key
  49. XmlSchemaIdentityConstraint target = schema.NamedIdentities [this.Refer] as XmlSchemaIdentityConstraint;
  50. if (target == null)
  51. error (h, "Target key was not found.");
  52. else if (target is XmlSchemaKeyref)
  53. error (h, "Target identity constraint was keyref.");
  54. else if (target.Fields.Count != this.Fields.Count)
  55. error (h, "Target identity constraint has different number of fields.");
  56. else
  57. this.target = target;
  58. return errorCount;
  59. }
  60. /*
  61. internal new void error(ValidationEventHandler handle, string message)
  62. {
  63. errorCount++;
  64. ValidationHandler.RaiseValidationError(handle, this, message);
  65. }
  66. */
  67. //<key
  68. // id = ID
  69. // name = NCName
  70. // refer = QName
  71. // {any attributes with non-schema namespace . . .}>
  72. // Content: (annotation?, (selector, field+))
  73. //</key>
  74. internal static XmlSchemaKeyref Read(XmlSchemaReader reader, ValidationEventHandler h)
  75. {
  76. XmlSchemaKeyref keyref = new XmlSchemaKeyref();
  77. reader.MoveToElement();
  78. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  79. {
  80. error(h,"Should not happen :1: XmlSchemaKeyref.Read, name="+reader.Name,null);
  81. reader.Skip();
  82. return null;
  83. }
  84. keyref.LineNumber = reader.LineNumber;
  85. keyref.LinePosition = reader.LinePosition;
  86. keyref.SourceUri = reader.BaseURI;
  87. while(reader.MoveToNextAttribute())
  88. {
  89. if(reader.Name == "id")
  90. {
  91. keyref.Id = reader.Value;
  92. }
  93. else if(reader.Name == "name")
  94. {
  95. keyref.Name = reader.Value;
  96. }
  97. else if(reader.Name == "refer")
  98. {
  99. Exception innerex;
  100. keyref.refer = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  101. if(innerex != null)
  102. error(h, reader.Value + " is not a valid value for refer attribute",innerex);
  103. }
  104. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  105. {
  106. error(h,reader.Name + " is not a valid attribute for keyref",null);
  107. }
  108. else
  109. {
  110. XmlSchemaUtil.ReadUnhandledAttribute(reader,keyref);
  111. }
  112. }
  113. reader.MoveToElement();
  114. if(reader.IsEmptyElement)
  115. return keyref;
  116. // Content: annotation?, selector, field+
  117. int level = 1;
  118. while(reader.ReadNextElement())
  119. {
  120. if(reader.NodeType == XmlNodeType.EndElement)
  121. {
  122. if(reader.LocalName != xmlname)
  123. error(h,"Should not happen :2: XmlSchemaKeyref.Read, name="+reader.Name,null);
  124. break;
  125. }
  126. if(level <= 1 && reader.LocalName == "annotation")
  127. {
  128. level = 2; //Only one annotation
  129. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  130. if(annotation != null)
  131. keyref.Annotation = annotation;
  132. continue;
  133. }
  134. if(level <= 2 && reader.LocalName == "selector")
  135. {
  136. level = 3;
  137. XmlSchemaXPath selector = XmlSchemaXPath.Read(reader,h,"selector");
  138. if(selector != null)
  139. keyref.Selector = selector;
  140. continue;
  141. }
  142. if(level <= 3 && reader.LocalName == "field")
  143. {
  144. level = 3;
  145. if(keyref.Selector == null)
  146. error(h,"selector must be defined before field declarations",null);
  147. XmlSchemaXPath field = XmlSchemaXPath.Read(reader,h,"field");
  148. if(field != null)
  149. keyref.Fields.Add(field);
  150. continue;
  151. }
  152. reader.RaiseInvalidElementError();
  153. }
  154. return keyref;
  155. }
  156. }
  157. }