2004-08-10 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         public sealed class XmlSchemaCollection : ICollection, IEnumerable
44         {
45                 //private fields
46                 private XmlSchemaSet schemaSet;
47
48                 public XmlSchemaCollection ()
49                         : this (new NameTable ())
50                 {
51                 }
52
53                 public XmlSchemaCollection (XmlNameTable nameTable)
54                         : this (new XmlSchemaSet (nameTable))
55                 {
56                         this.schemaSet.SchemaCollection = this;
57                 }
58
59                 internal XmlSchemaCollection (XmlSchemaSet schemaSet)
60                 {
61                         this.schemaSet = schemaSet;
62                 }
63
64                 //properties
65                 internal XmlSchemaSet SchemaSet {
66                         get { return schemaSet; }
67                 }
68
69                 public int Count {
70                         get { return schemaSet.Count; }
71                 }
72
73                 public XmlNameTable NameTable { 
74                         get { return schemaSet.NameTable; }
75                 }
76
77                 public XmlSchema this [ string ns ] { 
78                         get { return schemaSet.Get (ns); }
79                 }
80
81                 // Events
82                 public event ValidationEventHandler ValidationEventHandler;
83
84                 // Methods
85                 public XmlSchema Add (string ns, XmlReader reader)
86                 {
87                         return Add (ns, reader, new XmlUrlResolver ());
88                 }
89
90 #if NET_1_0
91                 internal XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
92 #else
93                 public XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
94 #endif
95                 {
96                         XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
97                         schema.Compile (ValidationEventHandler, this, resolver);
98                         lock (schemaSet) {
99                                 return schemaSet.Add (schema);
100                         }
101                 }
102
103                 public XmlSchema Add (string ns, string uri)
104                 {
105                         lock (schemaSet) {
106                                 return schemaSet.Add (ns, uri);
107                         }
108                 }
109
110                 public XmlSchema Add (XmlSchema schema)
111                 {
112                         return Add (schema, new XmlUrlResolver ());
113                 }
114
115                 public XmlSchema Add (XmlSchema schema, XmlResolver resolver)
116                 {
117                         if (schema == null)
118                                 throw new ArgumentNullException ("schema");
119
120                         // XmlSchemaCollection.Add() compiles, while XmlSchemaSet.Add() does not
121                         if (!schema.IsCompiled)
122                                 schema.Compile (ValidationEventHandler, this, resolver);
123
124                         string ns = GetSafeNs (schema.TargetNamespace);
125                         lock (schemaSet) {
126                                 if (schemaSet.Contains (ns))
127                                         schemaSet.Remove (schemaSet.Get (ns));
128                                 return schemaSet.Add (schema);
129                         }
130                 }
131
132                 private string GetSafeNs (string ns)
133                 {
134                         return ns != null ? ns : String.Empty;
135                 }
136
137                 public void Add (XmlSchemaCollection schema)
138                 {
139                         if (schema == null)
140                                 throw new ArgumentNullException ("schema");
141
142                         /*
143                         foreach (XmlSchema s in schema) {
144                                 string ns = GetSafeNs (s.TargetNamespace);
145                                 lock (schemaSet) {
146                                         if (schemaSet.Contains (ns))
147                                                 schemaSet.Remove (schemaSet.Get (ns));
148                                         schemaSet.Add (s);
149                                 }
150                         }
151                         */
152                         lock (schemaSet) {
153                                 schemaSet.Add (schema.schemaSet);
154                         }
155                 }
156
157                 public bool Contains (string ns)
158                 {
159                         lock (schemaSet) {
160                                 return schemaSet.Contains (ns);
161                         }
162                 }
163
164                 public bool Contains (XmlSchema schema)
165                 {
166                         lock (schemaSet) {
167                                 return schemaSet.Contains (schema);
168                         }
169                 }
170
171                 public void CopyTo (XmlSchema[] array, int index)
172                 {
173                         lock (schemaSet) {
174                                 schemaSet.CopyTo (array, index);
175                         }
176                 }
177
178                 public XmlSchemaCollectionEnumerator GetEnumerator ()
179                 {
180                         return new XmlSchemaCollectionEnumerator (this);
181                 }
182                 
183                 // interface Methods
184                 void ICollection.CopyTo (Array array, int index)
185                 {
186                         lock (schemaSet) {
187                                 schemaSet.CopyTo (array, index);
188                         }
189                 }
190
191                 bool ICollection.IsSynchronized
192                 {
193                         get { return true; } // always
194                 }
195
196                 IEnumerator IEnumerable.GetEnumerator ()
197                 {
198                         return schemaSet.GetEnumerator ();
199                 }
200
201                 Object ICollection.SyncRoot
202                 {
203                         get { return this; }
204                 }
205
206                 // Internal Methods
207                 internal XmlSchemaAttribute FindAttribute (XmlQualifiedName qname)
208                 {
209                         return (XmlSchemaAttribute) schemaSet.GlobalAttributes [qname];
210                 }
211
212                 internal XmlSchemaElement FindElement (XmlQualifiedName qname)
213                 {
214                         return (XmlSchemaElement) schemaSet.GlobalElements [qname];
215                 }
216
217                 internal object FindSchemaType (XmlQualifiedName qname)
218                 {
219                         return schemaSet.GlobalTypes [qname];
220                 }
221
222                 internal void OnValidationError (object o, ValidationEventArgs e)
223                 {
224                         if (ValidationEventHandler != null)
225                                 ValidationEventHandler (o, e);
226                         else if (e.Severity == XmlSeverityType.Error)
227                                 throw e.Exception;
228                 }
229
230         }
231 }