XmlSchemaAny.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. using System;
  4. using System.Xml;
  5. using System.Xml.Serialization;
  6. using System.ComponentModel;
  7. namespace System.Xml.Schema
  8. {
  9. /// <summary>
  10. /// Summary description for XmlSchemaAny.
  11. /// </summary>
  12. public class XmlSchemaAny : XmlSchemaParticle
  13. {
  14. private string nameSpace;
  15. private XmlSchemaContentProcessing processing;
  16. private static string xmlname = "any";
  17. public XmlSchemaAny()
  18. {
  19. }
  20. [System.Xml.Serialization.XmlAttribute("namespace")]
  21. public string Namespace
  22. {
  23. get{ return nameSpace; }
  24. set{ nameSpace = value; }
  25. }
  26. [DefaultValue(XmlSchemaContentProcessing.None)]
  27. [System.Xml.Serialization.XmlAttribute("processContents")]
  28. public XmlSchemaContentProcessing ProcessContents
  29. {
  30. get{ return processing; }
  31. set{ processing = value; }
  32. }
  33. /// <remarks>
  34. /// 1. id must be of type ID
  35. /// 2. namespace can have one of the following values:
  36. /// a) ##any or ##other
  37. /// b) list of anyURI and ##targetNamespace and ##local
  38. /// </remarks>
  39. [MonoTODO]
  40. internal int Compile(ValidationEventHandler h, XmlSchema schema)
  41. {
  42. // If this is already compiled this time, simply skip.
  43. if (this.IsComplied (schema.CompilationId))
  44. return 0;
  45. errorCount = 0;
  46. XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
  47. //define ##any=1,##other=2,##targetNamespace=4,##local=8,anyURI=16
  48. int nscount = 0;
  49. string[] nslist = XmlSchemaUtil.SplitList(Namespace);
  50. foreach(string ns in nslist)
  51. {
  52. switch(ns)
  53. {
  54. case "##any":
  55. nscount |= 1;
  56. break;
  57. case "##other":
  58. nscount |= 2;
  59. break;
  60. case "##targetNamespace":
  61. nscount |= 4;
  62. break;
  63. case "##local":
  64. nscount |= 8;
  65. break;
  66. default:
  67. if(!XmlSchemaUtil.CheckAnyUri(ns))
  68. error(h,"the namespace is not a valid anyURI");
  69. else
  70. nscount |= 16;
  71. break;
  72. }
  73. }
  74. if((nscount&1) == 1 && nscount != 1)
  75. error(h,"##any if present must be the only namespace attribute");
  76. if((nscount&2) == 2 && nscount != 2)
  77. error(h,"##other if present must be the only namespace attribute");
  78. this.CompilationId = schema.CompilationId;
  79. return errorCount;
  80. }
  81. [MonoTODO]
  82. internal int Validate(ValidationEventHandler h)
  83. {
  84. return errorCount;
  85. }
  86. //<any
  87. // id = ID
  88. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  89. // minOccurs = nonNegativeInteger : 1
  90. // namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) ) : ##any
  91. // processContents = (lax | skip | strict) : strict
  92. // {any attributes with non-schema namespace . . .}>
  93. // Content: (annotation?)
  94. //</any>
  95. internal static XmlSchemaAny Read(XmlSchemaReader reader, ValidationEventHandler h)
  96. {
  97. XmlSchemaAny any = new XmlSchemaAny();
  98. reader.MoveToElement();
  99. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  100. {
  101. error(h,"Should not happen :1: XmlSchemaAny.Read, name="+reader.Name,null);
  102. reader.SkipToEnd();
  103. return null;
  104. }
  105. any.LineNumber = reader.LineNumber;
  106. any.LinePosition = reader.LinePosition;
  107. any.SourceUri = reader.BaseURI;
  108. while(reader.MoveToNextAttribute())
  109. {
  110. if(reader.Name == "id")
  111. {
  112. any.Id = reader.Value;
  113. }
  114. else if(reader.Name == "maxOccurs")
  115. {
  116. try
  117. {
  118. any.MaxOccursString = reader.Value;
  119. }
  120. catch(Exception e)
  121. {
  122. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  123. }
  124. }
  125. else if(reader.Name == "minOccurs")
  126. {
  127. try
  128. {
  129. any.MinOccursString = reader.Value;
  130. }
  131. catch(Exception e)
  132. {
  133. error(h,reader.Value + " is an invalid value for minOccurs", e);
  134. }
  135. }
  136. else if(reader.Name == "namespace")
  137. {
  138. any.nameSpace = reader.Value;
  139. }
  140. else if(reader.Name == "processContents")
  141. {
  142. Exception innerex;
  143. any.processing = XmlSchemaUtil.ReadProcessingAttribute(reader,out innerex);
  144. if(innerex != null)
  145. error(h, reader.Value + " is not a valid value for processContents",innerex);
  146. }
  147. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  148. {
  149. error(h,reader.Name + " is not a valid attribute for any",null);
  150. }
  151. else
  152. {
  153. XmlSchemaUtil.ReadUnhandledAttribute(reader,any);
  154. }
  155. }
  156. reader.MoveToElement();
  157. if(reader.IsEmptyElement)
  158. return any;
  159. // Content: (annotation?)
  160. int level = 1;
  161. while(reader.ReadNextElement())
  162. {
  163. if(reader.NodeType == XmlNodeType.EndElement)
  164. {
  165. if(reader.LocalName != xmlname)
  166. error(h,"Should not happen :2: XmlSchemaAny.Read, name="+reader.Name,null);
  167. break;
  168. }
  169. if(level <= 1 && reader.LocalName == "annotation")
  170. {
  171. level = 2; //Only one annotation
  172. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  173. if(annotation != null)
  174. any.Annotation = annotation;
  175. continue;
  176. }
  177. reader.RaiseInvalidElementError();
  178. }
  179. return any;
  180. }
  181. }
  182. }