Merge pull request #439 from mono-soc-2012/garyb/iconfix
[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                 public XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
103                 {
104                         XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
105                         if (schema.TargetNamespace == null)
106                                 schema.TargetNamespace = ns;
107                         else if (ns != null && schema.TargetNamespace != ns)
108                                 throw new XmlSchemaException ("The actual targetNamespace in the schema does not match the parameter.");
109
110                         return Add (schema);
111                 }
112
113                 public XmlSchema Add (string ns, string uri)
114                 {
115                         XmlReader reader = new XmlTextReader (uri);
116                         try {
117                                 return Add (ns, reader);
118                         } finally {
119                                 reader.Close ();
120                         }
121                 }
122
123                 public XmlSchema Add (XmlSchema schema)
124                 {
125                         return Add (schema, new XmlUrlResolver ());
126                 }
127
128                 public XmlSchema Add (XmlSchema schema, XmlResolver resolver)
129                 {
130                         if (schema == null)
131                                 throw new ArgumentNullException ("schema");
132
133                         XmlSchemaSet xss = new XmlSchemaSet (schemaSet.NameTable);
134                         xss.Add (schemaSet);
135
136                         // FIXME: maybe it requires Reprocess()
137                         xss.Add (schema);
138                         xss.ValidationEventHandler += ValidationEventHandler;
139                         xss.XmlResolver = resolver;
140                         xss.Compile ();
141                         if (!xss.IsCompiled)
142                                 return null;
143                         // It is set only when the compilation was successful.
144                         schemaSet = xss;
145                         return schema;
146                 }
147
148                 public void Add (XmlSchemaCollection schema)
149                 {
150                         if (schema == null)
151                                 throw new ArgumentNullException ("schema");
152
153                         XmlSchemaSet xss = new XmlSchemaSet (schemaSet.NameTable);
154                         xss.Add (schemaSet);
155
156                         // FIXME: maybe it requires Reprocess()
157                         xss.Add (schema.schemaSet);
158                         xss.ValidationEventHandler += ValidationEventHandler;
159                         xss.XmlResolver = schemaSet.XmlResolver;
160                         xss.Compile ();
161                         if (!xss.IsCompiled)
162                                 return;
163                         // It is set only when the compilation was successful.
164                         schemaSet = xss;
165                 }
166
167                 public bool Contains (string ns)
168                 {
169                         lock (schemaSet) {
170                                 return schemaSet.Contains (ns);
171                         }
172                 }
173
174                 public bool Contains (XmlSchema schema)
175                 {
176                         lock (schemaSet) {
177                                 return schemaSet.Contains (schema);
178                         }
179                 }
180
181                 public void CopyTo (XmlSchema[] array, int index)
182                 {
183                         lock (schemaSet) {
184                                 schemaSet.CopyTo (array, index);
185                         }
186                 }
187
188                 public XmlSchemaCollectionEnumerator GetEnumerator ()
189                 {
190                         // The actual collection is schemaSet.Schemas()
191                         return new XmlSchemaCollectionEnumerator(schemaSet.Schemas());
192                 }
193
194                 int ICollection.Count {
195                         get { return Count; }
196                 }
197
198                 // interface Methods
199                 void ICollection.CopyTo (Array array, int index)
200                 {
201                         lock (schemaSet) {
202                                 schemaSet.CopyTo (array, index);
203                         }
204                 }
205
206                 bool ICollection.IsSynchronized
207                 {
208                         get { return true; } // always
209                 }
210
211                 IEnumerator IEnumerable.GetEnumerator ()
212                 {
213                         return this.GetEnumerator ();
214                 }
215
216                 Object ICollection.SyncRoot
217                 {
218                         get { return this; }
219                 }
220
221                 void OnValidationError (object o, ValidationEventArgs e)
222                 {
223                         if (ValidationEventHandler != null)
224                                 ValidationEventHandler (o, e);
225                         else if (e.Severity == XmlSeverityType.Error)
226                                 throw e.Exception;
227                 }
228
229         }
230 }