XmlSchemaSimpleContentExtension.cs 7.2 KB

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