XmlSchemaSimpleContent.cs 4.3 KB

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