Merge pull request #475 from pruiz/xamarin-bug-7408
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / XmlSchemas.cs
1 // 
2 // System.Xml.Serialization.XmlSchemas 
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2002
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections;
32 #if NET_2_0
33 using System.Collections.Generic;
34 #endif
35 using System.Xml.Schema;
36
37 namespace System.Xml.Serialization
38 {
39 #if NET_2_0
40         public class XmlSchemas : CollectionBase, IEnumerable<XmlSchema>
41 #else
42         public class XmlSchemas : CollectionBase
43 #endif
44         {
45
46                 #region Fields
47                 private static string msdataNS = "urn:schemas-microsoft-com:xml-msdata";
48
49                 Hashtable table = new Hashtable ();
50
51                 #endregion
52
53                 #region Constructors
54
55                 public XmlSchemas ()
56                 {
57                 }
58
59                 #endregion // Constructors
60
61                 #region Properties
62
63                 public XmlSchema this [int index] {
64                         get { 
65                                 if (index < 0 || index > Count)
66                                         throw new ArgumentOutOfRangeException ();
67
68                                 return (XmlSchema) List [index]; 
69                         }
70                         set { List [index] = value; }
71                 }
72
73                 public XmlSchema this [string ns] {
74                         get { return (XmlSchema) table[ns!=null?ns:""]; }
75                 }
76                 
77 #if NET_2_0
78                 [MonoTODO]
79                 public bool IsCompiled 
80                 {
81                         get { throw new NotImplementedException (); }
82                 }
83                 
84                 [MonoTODO]
85                 public void Compile (ValidationEventHandler handler, bool fullCompile)
86                 {
87                         foreach (XmlSchema xs in this)
88                                 if (fullCompile || !xs.IsCompiled)
89                                         xs.Compile (handler);
90                 }
91 #endif
92
93
94                 #endregion // Properties
95
96                 #region Methods
97
98                 public int Add (XmlSchema schema)
99                 {
100                         Insert (Count, schema);
101                         return (Count - 1);
102                 }
103
104                 public void Add (XmlSchemas schemas) 
105                 {
106                         foreach (XmlSchema schema in schemas) 
107                                 Add (schema);
108                 }
109
110 #if NET_2_0
111                 [MonoNotSupported("")]
112                 public int Add (XmlSchema schema, Uri baseUri)
113                 {
114                         throw new NotImplementedException ();
115                 }
116
117                 [MonoNotSupported("")]
118                 public void AddReference (XmlSchema schema)
119                 {
120                         throw new NotImplementedException ();
121                 }
122 #endif
123
124                 public bool Contains (XmlSchema schema)
125                 {
126                         return List.Contains (schema);
127                 }
128
129 #if NET_2_0
130                 [MonoNotSupported("")]
131                 public bool Contains (string targetNamespace)
132                 {
133                         throw new NotImplementedException ();
134                 }
135 #endif
136
137                 public void CopyTo (XmlSchema[] array, int index) 
138                 {
139                         List.CopyTo (array, index);
140                 }
141
142                 public object Find (XmlQualifiedName name, Type type)
143                 {
144                         XmlSchema schema = table [name.Namespace] as XmlSchema;
145                         if (schema == null)
146                         {
147                                 // An schema may import other schemas. An imported schema would
148                                 // not be in the table, but its elements (although from another
149                                 // namespace) would be in the schema that imported it. So, we
150                                 // need know to check for every schema in the table.
151                                 
152                                 foreach (XmlSchema s in this)
153                                 {
154                                         object ob = Find (s, name, type);
155                                         if (ob != null) return ob;
156                                 }
157                                 return null;
158                         }
159                         else {
160                                 object fschema = Find (schema, name, type);
161 #if NET_2_0
162                                 if (fschema == null) {
163                                         // still didn't find it
164                                         // (possibly table[name.Namespace] was overwritten in table due to duplicate "" keys),
165                                         // so look in all schemas (for consistiency with MS behaviour)
166                                         foreach (XmlSchema s in this) {
167                                                 object ob = Find (s, name, type);
168                                                 if (ob != null) return ob;
169                                         }
170                                 }
171 #endif
172                                 return fschema;
173                         }
174                 }
175
176                 object Find (XmlSchema schema, XmlQualifiedName name, Type type)
177                 {
178                         if (!schema.IsCompiled) {
179                                 schema.Compile (null);
180                         }
181
182                         XmlSchemaObjectTable tbl = null;
183
184                         if (type == typeof (XmlSchemaSimpleType) || type == typeof (XmlSchemaComplexType))
185                                 tbl = schema.SchemaTypes;
186                         else if (type == typeof (XmlSchemaAttribute))
187                                 tbl = schema.Attributes;
188                         else if (type == typeof (XmlSchemaAttributeGroup))
189                                 tbl = schema.AttributeGroups;
190                         else if (type == typeof (XmlSchemaElement))
191                                 tbl = schema.Elements;
192                         else if (type == typeof (XmlSchemaGroup))
193                                 tbl = schema.Groups;
194                         else if (type == typeof (XmlSchemaNotation))
195                                 tbl = schema.Notations;
196
197                         object res = (tbl != null) ? tbl [name] : null;
198                         if (res != null && res.GetType () != type) return null;
199                         else return res;
200                 }
201
202 #if NET_2_0
203                 [MonoNotSupported("")]
204                 public IList GetSchemas (string ns)
205                 {
206                         throw new NotImplementedException ();
207                 }
208 #endif
209
210                 public int IndexOf (XmlSchema schema)
211                 {
212                         return List.IndexOf (schema);
213                 }
214
215                 public void Insert (int index, XmlSchema schema)
216                 {
217                         List.Insert (index, schema);
218                 }
219
220                 public static bool IsDataSet (XmlSchema schema)
221                 {
222                         XmlSchemaElement el = schema.Items.Count == 1 ?
223                                 schema.Items [0] as XmlSchemaElement : null;
224                         if (el != null && el.UnhandledAttributes != null && el.UnhandledAttributes.Length > 0) {
225                                 for (int i = 0; i < el.UnhandledAttributes.Length; i++) {
226                                         XmlAttribute attr = el.UnhandledAttributes [i];
227                                         if (attr.NamespaceURI == msdataNS && attr.LocalName == "IsDataSet")
228                                                 return (attr.Value.ToLower (System.Globalization.CultureInfo.InvariantCulture) == "true");
229                                 }
230                         }
231                         return false;
232                 }
233
234                 protected override void OnClear ()
235                 {
236                         table.Clear ();
237                 }
238
239                 protected override void OnInsert (int index, object value)
240                 {
241                         string ns = ((XmlSchema) value).TargetNamespace;
242                         if (ns == null) ns = "";
243                         table [ns] = value;
244                 }
245
246                 protected override void OnRemove (int index, object value)
247                 {
248                         table.Remove (value);
249                 }
250
251                 protected override void OnSet (int index, object oldValue, object newValue)
252                 {
253                         string ns = ((XmlSchema) oldValue).TargetNamespace;
254                         if (ns == null) ns = "";
255                         table [ns] = newValue;
256                 }
257         
258                 public void Remove (XmlSchema schema)
259                 {
260                         List.Remove (schema);
261                 }
262
263 #if NET_2_0
264                 IEnumerator<XmlSchema> IEnumerable<XmlSchema>.GetEnumerator ()
265                 {
266                         return new XmlSchemaEnumerator (this);
267                 }
268 #endif
269
270                 #endregion // Methods
271         }
272 }