XmlSchemaAny.cs 8.9 KB

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