XmlSchemaSimpleContentExtension.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // System.Xml.Schema.XmlSchemaSimpleContentExtension.cs
  3. //
  4. // Author:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. using System;
  9. using System.Xml;
  10. using System.Xml.Serialization;
  11. namespace System.Xml.Schema
  12. {
  13. /// <summary>
  14. /// Summary description for XmlSchemaSimpleContentExtension.
  15. /// </summary>
  16. public class XmlSchemaSimpleContentExtension : XmlSchemaContent
  17. {
  18. private XmlSchemaAnyAttribute any;
  19. private XmlSchemaObjectCollection attributes;
  20. private XmlQualifiedName baseTypeName;
  21. private static string xmlname = "extension";
  22. public XmlSchemaSimpleContentExtension()
  23. {
  24. baseTypeName = XmlQualifiedName.Empty;
  25. attributes = new XmlSchemaObjectCollection();
  26. }
  27. [System.Xml.Serialization.XmlAttribute("base")]
  28. public XmlQualifiedName BaseTypeName
  29. {
  30. get{ return baseTypeName; }
  31. set{ baseTypeName = value; }
  32. }
  33. [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace=XmlSchema.Namespace)]
  34. [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace=XmlSchema.Namespace)]
  35. public XmlSchemaObjectCollection Attributes
  36. {
  37. get{ return attributes; }
  38. }
  39. [XmlElement("anyAttribute",Namespace=XmlSchema.Namespace)]
  40. public XmlSchemaAnyAttribute AnyAttribute
  41. {
  42. get{ return any; }
  43. set{ any = value; }
  44. }
  45. ///<remarks>
  46. /// 1. Base must be present and a QName
  47. ///</remarks>
  48. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  49. {
  50. // If this is already compiled this time, simply skip.
  51. if (this.IsComplied (schema.CompilationId))
  52. return 0;
  53. if (this.isRedefinedComponent) {
  54. if (Annotation != null)
  55. Annotation.isRedefinedComponent = true;
  56. if (AnyAttribute != null)
  57. AnyAttribute.isRedefinedComponent = true;
  58. foreach (XmlSchemaObject obj in Attributes)
  59. obj.isRedefinedComponent = true;
  60. }
  61. if(BaseTypeName == null || BaseTypeName.IsEmpty)
  62. {
  63. error(h, "base must be present, as a QName");
  64. }
  65. else if(!XmlSchemaUtil.CheckQName(BaseTypeName))
  66. error(h,"BaseTypeName must be a QName");
  67. if(this.AnyAttribute != null)
  68. {
  69. errorCount += AnyAttribute.Compile(h,schema);
  70. }
  71. foreach(XmlSchemaObject obj in Attributes)
  72. {
  73. if(obj is XmlSchemaAttribute)
  74. {
  75. XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
  76. errorCount += attr.Compile(h,schema);
  77. }
  78. else if(obj is XmlSchemaAttributeGroupRef)
  79. {
  80. XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;
  81. errorCount += atgrp.Compile(h,schema);
  82. }
  83. else
  84. error(h,obj.GetType() +" is not valid in this place::SimpleConentExtension");
  85. }
  86. XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
  87. this.CompilationId = schema.CompilationId;
  88. return errorCount;
  89. }
  90. internal override XmlQualifiedName GetBaseTypeName ()
  91. {
  92. return baseTypeName;
  93. }
  94. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  95. {
  96. if (IsValidated (schema.ValidationId))
  97. return errorCount;
  98. XmlSchemaType st = schema.SchemaTypes [baseTypeName] as XmlSchemaType;
  99. if (st != null) {
  100. XmlSchemaComplexType ct = st as XmlSchemaComplexType;
  101. if (ct != null && ct.ContentModel is XmlSchemaComplexContent)
  102. error (h, "Specified type is complex type which contains complex content.");
  103. st.Validate (h, schema);
  104. actualBaseSchemaType = st;
  105. } else if (baseTypeName == XmlSchemaComplexType.AnyTypeName) {
  106. actualBaseSchemaType = XmlSchemaComplexType.AnyType;
  107. } else if (baseTypeName.Namespace == XmlSchema.Namespace) {
  108. actualBaseSchemaType = XmlSchemaDatatype.FromName (baseTypeName);
  109. if (actualBaseSchemaType == null)
  110. error (h, "Invalid schema datatype name is specified.");
  111. }
  112. // otherwise, it might be missing sub components.
  113. else if (!schema.IsNamespaceAbsent (baseTypeName.Namespace))
  114. error (h, "Referenced base schema type " + baseTypeName + " was not found in the corresponding schema.");
  115. ValidationId = schema.ValidationId;
  116. return errorCount;
  117. }
  118. //<extension
  119. //base = QName
  120. //id = ID
  121. //{any attributes with non-schema namespace . . .}>
  122. //Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))
  123. //</extension>
  124. internal static XmlSchemaSimpleContentExtension Read(XmlSchemaReader reader, ValidationEventHandler h)
  125. {
  126. XmlSchemaSimpleContentExtension extension = new XmlSchemaSimpleContentExtension();
  127. reader.MoveToElement();
  128. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  129. {
  130. error(h,"Should not happen :1: XmlSchemaAttributeGroup.Read, name="+reader.Name,null);
  131. reader.Skip();
  132. return null;
  133. }
  134. extension.LineNumber = reader.LineNumber;
  135. extension.LinePosition = reader.LinePosition;
  136. extension.SourceUri = reader.BaseURI;
  137. while(reader.MoveToNextAttribute())
  138. {
  139. if(reader.Name == "base")
  140. {
  141. Exception innerex;
  142. extension.baseTypeName= XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  143. if(innerex != null)
  144. error(h, reader.Value + " is not a valid value for base attribute",innerex);
  145. }
  146. else if(reader.Name == "id")
  147. {
  148. extension.Id = reader.Value;
  149. }
  150. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  151. {
  152. error(h,reader.Name + " is not a valid attribute for extension in this context",null);
  153. }
  154. else
  155. {
  156. XmlSchemaUtil.ReadUnhandledAttribute(reader,extension);
  157. }
  158. }
  159. reader.MoveToElement();
  160. if(reader.IsEmptyElement)
  161. return extension;
  162. //Content: 1.annotation?, 2.(attribute | attributeGroup)*, 3.anyAttribute?
  163. int level = 1;
  164. while(reader.ReadNextElement())
  165. {
  166. if(reader.NodeType == XmlNodeType.EndElement)
  167. {
  168. if(reader.LocalName != xmlname)
  169. error(h,"Should not happen :2: XmlSchemaSimpleContentExtension.Read, name="+reader.Name,null);
  170. break;
  171. }
  172. if(level <= 1 && reader.LocalName == "annotation")
  173. {
  174. level = 2; //Only one annotation
  175. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  176. if(annotation != null)
  177. extension.Annotation = annotation;
  178. continue;
  179. }
  180. if(level <= 2)
  181. {
  182. if(reader.LocalName == "attribute")
  183. {
  184. level = 2;
  185. XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
  186. if(attr != null)
  187. extension.Attributes.Add(attr);
  188. continue;
  189. }
  190. if(reader.LocalName == "attributeGroup")
  191. {
  192. level = 2;
  193. XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
  194. if(attr != null)
  195. extension.attributes.Add(attr);
  196. continue;
  197. }
  198. }
  199. if(level <= 3 && reader.LocalName == "anyAttribute")
  200. {
  201. level = 4;
  202. XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
  203. if(anyattr != null)
  204. extension.AnyAttribute = anyattr;
  205. continue;
  206. }
  207. reader.RaiseInvalidElementError();
  208. }
  209. return extension;
  210. }
  211. }
  212. }