XmlSchemaCollection.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // System.Xml.Schema.XmlSchemaCollection.cs
  3. //
  4. // Authors:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. using System;
  9. using System.Collections;
  10. using System.Xml;
  11. namespace System.Xml.Schema
  12. {
  13. /// <summary>
  14. /// Summary description for XmlSchemaCollection.
  15. /// </summary>
  16. public sealed class XmlSchemaCollection : ICollection, IEnumerable
  17. {
  18. //private fields
  19. private Hashtable htable;
  20. private Hashtable uriTable;
  21. private XmlNameTable ntable;
  22. internal Guid CompilationId;
  23. public XmlSchemaCollection()
  24. : this (new NameTable ())
  25. {
  26. }
  27. public XmlSchemaCollection(XmlNameTable nametable)
  28. {
  29. htable = new Hashtable();
  30. uriTable = new Hashtable ();
  31. ntable = nametable;
  32. CompilationId = Guid.NewGuid ();
  33. }
  34. //properties
  35. public int Count
  36. {
  37. get
  38. {
  39. return this.htable.Count;
  40. }
  41. }
  42. public XmlNameTable NameTable
  43. {
  44. get
  45. {
  46. return this.ntable;
  47. }
  48. }
  49. public XmlSchema this[ string ns ]
  50. {
  51. get
  52. {
  53. return (XmlSchema) this.htable[GetSafeNs(ns)];
  54. }
  55. }
  56. // Events
  57. public event ValidationEventHandler ValidationEventHandler;
  58. // Methods
  59. public XmlSchema Add (string ns, XmlReader reader)
  60. {
  61. if (reader == null)
  62. throw new ArgumentNullException ("reader");
  63. XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
  64. return Add (schema);
  65. }
  66. public XmlSchema Add(string ns, string uri)
  67. {
  68. return Add (ns, new XmlTextReader (uri));
  69. }
  70. public XmlSchema Add(XmlSchema schema)
  71. {
  72. if (schema == null)
  73. throw new ArgumentNullException ("schema");
  74. if (!schema.IsCompiled)
  75. schema.Compile (null, this);
  76. /*
  77. // This is requried to complete maybe missing sub components.
  78. foreach (XmlSchema existing in htable.Values)
  79. if (existing.CompilationId != this.CompilationId)
  80. existing.Compile (null, this);
  81. */
  82. htable [GetSafeNs(schema.TargetNamespace)] = schema;
  83. return schema;
  84. }
  85. public void Add(XmlSchemaCollection schema)
  86. {
  87. if (schema == null)
  88. throw new ArgumentNullException ("schema");
  89. foreach (XmlSchema s in schema)
  90. Add (s);
  91. }
  92. string GetSafeNs (string ns)
  93. {
  94. return ns == null ? "" : ns;
  95. }
  96. public bool Contains(string ns)
  97. {
  98. return this.htable.Contains(GetSafeNs(ns));
  99. }
  100. public bool Contains(XmlSchema schema)
  101. {
  102. return this.htable.Contains(GetSafeNs(schema.TargetNamespace));
  103. }
  104. public void CopyTo(XmlSchema[] array, int index)
  105. {
  106. throw new NotImplementedException ();
  107. }
  108. public XmlSchemaCollectionEnumerator GetEnumerator()
  109. {
  110. return new XmlSchemaCollectionEnumerator(this);
  111. }
  112. // interface Methods
  113. [MonoTODO]
  114. void ICollection.CopyTo(Array array, int index)
  115. {
  116. throw new NotImplementedException ();
  117. }
  118. bool ICollection.IsSynchronized
  119. {
  120. get { return false; }
  121. }
  122. IEnumerator IEnumerable.GetEnumerator()
  123. {
  124. return this.htable.GetEnumerator();
  125. }
  126. Object ICollection.SyncRoot
  127. {
  128. get { return this; }
  129. }
  130. // Internal Methods
  131. internal XmlSchemaAttribute FindAttribute (XmlQualifiedName qname)
  132. {
  133. XmlSchemaAttribute found = null;
  134. XmlSchema target = this [qname.Namespace];
  135. if (target != null)
  136. found = target.Attributes [qname] as XmlSchemaAttribute;
  137. if (found != null)
  138. return found;
  139. foreach (XmlSchema schema in htable.Values) {
  140. found = schema.Attributes [qname] as XmlSchemaAttribute;
  141. if (found != null)
  142. return found;
  143. }
  144. return null;
  145. }
  146. internal XmlSchemaElement FindElement (XmlQualifiedName qname)
  147. {
  148. XmlSchemaElement found = null;
  149. XmlSchema target = this [qname.Namespace];
  150. if (target != null)
  151. found = target.Elements [qname] as XmlSchemaElement;
  152. if (found != null)
  153. return found;
  154. foreach (XmlSchema schema in htable.Values) {
  155. found = schema.Elements [qname] as XmlSchemaElement;
  156. if (found != null)
  157. return found;
  158. }
  159. return null;
  160. }
  161. internal object FindSchemaType (XmlQualifiedName qname)
  162. {
  163. if (qname == XmlSchemaComplexType.AnyTypeName)
  164. return XmlSchemaComplexType.AnyType;
  165. else if (qname.Namespace == XmlSchema.Namespace)
  166. return XmlSchemaDatatype.FromName (qname);
  167. XmlSchemaType found = null;
  168. XmlSchema target = this [qname.Namespace];
  169. if (target != null)
  170. found = target.SchemaTypes [qname] as XmlSchemaType;
  171. if (found != null)
  172. return found;
  173. foreach (XmlSchema schema in htable.Values) {
  174. found = schema.SchemaTypes [qname] as XmlSchemaType;
  175. if (found != null)
  176. return found;
  177. }
  178. return null;
  179. }
  180. }
  181. }