XmlSchemaAny.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //
  2. // System.Xml.Schema.XmlSchemaAny.cs
  3. //
  4. // Author:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Specialized;
  31. using System.Xml;
  32. using System.Xml.Serialization;
  33. using System.ComponentModel;
  34. using Mono.Xml.Schema;
  35. namespace System.Xml.Schema
  36. {
  37. /// <summary>
  38. /// Summary description for XmlSchemaAny.
  39. /// </summary>
  40. public class XmlSchemaAny : XmlSchemaParticle
  41. {
  42. static XmlSchemaAny anyTypeContent;
  43. internal static XmlSchemaAny AnyTypeContent {
  44. get {
  45. if (anyTypeContent == null) {
  46. anyTypeContent = new XmlSchemaAny ();
  47. anyTypeContent.MaxOccursString = "unbounded";
  48. anyTypeContent.MinOccurs = 0;
  49. anyTypeContent.CompileOccurence (null, null);
  50. anyTypeContent.Namespace = "##any";
  51. anyTypeContent.wildcard.HasValueAny = true;
  52. anyTypeContent.wildcard.ResolvedNamespaces = new StringCollection ();
  53. // Although it is not documented by W3C, but it should be.
  54. anyTypeContent.wildcard.ResolvedProcessing =
  55. anyTypeContent.ProcessContents = XmlSchemaContentProcessing.Lax;
  56. }
  57. return anyTypeContent;
  58. }
  59. }
  60. private string nameSpace;
  61. private XmlSchemaContentProcessing processing;
  62. const string xmlname = "any";
  63. private XsdWildcard wildcard;
  64. public XmlSchemaAny()
  65. {
  66. wildcard = new XsdWildcard (this);
  67. }
  68. [System.Xml.Serialization.XmlAttribute("namespace")]
  69. public string Namespace
  70. {
  71. get{ return nameSpace; }
  72. set{ nameSpace = value; }
  73. }
  74. [DefaultValue(XmlSchemaContentProcessing.None)]
  75. [System.Xml.Serialization.XmlAttribute("processContents")]
  76. public XmlSchemaContentProcessing ProcessContents
  77. {
  78. get{ return processing; }
  79. set{ processing = value; }
  80. }
  81. // Post Compilation Schema Infoset
  82. internal bool HasValueAny {
  83. get { return wildcard.HasValueAny; }
  84. }
  85. internal bool HasValueLocal {
  86. get { return wildcard.HasValueLocal; }
  87. }
  88. internal bool HasValueOther {
  89. get { return wildcard.HasValueOther; }
  90. }
  91. internal bool HasValueTargetNamespace {
  92. get { return wildcard.HasValueTargetNamespace; }
  93. }
  94. internal StringCollection ResolvedNamespaces {
  95. get { return wildcard.ResolvedNamespaces; }
  96. }
  97. internal XmlSchemaContentProcessing ResolvedProcessContents
  98. {
  99. get{ return wildcard.ResolvedProcessing; }
  100. }
  101. internal string TargetNamespace
  102. {
  103. get { return wildcard.TargetNamespace; }
  104. }
  105. /// <remarks>
  106. /// 1. id must be of type ID
  107. /// 2. namespace can have one of the following values:
  108. /// a) ##any or ##other
  109. /// b) list of anyURI and ##targetNamespace and ##local
  110. /// </remarks>
  111. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  112. {
  113. // If this is already compiled this time, simply skip.
  114. if (this.IsComplied (schema.CompilationId))
  115. return 0;
  116. errorCount = 0;
  117. XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
  118. wildcard.TargetNamespace = schema.TargetNamespace;
  119. if (wildcard.TargetNamespace == null)
  120. wildcard.TargetNamespace = "";
  121. CompileOccurence (h, schema);
  122. wildcard.Compile (Namespace, h, schema);
  123. if (processing == XmlSchemaContentProcessing.None)
  124. wildcard.ResolvedProcessing = XmlSchemaContentProcessing.Strict;
  125. else
  126. wildcard.ResolvedProcessing = processing;
  127. this.CompilationId = schema.CompilationId;
  128. return errorCount;
  129. }
  130. internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
  131. {
  132. if (OptimizedParticle != null)
  133. return OptimizedParticle;
  134. // Uncommenting this causes incorrect validation.
  135. // It will prevent UPA e.g. msxsdtest/Particles/particlesJf006.xsd
  136. // if (ValidatedMaxOccurs == 0) {
  137. // OptimizedParticle = XmlSchemaParticle.Empty;
  138. // return OptimizedParticle;
  139. // }
  140. XmlSchemaAny any = new XmlSchemaAny ();
  141. CopyInfo (any);
  142. any.CompileOccurence (null, null);
  143. any.wildcard = this.wildcard;
  144. OptimizedParticle = any;
  145. // properties which never contribute to validation
  146. any.Namespace = Namespace;
  147. any.ProcessContents = ProcessContents;
  148. any.Annotation = Annotation;
  149. any.UnhandledAttributes = UnhandledAttributes;
  150. return OptimizedParticle;
  151. }
  152. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  153. {
  154. return errorCount;
  155. }
  156. internal override bool ParticleEquals (XmlSchemaParticle other)
  157. {
  158. XmlSchemaAny any = other as XmlSchemaAny;
  159. if (any == null)
  160. return false;
  161. if (this.HasValueAny != any.HasValueAny ||
  162. this.HasValueLocal != any.HasValueLocal ||
  163. this.HasValueOther != any.HasValueOther ||
  164. this.HasValueTargetNamespace != any.HasValueTargetNamespace ||
  165. this.ResolvedProcessContents != any.ResolvedProcessContents ||
  166. this.ValidatedMaxOccurs != any.ValidatedMaxOccurs ||
  167. this.ValidatedMinOccurs != any.ValidatedMinOccurs ||
  168. this.ResolvedNamespaces.Count != any.ResolvedNamespaces.Count)
  169. return false;
  170. for (int i = 0; i < ResolvedNamespaces.Count; i++)
  171. if (ResolvedNamespaces [i] != any.ResolvedNamespaces [i])
  172. return false;
  173. return true;
  174. }
  175. // 3.8.6. Attribute Wildcard Intersection
  176. // Only try to examine if their intersection is expressible, and
  177. // returns if the result is empty.
  178. internal bool ExamineAttributeWildcardIntersection (XmlSchemaAny other,
  179. ValidationEventHandler h, XmlSchema schema)
  180. {
  181. return wildcard.ExamineAttributeWildcardIntersection (other, h, schema);
  182. }
  183. internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  184. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  185. {
  186. XmlSchemaAny baseAny = baseParticle as XmlSchemaAny;
  187. if (baseAny == null) {
  188. if (raiseError)
  189. error (h, "Invalid particle derivation by restriction was found.");
  190. return false;
  191. }
  192. // 3.9.6 Particle Derivation OK (Any:Any - NSSubset)
  193. if (!ValidateOccurenceRangeOK (baseParticle, h, schema, raiseError))
  194. return false;
  195. return wildcard.ValidateWildcardSubset (baseAny.wildcard, h, schema, raiseError);
  196. }
  197. internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
  198. {
  199. // do nothing
  200. }
  201. internal override void ValidateUniqueParticleAttribution (
  202. XmlSchemaObjectTable qnames, ArrayList nsNames,
  203. ValidationEventHandler h, XmlSchema schema)
  204. {
  205. // Wildcard Intersection check.
  206. foreach (XmlSchemaAny other in nsNames)
  207. if (!ExamineAttributeWildcardIntersection (other, h, schema))
  208. error (h, "Ambiguous -any- particle was found.");
  209. nsNames.Add (this);
  210. }
  211. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  212. ValidationEventHandler h, XmlSchema schema)
  213. {
  214. // do nothing
  215. }
  216. // 3.10.4 Wildcard Allows Namespace Name. (In fact it is almost copy...)
  217. internal bool ValidateWildcardAllowsNamespaceName (string ns,
  218. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  219. {
  220. return wildcard.ValidateWildcardAllowsNamespaceName (ns, h, schema, raiseError);
  221. }
  222. //<any
  223. // id = ID
  224. // maxOccurs = (nonNegativeInteger | unbounded) : 1
  225. // minOccurs = nonNegativeInteger : 1
  226. // namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) ) : ##any
  227. // processContents = (lax | skip | strict) : strict
  228. // {any attributes with non-schema namespace . . .}>
  229. // Content: (annotation?)
  230. //</any>
  231. internal static XmlSchemaAny Read(XmlSchemaReader reader, ValidationEventHandler h)
  232. {
  233. XmlSchemaAny any = new XmlSchemaAny();
  234. reader.MoveToElement();
  235. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  236. {
  237. error(h,"Should not happen :1: XmlSchemaAny.Read, name="+reader.Name,null);
  238. reader.SkipToEnd();
  239. return null;
  240. }
  241. any.LineNumber = reader.LineNumber;
  242. any.LinePosition = reader.LinePosition;
  243. any.SourceUri = reader.BaseURI;
  244. while(reader.MoveToNextAttribute())
  245. {
  246. if(reader.Name == "id")
  247. {
  248. any.Id = reader.Value;
  249. }
  250. else if(reader.Name == "maxOccurs")
  251. {
  252. try
  253. {
  254. any.MaxOccursString = reader.Value;
  255. }
  256. catch(Exception e)
  257. {
  258. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  259. }
  260. }
  261. else if(reader.Name == "minOccurs")
  262. {
  263. try
  264. {
  265. any.MinOccursString = reader.Value;
  266. }
  267. catch(Exception e)
  268. {
  269. error(h,reader.Value + " is an invalid value for minOccurs", e);
  270. }
  271. }
  272. else if(reader.Name == "namespace")
  273. {
  274. any.nameSpace = reader.Value;
  275. }
  276. else if(reader.Name == "processContents")
  277. {
  278. Exception innerex;
  279. any.processing = XmlSchemaUtil.ReadProcessingAttribute(reader,out innerex);
  280. if(innerex != null)
  281. error(h, reader.Value + " is not a valid value for processContents",innerex);
  282. }
  283. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  284. {
  285. error(h,reader.Name + " is not a valid attribute for any",null);
  286. }
  287. else
  288. {
  289. XmlSchemaUtil.ReadUnhandledAttribute(reader,any);
  290. }
  291. }
  292. reader.MoveToElement();
  293. if(reader.IsEmptyElement)
  294. return any;
  295. // Content: (annotation?)
  296. int level = 1;
  297. while(reader.ReadNextElement())
  298. {
  299. if(reader.NodeType == XmlNodeType.EndElement)
  300. {
  301. if(reader.LocalName != xmlname)
  302. error(h,"Should not happen :2: XmlSchemaAny.Read, name="+reader.Name,null);
  303. break;
  304. }
  305. if(level <= 1 && reader.LocalName == "annotation")
  306. {
  307. level = 2; //Only one annotation
  308. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  309. if(annotation != null)
  310. any.Annotation = annotation;
  311. continue;
  312. }
  313. reader.RaiseInvalidElementError();
  314. }
  315. return any;
  316. }
  317. }
  318. }