2
0

XmlSchemaSimpleContentExtension.cs 5.5 KB

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