**** Merged from MCS ****
[mono-project.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaCollection.cs
blobef73268ebebbebad2616bd34a14aa9ac15cf5fba
1 //
2 // System.Xml.Schema.XmlSchemaCollection.cs
3 //
4 // Authors:
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
6 // Atsushi Enomoto ginga@kit.hi-ho.ne.jp
7 //
9 //
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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.
29 using System;
30 using System.Collections;
31 using System.Xml;
34 namespace System.Xml.Schema
36 /// <summary>
37 /// Summary description for XmlSchemaCollection.
38 ///
39 /// It is just a wrapper for XmlSchemaSet (unlike MS.NET, our
40 /// XmlSchemaCollection is originally designed to be conformant to
41 /// W3C specification).
42 /// </summary>
43 public sealed class XmlSchemaCollection : ICollection, IEnumerable
45 //private fields
46 private XmlSchemaSet schemaSet;
48 public XmlSchemaCollection ()
49 : this (new NameTable ())
53 public XmlSchemaCollection (XmlNameTable nameTable)
54 : this (new XmlSchemaSet (nameTable))
58 internal XmlSchemaCollection (XmlSchemaSet schemaSet)
60 this.schemaSet = schemaSet;
63 //properties
64 internal XmlSchemaSet SchemaSet {
65 get { return schemaSet; }
68 public int Count {
69 get { return schemaSet.Count; }
72 public XmlNameTable NameTable {
73 get { return schemaSet.NameTable; }
76 public XmlSchema this [ string ns ] {
77 get {
78 ICollection col = schemaSet.Schemas (ns);
79 if (col == null)
80 return null;
81 IEnumerator e = col.GetEnumerator ();
82 if (e.MoveNext ())
83 return (XmlSchema) e.Current;
84 else
85 return null;
89 // Events
90 public event ValidationEventHandler ValidationEventHandler;
92 // Methods
93 public XmlSchema Add (string ns, XmlReader reader)
95 return Add (ns, reader, new XmlUrlResolver ());
98 #if NET_1_1
99 public XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
100 #else
101 internal XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
102 #endif
104 XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
105 schema.Compile (ValidationEventHandler, schemaSet, resolver);
106 lock (schemaSet) {
107 return schemaSet.Add (schema);
111 public XmlSchema Add (string ns, string uri)
113 lock (schemaSet) {
114 return schemaSet.Add (ns, uri);
118 public XmlSchema Add (XmlSchema schema)
120 return Add (schema, new XmlUrlResolver ());
123 public XmlSchema Add (XmlSchema schema, XmlResolver resolver)
125 if (schema == null)
126 throw new ArgumentNullException ("schema");
128 // XmlSchemaCollection.Add() compiles, while XmlSchemaSet.Add() does not
129 if (!schema.IsCompiled)
130 schema.Compile (ValidationEventHandler, schemaSet, resolver);
131 // compilation error -> returns null (and don't add to the collection).
132 if (!schema.IsCompiled)
133 return null;
135 string ns = GetSafeNs (schema.TargetNamespace);
136 lock (schemaSet) {
137 // consider imported schemas.
138 schemaSet.Add (schema.Schemas);
139 return schema;
143 private string GetSafeNs (string ns)
145 return ns != null ? ns : String.Empty;
148 public void Add (XmlSchemaCollection schema)
150 if (schema == null)
151 throw new ArgumentNullException ("schema");
153 lock (schemaSet) {
154 schemaSet.Add (schema.schemaSet);
158 public bool Contains (string ns)
160 lock (schemaSet) {
161 return schemaSet.Contains (ns);
165 public bool Contains (XmlSchema schema)
167 lock (schemaSet) {
168 return schemaSet.Contains (schema);
172 public void CopyTo (XmlSchema[] array, int index)
174 lock (schemaSet) {
175 schemaSet.CopyTo (array, index);
179 public XmlSchemaCollectionEnumerator GetEnumerator ()
181 return new XmlSchemaCollectionEnumerator (this);
184 // interface Methods
185 void ICollection.CopyTo (Array array, int index)
187 lock (schemaSet) {
188 schemaSet.CopyTo (array, index);
192 bool ICollection.IsSynchronized
194 get { return true; } // always
197 IEnumerator IEnumerable.GetEnumerator ()
199 return this.GetEnumerator ();
202 Object ICollection.SyncRoot
204 get { return this; }
207 internal void OnValidationError (object o, ValidationEventArgs e)
209 if (ValidationEventHandler != null)
210 ValidationEventHandler (o, e);
211 else if (e.Severity == XmlSeverityType.Error)
212 throw e.Exception;