This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaCollection.cs
1 //\r
2 // System.Xml.Schema.XmlSchemaCollection.cs\r
3 //\r
4 // Authors:\r
5 //      Dwivedi, Ajay kumar  Adwiv@Yahoo.com\r
6 //      Atsushi Enomoto      ginga@kit.hi-ho.ne.jp\r
7 //\r
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;\r
30 using System.Collections;\r
31 using System.Xml;\r
32 \r
33 \r
34 namespace System.Xml.Schema\r
35 {\r
36         /// <summary>\r
37         /// Summary description for XmlSchemaCollection.\r
38         /// </summary>\r
39         public sealed class XmlSchemaCollection : ICollection, IEnumerable\r
40         {\r
41                 //private fields\r
42                 private XmlSchemaSet schemaSet;\r
43 \r
44                 public XmlSchemaCollection ()\r
45                         : this (new NameTable ())\r
46                 {\r
47                 }\r
48 \r
49                 public XmlSchemaCollection (XmlNameTable nameTable)\r
50                         : this (new XmlSchemaSet (nameTable))\r
51                 {\r
52                         this.schemaSet.SchemaCollection = this;\r
53                 }\r
54 \r
55                 internal XmlSchemaCollection (XmlSchemaSet schemaSet)\r
56                 {\r
57                         this.schemaSet = schemaSet;\r
58                 }\r
59 \r
60                 //properties\r
61                 internal XmlSchemaSet SchemaSet {\r
62                         get { return schemaSet; }\r
63                 }\r
64 \r
65                 public int Count {\r
66                         get { return schemaSet.Count; }\r
67                 }\r
68 \r
69                 public XmlNameTable NameTable { \r
70                         get { return schemaSet.NameTable; }\r
71                 }\r
72 \r
73                 public XmlSchema this [ string ns ] { \r
74                         get { return schemaSet.Get (ns); }\r
75                 }\r
76 \r
77                 // Events\r
78                 public event ValidationEventHandler ValidationEventHandler;\r
79 \r
80                 // Methods\r
81                 public XmlSchema Add (string ns, XmlReader reader)\r
82                 {\r
83                         return Add (ns, reader, new XmlUrlResolver ());\r
84                 }\r
85 \r
86 #if NET_1_0\r
87                 internal XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)\r
88 #else\r
89                 public XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)\r
90 #endif\r
91                 {\r
92                         XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);\r
93                         schema.Compile (ValidationEventHandler, this, resolver);\r
94                         lock (schemaSet) {\r
95                                 return schemaSet.Add (schema);\r
96                         }\r
97                 }\r
98 \r
99                 public XmlSchema Add (string ns, string uri)\r
100                 {\r
101                         lock (schemaSet) {\r
102                                 return schemaSet.Add (ns, uri);\r
103                         }\r
104                 }\r
105 \r
106                 public XmlSchema Add (XmlSchema schema)\r
107                 {\r
108                         return Add (schema, new XmlUrlResolver ());\r
109                 }\r
110 \r
111                 public XmlSchema Add (XmlSchema schema, XmlResolver resolver)\r
112                 {\r
113                         if (schema == null)\r
114                                 throw new ArgumentNullException ("schema");\r
115 \r
116                         // XmlSchemaCollection.Add() compiles, while XmlSchemaSet.Add() does not\r
117                         if (!schema.IsCompiled)\r
118                                 schema.Compile (ValidationEventHandler, this, resolver);\r
119 \r
120                         string ns = GetSafeNs (schema.TargetNamespace);\r
121                         lock (schemaSet) {\r
122                                 if (schemaSet.Contains (ns))\r
123                                         schemaSet.Remove (schemaSet.Get (ns));\r
124                                 return schemaSet.Add (schema);\r
125                         }\r
126                 }\r
127 \r
128                 private string GetSafeNs (string ns)\r
129                 {\r
130                         return ns != null ? ns : String.Empty;\r
131                 }\r
132 \r
133                 public void Add (XmlSchemaCollection schema)\r
134                 {\r
135                         if (schema == null)\r
136                                 throw new ArgumentNullException ("schema");\r
137 \r
138                         foreach (XmlSchema s in schema) {\r
139                                 string ns = GetSafeNs (s.TargetNamespace);\r
140                                 lock (schemaSet) {\r
141                                         if (schemaSet.Contains (ns))\r
142                                                 schemaSet.Remove (schemaSet.Get (ns));\r
143                                         schemaSet.Add (s);\r
144                                 }\r
145                         }\r
146                 }\r
147 \r
148                 public bool Contains (string ns)\r
149                 {\r
150                         lock (schemaSet) {\r
151                                 return schemaSet.Contains (ns);\r
152                         }\r
153                 }\r
154 \r
155                 public bool Contains (XmlSchema schema)\r
156                 {\r
157                         lock (schemaSet) {\r
158                                 return schemaSet.Contains (schema);\r
159                         }\r
160                 }\r
161 \r
162                 public void CopyTo (XmlSchema[] array, int index)\r
163                 {\r
164                         lock (schemaSet) {\r
165                                 schemaSet.CopyTo (array, index);\r
166                         }\r
167                 }\r
168 \r
169                 public XmlSchemaCollectionEnumerator GetEnumerator ()\r
170                 {\r
171                         return new XmlSchemaCollectionEnumerator (this);\r
172                 }\r
173                 \r
174                 // interface Methods\r
175                 void ICollection.CopyTo (Array array, int index)\r
176                 {\r
177                         lock (schemaSet) {\r
178                                 schemaSet.CopyTo (array, index);\r
179                         }\r
180                 }\r
181 \r
182                 bool ICollection.IsSynchronized\r
183                 {\r
184                         get { return true; } // always\r
185                 }\r
186 \r
187                 IEnumerator IEnumerable.GetEnumerator ()\r
188                 {\r
189                         return schemaSet.GetEnumerator ();\r
190                 }\r
191 \r
192                 Object ICollection.SyncRoot\r
193                 {\r
194                         get { return this; }\r
195                 }\r
196 \r
197                 // Internal Methods\r
198                 internal XmlSchemaAttribute FindAttribute (XmlQualifiedName qname)\r
199                 {\r
200                         return (XmlSchemaAttribute) schemaSet.GlobalAttributes [qname];\r
201                 }\r
202 \r
203                 internal XmlSchemaElement FindElement (XmlQualifiedName qname)\r
204                 {\r
205                         return (XmlSchemaElement) schemaSet.GlobalElements [qname];\r
206                 }\r
207 \r
208                 internal object FindSchemaType (XmlQualifiedName qname)\r
209                 {\r
210                         return schemaSet.GlobalTypes [qname];\r
211                 }\r
212 \r
213                 internal void OnValidationError (object o, ValidationEventArgs e)
214                 {
215                         if (ValidationEventHandler != null)
216                                 ValidationEventHandler (o, e);
217                         else if (e.Severity == XmlSeverityType.Error)
218                                 throw e.Exception;
219                 }
220
221         }\r
222 }\r