XmlSchemaAny.cs 9.0 KB

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