XmlSchemaAny.cs 9.8 KB

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