XmlSchemaComplexContent.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // System.Xml.Schema.XmlSchemaComplexContent.cs
  3. //
  4. // Author:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Xml.Serialization;
  30. using System.Xml;
  31. namespace System.Xml.Schema
  32. {
  33. /// <summary>
  34. /// Summary description for XmlSchemaComplexContent.
  35. /// </summary>
  36. public class XmlSchemaComplexContent : XmlSchemaContentModel
  37. {
  38. private XmlSchemaContent content;
  39. private bool isMixed;
  40. const string xmlname = "complexContent";
  41. public XmlSchemaComplexContent()
  42. {}
  43. [System.Xml.Serialization.XmlAttribute("mixed")]
  44. public bool IsMixed
  45. {
  46. get{ return isMixed; }
  47. set{ isMixed = value; }
  48. }
  49. [XmlElement("restriction",typeof(XmlSchemaComplexContentRestriction),Namespace=XmlSchema.Namespace)]
  50. [XmlElement("extension",typeof(XmlSchemaComplexContentExtension),Namespace=XmlSchema.Namespace)]
  51. public override XmlSchemaContent Content
  52. {
  53. get{ return content; }
  54. set{ content = value; }
  55. }
  56. /// <remarks>
  57. /// 1. Content must be present
  58. /// </remarks>
  59. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  60. {
  61. // If this is already compiled this time, simply skip.
  62. if (this.IsComplied (schema.CompilationId))
  63. return 0;
  64. if (isRedefinedComponent) {
  65. if (Annotation != null)
  66. Annotation.isRedefinedComponent = true;
  67. if (Content != null)
  68. Content.isRedefinedComponent = true;
  69. }
  70. if(Content == null)
  71. {
  72. error(h, "Content must be present in a complexContent");
  73. }
  74. else
  75. {
  76. if(Content is XmlSchemaComplexContentRestriction)
  77. {
  78. XmlSchemaComplexContentRestriction xscr = (XmlSchemaComplexContentRestriction) Content;
  79. errorCount += xscr.Compile(h, schema);
  80. }
  81. else if(Content is XmlSchemaComplexContentExtension)
  82. {
  83. XmlSchemaComplexContentExtension xsce = (XmlSchemaComplexContentExtension) Content;
  84. errorCount += xsce.Compile(h, schema);
  85. }
  86. else
  87. error(h,"complexContent can't have any value other than restriction or extention");
  88. }
  89. XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
  90. this.CompilationId = schema.CompilationId;
  91. return errorCount;
  92. }
  93. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  94. {
  95. if (IsValidated (schema.ValidationId))
  96. return errorCount;
  97. errorCount += Content.Validate (h, schema);
  98. ValidationId = schema.ValidationId;
  99. return errorCount;
  100. }
  101. //<complexContent
  102. // id = ID
  103. // mixed = boolean
  104. // {any attributes with non-schema namespace . . .}>
  105. // Content: (annotation?, (restriction | extension))
  106. //</complexContent>
  107. internal static XmlSchemaComplexContent Read(XmlSchemaReader reader, ValidationEventHandler h)
  108. {
  109. XmlSchemaComplexContent complex = new XmlSchemaComplexContent();
  110. reader.MoveToElement();
  111. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  112. {
  113. error(h,"Should not happen :1: XmlSchemaComplexContent.Read, name="+reader.Name,null);
  114. reader.Skip();
  115. return null;
  116. }
  117. complex.LineNumber = reader.LineNumber;
  118. complex.LinePosition = reader.LinePosition;
  119. complex.SourceUri = reader.BaseURI;
  120. while(reader.MoveToNextAttribute())
  121. {
  122. if(reader.Name == "id")
  123. {
  124. complex.Id = reader.Value;
  125. }
  126. else if(reader.Name == "mixed")
  127. {
  128. Exception innerex;
  129. complex.isMixed = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
  130. if(innerex != null)
  131. error(h,reader.Value + " is an invalid value for mixed",innerex);
  132. }
  133. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  134. {
  135. error(h,reader.Name + " is not a valid attribute for complexContent",null);
  136. }
  137. else
  138. {
  139. XmlSchemaUtil.ReadUnhandledAttribute(reader,complex);
  140. }
  141. }
  142. reader.MoveToElement();
  143. if(reader.IsEmptyElement)
  144. return complex;
  145. //Content: (annotation?, (restriction | extension))
  146. int level = 1;
  147. while(reader.ReadNextElement())
  148. {
  149. if(reader.NodeType == XmlNodeType.EndElement)
  150. {
  151. if(reader.LocalName != xmlname)
  152. error(h,"Should not happen :2: XmlSchemaComplexContent.Read, name="+reader.Name,null);
  153. break;
  154. }
  155. if(level <= 1 && reader.LocalName == "annotation")
  156. {
  157. level = 2; //Only one annotation
  158. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  159. if(annotation != null)
  160. complex.Annotation = annotation;
  161. continue;
  162. }
  163. if(level <=2)
  164. {
  165. if(reader.LocalName == "restriction")
  166. {
  167. level = 3;
  168. XmlSchemaComplexContentRestriction restriction = XmlSchemaComplexContentRestriction.Read(reader,h);
  169. if(restriction != null)
  170. complex.content = restriction;
  171. continue;
  172. }
  173. if(reader.LocalName == "extension")
  174. {
  175. level = 3;
  176. XmlSchemaComplexContentExtension extension = XmlSchemaComplexContentExtension.Read(reader,h);
  177. if(extension != null)
  178. complex.content = extension;
  179. continue;
  180. }
  181. }
  182. reader.RaiseInvalidElementError();
  183. }
  184. return complex;
  185. }
  186. }
  187. }