XmlSchemaAnyAttribute.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Specialized;
  6. using System.Xml;
  7. using System.ComponentModel;
  8. using System.Xml.Serialization;
  9. using Mono.Xml.Schema;
  10. namespace System.Xml.Schema
  11. {
  12. /// <summary>
  13. /// Summary description for XmlSchemaAnyAttribute.
  14. /// </summary>
  15. public class XmlSchemaAnyAttribute : XmlSchemaAnnotated
  16. {
  17. private string nameSpace;
  18. private XmlSchemaContentProcessing processing;
  19. private static string xmlname = "anyAttribute";
  20. private XsdWildcard wildcard;
  21. public XmlSchemaAnyAttribute()
  22. {
  23. wildcard = new XsdWildcard (this);
  24. }
  25. [System.Xml.Serialization.XmlAttribute("namespace")]
  26. public string Namespace
  27. {
  28. get{ return nameSpace; }
  29. set{ nameSpace = value; }
  30. }
  31. [DefaultValue(XmlSchemaContentProcessing.None)]
  32. [System.Xml.Serialization.XmlAttribute("processContents")]
  33. public XmlSchemaContentProcessing ProcessContents
  34. {
  35. get{ return processing; }
  36. set{ processing = value; }
  37. }
  38. // Internal
  39. internal bool HasValueAny {
  40. get { return wildcard.HasValueAny; }
  41. }
  42. internal bool HasValueLocal {
  43. get { return wildcard.HasValueLocal; }
  44. }
  45. internal bool HasValueOther {
  46. get { return wildcard.HasValueOther; }
  47. }
  48. internal bool HasValueTargetNamespace {
  49. get { return wildcard.HasValueTargetNamespace; }
  50. }
  51. internal StringCollection ResolvedNamespaces {
  52. get { return wildcard.ResolvedNamespaces; }
  53. }
  54. internal XmlSchemaContentProcessing ResolvedProcessContents
  55. {
  56. get{ return wildcard.ResolvedProcessing; }
  57. }
  58. internal string TargetNamespace
  59. {
  60. get { return wildcard.TargetNamespace; }
  61. }
  62. /// <remarks>
  63. /// 1. id must be of type ID
  64. /// 2. namespace can have one of the following values:
  65. /// a) ##any or ##other
  66. /// b) list of anyURI and ##targetNamespace and ##local
  67. /// </remarks>
  68. [MonoTODO]
  69. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  70. {
  71. // If this is already compiled this time, simply skip.
  72. if (this.IsComplied (schema.CompilationId))
  73. return 0;
  74. errorCount = 0;
  75. wildcard.TargetNamespace = schema.TargetNamespace;
  76. if (wildcard.TargetNamespace == null)
  77. wildcard.TargetNamespace = "";
  78. XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
  79. wildcard.Compile (Namespace, h, schema);
  80. if (processing == XmlSchemaContentProcessing.None)
  81. wildcard.ResolvedProcessing = XmlSchemaContentProcessing.Strict;
  82. else
  83. wildcard.ResolvedProcessing = processing;
  84. this.CompilationId = schema.CompilationId;
  85. return errorCount;
  86. }
  87. [MonoTODO]
  88. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  89. {
  90. return errorCount;
  91. }
  92. // 3.10.6 Wildcard Subset
  93. internal void ValidateWildcardSubset (XmlSchemaAnyAttribute other,
  94. ValidationEventHandler h, XmlSchema schema)
  95. {
  96. wildcard.ValidateWildcardSubset (other, h, schema);
  97. /*
  98. // 1.
  99. if (this.hasValueAny)
  100. return;
  101. if (this.hasValueOther) {
  102. if (other.hasValueOther) {
  103. // 2.1 and 2.2
  104. if (this.targetNamespace == other.targetNamespace ||
  105. other.targetNamespace == null || other.targetNamespace == "")
  106. return;
  107. }
  108. // 3.2.2
  109. else if (this.targetNamespace == null || targetNamespace == String.Empty)
  110. return;
  111. else {
  112. foreach (string ns in other.resolvedNamespaces)
  113. if (ns == this.targetNamespace) {
  114. error (h, "Invalid wildcard subset was found.");
  115. return;
  116. }
  117. }
  118. } else {
  119. // 3.1
  120. if (!this.hasValueLocal && other.hasValueLocal) {
  121. error (h, "Invalid wildcard subset was found.");
  122. } else if (other.resolvedNamespaces.Count == 0)
  123. return;
  124. else {
  125. ArrayList al = new ArrayList (this.resolvedNamespaces);
  126. foreach (string ns in other.resolvedNamespaces)
  127. if (!al.Contains (ns)) {
  128. error (h, "Invalid wildcard subset was found.");
  129. return;
  130. }
  131. }
  132. }
  133. */
  134. }
  135. //<anyAttribute
  136. // id = ID
  137. // namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) ) : ##any
  138. // processContents = (lax | skip | strict) : strict
  139. // {any attributes with non-schema namespace . . .}>
  140. // Content: (annotation?)
  141. //</anyAttribute>
  142. internal static XmlSchemaAnyAttribute Read(XmlSchemaReader reader, ValidationEventHandler h)
  143. {
  144. XmlSchemaAnyAttribute any = new XmlSchemaAnyAttribute();
  145. reader.MoveToElement();
  146. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  147. {
  148. error(h,"Should not happen :1: XmlSchemaAnyAttribute.Read, name="+reader.Name,null);
  149. reader.SkipToEnd();
  150. return null;
  151. }
  152. any.LineNumber = reader.LineNumber;
  153. any.LinePosition = reader.LinePosition;
  154. any.SourceUri = reader.BaseURI;
  155. while(reader.MoveToNextAttribute())
  156. {
  157. if(reader.Name == "id")
  158. {
  159. any.Id = reader.Value;
  160. }
  161. else if(reader.Name == "namespace")
  162. {
  163. any.nameSpace = reader.Value;
  164. }
  165. else if(reader.Name == "processContents")
  166. {
  167. Exception innerex;
  168. any.processing = XmlSchemaUtil.ReadProcessingAttribute(reader,out innerex);
  169. if(innerex != null)
  170. error(h, reader.Value + " is not a valid value for processContents",innerex);
  171. }
  172. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  173. {
  174. error(h,reader.Name + " is not a valid attribute for anyAttribute",null);
  175. }
  176. else
  177. {
  178. XmlSchemaUtil.ReadUnhandledAttribute(reader,any);
  179. }
  180. }
  181. reader.MoveToElement();
  182. if(reader.IsEmptyElement)
  183. return any;
  184. // Content: (annotation?)
  185. int level = 1;
  186. while(reader.ReadNextElement())
  187. {
  188. if(reader.NodeType == XmlNodeType.EndElement)
  189. {
  190. if(reader.LocalName != xmlname)
  191. error(h,"Should not happen :2: XmlSchemaAnyAttribute.Read, name="+reader.Name,null);
  192. break;
  193. }
  194. if(level <= 1 && reader.LocalName == "annotation")
  195. {
  196. level = 2; //Only one annotation
  197. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  198. if(annotation != null)
  199. any.Annotation = annotation;
  200. continue;
  201. }
  202. reader.RaiseInvalidElementError();
  203. }
  204. return any;
  205. }
  206. }
  207. }