XmlSchemaCollection.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. return schemaSet.Add (schema);
  79. }
  80. public void Add (XmlSchemaCollection schema)
  81. {
  82. if (schema == null)
  83. throw new ArgumentNullException ("schema");
  84. foreach (XmlSchema s in schema)
  85. schemaSet.Add (s);
  86. }
  87. public bool Contains (string ns)
  88. {
  89. return schemaSet.Contains (ns);
  90. }
  91. public bool Contains (XmlSchema schema)
  92. {
  93. return schemaSet.Contains (schema);
  94. }
  95. public void CopyTo (XmlSchema[] array, int index)
  96. {
  97. schemaSet.CopyTo (array, index);
  98. }
  99. public XmlSchemaCollectionEnumerator GetEnumerator ()
  100. {
  101. return new XmlSchemaCollectionEnumerator (this);
  102. }
  103. // interface Methods
  104. void ICollection.CopyTo (Array array, int index)
  105. {
  106. schemaSet.CopyTo (array, index);
  107. }
  108. [MonoTODO]
  109. bool ICollection.IsSynchronized
  110. {
  111. get { throw new NotImplementedException (); }
  112. }
  113. IEnumerator IEnumerable.GetEnumerator ()
  114. {
  115. return schemaSet.GetEnumerator ();
  116. }
  117. [MonoTODO]
  118. Object ICollection.SyncRoot
  119. {
  120. get { throw new NotImplementedException (); }
  121. }
  122. // Internal Methods
  123. internal XmlSchemaAttribute FindAttribute (XmlQualifiedName qname)
  124. {
  125. return (XmlSchemaAttribute) schemaSet.GlobalAttributes [qname];
  126. }
  127. internal XmlSchemaElement FindElement (XmlQualifiedName qname)
  128. {
  129. return (XmlSchemaElement) schemaSet.GlobalElements [qname];
  130. }
  131. internal object FindSchemaType (XmlQualifiedName qname)
  132. {
  133. return schemaSet.GlobalTypes [qname];
  134. }
  135. internal void OnValidationError (object o, ValidationEventArgs e)
  136. {
  137. if (ValidationEventHandler != null)
  138. ValidationEventHandler (o, e);
  139. else
  140. throw e.Exception;
  141. }
  142. }
  143. }