XmlSchemaCollection.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // System.Xml.Schema.XmlSchemaCollection.cs
  3. //
  4. // Authors:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. using System;
  9. using System.Collections;
  10. using System.Xml;
  11. namespace System.Xml.Schema
  12. {
  13. /// <summary>
  14. /// Summary description for XmlSchemaCollection.
  15. /// </summary>
  16. public sealed class XmlSchemaCollection : ICollection, IEnumerable
  17. {
  18. //private fields
  19. private XmlSchemaSet schemaSet;
  20. public XmlSchemaCollection ()
  21. : this (new NameTable ())
  22. {
  23. }
  24. public XmlSchemaCollection (XmlNameTable nameTable)
  25. : this (new XmlSchemaSet (nameTable))
  26. {
  27. this.schemaSet.SchemaCollection = this;
  28. }
  29. internal XmlSchemaCollection (XmlSchemaSet schemaSet)
  30. {
  31. this.schemaSet = schemaSet;
  32. }
  33. //properties
  34. internal XmlSchemaSet SchemaSet {
  35. get { return schemaSet; }
  36. }
  37. public int Count {
  38. get { return schemaSet.Count; }
  39. }
  40. public XmlNameTable NameTable {
  41. get { return schemaSet.NameTable; }
  42. }
  43. public XmlSchema this [ string ns ] {
  44. get { return schemaSet.Get (ns); }
  45. }
  46. // Events
  47. public event ValidationEventHandler ValidationEventHandler;
  48. // Methods
  49. public XmlSchema Add (string ns, XmlReader reader)
  50. {
  51. return Add (ns, reader, new XmlUrlResolver ());
  52. }
  53. #if NET_1_0
  54. internal XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
  55. #else
  56. public XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
  57. #endif
  58. {
  59. XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
  60. schema.Compile (ValidationEventHandler, this, resolver);
  61. return schemaSet.Add (schema);
  62. }
  63. public XmlSchema Add (string ns, string uri)
  64. {
  65. return schemaSet.Add (ns, uri);
  66. }
  67. public XmlSchema Add (XmlSchema schema)
  68. {
  69. return Add (schema, new XmlUrlResolver ());
  70. }
  71. public XmlSchema Add (XmlSchema schema, XmlResolver resolver)
  72. {
  73. if (schema == null)
  74. throw new ArgumentNullException ("schema");
  75. // XmlSchemaCollection.Add() compiles, while XmlSchemaSet.Add() does not
  76. if (!schema.IsCompiled)
  77. schema.Compile (ValidationEventHandler, this, resolver);
  78. string ns = GetSafeNs (schema.TargetNamespace);
  79. if (schemaSet.Contains (ns))
  80. schemaSet.Remove (schemaSet.Get (ns));
  81. return schemaSet.Add (schema);
  82. }
  83. private string GetSafeNs (string ns)
  84. {
  85. return ns != null ? ns : String.Empty;
  86. }
  87. public void Add (XmlSchemaCollection schema)
  88. {
  89. if (schema == null)
  90. throw new ArgumentNullException ("schema");
  91. foreach (XmlSchema s in schema) {
  92. string ns = GetSafeNs (s.TargetNamespace);
  93. if (schemaSet.Contains (ns))
  94. schemaSet.Remove (schemaSet.Get (ns));
  95. schemaSet.Add (s);
  96. }
  97. }
  98. public bool Contains (string ns)
  99. {
  100. return schemaSet.Contains (ns);
  101. }
  102. public bool Contains (XmlSchema schema)
  103. {
  104. return schemaSet.Contains (schema);
  105. }
  106. public void CopyTo (XmlSchema[] array, int index)
  107. {
  108. schemaSet.CopyTo (array, index);
  109. }
  110. public XmlSchemaCollectionEnumerator GetEnumerator ()
  111. {
  112. return new XmlSchemaCollectionEnumerator (this);
  113. }
  114. // interface Methods
  115. void ICollection.CopyTo (Array array, int index)
  116. {
  117. schemaSet.CopyTo (array, index);
  118. }
  119. [MonoTODO]
  120. bool ICollection.IsSynchronized
  121. {
  122. get { throw new NotImplementedException (); }
  123. }
  124. IEnumerator IEnumerable.GetEnumerator ()
  125. {
  126. return schemaSet.GetEnumerator ();
  127. }
  128. [MonoTODO]
  129. Object ICollection.SyncRoot
  130. {
  131. get { throw new NotImplementedException (); }
  132. }
  133. // Internal Methods
  134. internal XmlSchemaAttribute FindAttribute (XmlQualifiedName qname)
  135. {
  136. return (XmlSchemaAttribute) schemaSet.GlobalAttributes [qname];
  137. }
  138. internal XmlSchemaElement FindElement (XmlQualifiedName qname)
  139. {
  140. return (XmlSchemaElement) schemaSet.GlobalElements [qname];
  141. }
  142. internal object FindSchemaType (XmlQualifiedName qname)
  143. {
  144. return schemaSet.GlobalTypes [qname];
  145. }
  146. internal void OnValidationError (object o, ValidationEventArgs e)
  147. {
  148. if (ValidationEventHandler != null)
  149. ValidationEventHandler (o, e);
  150. else if (e.Severity == XmlSeverityType.Error)
  151. throw e.Exception;
  152. }
  153. }
  154. }