XmlSchemaCollection.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. ((ICollection) this).CopyTo (array, index);
  107. }
  108. public XmlSchemaCollectionEnumerator GetEnumerator()
  109. {
  110. return new XmlSchemaCollectionEnumerator(this);
  111. }
  112. // interface Methods
  113. void ICollection.CopyTo(Array array, int index)
  114. {
  115. htable.Values.CopyTo (array, index);
  116. }
  117. bool ICollection.IsSynchronized
  118. {
  119. get { return false; }
  120. }
  121. IEnumerator IEnumerable.GetEnumerator()
  122. {
  123. return this.htable.GetEnumerator();
  124. }
  125. Object ICollection.SyncRoot
  126. {
  127. get { return this; }
  128. }
  129. // Internal Methods
  130. internal XmlSchemaAttribute FindAttribute (XmlQualifiedName qname)
  131. {
  132. XmlSchemaAttribute found = null;
  133. XmlSchema target = this [qname.Namespace];
  134. if (target != null)
  135. found = target.Attributes [qname] as XmlSchemaAttribute;
  136. if (found != null)
  137. return found;
  138. foreach (XmlSchema schema in htable.Values) {
  139. found = schema.Attributes [qname] as XmlSchemaAttribute;
  140. if (found != null)
  141. return found;
  142. }
  143. return null;
  144. }
  145. internal XmlSchemaElement FindElement (XmlQualifiedName qname)
  146. {
  147. XmlSchemaElement found = null;
  148. XmlSchema target = this [qname.Namespace];
  149. if (target != null)
  150. found = target.Elements [qname] as XmlSchemaElement;
  151. if (found != null)
  152. return found;
  153. foreach (XmlSchema schema in htable.Values) {
  154. found = schema.Elements [qname] as XmlSchemaElement;
  155. if (found != null)
  156. return found;
  157. }
  158. return null;
  159. }
  160. internal object FindSchemaType (XmlQualifiedName qname)
  161. {
  162. if (qname == XmlSchemaComplexType.AnyTypeName)
  163. return XmlSchemaComplexType.AnyType;
  164. else if (qname.Namespace == XmlSchema.Namespace)
  165. return XmlSchemaDatatype.FromName (qname);
  166. XmlSchemaType found = null;
  167. XmlSchema target = this [qname.Namespace];
  168. if (target != null)
  169. found = target.SchemaTypes [qname] as XmlSchemaType;
  170. if (found != null)
  171. return found;
  172. foreach (XmlSchema schema in htable.Values) {
  173. found = schema.SchemaTypes [qname] as XmlSchemaType;
  174. if (found != null)
  175. return found;
  176. }
  177. return null;
  178. }
  179. }
  180. }