2 // System.Xml.Schema.XmlSchemaCollection.cs
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
6 // Atsushi Enomoto ginga@kit.hi-ho.ne.jp
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
34 namespace System.Xml.Schema
37 /// Summary description for XmlSchemaCollection.
39 /// It is just a wrapper for XmlSchemaSet (unlike MS.NET, our
40 /// XmlSchemaCollection is originally designed to be conformant to
41 /// W3C specification).
43 public sealed class XmlSchemaCollection : ICollection, IEnumerable
46 private XmlSchemaSet schemaSet;
48 public XmlSchemaCollection ()
49 : this (new NameTable ())
53 public XmlSchemaCollection (XmlNameTable nameTable)
54 : this (new XmlSchemaSet (nameTable))
56 this.schemaSet.SchemaCollection = this;
59 internal XmlSchemaCollection (XmlSchemaSet schemaSet)
61 this.schemaSet = schemaSet;
65 internal XmlSchemaSet SchemaSet {
66 get { return schemaSet; }
70 get { return schemaSet.Count; }
73 public XmlNameTable NameTable {
74 get { return schemaSet.NameTable; }
77 public XmlSchema this [ string ns ] {
78 get { return schemaSet.Get (ns); }
82 public event ValidationEventHandler ValidationEventHandler;
85 public XmlSchema Add (string ns, XmlReader reader)
87 return Add (ns, reader, new XmlUrlResolver ());
91 internal XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
93 public XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
96 XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
97 schema.Compile (ValidationEventHandler, this, resolver);
99 return schemaSet.Add (schema);
103 public XmlSchema Add (string ns, string uri)
106 return schemaSet.Add (ns, uri);
110 public XmlSchema Add (XmlSchema schema)
112 return Add (schema, new XmlUrlResolver ());
115 public XmlSchema Add (XmlSchema schema, XmlResolver resolver)
118 throw new ArgumentNullException ("schema");
120 // XmlSchemaCollection.Add() compiles, while XmlSchemaSet.Add() does not
121 if (!schema.IsCompiled)
122 schema.Compile (ValidationEventHandler, this, resolver);
124 string ns = GetSafeNs (schema.TargetNamespace);
126 if (schemaSet.Contains (ns))
127 schemaSet.Remove (schemaSet.Get (ns));
128 return schemaSet.Add (schema);
132 private string GetSafeNs (string ns)
134 return ns != null ? ns : String.Empty;
137 public void Add (XmlSchemaCollection schema)
140 throw new ArgumentNullException ("schema");
143 foreach (XmlSchema s in schema) {
144 string ns = GetSafeNs (s.TargetNamespace);
146 if (schemaSet.Contains (ns))
147 schemaSet.Remove (schemaSet.Get (ns));
153 schemaSet.Add (schema.schemaSet);
157 public bool Contains (string ns)
160 return schemaSet.Contains (ns);
164 public bool Contains (XmlSchema schema)
167 return schemaSet.Contains (schema);
171 public void CopyTo (XmlSchema[] array, int index)
174 schemaSet.CopyTo (array, index);
178 public XmlSchemaCollectionEnumerator GetEnumerator ()
180 return new XmlSchemaCollectionEnumerator (this);
184 void ICollection.CopyTo (Array array, int index)
187 schemaSet.CopyTo (array, index);
191 bool ICollection.IsSynchronized
193 get { return true; } // always
196 IEnumerator IEnumerable.GetEnumerator ()
198 return schemaSet.GetEnumerator ();
201 Object ICollection.SyncRoot
207 internal XmlSchemaAttribute FindAttribute (XmlQualifiedName qname)
209 return (XmlSchemaAttribute) schemaSet.GlobalAttributes [qname];
212 internal XmlSchemaElement FindElement (XmlQualifiedName qname)
214 return (XmlSchemaElement) schemaSet.GlobalElements [qname];
217 internal object FindSchemaType (XmlQualifiedName qname)
219 return schemaSet.GlobalTypes [qname];
222 internal void OnValidationError (object o, ValidationEventArgs e)
224 if (ValidationEventHandler != null)
225 ValidationEventHandler (o, e);
226 else if (e.Severity == XmlSeverityType.Error)