XmlSchemaSimpleContentExtension.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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="http://www.w3.org/2001/XMLSchema")]
  29. [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace="http://www.w3.org/2001/XMLSchema")]
  30. public XmlSchemaObjectCollection Attributes
  31. {
  32. get{ return attributes; }
  33. }
  34. [XmlElement("anyAttribute",Namespace="http://www.w3.org/2001/XMLSchema")]
  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, XmlSchemaInfo info)
  45. {
  46. if(BaseTypeName == null || BaseTypeName.IsEmpty)
  47. {
  48. error(h, "base must be present and a QName");
  49. }
  50. else if(!XmlSchemaUtil.CheckQName(BaseTypeName))
  51. error(h,"BaseTypeName must be a QName");
  52. if(this.AnyAttribute != null)
  53. {
  54. errorCount += AnyAttribute.Compile(h,info);
  55. }
  56. foreach(XmlSchemaObject obj in Attributes)
  57. {
  58. if(obj is XmlSchemaAttribute)
  59. {
  60. XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
  61. errorCount += attr.Compile(h,info);
  62. }
  63. else if(obj is XmlSchemaAttributeGroupRef)
  64. {
  65. XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;
  66. errorCount += atgrp.Compile(h,info);
  67. }
  68. else
  69. error(h,obj.GetType() +" is not valid in this place::SimpleConentExtension");
  70. }
  71. XmlSchemaUtil.CompileID(Id,this,info.IDCollection,h);
  72. return errorCount;
  73. }
  74. [MonoTODO]
  75. internal int Validate(ValidationEventHandler h)
  76. {
  77. return errorCount;
  78. }
  79. //<extension
  80. //base = QName
  81. //id = ID
  82. //{any attributes with non-schema namespace . . .}>
  83. //Content: (annotation?, ((attribute | attributeGroup)*, anyAttribute?))
  84. //</extension>
  85. internal static XmlSchemaSimpleContentExtension Read(XmlSchemaReader reader, ValidationEventHandler h)
  86. {
  87. XmlSchemaSimpleContentExtension extension = new XmlSchemaSimpleContentExtension();
  88. reader.MoveToElement();
  89. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  90. {
  91. error(h,"Should not happen :1: XmlSchemaAttributeGroup.Read, name="+reader.Name,null);
  92. reader.Skip();
  93. return null;
  94. }
  95. extension.LineNumber = reader.LineNumber;
  96. extension.LinePosition = reader.LinePosition;
  97. extension.SourceUri = reader.BaseURI;
  98. while(reader.MoveToNextAttribute())
  99. {
  100. if(reader.Name == "base")
  101. {
  102. Exception innerex;
  103. extension.baseTypeName= XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  104. if(innerex != null)
  105. error(h, reader.Value + " is not a valid value for base attribute",innerex);
  106. }
  107. else if(reader.Name == "id")
  108. {
  109. extension.Id = reader.Value;
  110. }
  111. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  112. {
  113. error(h,reader.Name + " is not a valid attribute for extension in this context",null);
  114. }
  115. else
  116. {
  117. XmlSchemaUtil.ReadUnhandledAttribute(reader,extension);
  118. }
  119. }
  120. reader.MoveToElement();
  121. if(reader.IsEmptyElement)
  122. return extension;
  123. //Content: 1.annotation?, 2.(attribute | attributeGroup)*, 3.anyAttribute?
  124. int level = 1;
  125. while(reader.ReadNextElement())
  126. {
  127. if(reader.NodeType == XmlNodeType.EndElement)
  128. {
  129. if(reader.LocalName != xmlname)
  130. error(h,"Should not happen :2: XmlSchemaSimpleContentExtension.Read, name="+reader.Name,null);
  131. break;
  132. }
  133. if(level <= 1 && reader.LocalName == "annotation")
  134. {
  135. level = 2; //Only one annotation
  136. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  137. if(annotation != null)
  138. extension.Annotation = annotation;
  139. continue;
  140. }
  141. if(level <= 2)
  142. {
  143. if(reader.LocalName == "attribute")
  144. {
  145. level = 2;
  146. XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
  147. if(attr != null)
  148. extension.Attributes.Add(attr);
  149. continue;
  150. }
  151. if(reader.LocalName == "attributeGroup")
  152. {
  153. level = 2;
  154. XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
  155. if(attr != null)
  156. extension.attributes.Add(attr);
  157. continue;
  158. }
  159. }
  160. if(level <= 3 && reader.LocalName == "anyAttribute")
  161. {
  162. level = 4;
  163. XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
  164. if(anyattr != null)
  165. extension.AnyAttribute = anyattr;
  166. continue;
  167. }
  168. reader.RaiseInvalidElementError();
  169. }
  170. return extension;
  171. }
  172. }
  173. }