XmlSchemaKeyref.cs 5.7 KB

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