XmlSchemaCollection.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // System.Xml.Schema.XmlSchemaCollection.cs
  3. //
  4. // Authors:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Xml;
  31. namespace System.Xml.Schema
  32. {
  33. /// <summary>
  34. /// Summary description for XmlSchemaCollection.
  35. ///
  36. /// It is just a wrapper for XmlSchemaSet (unlike MS.NET, our
  37. /// XmlSchemaCollection is originally designed to be conformant to
  38. /// W3C specification).
  39. /// </summary>
  40. #if NET_2_0
  41. [Obsolete ("Use XmlSchemaSet.")]
  42. #endif
  43. public sealed class XmlSchemaCollection : ICollection, IEnumerable
  44. {
  45. //private fields
  46. private XmlSchemaSet schemaSet;
  47. public XmlSchemaCollection ()
  48. : this (new NameTable ())
  49. {
  50. }
  51. public XmlSchemaCollection (XmlNameTable nameTable)
  52. : this (new XmlSchemaSet (nameTable))
  53. {
  54. }
  55. internal XmlSchemaCollection (XmlSchemaSet schemaSet)
  56. {
  57. this.schemaSet = schemaSet;
  58. }
  59. //properties
  60. internal XmlSchemaSet SchemaSet {
  61. get { return schemaSet; }
  62. }
  63. public int Count {
  64. get { return schemaSet.Count; }
  65. }
  66. public XmlNameTable NameTable {
  67. get { return schemaSet.NameTable; }
  68. }
  69. public XmlSchema this [ string ns ] {
  70. get {
  71. ICollection col = schemaSet.Schemas (ns);
  72. if (col == null)
  73. return null;
  74. IEnumerator e = col.GetEnumerator ();
  75. if (e.MoveNext ())
  76. return (XmlSchema) e.Current;
  77. else
  78. return null;
  79. }
  80. }
  81. // Events
  82. public event ValidationEventHandler ValidationEventHandler;
  83. // Methods
  84. public XmlSchema Add (string ns, XmlReader reader)
  85. {
  86. return Add (ns, reader, new XmlUrlResolver ());
  87. }
  88. #if NET_1_1
  89. public XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
  90. #else
  91. internal XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
  92. #endif
  93. {
  94. XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
  95. schema.Compile (ValidationEventHandler, schemaSet, resolver);
  96. lock (schemaSet) {
  97. return schemaSet.Add (schema);
  98. }
  99. }
  100. public XmlSchema Add (string ns, string uri)
  101. {
  102. lock (schemaSet) {
  103. return schemaSet.Add (ns, uri);
  104. }
  105. }
  106. public XmlSchema Add (XmlSchema schema)
  107. {
  108. return Add (schema, new XmlUrlResolver ());
  109. }
  110. public XmlSchema Add (XmlSchema schema, XmlResolver resolver)
  111. {
  112. if (schema == null)
  113. throw new ArgumentNullException ("schema");
  114. // XmlSchemaCollection.Add() compiles, while XmlSchemaSet.Add() does not
  115. if (!schema.IsCompiled)
  116. schema.Compile (ValidationEventHandler, schemaSet, resolver);
  117. // compilation error -> returns null (and don't add to the collection).
  118. if (!schema.IsCompiled)
  119. return null;
  120. lock (schemaSet) {
  121. // consider imported schemas.
  122. schemaSet.Add (schema.Schemas);
  123. return schema;
  124. }
  125. }
  126. public void Add (XmlSchemaCollection schema)
  127. {
  128. if (schema == null)
  129. throw new ArgumentNullException ("schema");
  130. lock (schemaSet) {
  131. schemaSet.Add (schema.schemaSet);
  132. }
  133. }
  134. public bool Contains (string ns)
  135. {
  136. lock (schemaSet) {
  137. return schemaSet.Contains (ns);
  138. }
  139. }
  140. public bool Contains (XmlSchema schema)
  141. {
  142. lock (schemaSet) {
  143. return schemaSet.Contains (schema);
  144. }
  145. }
  146. public void CopyTo (XmlSchema[] array, int index)
  147. {
  148. lock (schemaSet) {
  149. schemaSet.CopyTo (array, index);
  150. }
  151. }
  152. public XmlSchemaCollectionEnumerator GetEnumerator ()
  153. {
  154. // The actual collection is schemaSet.Schemas()
  155. return new XmlSchemaCollectionEnumerator(schemaSet.Schemas());
  156. }
  157. // interface Methods
  158. void ICollection.CopyTo (Array array, int index)
  159. {
  160. lock (schemaSet) {
  161. schemaSet.CopyTo (array, index);
  162. }
  163. }
  164. bool ICollection.IsSynchronized
  165. {
  166. get { return true; } // always
  167. }
  168. IEnumerator IEnumerable.GetEnumerator ()
  169. {
  170. return this.GetEnumerator ();
  171. }
  172. Object ICollection.SyncRoot
  173. {
  174. get { return this; }
  175. }
  176. internal void OnValidationError (object o, ValidationEventArgs e)
  177. {
  178. if (ValidationEventHandler != null)
  179. ValidationEventHandler (o, e);
  180. else if (e.Severity == XmlSeverityType.Error)
  181. throw e.Exception;
  182. }
  183. }
  184. }