2 // System.Xml.Schema.XmlSchemaCollection.cs
\r
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
\r
6 // Atsushi Enomoto ginga@kit.hi-ho.ne.jp
\r
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;
\r
34 namespace System.Xml.Schema
\r
37 /// Summary description for XmlSchemaCollection.
\r
39 public sealed class XmlSchemaCollection : ICollection, IEnumerable
\r
42 private XmlSchemaSet schemaSet;
\r
44 public XmlSchemaCollection ()
\r
45 : this (new NameTable ())
\r
49 public XmlSchemaCollection (XmlNameTable nameTable)
\r
50 : this (new XmlSchemaSet (nameTable))
\r
52 this.schemaSet.SchemaCollection = this;
\r
55 internal XmlSchemaCollection (XmlSchemaSet schemaSet)
\r
57 this.schemaSet = schemaSet;
\r
61 internal XmlSchemaSet SchemaSet {
\r
62 get { return schemaSet; }
\r
66 get { return schemaSet.Count; }
\r
69 public XmlNameTable NameTable {
\r
70 get { return schemaSet.NameTable; }
\r
73 public XmlSchema this [ string ns ] {
\r
74 get { return schemaSet.Get (ns); }
\r
78 public event ValidationEventHandler ValidationEventHandler;
\r
81 public XmlSchema Add (string ns, XmlReader reader)
\r
83 return Add (ns, reader, new XmlUrlResolver ());
\r
87 internal XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
\r
89 public XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
\r
92 XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
\r
93 schema.Compile (ValidationEventHandler, this, resolver);
\r
95 return schemaSet.Add (schema);
\r
99 public XmlSchema Add (string ns, string uri)
\r
102 return schemaSet.Add (ns, uri);
\r
106 public XmlSchema Add (XmlSchema schema)
\r
108 return Add (schema, new XmlUrlResolver ());
\r
111 public XmlSchema Add (XmlSchema schema, XmlResolver resolver)
\r
113 if (schema == null)
\r
114 throw new ArgumentNullException ("schema");
\r
116 // XmlSchemaCollection.Add() compiles, while XmlSchemaSet.Add() does not
\r
117 if (!schema.IsCompiled)
\r
118 schema.Compile (ValidationEventHandler, this, resolver);
\r
120 string ns = GetSafeNs (schema.TargetNamespace);
\r
122 if (schemaSet.Contains (ns))
\r
123 schemaSet.Remove (schemaSet.Get (ns));
\r
124 return schemaSet.Add (schema);
\r
128 private string GetSafeNs (string ns)
\r
130 return ns != null ? ns : String.Empty;
\r
133 public void Add (XmlSchemaCollection schema)
\r
135 if (schema == null)
\r
136 throw new ArgumentNullException ("schema");
\r
138 foreach (XmlSchema s in schema) {
\r
139 string ns = GetSafeNs (s.TargetNamespace);
\r
141 if (schemaSet.Contains (ns))
\r
142 schemaSet.Remove (schemaSet.Get (ns));
\r
148 public bool Contains (string ns)
\r
151 return schemaSet.Contains (ns);
\r
155 public bool Contains (XmlSchema schema)
\r
158 return schemaSet.Contains (schema);
\r
162 public void CopyTo (XmlSchema[] array, int index)
\r
165 schemaSet.CopyTo (array, index);
\r
169 public XmlSchemaCollectionEnumerator GetEnumerator ()
\r
171 return new XmlSchemaCollectionEnumerator (this);
\r
174 // interface Methods
\r
175 void ICollection.CopyTo (Array array, int index)
\r
178 schemaSet.CopyTo (array, index);
\r
182 bool ICollection.IsSynchronized
\r
184 get { return true; } // always
\r
187 IEnumerator IEnumerable.GetEnumerator ()
\r
189 return schemaSet.GetEnumerator ();
\r
192 Object ICollection.SyncRoot
\r
194 get { return this; }
\r
197 // Internal Methods
\r
198 internal XmlSchemaAttribute FindAttribute (XmlQualifiedName qname)
\r
200 return (XmlSchemaAttribute) schemaSet.GlobalAttributes [qname];
\r
203 internal XmlSchemaElement FindElement (XmlQualifiedName qname)
\r
205 return (XmlSchemaElement) schemaSet.GlobalElements [qname];
\r
208 internal object FindSchemaType (XmlQualifiedName qname)
\r
210 return schemaSet.GlobalTypes [qname];
\r
213 internal void OnValidationError (object o, ValidationEventArgs e)
215 if (ValidationEventHandler != null)
216 ValidationEventHandler (o, e);
217 else if (e.Severity == XmlSeverityType.Error)