XmlSchemaComplexContent.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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))]
  50. [XmlElement("extension",typeof(XmlSchemaComplexContentExtension))]
  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 NET_2_0
  65. if (Content != null)
  66. Content.Parent = this;
  67. #endif
  68. if (isRedefinedComponent) {
  69. if (Annotation != null)
  70. Annotation.isRedefinedComponent = true;
  71. if (Content != null)
  72. Content.isRedefinedComponent = true;
  73. }
  74. if(Content == null)
  75. {
  76. error(h, "Content must be present in a complexContent");
  77. }
  78. else
  79. {
  80. if(Content is XmlSchemaComplexContentRestriction)
  81. {
  82. XmlSchemaComplexContentRestriction xscr = (XmlSchemaComplexContentRestriction) Content;
  83. errorCount += xscr.Compile(h, schema);
  84. }
  85. else if(Content is XmlSchemaComplexContentExtension)
  86. {
  87. XmlSchemaComplexContentExtension xsce = (XmlSchemaComplexContentExtension) Content;
  88. errorCount += xsce.Compile(h, schema);
  89. }
  90. else
  91. error(h,"complexContent can't have any value other than restriction or extention");
  92. }
  93. XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
  94. this.CompilationId = schema.CompilationId;
  95. return errorCount;
  96. }
  97. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  98. {
  99. if (IsValidated (schema.ValidationId))
  100. return errorCount;
  101. errorCount += Content.Validate (h, schema);
  102. ValidationId = schema.ValidationId;
  103. return errorCount;
  104. }
  105. //<complexContent
  106. // id = ID
  107. // mixed = boolean
  108. // {any attributes with non-schema namespace . . .}>
  109. // Content: (annotation?, (restriction | extension))
  110. //</complexContent>
  111. internal static XmlSchemaComplexContent Read(XmlSchemaReader reader, ValidationEventHandler h)
  112. {
  113. XmlSchemaComplexContent complex = new XmlSchemaComplexContent();
  114. reader.MoveToElement();
  115. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  116. {
  117. error(h,"Should not happen :1: XmlSchemaComplexContent.Read, name="+reader.Name,null);
  118. reader.Skip();
  119. return null;
  120. }
  121. complex.LineNumber = reader.LineNumber;
  122. complex.LinePosition = reader.LinePosition;
  123. complex.SourceUri = reader.BaseURI;
  124. while(reader.MoveToNextAttribute())
  125. {
  126. if(reader.Name == "id")
  127. {
  128. complex.Id = reader.Value;
  129. }
  130. else if(reader.Name == "mixed")
  131. {
  132. Exception innerex;
  133. complex.isMixed = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
  134. if(innerex != null)
  135. error(h,reader.Value + " is an invalid value for mixed",innerex);
  136. }
  137. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  138. {
  139. error(h,reader.Name + " is not a valid attribute for complexContent",null);
  140. }
  141. else
  142. {
  143. XmlSchemaUtil.ReadUnhandledAttribute(reader,complex);
  144. }
  145. }
  146. reader.MoveToElement();
  147. if(reader.IsEmptyElement)
  148. return complex;
  149. //Content: (annotation?, (restriction | extension))
  150. int level = 1;
  151. while(reader.ReadNextElement())
  152. {
  153. if(reader.NodeType == XmlNodeType.EndElement)
  154. {
  155. if(reader.LocalName != xmlname)
  156. error(h,"Should not happen :2: XmlSchemaComplexContent.Read, name="+reader.Name,null);
  157. break;
  158. }
  159. if(level <= 1 && reader.LocalName == "annotation")
  160. {
  161. level = 2; //Only one annotation
  162. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  163. if(annotation != null)
  164. complex.Annotation = annotation;
  165. continue;
  166. }
  167. if(level <=2)
  168. {
  169. if(reader.LocalName == "restriction")
  170. {
  171. level = 3;
  172. XmlSchemaComplexContentRestriction restriction = XmlSchemaComplexContentRestriction.Read(reader,h);
  173. if(restriction != null)
  174. complex.content = restriction;
  175. continue;
  176. }
  177. if(reader.LocalName == "extension")
  178. {
  179. level = 3;
  180. XmlSchemaComplexContentExtension extension = XmlSchemaComplexContentExtension.Read(reader,h);
  181. if(extension != null)
  182. complex.content = extension;
  183. continue;
  184. }
  185. }
  186. reader.RaiseInvalidElementError();
  187. }
  188. return complex;
  189. }
  190. }
  191. }