XmlSchemaAny.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. // Post Compilation Schema Infoset
  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. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  92. {
  93. // If this is already compiled this time, simply skip.
  94. if (this.IsComplied (schema.CompilationId))
  95. return 0;
  96. errorCount = 0;
  97. XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
  98. wildcard.TargetNamespace = schema.TargetNamespace;
  99. if (wildcard.TargetNamespace == null)
  100. wildcard.TargetNamespace = "";
  101. CompileOccurence (h, schema);
  102. wildcard.Compile (Namespace, h, schema);
  103. if (processing == XmlSchemaContentProcessing.None)
  104. wildcard.ResolvedProcessing = XmlSchemaContentProcessing.Strict;
  105. else
  106. wildcard.ResolvedProcessing = processing;
  107. this.CompilationId = schema.CompilationId;
  108. return errorCount;
  109. }
  110. internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
  111. {
  112. if (OptimizedParticle != null)
  113. return OptimizedParticle;
  114. // Uncommenting this causes incorrect validation.
  115. // It will prevent UPA e.g. msxsdtest/Particles/particlesJf006.xsd
  116. // if (ValidatedMaxOccurs == 0) {
  117. // OptimizedParticle = XmlSchemaParticle.Empty;
  118. // return OptimizedParticle;
  119. // }
  120. XmlSchemaAny any = new XmlSchemaAny ();
  121. CopyInfo (any);
  122. any.wildcard = this.wildcard;
  123. OptimizedParticle = any;
  124. return OptimizedParticle;
  125. }
  126. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  127. {
  128. return errorCount;
  129. }
  130. internal override bool ParticleEquals (XmlSchemaParticle other)
  131. {
  132. XmlSchemaAny any = other as XmlSchemaAny;
  133. if (any == null)
  134. return false;
  135. if (this.HasValueAny != any.HasValueAny ||
  136. this.HasValueLocal != any.HasValueLocal ||
  137. this.HasValueOther != any.HasValueOther ||
  138. this.HasValueTargetNamespace != any.HasValueTargetNamespace ||
  139. this.ResolvedProcessContents != any.ResolvedProcessContents ||
  140. this.ValidatedMaxOccurs != any.ValidatedMaxOccurs ||
  141. this.ValidatedMinOccurs != any.ValidatedMinOccurs ||
  142. this.ResolvedNamespaces.Count != any.ResolvedNamespaces.Count)
  143. return false;
  144. for (int i = 0; i < ResolvedNamespaces.Count; i++)
  145. if (ResolvedNamespaces [i] != any.ResolvedNamespaces [i])
  146. return false;
  147. return true;
  148. }
  149. // 3.8.6. Attribute Wildcard Intersection
  150. // Only try to examine if their intersection is expressible, and
  151. // returns if the result is empty.
  152. internal bool ExamineAttributeWildcardIntersection (XmlSchemaAny other,
  153. ValidationEventHandler h, XmlSchema schema)
  154. {
  155. return wildcard.ExamineAttributeWildcardIntersection (other, h, schema);
  156. }
  157. internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  158. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  159. {
  160. XmlSchemaAny baseAny = baseParticle as XmlSchemaAny;
  161. if (baseAny == null) {
  162. if (raiseError)
  163. error (h, "Invalid particle derivation by restriction was found.");
  164. return false;
  165. }
  166. // 3.9.6 Particle Derivation OK (Any:Any - NSSubset)
  167. if (!ValidateOccurenceRangeOK (baseParticle, h, schema, raiseError))
  168. return false;
  169. return wildcard.ValidateWildcardSubset (baseAny.wildcard, h, schema, raiseError);
  170. }
  171. internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  172. {
  173. // do nothing
  174. }
  175. internal override void ValidateUniqueParticleAttribution (
  176. XmlSchemaObjectTable qnames, ArrayList nsNames,
  177. ValidationEventHandler h, XmlSchema schema)
  178. {
  179. // Wildcard Intersection check.
  180. foreach (XmlSchemaAny other in nsNames)
  181. if (!ExamineAttributeWildcardIntersection (other, h, schema))
  182. error (h, "Ambiguous -any- particle was found.");
  183. nsNames.Add (this);
  184. }
  185. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  186. ValidationEventHandler h, XmlSchema schema)
  187. {
  188. // do nothing
  189. }
  190. // 3.10.4 Wildcard Allows Namespace Name. (In fact it is almost copy...)
  191. internal bool ValidateWildcardAllowsNamespaceName (string ns,
  192. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  193. {
  194. return wildcard.ValidateWildcardAllowsNamespaceName (ns, h, schema, raiseError);
  195. }
  196. //<any
  197. // id = ID
  198. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  199. // minOccurs = nonNegativeInteger : 1
  200. // namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) ) : ##any
  201. // processContents = (lax | skip | strict) : strict
  202. // {any attributes with non-schema namespace . . .}>
  203. // Content: (annotation?)
  204. //</any>
  205. internal static XmlSchemaAny Read(XmlSchemaReader reader, ValidationEventHandler h)
  206. {
  207. XmlSchemaAny any = new XmlSchemaAny();
  208. reader.MoveToElement();
  209. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  210. {
  211. error(h,"Should not happen :1: XmlSchemaAny.Read, name="+reader.Name,null);
  212. reader.SkipToEnd();
  213. return null;
  214. }
  215. any.LineNumber = reader.LineNumber;
  216. any.LinePosition = reader.LinePosition;
  217. any.SourceUri = reader.BaseURI;
  218. while(reader.MoveToNextAttribute())
  219. {
  220. if(reader.Name == "id")
  221. {
  222. any.Id = reader.Value;
  223. }
  224. else if(reader.Name == "maxOccurs")
  225. {
  226. try
  227. {
  228. any.MaxOccursString = reader.Value;
  229. }
  230. catch(Exception e)
  231. {
  232. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  233. }
  234. }
  235. else if(reader.Name == "minOccurs")
  236. {
  237. try
  238. {
  239. any.MinOccursString = reader.Value;
  240. }
  241. catch(Exception e)
  242. {
  243. error(h,reader.Value + " is an invalid value for minOccurs", e);
  244. }
  245. }
  246. else if(reader.Name == "namespace")
  247. {
  248. any.nameSpace = reader.Value;
  249. }
  250. else if(reader.Name == "processContents")
  251. {
  252. Exception innerex;
  253. any.processing = XmlSchemaUtil.ReadProcessingAttribute(reader,out innerex);
  254. if(innerex != null)
  255. error(h, reader.Value + " is not a valid value for processContents",innerex);
  256. }
  257. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  258. {
  259. error(h,reader.Name + " is not a valid attribute for any",null);
  260. }
  261. else
  262. {
  263. XmlSchemaUtil.ReadUnhandledAttribute(reader,any);
  264. }
  265. }
  266. reader.MoveToElement();
  267. if(reader.IsEmptyElement)
  268. return any;
  269. // Content: (annotation?)
  270. int level = 1;
  271. while(reader.ReadNextElement())
  272. {
  273. if(reader.NodeType == XmlNodeType.EndElement)
  274. {
  275. if(reader.LocalName != xmlname)
  276. error(h,"Should not happen :2: XmlSchemaAny.Read, name="+reader.Name,null);
  277. break;
  278. }
  279. if(level <= 1 && reader.LocalName == "annotation")
  280. {
  281. level = 2; //Only one annotation
  282. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  283. if(annotation != null)
  284. any.Annotation = annotation;
  285. continue;
  286. }
  287. reader.RaiseInvalidElementError();
  288. }
  289. return any;
  290. }
  291. }
  292. }