XmlSchemaCollection.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. schemaSet.ValidationEventHandler += new ValidationEventHandler (OnValidationError);
  55. }
  56. internal XmlSchemaCollection (XmlSchemaSet schemaSet)
  57. {
  58. this.schemaSet = schemaSet;
  59. }
  60. //properties
  61. internal XmlSchemaSet SchemaSet {
  62. get { return schemaSet; }
  63. }
  64. public int Count {
  65. get { return schemaSet.Count; }
  66. }
  67. public XmlNameTable NameTable {
  68. get { return schemaSet.NameTable; }
  69. }
  70. public XmlSchema this [ string ns ] {
  71. get {
  72. ICollection col = schemaSet.Schemas (ns);
  73. if (col == null)
  74. return null;
  75. IEnumerator e = col.GetEnumerator ();
  76. if (e.MoveNext ())
  77. return (XmlSchema) e.Current;
  78. else
  79. return null;
  80. }
  81. }
  82. // Events
  83. public event ValidationEventHandler ValidationEventHandler;
  84. // Methods
  85. public XmlSchema Add (string ns, XmlReader reader)
  86. {
  87. return Add (ns, reader, new XmlUrlResolver ());
  88. }
  89. #if NET_1_1
  90. public XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
  91. #else
  92. internal XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
  93. #endif
  94. {
  95. XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
  96. if (schema.TargetNamespace == null)
  97. schema.TargetNamespace = ns;
  98. else if (ns != null && schema.TargetNamespace != ns)
  99. throw new XmlSchemaException ("The actual targetNamespace in the schema does not match the parameter.");
  100. return Add (schema);
  101. }
  102. public XmlSchema Add (string ns, string uri)
  103. {
  104. XmlReader reader = new XmlTextReader (uri);
  105. try {
  106. return Add (ns, reader);
  107. } finally {
  108. reader.Close ();
  109. }
  110. }
  111. public XmlSchema Add (XmlSchema schema)
  112. {
  113. return Add (schema, new XmlUrlResolver ());
  114. }
  115. public XmlSchema Add (XmlSchema schema, XmlResolver resolver)
  116. {
  117. if (schema == null)
  118. throw new ArgumentNullException ("schema");
  119. XmlSchemaSet xss = new XmlSchemaSet (schemaSet.NameTable);
  120. xss.Add (schemaSet);
  121. // FIXME: maybe it requires Reprocess()
  122. xss.Add (schema);
  123. xss.ValidationEventHandler += ValidationEventHandler;
  124. xss.XmlResolver = resolver;
  125. xss.Compile ();
  126. if (!xss.IsCompiled)
  127. return null;
  128. // It is set only when the compilation was successful.
  129. schemaSet = xss;
  130. return schema;
  131. }
  132. public void Add (XmlSchemaCollection schema)
  133. {
  134. if (schema == null)
  135. throw new ArgumentNullException ("schema");
  136. XmlSchemaSet xss = new XmlSchemaSet (schemaSet.NameTable);
  137. xss.Add (schemaSet);
  138. // FIXME: maybe it requires Reprocess()
  139. xss.Add (schema.schemaSet);
  140. xss.ValidationEventHandler += ValidationEventHandler;
  141. xss.XmlResolver = schemaSet.XmlResolver;
  142. xss.Compile ();
  143. if (!xss.IsCompiled)
  144. return;
  145. // It is set only when the compilation was successful.
  146. schemaSet = xss;
  147. }
  148. public bool Contains (string ns)
  149. {
  150. lock (schemaSet) {
  151. return schemaSet.Contains (ns);
  152. }
  153. }
  154. public bool Contains (XmlSchema schema)
  155. {
  156. lock (schemaSet) {
  157. return schemaSet.Contains (schema);
  158. }
  159. }
  160. public void CopyTo (XmlSchema[] array, int index)
  161. {
  162. lock (schemaSet) {
  163. schemaSet.CopyTo (array, index);
  164. }
  165. }
  166. public XmlSchemaCollectionEnumerator GetEnumerator ()
  167. {
  168. // The actual collection is schemaSet.Schemas()
  169. return new XmlSchemaCollectionEnumerator(schemaSet.Schemas());
  170. }
  171. int ICollection.Count {
  172. get { return Count; }
  173. }
  174. // interface Methods
  175. void ICollection.CopyTo (Array array, int index)
  176. {
  177. lock (schemaSet) {
  178. schemaSet.CopyTo (array, index);
  179. }
  180. }
  181. bool ICollection.IsSynchronized
  182. {
  183. get { return true; } // always
  184. }
  185. IEnumerator IEnumerable.GetEnumerator ()
  186. {
  187. return this.GetEnumerator ();
  188. }
  189. Object ICollection.SyncRoot
  190. {
  191. get { return this; }
  192. }
  193. void OnValidationError (object o, ValidationEventArgs e)
  194. {
  195. if (ValidationEventHandler != null)
  196. ValidationEventHandler (o, e);
  197. else if (e.Severity == XmlSeverityType.Error)
  198. throw e.Exception;
  199. }
  200. }
  201. }