* Mono.Posix.dll.sources: Rename Mono.Posix to Mono.Unix.
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / XmlSchemas.cs
1 // \r
2 // System.Xml.Serialization.XmlSchemas \r
3 //\r
4 // Author:\r
5 //   Tim Coleman (tim@timcoleman.com)\r
6 //\r
7 // Copyright (C) Tim Coleman, 2002\r
8 //\r
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 \r
31 using System.Collections;\r
32 using System.Xml.Schema;\r
33 \r
34 namespace System.Xml.Serialization {\r
35         public class XmlSchemas : CollectionBase {\r
36 \r
37                 #region Fields\r
38                 private static string msdataNS = "urn:schemas-microsoft-com:xml-msdata";\r
39 \r
40                 Hashtable table = new Hashtable ();\r
41 \r
42                 #endregion\r
43 \r
44                 #region Constructors\r
45 \r
46                 public XmlSchemas ()\r
47                 {\r
48                 }\r
49 \r
50                 #endregion // Constructors\r
51 \r
52                 #region Properties\r
53 \r
54                 public XmlSchema this [int index] {\r
55                         get { \r
56                                 if (index < 0 || index > Count)\r
57                                         throw new ArgumentOutOfRangeException ();\r
58 \r
59                                 return (XmlSchema) List [index]; \r
60                         }\r
61                         set { List [index] = value; }\r
62                 }\r
63 \r
64                 public XmlSchema this [string ns] {\r
65                         get { return (XmlSchema) table[ns!=null?ns:""]; }\r
66                 }\r
67                 \r
68 #if NET_2_0\r
69                 [MonoTODO]\r
70                 public bool IsCompiled \r
71                 {\r
72                         get { throw new NotImplementedException (); }\r
73                 }\r
74                 \r
75                 [MonoTODO]\r
76                 public ICollection Schemas \r
77                 {\r
78                         get { throw new NotImplementedException (); }\r
79                 }\r
80                 \r
81                 [MonoTODO]\r
82                 public void Compile (ValidationEventHandler handler, bool fullCompile)\r
83                 {\r
84                 }\r
85 #endif\r
86 \r
87 \r
88                 #endregion // Properties\r
89 \r
90                 #region Methods\r
91 \r
92                 public int Add (XmlSchema schema)\r
93                 {\r
94                         Insert (Count, schema);\r
95                         return (Count - 1);\r
96                 }\r
97 \r
98                 public void Add (XmlSchemas schemas) \r
99                 {\r
100                         foreach (XmlSchema schema in schemas) \r
101                                 Add (schema);\r
102                 }\r
103 \r
104                 public bool Contains (XmlSchema schema)\r
105                 {\r
106                         return List.Contains (schema);\r
107                 }\r
108 \r
109                 public void CopyTo (XmlSchema[] array, int index) \r
110                 {\r
111                         List.CopyTo (array, index);\r
112                 }\r
113 \r
114                 public object Find (XmlQualifiedName name, Type type)\r
115                 {\r
116                         XmlSchema schema = table [name.Namespace] as XmlSchema;\r
117                         if (schema == null)\r
118                         {\r
119                                 // An schema may import other schemas. An imported schema would\r
120                                 // not be in the table, but its elements (although from another\r
121                                 // namespace) would be in the schema that imported it. So, we\r
122                                 // need know to check for every schema in the table.\r
123                                 \r
124                                 foreach (XmlSchema s in this)\r
125                                 {\r
126                                         object ob = Find (s, name, type);\r
127                                         if (ob != null) return ob;\r
128                                 }\r
129                                 return null;\r
130                         }\r
131                         else\r
132                                 return Find (schema, name, type);\r
133                 }\r
134 \r
135                 object Find (XmlSchema schema, XmlQualifiedName name, Type type)\r
136                 {\r
137                         if (!schema.IsCompiled) {\r
138                                 schema.Compile (null);\r
139                         }\r
140 \r
141                         XmlSchemaObjectTable tbl = null;\r
142 \r
143                         if (type == typeof (XmlSchemaSimpleType) || type == typeof (XmlSchemaComplexType))\r
144                                 tbl = schema.SchemaTypes;\r
145                         else if (type == typeof (XmlSchemaAttribute))\r
146                                 tbl = schema.Attributes;\r
147                         else if (type == typeof (XmlSchemaAttributeGroup))\r
148                                 tbl = schema.AttributeGroups;\r
149                         else if (type == typeof (XmlSchemaElement))\r
150                                 tbl = schema.Elements;\r
151                         else if (type == typeof (XmlSchemaGroup))\r
152                                 tbl = schema.Groups;\r
153                         else if (type == typeof (XmlSchemaNotation))\r
154                                 tbl = schema.Notations;\r
155 \r
156                         object res = (tbl != null) ? tbl [name] : null;\r
157                         if (res != null && res.GetType () != type) return null;\r
158                         else return res;\r
159                 }\r
160 \r
161                 public int IndexOf (XmlSchema schema)\r
162                 {\r
163                         return List.IndexOf (schema);\r
164                 }\r
165 \r
166                 public void Insert (int index, XmlSchema schema)\r
167                 {\r
168                         List.Insert (index, schema);\r
169                 }\r
170 \r
171                 public static bool IsDataSet (XmlSchema schema)\r
172                 {\r
173                         XmlSchemaElement el = schema.Items.Count == 1 ?\r
174                                 schema.Items [0] as XmlSchemaElement : null;\r
175                         if (el != null && el.UnhandledAttributes.Length > 0) {\r
176                                 for (int i = 0; i < el.UnhandledAttributes.Length; i++) {\r
177                                         XmlAttribute attr = el.UnhandledAttributes [i];\r
178                                         if (attr.NamespaceURI == msdataNS && attr.LocalName == "IsDataSet")\r
179                                                 return (attr.Value.ToLower (System.Globalization.CultureInfo.InvariantCulture) == "true");\r
180                                 }\r
181                         }\r
182                         return false;\r
183                 }\r
184 \r
185                 protected override void OnClear ()\r
186                 {\r
187                         table.Clear ();\r
188                 }\r
189 \r
190                 protected override void OnInsert (int index, object value)\r
191                 {\r
192                         string ns = ((XmlSchema) value).TargetNamespace;\r
193                         if (ns == null) ns = "";\r
194                         table [ns] = value;\r
195                 }\r
196 \r
197                 protected override void OnRemove (int index, object value)\r
198                 {\r
199                         table.Remove (value);\r
200                 }\r
201 \r
202                 protected override void OnSet (int index, object oldValue, object newValue)\r
203                 {\r
204                         string ns = ((XmlSchema) oldValue).TargetNamespace;\r
205                         if (ns == null) ns = "";\r
206                         table [ns] = newValue;\r
207                 }\r
208         \r
209                 public void Remove (XmlSchema schema)\r
210                 {\r
211                         List.Remove (schema);\r
212                 }\r
213 \r
214                 #endregion // Methods\r
215         }\r
216 }