XmlSchemaAny.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Specialized;
  6. using System.Xml;
  7. using System.Xml.Serialization;
  8. using System.ComponentModel;
  9. using Mono.Xml.Schema;
  10. namespace System.Xml.Schema
  11. {
  12. /// <summary>
  13. /// Summary description for XmlSchemaAny.
  14. /// </summary>
  15. public class XmlSchemaAny : XmlSchemaParticle
  16. {
  17. static XmlSchemaAny anyTypeContent;
  18. internal static XmlSchemaAny AnyTypeContent {
  19. get {
  20. if (anyTypeContent == null) {
  21. anyTypeContent = new XmlSchemaAny ();
  22. anyTypeContent.MaxOccursString = "unbounded";
  23. anyTypeContent.MinOccurs = 0;
  24. anyTypeContent.CompileOccurence (null, null);
  25. anyTypeContent.Namespace = "##any";
  26. anyTypeContent.wildcard.HasValueAny = true;
  27. anyTypeContent.wildcard.ResolvedNamespaces = new StringCollection ();
  28. // Although it is not documented by W3C, but it should be.
  29. anyTypeContent.wildcard.ResolvedProcessing =
  30. anyTypeContent.ProcessContents = XmlSchemaContentProcessing.Lax;
  31. }
  32. return anyTypeContent;
  33. }
  34. }
  35. private string nameSpace;
  36. private XmlSchemaContentProcessing processing;
  37. private static string xmlname = "any";
  38. private XsdWildcard wildcard;
  39. public XmlSchemaAny()
  40. {
  41. wildcard = new XsdWildcard (this);
  42. }
  43. [System.Xml.Serialization.XmlAttribute("namespace")]
  44. public string Namespace
  45. {
  46. get{ return nameSpace; }
  47. set{ nameSpace = value; }
  48. }
  49. [DefaultValue(XmlSchemaContentProcessing.None)]
  50. [System.Xml.Serialization.XmlAttribute("processContents")]
  51. public XmlSchemaContentProcessing ProcessContents
  52. {
  53. get{ return processing; }
  54. set{ processing = value; }
  55. }
  56. // Internal
  57. internal bool HasValueAny {
  58. get { return wildcard.HasValueAny; }
  59. }
  60. internal bool HasValueLocal {
  61. get { return wildcard.HasValueLocal; }
  62. }
  63. internal bool HasValueOther {
  64. get { return wildcard.HasValueOther; }
  65. }
  66. internal bool HasValueTargetNamespace {
  67. get { return wildcard.HasValueTargetNamespace; }
  68. }
  69. internal StringCollection ResolvedNamespaces {
  70. get { return wildcard.ResolvedNamespaces; }
  71. }
  72. internal XmlSchemaContentProcessing ResolvedProcessContents
  73. {
  74. get{ return wildcard.ResolvedProcessing; }
  75. }
  76. internal string TargetNamespace
  77. {
  78. get { return wildcard.TargetNamespace; }
  79. }
  80. /// <remarks>
  81. /// 1. id must be of type ID
  82. /// 2. namespace can have one of the following values:
  83. /// a) ##any or ##other
  84. /// b) list of anyURI and ##targetNamespace and ##local
  85. /// </remarks>
  86. [MonoTODO]
  87. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  88. {
  89. // If this is already compiled this time, simply skip.
  90. if (this.IsComplied (schema.CompilationId))
  91. return 0;
  92. errorCount = 0;
  93. XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
  94. wildcard.TargetNamespace = schema.TargetNamespace;
  95. if (wildcard.TargetNamespace == null)
  96. wildcard.TargetNamespace = "";
  97. CompileOccurence (h, schema);
  98. wildcard.Compile (Namespace, h, schema);
  99. if (processing == XmlSchemaContentProcessing.None)
  100. wildcard.ResolvedProcessing = XmlSchemaContentProcessing.Strict;
  101. else
  102. wildcard.ResolvedProcessing = processing;
  103. this.CompilationId = schema.CompilationId;
  104. return errorCount;
  105. }
  106. [MonoTODO]
  107. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  108. {
  109. return errorCount;
  110. }
  111. // 3.8.6. Attribute Wildcard Intersection
  112. // Only try to examine if their intersection is expressible, and
  113. // returns if the result is empty.
  114. internal bool ExamineAttributeWildcardIntersection (XmlSchemaAny other,
  115. ValidationEventHandler h, XmlSchema schema)
  116. {
  117. return wildcard.ExamineAttributeWildcardIntersection (other, h, schema);
  118. }
  119. internal override void ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  120. ValidationEventHandler h, XmlSchema schema)
  121. {
  122. // TODO
  123. }
  124. internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  125. {
  126. // do nothing
  127. }
  128. internal override void ValidateUniqueParticleAttribution (
  129. XmlSchemaObjectTable qnames, ArrayList nsNames,
  130. ValidationEventHandler h, XmlSchema schema)
  131. {
  132. // Wildcard Intersection check.
  133. foreach (XmlSchemaAny other in nsNames)
  134. if (!ExamineAttributeWildcardIntersection (other, h, schema))
  135. error (h, "Ambiguous -any- particle was found.");
  136. nsNames.Add (this);
  137. }
  138. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  139. ValidationEventHandler h, XmlSchema schema)
  140. {
  141. // do nothing
  142. }
  143. // 3.10.4 Wildcard Allows Namespace Name. (In fact it is almost copy...)
  144. internal bool ValidateWildcardAllowsNamespaceName (string ns,
  145. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  146. {
  147. return wildcard.ValidateWildcardAllowsNamespaceName (ns, h, schema, raiseError);
  148. }
  149. //<any
  150. // id = ID
  151. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  152. // minOccurs = nonNegativeInteger : 1
  153. // namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) ) : ##any
  154. // processContents = (lax | skip | strict) : strict
  155. // {any attributes with non-schema namespace . . .}>
  156. // Content: (annotation?)
  157. //</any>
  158. internal static XmlSchemaAny Read(XmlSchemaReader reader, ValidationEventHandler h)
  159. {
  160. XmlSchemaAny any = new XmlSchemaAny();
  161. reader.MoveToElement();
  162. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  163. {
  164. error(h,"Should not happen :1: XmlSchemaAny.Read, name="+reader.Name,null);
  165. reader.SkipToEnd();
  166. return null;
  167. }
  168. any.LineNumber = reader.LineNumber;
  169. any.LinePosition = reader.LinePosition;
  170. any.SourceUri = reader.BaseURI;
  171. while(reader.MoveToNextAttribute())
  172. {
  173. if(reader.Name == "id")
  174. {
  175. any.Id = reader.Value;
  176. }
  177. else if(reader.Name == "maxOccurs")
  178. {
  179. try
  180. {
  181. any.MaxOccursString = reader.Value;
  182. }
  183. catch(Exception e)
  184. {
  185. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  186. }
  187. }
  188. else if(reader.Name == "minOccurs")
  189. {
  190. try
  191. {
  192. any.MinOccursString = reader.Value;
  193. }
  194. catch(Exception e)
  195. {
  196. error(h,reader.Value + " is an invalid value for minOccurs", e);
  197. }
  198. }
  199. else if(reader.Name == "namespace")
  200. {
  201. any.nameSpace = reader.Value;
  202. }
  203. else if(reader.Name == "processContents")
  204. {
  205. Exception innerex;
  206. any.processing = XmlSchemaUtil.ReadProcessingAttribute(reader,out innerex);
  207. if(innerex != null)
  208. error(h, reader.Value + " is not a valid value for processContents",innerex);
  209. }
  210. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  211. {
  212. error(h,reader.Name + " is not a valid attribute for any",null);
  213. }
  214. else
  215. {
  216. XmlSchemaUtil.ReadUnhandledAttribute(reader,any);
  217. }
  218. }
  219. reader.MoveToElement();
  220. if(reader.IsEmptyElement)
  221. return any;
  222. // Content: (annotation?)
  223. int level = 1;
  224. while(reader.ReadNextElement())
  225. {
  226. if(reader.NodeType == XmlNodeType.EndElement)
  227. {
  228. if(reader.LocalName != xmlname)
  229. error(h,"Should not happen :2: XmlSchemaAny.Read, name="+reader.Name,null);
  230. break;
  231. }
  232. if(level <= 1 && reader.LocalName == "annotation")
  233. {
  234. level = 2; //Only one annotation
  235. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  236. if(annotation != null)
  237. any.Annotation = annotation;
  238. continue;
  239. }
  240. reader.RaiseInvalidElementError();
  241. }
  242. return any;
  243. }
  244. }
  245. }