XmlSchemaCollection.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 XmlSchemaSet schemaSet;
  20. public XmlSchemaCollection ()
  21. : this (new NameTable ())
  22. {
  23. }
  24. public XmlSchemaCollection (XmlNameTable nameTable)
  25. : this (new XmlSchemaSet (nameTable))
  26. {
  27. this.schemaSet.SchemaCollection = this;
  28. }
  29. internal XmlSchemaCollection (XmlSchemaSet schemaSet)
  30. {
  31. this.schemaSet = schemaSet;
  32. }
  33. //properties
  34. internal XmlSchemaSet SchemaSet {
  35. get { return schemaSet; }
  36. }
  37. public int Count {
  38. get { return schemaSet.Count; }
  39. }
  40. public XmlNameTable NameTable {
  41. get { return schemaSet.NameTable; }
  42. }
  43. public XmlSchema this [ string ns ] {
  44. get { return schemaSet.Get (ns); }
  45. }
  46. // Events
  47. public event ValidationEventHandler ValidationEventHandler;
  48. // Methods
  49. public XmlSchema Add (string ns, XmlReader reader)
  50. {
  51. return Add (ns, reader, new XmlUrlResolver ());
  52. }
  53. #if NET_1_0
  54. internal XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
  55. #else
  56. public XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
  57. #endif
  58. {
  59. XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
  60. schema.Compile (ValidationEventHandler, this, resolver);
  61. lock (schemaSet) {
  62. return schemaSet.Add (schema);
  63. }
  64. }
  65. public XmlSchema Add (string ns, string uri)
  66. {
  67. lock (schemaSet) {
  68. return schemaSet.Add (ns, uri);
  69. }
  70. }
  71. public XmlSchema Add (XmlSchema schema)
  72. {
  73. return Add (schema, new XmlUrlResolver ());
  74. }
  75. public XmlSchema Add (XmlSchema schema, XmlResolver resolver)
  76. {
  77. if (schema == null)
  78. throw new ArgumentNullException ("schema");
  79. // XmlSchemaCollection.Add() compiles, while XmlSchemaSet.Add() does not
  80. if (!schema.IsCompiled)
  81. schema.Compile (ValidationEventHandler, this, resolver);
  82. string ns = GetSafeNs (schema.TargetNamespace);
  83. lock (schemaSet) {
  84. if (schemaSet.Contains (ns))
  85. schemaSet.Remove (schemaSet.Get (ns));
  86. return schemaSet.Add (schema);
  87. }
  88. }
  89. private string GetSafeNs (string ns)
  90. {
  91. return ns != null ? ns : String.Empty;
  92. }
  93. public void Add (XmlSchemaCollection schema)
  94. {
  95. if (schema == null)
  96. throw new ArgumentNullException ("schema");
  97. foreach (XmlSchema s in schema) {
  98. string ns = GetSafeNs (s.TargetNamespace);
  99. lock (schemaSet) {
  100. if (schemaSet.Contains (ns))
  101. schemaSet.Remove (schemaSet.Get (ns));
  102. schemaSet.Add (s);
  103. }
  104. }
  105. }
  106. public bool Contains (string ns)
  107. {
  108. lock (schemaSet) {
  109. return schemaSet.Contains (ns);
  110. }
  111. }
  112. public bool Contains (XmlSchema schema)
  113. {
  114. lock (schemaSet) {
  115. return schemaSet.Contains (schema);
  116. }
  117. }
  118. public void CopyTo (XmlSchema[] array, int index)
  119. {
  120. lock (schemaSet) {
  121. schemaSet.CopyTo (array, index);
  122. }
  123. }
  124. public XmlSchemaCollectionEnumerator GetEnumerator ()
  125. {
  126. return new XmlSchemaCollectionEnumerator (this);
  127. }
  128. // interface Methods
  129. void ICollection.CopyTo (Array array, int index)
  130. {
  131. lock (schemaSet) {
  132. schemaSet.CopyTo (array, index);
  133. }
  134. }
  135. bool ICollection.IsSynchronized
  136. {
  137. get { return true; } // always
  138. }
  139. IEnumerator IEnumerable.GetEnumerator ()
  140. {
  141. return schemaSet.GetEnumerator ();
  142. }
  143. Object ICollection.SyncRoot
  144. {
  145. get { return this; }
  146. }
  147. // Internal Methods
  148. internal XmlSchemaAttribute FindAttribute (XmlQualifiedName qname)
  149. {
  150. return (XmlSchemaAttribute) schemaSet.GlobalAttributes [qname];
  151. }
  152. internal XmlSchemaElement FindElement (XmlQualifiedName qname)
  153. {
  154. return (XmlSchemaElement) schemaSet.GlobalElements [qname];
  155. }
  156. internal object FindSchemaType (XmlQualifiedName qname)
  157. {
  158. return schemaSet.GlobalTypes [qname];
  159. }
  160. internal void OnValidationError (object o, ValidationEventArgs e)
  161. {
  162. if (ValidationEventHandler != null)
  163. ValidationEventHandler (o, e);
  164. else if (e.Severity == XmlSeverityType.Error)
  165. throw e.Exception;
  166. }
  167. }
  168. }