XmlSchemaSimpleContentExtension.cs 8.3 KB

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