XmlSchemaSimpleContent.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 NET_2_0
  65. Content.Parent = this;
  66. #endif
  67. if(Content is XmlSchemaSimpleContentRestriction)
  68. {
  69. XmlSchemaSimpleContentRestriction xscr = (XmlSchemaSimpleContentRestriction) Content;
  70. errorCount += xscr.Compile(h, schema);
  71. }
  72. else if(Content is XmlSchemaSimpleContentExtension)
  73. {
  74. XmlSchemaSimpleContentExtension xsce = (XmlSchemaSimpleContentExtension) Content;
  75. errorCount += xsce.Compile(h, schema);
  76. }
  77. else
  78. error(h,"simpleContent can't have any value other than restriction or extention");
  79. }
  80. XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
  81. this.CompilationId = schema.CompilationId;
  82. return errorCount;
  83. }
  84. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  85. {
  86. if (IsValidated (schema.ValidationId))
  87. return errorCount;
  88. errorCount += this.Content.Validate (h, schema);
  89. ValidationId = schema.ValidationId;
  90. return errorCount;
  91. }
  92. //<simpleContent
  93. // id = ID
  94. // {any attributes with non-schema namespace . . .}>
  95. // Content: (annotation?, (restriction | extension))
  96. //</simpleContent>
  97. internal static XmlSchemaSimpleContent Read(XmlSchemaReader reader, ValidationEventHandler h)
  98. {
  99. XmlSchemaSimpleContent simple = new XmlSchemaSimpleContent();
  100. reader.MoveToElement();
  101. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  102. {
  103. error(h,"Should not happen :1: XmlSchemaComplexContent.Read, name="+reader.Name,null);
  104. reader.SkipToEnd();
  105. return null;
  106. }
  107. simple.LineNumber = reader.LineNumber;
  108. simple.LinePosition = reader.LinePosition;
  109. simple.SourceUri = reader.BaseURI;
  110. while(reader.MoveToNextAttribute())
  111. {
  112. if(reader.Name == "id")
  113. {
  114. simple.Id = reader.Value;
  115. }
  116. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  117. {
  118. error(h,reader.Name + " is not a valid attribute for simpleContent",null);
  119. }
  120. else
  121. {
  122. XmlSchemaUtil.ReadUnhandledAttribute(reader,simple);
  123. }
  124. }
  125. reader.MoveToElement();
  126. if(reader.IsEmptyElement)
  127. return simple;
  128. //Content: (annotation?, (restriction | extension))
  129. int level = 1;
  130. while(reader.ReadNextElement())
  131. {
  132. if(reader.NodeType == XmlNodeType.EndElement)
  133. {
  134. if(reader.LocalName != xmlname)
  135. error(h,"Should not happen :2: XmlSchemaSimpleContent.Read, name="+reader.Name,null);
  136. break;
  137. }
  138. if(level <= 1 && reader.LocalName == "annotation")
  139. {
  140. level = 2; //Only one annotation
  141. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  142. if(annotation != null)
  143. simple.Annotation = annotation;
  144. continue;
  145. }
  146. if(level <=2)
  147. {
  148. if(reader.LocalName == "restriction")
  149. {
  150. level = 3;
  151. XmlSchemaSimpleContentRestriction restriction = XmlSchemaSimpleContentRestriction.Read(reader,h);
  152. if(restriction != null)
  153. simple.content = restriction;
  154. continue;
  155. }
  156. if(reader.LocalName == "extension")
  157. {
  158. level = 3;
  159. XmlSchemaSimpleContentExtension extension = XmlSchemaSimpleContentExtension.Read(reader,h);
  160. if(extension != null)
  161. simple.content = extension;
  162. continue;
  163. }
  164. }
  165. reader.RaiseInvalidElementError();
  166. }
  167. return simple;
  168. }
  169. }
  170. }