XmlSchemaCollection.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. /// </summary>
  36. public sealed class XmlSchemaCollection : ICollection, IEnumerable
  37. {
  38. //private fields
  39. private XmlSchemaSet schemaSet;
  40. public XmlSchemaCollection ()
  41. : this (new NameTable ())
  42. {
  43. }
  44. public XmlSchemaCollection (XmlNameTable nameTable)
  45. : this (new XmlSchemaSet (nameTable))
  46. {
  47. this.schemaSet.SchemaCollection = this;
  48. }
  49. internal XmlSchemaCollection (XmlSchemaSet schemaSet)
  50. {
  51. this.schemaSet = schemaSet;
  52. }
  53. //properties
  54. internal XmlSchemaSet SchemaSet {
  55. get { return schemaSet; }
  56. }
  57. public int Count {
  58. get { return schemaSet.Count; }
  59. }
  60. public XmlNameTable NameTable {
  61. get { return schemaSet.NameTable; }
  62. }
  63. public XmlSchema this [ string ns ] {
  64. get { return schemaSet.Get (ns); }
  65. }
  66. // Events
  67. public event ValidationEventHandler ValidationEventHandler;
  68. // Methods
  69. public XmlSchema Add (string ns, XmlReader reader)
  70. {
  71. return Add (ns, reader, new XmlUrlResolver ());
  72. }
  73. #if NET_1_0
  74. internal XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
  75. #else
  76. public XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
  77. #endif
  78. {
  79. XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
  80. schema.Compile (ValidationEventHandler, this, resolver);
  81. lock (schemaSet) {
  82. return schemaSet.Add (schema);
  83. }
  84. }
  85. public XmlSchema Add (string ns, string uri)
  86. {
  87. lock (schemaSet) {
  88. return schemaSet.Add (ns, uri);
  89. }
  90. }
  91. public XmlSchema Add (XmlSchema schema)
  92. {
  93. return Add (schema, new XmlUrlResolver ());
  94. }
  95. public XmlSchema Add (XmlSchema schema, XmlResolver resolver)
  96. {
  97. if (schema == null)
  98. throw new ArgumentNullException ("schema");
  99. // XmlSchemaCollection.Add() compiles, while XmlSchemaSet.Add() does not
  100. if (!schema.IsCompiled)
  101. schema.Compile (ValidationEventHandler, this, resolver);
  102. string ns = GetSafeNs (schema.TargetNamespace);
  103. lock (schemaSet) {
  104. if (schemaSet.Contains (ns))
  105. schemaSet.Remove (schemaSet.Get (ns));
  106. return schemaSet.Add (schema);
  107. }
  108. }
  109. private string GetSafeNs (string ns)
  110. {
  111. return ns != null ? ns : String.Empty;
  112. }
  113. public void Add (XmlSchemaCollection schema)
  114. {
  115. if (schema == null)
  116. throw new ArgumentNullException ("schema");
  117. foreach (XmlSchema s in schema) {
  118. string ns = GetSafeNs (s.TargetNamespace);
  119. lock (schemaSet) {
  120. if (schemaSet.Contains (ns))
  121. schemaSet.Remove (schemaSet.Get (ns));
  122. schemaSet.Add (s);
  123. }
  124. }
  125. }
  126. public bool Contains (string ns)
  127. {
  128. lock (schemaSet) {
  129. return schemaSet.Contains (ns);
  130. }
  131. }
  132. public bool Contains (XmlSchema schema)
  133. {
  134. lock (schemaSet) {
  135. return schemaSet.Contains (schema);
  136. }
  137. }
  138. public void CopyTo (XmlSchema[] array, int index)
  139. {
  140. lock (schemaSet) {
  141. schemaSet.CopyTo (array, index);
  142. }
  143. }
  144. public XmlSchemaCollectionEnumerator GetEnumerator ()
  145. {
  146. return new XmlSchemaCollectionEnumerator (this);
  147. }
  148. // interface Methods
  149. void ICollection.CopyTo (Array array, int index)
  150. {
  151. lock (schemaSet) {
  152. schemaSet.CopyTo (array, index);
  153. }
  154. }
  155. bool ICollection.IsSynchronized
  156. {
  157. get { return true; } // always
  158. }
  159. IEnumerator IEnumerable.GetEnumerator ()
  160. {
  161. return schemaSet.GetEnumerator ();
  162. }
  163. Object ICollection.SyncRoot
  164. {
  165. get { return this; }
  166. }
  167. // Internal Methods
  168. internal XmlSchemaAttribute FindAttribute (XmlQualifiedName qname)
  169. {
  170. return (XmlSchemaAttribute) schemaSet.GlobalAttributes [qname];
  171. }
  172. internal XmlSchemaElement FindElement (XmlQualifiedName qname)
  173. {
  174. return (XmlSchemaElement) schemaSet.GlobalElements [qname];
  175. }
  176. internal object FindSchemaType (XmlQualifiedName qname)
  177. {
  178. return schemaSet.GlobalTypes [qname];
  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. }