XmlSchemaSimpleContentExtension.cs 8.4 KB

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