This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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                 #endregion // Properties\r
69 \r
70                 #region Methods\r
71 \r
72                 public int Add (XmlSchema schema)\r
73                 {\r
74                         Insert (Count, schema);\r
75                         return (Count - 1);\r
76                 }\r
77 \r
78                 public void Add (XmlSchemas schemas) \r
79                 {\r
80                         foreach (XmlSchema schema in schemas) \r
81                                 Add (schema);\r
82                 }\r
83 \r
84                 public bool Contains (XmlSchema schema)\r
85                 {\r
86                         return List.Contains (schema);\r
87                 }\r
88 \r
89                 public void CopyTo (XmlSchema[] array, int index) \r
90                 {\r
91                         List.CopyTo (array, index);\r
92                 }\r
93 \r
94                 public object Find (XmlQualifiedName name, Type type)\r
95                 {\r
96                         XmlSchema schema = table [name.Namespace] as XmlSchema;\r
97                         if (schema == null)\r
98                         {\r
99                                 // An schema may import other schemas. An imported schema would\r
100                                 // not be in the table, but its elements (although from another\r
101                                 // namespace) would be in the schema that imported it. So, we\r
102                                 // need know to check for every schema in the table.\r
103                                 \r
104                                 foreach (XmlSchema s in this)\r
105                                 {\r
106                                         object ob = Find (s, name, type);\r
107                                         if (ob != null) return ob;\r
108                                 }\r
109                                 return null;\r
110                         }\r
111                         else\r
112                                 return Find (schema, name, type);\r
113                 }\r
114 \r
115                 object Find (XmlSchema schema, XmlQualifiedName name, Type type)\r
116                 {\r
117                         if (!schema.IsCompiled) {\r
118                                 schema.Compile (null);\r
119                         }\r
120 \r
121                         XmlSchemaObjectTable tbl = null;\r
122 \r
123                         if (type == typeof (XmlSchemaSimpleType) || type == typeof (XmlSchemaComplexType))\r
124                                 tbl = schema.SchemaTypes;\r
125                         else if (type == typeof (XmlSchemaAttribute))\r
126                                 tbl = schema.Attributes;\r
127                         else if (type == typeof (XmlSchemaAttributeGroup))\r
128                                 tbl = schema.AttributeGroups;\r
129                         else if (type == typeof (XmlSchemaElement))\r
130                                 tbl = schema.Elements;\r
131                         else if (type == typeof (XmlSchemaGroup))\r
132                                 tbl = schema.Groups;\r
133                         else if (type == typeof (XmlSchemaNotation))\r
134                                 tbl = schema.Notations;\r
135 \r
136                         object res = (tbl != null) ? tbl [name] : null;\r
137                         if (res != null && res.GetType () != type) return null;\r
138                         else return res;\r
139                 }\r
140 \r
141                 public int IndexOf (XmlSchema schema)\r
142                 {\r
143                         return List.IndexOf (schema);\r
144                 }\r
145 \r
146                 public void Insert (int index, XmlSchema schema)\r
147                 {\r
148                         List.Insert (index, schema);\r
149                 }\r
150 \r
151                 public static bool IsDataSet (XmlSchema schema)\r
152                 {\r
153                         XmlSchemaElement el = schema.Items.Count == 1 ?\r
154                                 schema.Items [0] as XmlSchemaElement : null;\r
155                         if (el != null && el.UnhandledAttributes.Length > 0) {\r
156                                 for (int i = 0; i < el.UnhandledAttributes.Length; i++) {\r
157                                         XmlAttribute attr = el.UnhandledAttributes [i];\r
158                                         if (attr.NamespaceURI == msdataNS && attr.LocalName == "IsDataSet")\r
159                                                 return (attr.Value.ToLower (System.Globalization.CultureInfo.InvariantCulture) == "true");\r
160                                 }\r
161                         }\r
162                         return false;\r
163                 }\r
164 \r
165                 protected override void OnClear ()\r
166                 {\r
167                         table.Clear ();\r
168                 }\r
169 \r
170                 protected override void OnInsert (int index, object value)\r
171                 {\r
172                         string ns = ((XmlSchema) value).TargetNamespace;\r
173                         if (ns == null) ns = "";\r
174                         table [ns] = value;\r
175                 }\r
176 \r
177                 protected override void OnRemove (int index, object value)\r
178                 {\r
179                         table.Remove (value);\r
180                 }\r
181 \r
182                 protected override void OnSet (int index, object oldValue, object newValue)\r
183                 {\r
184                         string ns = ((XmlSchema) oldValue).TargetNamespace;\r
185                         if (ns == null) ns = "";\r
186                         table [ns] = newValue;\r
187                 }\r
188         \r
189                 public void Remove (XmlSchema schema)\r
190                 {\r
191                         List.Remove (schema);\r
192                 }\r
193 \r
194                 #endregion // Methods\r
195         }\r
196 }