XmlSchemaComplexContent.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // System.Xml.Schema.XmlSchemaComplexContent.cs
  3. //
  4. // Author:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. using System;
  9. using System.Xml.Serialization;
  10. using System.Xml;
  11. namespace System.Xml.Schema
  12. {
  13. /// <summary>
  14. /// Summary description for XmlSchemaComplexContent.
  15. /// </summary>
  16. public class XmlSchemaComplexContent : XmlSchemaContentModel
  17. {
  18. private XmlSchemaContent content;
  19. private bool isMixed;
  20. const string xmlname = "complexContent";
  21. public XmlSchemaComplexContent()
  22. {}
  23. [System.Xml.Serialization.XmlAttribute("mixed")]
  24. public bool IsMixed
  25. {
  26. get{ return isMixed; }
  27. set{ isMixed = value; }
  28. }
  29. [XmlElement("restriction",typeof(XmlSchemaComplexContentRestriction),Namespace=XmlSchema.Namespace)]
  30. [XmlElement("extension",typeof(XmlSchemaComplexContentExtension),Namespace=XmlSchema.Namespace)]
  31. public override XmlSchemaContent Content
  32. {
  33. get{ return content; }
  34. set{ content = value; }
  35. }
  36. /// <remarks>
  37. /// 1. Content must be present
  38. /// </remarks>
  39. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  40. {
  41. // If this is already compiled this time, simply skip.
  42. if (this.IsComplied (schema.CompilationId))
  43. return 0;
  44. if (isRedefinedComponent) {
  45. if (Annotation != null)
  46. Annotation.isRedefinedComponent = true;
  47. if (Content != null)
  48. Content.isRedefinedComponent = true;
  49. }
  50. if(Content == null)
  51. {
  52. error(h, "Content must be present in a complexContent");
  53. }
  54. else
  55. {
  56. if(Content is XmlSchemaComplexContentRestriction)
  57. {
  58. XmlSchemaComplexContentRestriction xscr = (XmlSchemaComplexContentRestriction) Content;
  59. errorCount += xscr.Compile(h, schema);
  60. }
  61. else if(Content is XmlSchemaComplexContentExtension)
  62. {
  63. XmlSchemaComplexContentExtension xsce = (XmlSchemaComplexContentExtension) Content;
  64. errorCount += xsce.Compile(h, schema);
  65. }
  66. else
  67. error(h,"complexContent can't have any value other than restriction or extention");
  68. }
  69. XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
  70. this.CompilationId = schema.CompilationId;
  71. return errorCount;
  72. }
  73. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  74. {
  75. if (IsValidated (schema.ValidationId))
  76. return errorCount;
  77. errorCount += Content.Validate (h, schema);
  78. ValidationId = schema.ValidationId;
  79. return errorCount;
  80. }
  81. //<complexContent
  82. // id = ID
  83. // mixed = boolean
  84. // {any attributes with non-schema namespace . . .}>
  85. // Content: (annotation?, (restriction | extension))
  86. //</complexContent>
  87. internal static XmlSchemaComplexContent Read(XmlSchemaReader reader, ValidationEventHandler h)
  88. {
  89. XmlSchemaComplexContent complex = new XmlSchemaComplexContent();
  90. reader.MoveToElement();
  91. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  92. {
  93. error(h,"Should not happen :1: XmlSchemaComplexContent.Read, name="+reader.Name,null);
  94. reader.Skip();
  95. return null;
  96. }
  97. complex.LineNumber = reader.LineNumber;
  98. complex.LinePosition = reader.LinePosition;
  99. complex.SourceUri = reader.BaseURI;
  100. while(reader.MoveToNextAttribute())
  101. {
  102. if(reader.Name == "id")
  103. {
  104. complex.Id = reader.Value;
  105. }
  106. else if(reader.Name == "mixed")
  107. {
  108. Exception innerex;
  109. complex.isMixed = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
  110. if(innerex != null)
  111. error(h,reader.Value + " is an invalid value for mixed",innerex);
  112. }
  113. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  114. {
  115. error(h,reader.Name + " is not a valid attribute for complexContent",null);
  116. }
  117. else
  118. {
  119. XmlSchemaUtil.ReadUnhandledAttribute(reader,complex);
  120. }
  121. }
  122. reader.MoveToElement();
  123. if(reader.IsEmptyElement)
  124. return complex;
  125. //Content: (annotation?, (restriction | extension))
  126. int level = 1;
  127. while(reader.ReadNextElement())
  128. {
  129. if(reader.NodeType == XmlNodeType.EndElement)
  130. {
  131. if(reader.LocalName != xmlname)
  132. error(h,"Should not happen :2: XmlSchemaComplexContent.Read, name="+reader.Name,null);
  133. break;
  134. }
  135. if(level <= 1 && reader.LocalName == "annotation")
  136. {
  137. level = 2; //Only one annotation
  138. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  139. if(annotation != null)
  140. complex.Annotation = annotation;
  141. continue;
  142. }
  143. if(level <=2)
  144. {
  145. if(reader.LocalName == "restriction")
  146. {
  147. level = 3;
  148. XmlSchemaComplexContentRestriction restriction = XmlSchemaComplexContentRestriction.Read(reader,h);
  149. if(restriction != null)
  150. complex.content = restriction;
  151. continue;
  152. }
  153. if(reader.LocalName == "extension")
  154. {
  155. level = 3;
  156. XmlSchemaComplexContentExtension extension = XmlSchemaComplexContentExtension.Read(reader,h);
  157. if(extension != null)
  158. complex.content = extension;
  159. continue;
  160. }
  161. }
  162. reader.RaiseInvalidElementError();
  163. }
  164. return complex;
  165. }
  166. }
  167. }