2005-03-22 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaCollection.cs
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 //
8
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.
28 //
29 using System;
30 using System.Collections;
31 using System.Xml;
32
33
34 namespace System.Xml.Schema
35 {
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 #if NET_2_0
44         [Obsolete ("Use XmlSchemaSet.")]
45 #endif
46         public sealed class XmlSchemaCollection : ICollection, IEnumerable
47         {
48                 //private fields
49                 private XmlSchemaSet schemaSet;
50
51                 public XmlSchemaCollection ()
52                         : this (new NameTable ())
53                 {
54                 }
55
56                 public XmlSchemaCollection (XmlNameTable nameTable)
57                         : this (new XmlSchemaSet (nameTable))
58                 {
59                 }
60
61                 internal XmlSchemaCollection (XmlSchemaSet schemaSet)
62                 {
63                         this.schemaSet = schemaSet;
64                 }
65
66                 //properties
67                 internal XmlSchemaSet SchemaSet {
68                         get { return schemaSet; }
69                 }
70
71                 public int Count {
72                         get { return schemaSet.Count; }
73                 }
74
75                 public XmlNameTable NameTable { 
76                         get { return schemaSet.NameTable; }
77                 }
78
79                 public XmlSchema this [ string ns ] { 
80                         get {
81                                 ICollection col = schemaSet.Schemas (ns);
82                                 if (col == null)
83                                         return null;
84                                 IEnumerator e = col.GetEnumerator ();
85                                 if (e.MoveNext ())
86                                         return (XmlSchema) e.Current;
87                                 else
88                                         return null;
89                         }
90                 }
91
92                 // Events
93                 public event ValidationEventHandler ValidationEventHandler;
94
95                 // Methods
96                 public XmlSchema Add (string ns, XmlReader reader)
97                 {
98                         return Add (ns, reader, new XmlUrlResolver ());
99                 }
100
101 #if NET_1_1
102                 public XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
103 #else
104                 internal XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
105 #endif
106                 {
107                         XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
108                         schema.Compile (ValidationEventHandler, schemaSet, resolver);
109                         lock (schemaSet) {
110                                 return schemaSet.Add (schema);
111                         }
112                 }
113
114                 public XmlSchema Add (string ns, string uri)
115                 {
116                         lock (schemaSet) {
117                                 return schemaSet.Add (ns, uri);
118                         }
119                 }
120
121                 public XmlSchema Add (XmlSchema schema)
122                 {
123                         return Add (schema, new XmlUrlResolver ());
124                 }
125
126                 public XmlSchema Add (XmlSchema schema, XmlResolver resolver)
127                 {
128                         if (schema == null)
129                                 throw new ArgumentNullException ("schema");
130
131                         // XmlSchemaCollection.Add() compiles, while XmlSchemaSet.Add() does not
132                         if (!schema.IsCompiled)
133                                 schema.Compile (ValidationEventHandler, schemaSet, resolver);
134                         // compilation error -> returns null (and don't add to the collection).
135                         if (!schema.IsCompiled)
136                                 return null;
137
138                         lock (schemaSet) {
139                                 // consider imported schemas.
140                                 schemaSet.Add (schema.Schemas);
141                                 return schema;
142                         }
143                 }
144
145                 public void Add (XmlSchemaCollection schema)
146                 {
147                         if (schema == null)
148                                 throw new ArgumentNullException ("schema");
149
150                         lock (schemaSet) {
151                                 schemaSet.Add (schema.schemaSet);
152                         }
153                 }
154
155                 public bool Contains (string ns)
156                 {
157                         lock (schemaSet) {
158                                 return schemaSet.Contains (ns);
159                         }
160                 }
161
162                 public bool Contains (XmlSchema schema)
163                 {
164                         lock (schemaSet) {
165                                 return schemaSet.Contains (schema);
166                         }
167                 }
168
169                 public void CopyTo (XmlSchema[] array, int index)
170                 {
171                         lock (schemaSet) {
172                                 schemaSet.CopyTo (array, index);
173                         }
174                 }
175
176                 public XmlSchemaCollectionEnumerator GetEnumerator ()
177                 {
178                         return new XmlSchemaCollectionEnumerator (this);
179                 }
180
181                 // interface Methods
182                 void ICollection.CopyTo (Array array, int index)
183                 {
184                         lock (schemaSet) {
185                                 schemaSet.CopyTo (array, index);
186                         }
187                 }
188
189                 bool ICollection.IsSynchronized
190                 {
191                         get { return true; } // always
192                 }
193
194                 IEnumerator IEnumerable.GetEnumerator ()
195                 {
196                         return this.GetEnumerator ();
197                 }
198
199                 Object ICollection.SyncRoot
200                 {
201                         get { return this; }
202                 }
203
204                 internal void OnValidationError (object o, ValidationEventArgs e)
205                 {
206                         if (ValidationEventHandler != null)
207                                 ValidationEventHandler (o, e);
208                         else if (e.Severity == XmlSeverityType.Error)
209                                 throw e.Exception;
210                 }
211
212         }
213 }