BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[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                         schemaSet.ValidationEventHandler += new ValidationEventHandler (OnValidationError);
60                 }
61
62                 internal XmlSchemaCollection (XmlSchemaSet schemaSet)
63                 {
64                         this.schemaSet = schemaSet;
65                 }
66
67                 //properties
68                 internal XmlSchemaSet SchemaSet {
69                         get { return schemaSet; }
70                 }
71
72                 public int Count {
73                         get { return schemaSet.Count; }
74                 }
75
76                 public XmlNameTable NameTable { 
77                         get { return schemaSet.NameTable; }
78                 }
79
80                 public XmlSchema this [ string ns ] { 
81                         get {
82                                 ICollection col = schemaSet.Schemas (ns);
83                                 if (col == null)
84                                         return null;
85                                 IEnumerator e = col.GetEnumerator ();
86                                 if (e.MoveNext ())
87                                         return (XmlSchema) e.Current;
88                                 else
89                                         return null;
90                         }
91                 }
92
93                 // Events
94                 public event ValidationEventHandler ValidationEventHandler;
95
96                 // Methods
97                 public XmlSchema Add (string ns, XmlReader reader)
98                 {
99                         return Add (ns, reader, new XmlUrlResolver ());
100                 }
101
102 #if NET_1_1
103                 public XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
104 #else
105                 internal XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
106 #endif
107                 {
108                         XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
109                         if (schema.TargetNamespace == null)
110                                 schema.TargetNamespace = ns;
111                         else if (ns != null && schema.TargetNamespace != ns)
112                                 throw new XmlSchemaException ("The actual targetNamespace in the schema does not match the parameter.");
113
114                         return Add (schema);
115                 }
116
117                 public XmlSchema Add (string ns, string uri)
118                 {
119                         XmlReader reader = new XmlTextReader (uri);
120                         try {
121                                 return Add (ns, reader);
122                         } finally {
123                                 reader.Close ();
124                         }
125                 }
126
127                 public XmlSchema Add (XmlSchema schema)
128                 {
129                         return Add (schema, new XmlUrlResolver ());
130                 }
131
132                 public XmlSchema Add (XmlSchema schema, XmlResolver resolver)
133                 {
134                         if (schema == null)
135                                 throw new ArgumentNullException ("schema");
136
137                         XmlSchemaSet xss = new XmlSchemaSet (schemaSet.NameTable);
138                         xss.Add (schemaSet);
139
140                         // FIXME: maybe it requires Reprocess()
141                         xss.Add (schema);
142                         xss.ValidationEventHandler += ValidationEventHandler;
143                         xss.XmlResolver = resolver;
144                         xss.Compile ();
145                         if (!xss.IsCompiled)
146                                 return null;
147                         // It is set only when the compilation was successful.
148                         schemaSet = xss;
149                         return schema;
150                 }
151
152                 public void Add (XmlSchemaCollection schema)
153                 {
154                         if (schema == null)
155                                 throw new ArgumentNullException ("schema");
156
157                         XmlSchemaSet xss = new XmlSchemaSet (schemaSet.NameTable);
158                         xss.Add (schemaSet);
159
160                         // FIXME: maybe it requires Reprocess()
161                         xss.Add (schema.schemaSet);
162                         xss.ValidationEventHandler += ValidationEventHandler;
163                         xss.XmlResolver = schemaSet.XmlResolver;
164                         xss.Compile ();
165                         if (!xss.IsCompiled)
166                                 return;
167                         // It is set only when the compilation was successful.
168                         schemaSet = xss;
169                 }
170
171                 public bool Contains (string ns)
172                 {
173                         lock (schemaSet) {
174                                 return schemaSet.Contains (ns);
175                         }
176                 }
177
178                 public bool Contains (XmlSchema schema)
179                 {
180                         lock (schemaSet) {
181                                 return schemaSet.Contains (schema);
182                         }
183                 }
184
185                 public void CopyTo (XmlSchema[] array, int index)
186                 {
187                         lock (schemaSet) {
188                                 schemaSet.CopyTo (array, index);
189                         }
190                 }
191
192                 public XmlSchemaCollectionEnumerator GetEnumerator ()
193                 {
194                         // The actual collection is schemaSet.Schemas()
195                         return new XmlSchemaCollectionEnumerator(schemaSet.Schemas());
196                 }
197
198                 int ICollection.Count {
199                         get { return Count; }
200                 }
201
202                 // interface Methods
203                 void ICollection.CopyTo (Array array, int index)
204                 {
205                         lock (schemaSet) {
206                                 schemaSet.CopyTo (array, index);
207                         }
208                 }
209
210                 bool ICollection.IsSynchronized
211                 {
212                         get { return true; } // always
213                 }
214
215                 IEnumerator IEnumerable.GetEnumerator ()
216                 {
217                         return this.GetEnumerator ();
218                 }
219
220                 Object ICollection.SyncRoot
221                 {
222                         get { return this; }
223                 }
224
225                 void OnValidationError (object o, ValidationEventArgs e)
226                 {
227                         if (ValidationEventHandler != null)
228                                 ValidationEventHandler (o, e);
229                         else if (e.Severity == XmlSeverityType.Error)
230                                 throw e.Exception;
231                 }
232
233         }
234 }