2 // System.Xml.Serialization.XmlSchemas
5 // Tim Coleman (tim@timcoleman.com)
7 // Copyright (C) Tim Coleman, 2002
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
33 using System.Collections.Generic;
35 using System.Xml.Schema;
37 namespace System.Xml.Serialization
40 public class XmlSchemas : CollectionBase, IEnumerable<XmlSchema>
42 public class XmlSchemas : CollectionBase
47 private static string msdataNS = "urn:schemas-microsoft-com:xml-msdata";
49 Hashtable table = new Hashtable ();
59 #endregion // Constructors
63 public XmlSchema this [int index] {
65 if (index < 0 || index > Count)
66 throw new ArgumentOutOfRangeException ();
68 return (XmlSchema) List [index];
70 set { List [index] = value; }
73 public XmlSchema this [string ns] {
74 get { return (XmlSchema) table[ns!=null?ns:""]; }
79 public bool IsCompiled
81 get { throw new NotImplementedException (); }
85 public void Compile (ValidationEventHandler handler, bool fullCompile)
87 foreach (XmlSchema xs in this)
88 if (fullCompile || !xs.IsCompiled)
94 #endregion // Properties
98 public int Add (XmlSchema schema)
100 Insert (Count, schema);
104 public void Add (XmlSchemas schemas)
106 foreach (XmlSchema schema in schemas)
111 [MonoNotSupported("")]
112 public int Add (XmlSchema schema, Uri baseUri)
114 throw new NotImplementedException ();
117 [MonoNotSupported("")]
118 public void AddReference (XmlSchema schema)
120 throw new NotImplementedException ();
124 public bool Contains (XmlSchema schema)
126 return List.Contains (schema);
130 [MonoNotSupported("")]
131 public bool Contains (string targetNamespace)
133 throw new NotImplementedException ();
137 public void CopyTo (XmlSchema[] array, int index)
139 List.CopyTo (array, index);
142 public object Find (XmlQualifiedName name, Type type)
144 XmlSchema schema = table [name.Namespace] as XmlSchema;
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.
152 foreach (XmlSchema s in this)
154 object ob = Find (s, name, type);
155 if (ob != null) return ob;
160 object fschema = Find (schema, name, type);
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;
176 object Find (XmlSchema schema, XmlQualifiedName name, Type type)
178 if (!schema.IsCompiled) {
179 schema.Compile (null);
182 XmlSchemaObjectTable tbl = null;
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))
194 else if (type == typeof (XmlSchemaNotation))
195 tbl = schema.Notations;
197 object res = (tbl != null) ? tbl [name] : null;
198 if (res != null && res.GetType () != type) return null;
203 [MonoNotSupported("")]
204 public IList GetSchemas (string ns)
206 throw new NotImplementedException ();
210 public int IndexOf (XmlSchema schema)
212 return List.IndexOf (schema);
215 public void Insert (int index, XmlSchema schema)
217 List.Insert (index, schema);
220 public static bool IsDataSet (XmlSchema schema)
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");
234 protected override void OnClear ()
239 protected override void OnInsert (int index, object value)
241 string ns = ((XmlSchema) value).TargetNamespace;
242 if (ns == null) ns = "";
246 protected override void OnRemove (int index, object value)
248 table.Remove (value);
251 protected override void OnSet (int index, object oldValue, object newValue)
253 string ns = ((XmlSchema) oldValue).TargetNamespace;
254 if (ns == null) ns = "";
255 table [ns] = newValue;
258 public void Remove (XmlSchema schema)
260 List.Remove (schema);
264 IEnumerator<XmlSchema> IEnumerable<XmlSchema>.GetEnumerator ()
266 return new XmlSchemaEnumerator (this);
270 #endregion // Methods