XmlSchemaSimpleContent.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // System.Xml.Schema.XmlSchemaSimpleContent.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 XmlSchemaSimpleContent.
  35. /// </summary>
  36. public class XmlSchemaSimpleContent : XmlSchemaContentModel
  37. {
  38. private XmlSchemaContent content;
  39. const string xmlname = "simpleContent";
  40. public XmlSchemaSimpleContent()
  41. {
  42. }
  43. [XmlElement("restriction",typeof(XmlSchemaSimpleContentRestriction),Namespace=XmlSchema.Namespace)]
  44. [XmlElement("extension",typeof(XmlSchemaSimpleContentExtension),Namespace=XmlSchema.Namespace)]
  45. public override XmlSchemaContent Content
  46. {
  47. get{ return content; }
  48. set{ content = value; }
  49. }
  50. ///<remarks>
  51. /// 1. Content must be present and one of restriction or extention
  52. ///</remarks>
  53. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  54. {
  55. // If this is already compiled this time, simply skip.
  56. if (this.IsComplied (schema.CompilationId))
  57. return 0;
  58. if(Content == null)
  59. {
  60. error(h, "Content must be present in a simpleContent");
  61. }
  62. else
  63. {
  64. if(Content is XmlSchemaSimpleContentRestriction)
  65. {
  66. XmlSchemaSimpleContentRestriction xscr = (XmlSchemaSimpleContentRestriction) Content;
  67. errorCount += xscr.Compile(h, schema);
  68. }
  69. else if(Content is XmlSchemaSimpleContentExtension)
  70. {
  71. XmlSchemaSimpleContentExtension xsce = (XmlSchemaSimpleContentExtension) Content;
  72. errorCount += xsce.Compile(h, schema);
  73. }
  74. else
  75. error(h,"simpleContent can't have any value other than restriction or extention");
  76. }
  77. XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
  78. this.CompilationId = schema.CompilationId;
  79. return errorCount;
  80. }
  81. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  82. {
  83. if (IsValidated (schema.ValidationId))
  84. return errorCount;
  85. errorCount += this.Content.Validate (h, schema);
  86. ValidationId = schema.ValidationId;
  87. return errorCount;
  88. }
  89. //<simpleContent
  90. // id = ID
  91. // {any attributes with non-schema namespace . . .}>
  92. // Content: (annotation?, (restriction | extension))
  93. //</simpleContent>
  94. internal static XmlSchemaSimpleContent Read(XmlSchemaReader reader, ValidationEventHandler h)
  95. {
  96. XmlSchemaSimpleContent simple = new XmlSchemaSimpleContent();
  97. reader.MoveToElement();
  98. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  99. {
  100. error(h,"Should not happen :1: XmlSchemaComplexContent.Read, name="+reader.Name,null);
  101. reader.SkipToEnd();
  102. return null;
  103. }
  104. simple.LineNumber = reader.LineNumber;
  105. simple.LinePosition = reader.LinePosition;
  106. simple.SourceUri = reader.BaseURI;
  107. while(reader.MoveToNextAttribute())
  108. {
  109. if(reader.Name == "id")
  110. {
  111. simple.Id = reader.Value;
  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 simpleContent",null);
  116. }
  117. else
  118. {
  119. XmlSchemaUtil.ReadUnhandledAttribute(reader,simple);
  120. }
  121. }
  122. reader.MoveToElement();
  123. if(reader.IsEmptyElement)
  124. return simple;
  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: XmlSchemaSimpleContent.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. simple.Annotation = annotation;
  141. continue;
  142. }
  143. if(level <=2)
  144. {
  145. if(reader.LocalName == "restriction")
  146. {
  147. level = 3;
  148. XmlSchemaSimpleContentRestriction restriction = XmlSchemaSimpleContentRestriction.Read(reader,h);
  149. if(restriction != null)
  150. simple.content = restriction;
  151. continue;
  152. }
  153. if(reader.LocalName == "extension")
  154. {
  155. level = 3;
  156. XmlSchemaSimpleContentExtension extension = XmlSchemaSimpleContentExtension.Read(reader,h);
  157. if(extension != null)
  158. simple.content = extension;
  159. continue;
  160. }
  161. }
  162. reader.RaiseInvalidElementError();
  163. }
  164. return simple;
  165. }
  166. }
  167. }