XmlSchemaSimpleContent.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // System.Xml.Schema.XmlSchemaSimpleContent.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 XmlSchemaSimpleContent.
  15. /// </summary>
  16. public class XmlSchemaSimpleContent : XmlSchemaContentModel
  17. {
  18. private XmlSchemaContent content;
  19. const string xmlname = "simpleContent";
  20. public XmlSchemaSimpleContent()
  21. {
  22. }
  23. [XmlElement("restriction",typeof(XmlSchemaSimpleContentRestriction),Namespace=XmlSchema.Namespace)]
  24. [XmlElement("extension",typeof(XmlSchemaSimpleContentExtension),Namespace=XmlSchema.Namespace)]
  25. public override XmlSchemaContent Content
  26. {
  27. get{ return content; }
  28. set{ content = value; }
  29. }
  30. ///<remarks>
  31. /// 1. Content must be present and one of restriction or extention
  32. ///</remarks>
  33. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  34. {
  35. // If this is already compiled this time, simply skip.
  36. if (this.IsComplied (schema.CompilationId))
  37. return 0;
  38. if(Content == null)
  39. {
  40. error(h, "Content must be present in a simpleContent");
  41. }
  42. else
  43. {
  44. if(Content is XmlSchemaSimpleContentRestriction)
  45. {
  46. XmlSchemaSimpleContentRestriction xscr = (XmlSchemaSimpleContentRestriction) Content;
  47. errorCount += xscr.Compile(h, schema);
  48. }
  49. else if(Content is XmlSchemaSimpleContentExtension)
  50. {
  51. XmlSchemaSimpleContentExtension xsce = (XmlSchemaSimpleContentExtension) Content;
  52. errorCount += xsce.Compile(h, schema);
  53. }
  54. else
  55. error(h,"simpleContent can't have any value other than restriction or extention");
  56. }
  57. XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
  58. this.CompilationId = schema.CompilationId;
  59. return errorCount;
  60. }
  61. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  62. {
  63. if (IsValidated (schema.ValidationId))
  64. return errorCount;
  65. errorCount += this.Content.Validate (h, schema);
  66. ValidationId = schema.ValidationId;
  67. return errorCount;
  68. }
  69. //<simpleContent
  70. // id = ID
  71. // {any attributes with non-schema namespace . . .}>
  72. // Content: (annotation?, (restriction | extension))
  73. //</simpleContent>
  74. internal static XmlSchemaSimpleContent Read(XmlSchemaReader reader, ValidationEventHandler h)
  75. {
  76. XmlSchemaSimpleContent simple = new XmlSchemaSimpleContent();
  77. reader.MoveToElement();
  78. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  79. {
  80. error(h,"Should not happen :1: XmlSchemaComplexContent.Read, name="+reader.Name,null);
  81. reader.SkipToEnd();
  82. return null;
  83. }
  84. simple.LineNumber = reader.LineNumber;
  85. simple.LinePosition = reader.LinePosition;
  86. simple.SourceUri = reader.BaseURI;
  87. while(reader.MoveToNextAttribute())
  88. {
  89. if(reader.Name == "id")
  90. {
  91. simple.Id = reader.Value;
  92. }
  93. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  94. {
  95. error(h,reader.Name + " is not a valid attribute for simpleContent",null);
  96. }
  97. else
  98. {
  99. XmlSchemaUtil.ReadUnhandledAttribute(reader,simple);
  100. }
  101. }
  102. reader.MoveToElement();
  103. if(reader.IsEmptyElement)
  104. return simple;
  105. //Content: (annotation?, (restriction | extension))
  106. int level = 1;
  107. while(reader.ReadNextElement())
  108. {
  109. if(reader.NodeType == XmlNodeType.EndElement)
  110. {
  111. if(reader.LocalName != xmlname)
  112. error(h,"Should not happen :2: XmlSchemaSimpleContent.Read, name="+reader.Name,null);
  113. break;
  114. }
  115. if(level <= 1 && reader.LocalName == "annotation")
  116. {
  117. level = 2; //Only one annotation
  118. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  119. if(annotation != null)
  120. simple.Annotation = annotation;
  121. continue;
  122. }
  123. if(level <=2)
  124. {
  125. if(reader.LocalName == "restriction")
  126. {
  127. level = 3;
  128. XmlSchemaSimpleContentRestriction restriction = XmlSchemaSimpleContentRestriction.Read(reader,h);
  129. if(restriction != null)
  130. simple.content = restriction;
  131. continue;
  132. }
  133. if(reader.LocalName == "extension")
  134. {
  135. level = 3;
  136. XmlSchemaSimpleContentExtension extension = XmlSchemaSimpleContentExtension.Read(reader,h);
  137. if(extension != null)
  138. simple.content = extension;
  139. continue;
  140. }
  141. }
  142. reader.RaiseInvalidElementError();
  143. }
  144. return simple;
  145. }
  146. }
  147. }