XmlSchemaCollection.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. private string GetSafeNs (string ns)
  127. {
  128. return ns != null ? ns : String.Empty;
  129. }
  130. public void Add (XmlSchemaCollection schema)
  131. {
  132. if (schema == null)
  133. throw new ArgumentNullException ("schema");
  134. lock (schemaSet) {
  135. schemaSet.Add (schema.schemaSet);
  136. }
  137. }
  138. public bool Contains (string ns)
  139. {
  140. lock (schemaSet) {
  141. return schemaSet.Contains (ns);
  142. }
  143. }
  144. public bool Contains (XmlSchema schema)
  145. {
  146. lock (schemaSet) {
  147. return schemaSet.Contains (schema);
  148. }
  149. }
  150. public void CopyTo (XmlSchema[] array, int index)
  151. {
  152. lock (schemaSet) {
  153. schemaSet.CopyTo (array, index);
  154. }
  155. }
  156. public XmlSchemaCollectionEnumerator GetEnumerator ()
  157. {
  158. return new XmlSchemaCollectionEnumerator (this);
  159. }
  160. // interface Methods
  161. void ICollection.CopyTo (Array array, int index)
  162. {
  163. lock (schemaSet) {
  164. schemaSet.CopyTo (array, index);
  165. }
  166. }
  167. bool ICollection.IsSynchronized
  168. {
  169. get { return true; } // always
  170. }
  171. IEnumerator IEnumerable.GetEnumerator ()
  172. {
  173. return this.GetEnumerator ();
  174. }
  175. Object ICollection.SyncRoot
  176. {
  177. get { return this; }
  178. }
  179. internal void OnValidationError (object o, ValidationEventArgs e)
  180. {
  181. if (ValidationEventHandler != null)
  182. ValidationEventHandler (o, e);
  183. else if (e.Severity == XmlSeverityType.Error)
  184. throw e.Exception;
  185. }
  186. }
  187. }