XmlSchemaSimpleContentExtension.cs 7.0 KB

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