XmlSchemaCollection.cs 5.1 KB

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