XmlSchemaCollection.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. public sealed class XmlSchemaCollection : ICollection, IEnumerable
  41. {
  42. //private fields
  43. private XmlSchemaSet schemaSet;
  44. public XmlSchemaCollection ()
  45. : this (new NameTable ())
  46. {
  47. }
  48. public XmlSchemaCollection (XmlNameTable nameTable)
  49. : this (new XmlSchemaSet (nameTable))
  50. {
  51. }
  52. internal XmlSchemaCollection (XmlSchemaSet schemaSet)
  53. {
  54. this.schemaSet = schemaSet;
  55. }
  56. //properties
  57. internal XmlSchemaSet SchemaSet {
  58. get { return schemaSet; }
  59. }
  60. public int Count {
  61. get { return schemaSet.Count; }
  62. }
  63. public XmlNameTable NameTable {
  64. get { return schemaSet.NameTable; }
  65. }
  66. public XmlSchema this [ string ns ] {
  67. get {
  68. ICollection col = schemaSet.Schemas (ns);
  69. if (col == null)
  70. return null;
  71. IEnumerator e = col.GetEnumerator ();
  72. if (e.MoveNext ())
  73. return (XmlSchema) e.Current;
  74. else
  75. return null;
  76. }
  77. }
  78. // Events
  79. public event ValidationEventHandler ValidationEventHandler;
  80. // Methods
  81. public XmlSchema Add (string ns, XmlReader reader)
  82. {
  83. return Add (ns, reader, new XmlUrlResolver ());
  84. }
  85. #if NET_1_1
  86. public XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
  87. #else
  88. internal XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
  89. #endif
  90. {
  91. XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
  92. schema.Compile (ValidationEventHandler, schemaSet, resolver);
  93. lock (schemaSet) {
  94. return schemaSet.Add (schema);
  95. }
  96. }
  97. public XmlSchema Add (string ns, string uri)
  98. {
  99. lock (schemaSet) {
  100. return schemaSet.Add (ns, uri);
  101. }
  102. }
  103. public XmlSchema Add (XmlSchema schema)
  104. {
  105. return Add (schema, new XmlUrlResolver ());
  106. }
  107. public XmlSchema Add (XmlSchema schema, XmlResolver resolver)
  108. {
  109. if (schema == null)
  110. throw new ArgumentNullException ("schema");
  111. // XmlSchemaCollection.Add() compiles, while XmlSchemaSet.Add() does not
  112. if (!schema.IsCompiled)
  113. schema.Compile (ValidationEventHandler, schemaSet, resolver);
  114. // compilation error -> returns null (and don't add to the collection).
  115. if (!schema.IsCompiled)
  116. return null;
  117. string ns = GetSafeNs (schema.TargetNamespace);
  118. lock (schemaSet) {
  119. // consider imported schemas.
  120. schemaSet.Add (schema.Schemas);
  121. return schema;
  122. }
  123. }
  124. private string GetSafeNs (string ns)
  125. {
  126. return ns != null ? ns : String.Empty;
  127. }
  128. public void Add (XmlSchemaCollection schema)
  129. {
  130. if (schema == null)
  131. throw new ArgumentNullException ("schema");
  132. lock (schemaSet) {
  133. schemaSet.Add (schema.schemaSet);
  134. }
  135. }
  136. public bool Contains (string ns)
  137. {
  138. lock (schemaSet) {
  139. return schemaSet.Contains (ns);
  140. }
  141. }
  142. public bool Contains (XmlSchema schema)
  143. {
  144. lock (schemaSet) {
  145. return schemaSet.Contains (schema);
  146. }
  147. }
  148. public void CopyTo (XmlSchema[] array, int index)
  149. {
  150. lock (schemaSet) {
  151. schemaSet.CopyTo (array, index);
  152. }
  153. }
  154. public XmlSchemaCollectionEnumerator GetEnumerator ()
  155. {
  156. return new XmlSchemaCollectionEnumerator (this);
  157. }
  158. // interface Methods
  159. void ICollection.CopyTo (Array array, int index)
  160. {
  161. lock (schemaSet) {
  162. schemaSet.CopyTo (array, index);
  163. }
  164. }
  165. bool ICollection.IsSynchronized
  166. {
  167. get { return true; } // always
  168. }
  169. IEnumerator IEnumerable.GetEnumerator ()
  170. {
  171. return this.GetEnumerator ();
  172. }
  173. Object ICollection.SyncRoot
  174. {
  175. get { return this; }
  176. }
  177. internal void OnValidationError (object o, ValidationEventArgs e)
  178. {
  179. if (ValidationEventHandler != null)
  180. ValidationEventHandler (o, e);
  181. else if (e.Severity == XmlSeverityType.Error)
  182. throw e.Exception;
  183. }
  184. }
  185. }