XmlSchemaComplexContent.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. private static 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. [MonoTODO]
  40. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  41. {
  42. // If this is already compiled this time, simply skip.
  43. if (this.IsComplied (schema.CompilationId))
  44. return 0;
  45. if (isRedefinedComponent) {
  46. if (Annotation != null)
  47. Annotation.isRedefinedComponent = true;
  48. if (Content != null)
  49. Content.isRedefinedComponent = true;
  50. }
  51. if(Content == null)
  52. {
  53. error(h, "Content must be present in a complexContent");
  54. }
  55. else
  56. {
  57. if(Content is XmlSchemaComplexContentRestriction)
  58. {
  59. XmlSchemaComplexContentRestriction xscr = (XmlSchemaComplexContentRestriction) Content;
  60. errorCount += xscr.Compile(h, schema);
  61. }
  62. else if(Content is XmlSchemaComplexContentExtension)
  63. {
  64. XmlSchemaComplexContentExtension xsce = (XmlSchemaComplexContentExtension) Content;
  65. errorCount += xsce.Compile(h, schema);
  66. }
  67. else
  68. error(h,"complexContent can't have any value other than restriction or extention");
  69. }
  70. XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
  71. this.CompilationId = schema.CompilationId;
  72. return errorCount;
  73. }
  74. [MonoTODO]
  75. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  76. {
  77. if (IsValidated (schema.ValidationId))
  78. return errorCount;
  79. errorCount += Content.Validate (h, schema);
  80. ValidationId = schema.ValidationId;
  81. return errorCount;
  82. }
  83. //<complexContent
  84. // id = ID
  85. // mixed = boolean
  86. // {any attributes with non-schema namespace . . .}>
  87. // Content: (annotation?, (restriction | extension))
  88. //</complexContent>
  89. internal static XmlSchemaComplexContent Read(XmlSchemaReader reader, ValidationEventHandler h)
  90. {
  91. XmlSchemaComplexContent complex = new XmlSchemaComplexContent();
  92. reader.MoveToElement();
  93. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  94. {
  95. error(h,"Should not happen :1: XmlSchemaComplexContent.Read, name="+reader.Name,null);
  96. reader.Skip();
  97. return null;
  98. }
  99. complex.LineNumber = reader.LineNumber;
  100. complex.LinePosition = reader.LinePosition;
  101. complex.SourceUri = reader.BaseURI;
  102. while(reader.MoveToNextAttribute())
  103. {
  104. if(reader.Name == "id")
  105. {
  106. complex.Id = reader.Value;
  107. }
  108. else if(reader.Name == "mixed")
  109. {
  110. Exception innerex;
  111. complex.isMixed = XmlSchemaUtil.ReadBoolAttribute(reader,out innerex);
  112. if(innerex != null)
  113. error(h,reader.Value + " is an invalid value for mixed",innerex);
  114. }
  115. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  116. {
  117. error(h,reader.Name + " is not a valid attribute for complexContent",null);
  118. }
  119. else
  120. {
  121. XmlSchemaUtil.ReadUnhandledAttribute(reader,complex);
  122. }
  123. }
  124. reader.MoveToElement();
  125. if(reader.IsEmptyElement)
  126. return complex;
  127. //Content: (annotation?, (restriction | extension))
  128. int level = 1;
  129. while(reader.ReadNextElement())
  130. {
  131. if(reader.NodeType == XmlNodeType.EndElement)
  132. {
  133. if(reader.LocalName != xmlname)
  134. error(h,"Should not happen :2: XmlSchemaComplexContent.Read, name="+reader.Name,null);
  135. break;
  136. }
  137. if(level <= 1 && reader.LocalName == "annotation")
  138. {
  139. level = 2; //Only one annotation
  140. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  141. if(annotation != null)
  142. complex.Annotation = annotation;
  143. continue;
  144. }
  145. if(level <=2)
  146. {
  147. if(reader.LocalName == "restriction")
  148. {
  149. level = 3;
  150. XmlSchemaComplexContentRestriction restriction = XmlSchemaComplexContentRestriction.Read(reader,h);
  151. if(restriction != null)
  152. complex.content = restriction;
  153. continue;
  154. }
  155. if(reader.LocalName == "extension")
  156. {
  157. level = 3;
  158. XmlSchemaComplexContentExtension extension = XmlSchemaComplexContentExtension.Read(reader,h);
  159. if(extension != null)
  160. complex.content = extension;
  161. continue;
  162. }
  163. }
  164. reader.RaiseInvalidElementError();
  165. }
  166. return complex;
  167. }
  168. }
  169. }