XmlSchemaCollection.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. this.schemaSet.SchemaCollection = this;
  52. }
  53. internal XmlSchemaCollection (XmlSchemaSet schemaSet)
  54. {
  55. this.schemaSet = schemaSet;
  56. }
  57. //properties
  58. internal XmlSchemaSet SchemaSet {
  59. get { return schemaSet; }
  60. }
  61. public int Count {
  62. get { return schemaSet.Count; }
  63. }
  64. public XmlNameTable NameTable {
  65. get { return schemaSet.NameTable; }
  66. }
  67. public XmlSchema this [ string ns ] {
  68. get { return schemaSet.Get (ns); }
  69. }
  70. // Events
  71. public event ValidationEventHandler ValidationEventHandler;
  72. // Methods
  73. public XmlSchema Add (string ns, XmlReader reader)
  74. {
  75. return Add (ns, reader, new XmlUrlResolver ());
  76. }
  77. #if NET_1_0
  78. internal XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
  79. #else
  80. public XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
  81. #endif
  82. {
  83. XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
  84. schema.Compile (ValidationEventHandler, this, resolver);
  85. lock (schemaSet) {
  86. return schemaSet.Add (schema);
  87. }
  88. }
  89. public XmlSchema Add (string ns, string uri)
  90. {
  91. lock (schemaSet) {
  92. return schemaSet.Add (ns, uri);
  93. }
  94. }
  95. public XmlSchema Add (XmlSchema schema)
  96. {
  97. return Add (schema, new XmlUrlResolver ());
  98. }
  99. public XmlSchema Add (XmlSchema schema, XmlResolver resolver)
  100. {
  101. if (schema == null)
  102. throw new ArgumentNullException ("schema");
  103. // XmlSchemaCollection.Add() compiles, while XmlSchemaSet.Add() does not
  104. if (!schema.IsCompiled)
  105. schema.Compile (ValidationEventHandler, this, resolver);
  106. string ns = GetSafeNs (schema.TargetNamespace);
  107. lock (schemaSet) {
  108. if (schemaSet.Contains (ns))
  109. schemaSet.Remove (schemaSet.Get (ns));
  110. return schemaSet.Add (schema);
  111. }
  112. }
  113. private string GetSafeNs (string ns)
  114. {
  115. return ns != null ? ns : String.Empty;
  116. }
  117. public void Add (XmlSchemaCollection schema)
  118. {
  119. if (schema == null)
  120. throw new ArgumentNullException ("schema");
  121. /*
  122. foreach (XmlSchema s in schema) {
  123. string ns = GetSafeNs (s.TargetNamespace);
  124. lock (schemaSet) {
  125. if (schemaSet.Contains (ns))
  126. schemaSet.Remove (schemaSet.Get (ns));
  127. schemaSet.Add (s);
  128. }
  129. }
  130. */
  131. lock (schemaSet) {
  132. schemaSet.Add (schema.schemaSet);
  133. }
  134. }
  135. public bool Contains (string ns)
  136. {
  137. lock (schemaSet) {
  138. return schemaSet.Contains (ns);
  139. }
  140. }
  141. public bool Contains (XmlSchema schema)
  142. {
  143. lock (schemaSet) {
  144. return schemaSet.Contains (schema);
  145. }
  146. }
  147. public void CopyTo (XmlSchema[] array, int index)
  148. {
  149. lock (schemaSet) {
  150. schemaSet.CopyTo (array, index);
  151. }
  152. }
  153. public XmlSchemaCollectionEnumerator GetEnumerator ()
  154. {
  155. return new XmlSchemaCollectionEnumerator (this);
  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 schemaSet.GetEnumerator ();
  171. }
  172. Object ICollection.SyncRoot
  173. {
  174. get { return this; }
  175. }
  176. // Internal Methods
  177. internal XmlSchemaAttribute FindAttribute (XmlQualifiedName qname)
  178. {
  179. return (XmlSchemaAttribute) schemaSet.GlobalAttributes [qname];
  180. }
  181. internal XmlSchemaElement FindElement (XmlQualifiedName qname)
  182. {
  183. return (XmlSchemaElement) schemaSet.GlobalElements [qname];
  184. }
  185. internal object FindSchemaType (XmlQualifiedName qname)
  186. {
  187. return schemaSet.GlobalTypes [qname];
  188. }
  189. internal void OnValidationError (object o, ValidationEventArgs e)
  190. {
  191. if (ValidationEventHandler != null)
  192. ValidationEventHandler (o, e);
  193. else if (e.Severity == XmlSeverityType.Error)
  194. throw e.Exception;
  195. }
  196. }
  197. }