XmlSchemaAny.cs 7.8 KB

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