2 // System.Xml.Serialization.XmlSchemas
\r
5 // Tim Coleman (tim@timcoleman.com)
\r
7 // Copyright (C) Tim Coleman, 2002
\r
11 // Permission is hereby granted, free of charge, to any person obtaining
\r
12 // a copy of this software and associated documentation files (the
\r
13 // "Software"), to deal in the Software without restriction, including
\r
14 // without limitation the rights to use, copy, modify, merge, publish,
\r
15 // distribute, sublicense, and/or sell copies of the Software, and to
\r
16 // permit persons to whom the Software is furnished to do so, subject to
\r
17 // the following conditions:
\r
19 // The above copyright notice and this permission notice shall be
\r
20 // included in all copies or substantial portions of the Software.
\r
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
31 using System.Collections;
\r
32 using System.Xml.Schema;
\r
34 namespace System.Xml.Serialization {
\r
35 public class XmlSchemas : CollectionBase {
\r
38 private static string msdataNS = "urn:schemas-microsoft-com:xml-msdata";
\r
40 Hashtable table = new Hashtable ();
\r
44 #region Constructors
\r
46 public XmlSchemas ()
\r
50 #endregion // Constructors
\r
54 public XmlSchema this [int index] {
\r
56 if (index < 0 || index > Count)
\r
57 throw new ArgumentOutOfRangeException ();
\r
59 return (XmlSchema) List [index];
\r
61 set { List [index] = value; }
\r
64 public XmlSchema this [string ns] {
\r
65 get { return (XmlSchema) table[ns!=null?ns:""]; }
\r
70 public bool IsCompiled
\r
72 get { throw new NotImplementedException (); }
\r
76 public ICollection Schemas
\r
78 get { throw new NotImplementedException (); }
\r
82 public void Compile (ValidationEventHandler handler, bool fullCompile)
\r
88 #endregion // Properties
\r
92 public int Add (XmlSchema schema)
\r
94 Insert (Count, schema);
\r
98 public void Add (XmlSchemas schemas)
\r
100 foreach (XmlSchema schema in schemas)
\r
104 public bool Contains (XmlSchema schema)
\r
106 return List.Contains (schema);
\r
109 public void CopyTo (XmlSchema[] array, int index)
\r
111 List.CopyTo (array, index);
\r
114 public object Find (XmlQualifiedName name, Type type)
\r
116 XmlSchema schema = table [name.Namespace] as XmlSchema;
\r
117 if (schema == null)
\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
124 foreach (XmlSchema s in this)
\r
126 object ob = Find (s, name, type);
\r
127 if (ob != null) return ob;
\r
132 object fschema = Find (schema, name, type);
\r
134 if (fschema == null) {
\r
135 // still didn't find it
\r
136 // (possibly table[name.Namespace] was overwritten in table due to duplicate "" keys),
\r
137 // so look in all schemas (for consistiency with MS behaviour)
\r
138 foreach (XmlSchema s in this) {
\r
139 object ob = Find (s, name, type);
\r
140 if (ob != null) return ob;
\r
148 object Find (XmlSchema schema, XmlQualifiedName name, Type type)
\r
150 if (!schema.IsCompiled) {
\r
151 schema.Compile (null);
\r
154 XmlSchemaObjectTable tbl = null;
\r
156 if (type == typeof (XmlSchemaSimpleType) || type == typeof (XmlSchemaComplexType))
\r
157 tbl = schema.SchemaTypes;
\r
158 else if (type == typeof (XmlSchemaAttribute))
\r
159 tbl = schema.Attributes;
\r
160 else if (type == typeof (XmlSchemaAttributeGroup))
\r
161 tbl = schema.AttributeGroups;
\r
162 else if (type == typeof (XmlSchemaElement))
\r
163 tbl = schema.Elements;
\r
164 else if (type == typeof (XmlSchemaGroup))
\r
165 tbl = schema.Groups;
\r
166 else if (type == typeof (XmlSchemaNotation))
\r
167 tbl = schema.Notations;
\r
169 object res = (tbl != null) ? tbl [name] : null;
\r
170 if (res != null && res.GetType () != type) return null;
\r
174 public int IndexOf (XmlSchema schema)
\r
176 return List.IndexOf (schema);
\r
179 public void Insert (int index, XmlSchema schema)
\r
181 List.Insert (index, schema);
\r
184 public static bool IsDataSet (XmlSchema schema)
\r
186 XmlSchemaElement el = schema.Items.Count == 1 ?
\r
187 schema.Items [0] as XmlSchemaElement : null;
\r
188 if (el != null && el.UnhandledAttributes != null && el.UnhandledAttributes.Length > 0) {
\r
189 for (int i = 0; i < el.UnhandledAttributes.Length; i++) {
\r
190 XmlAttribute attr = el.UnhandledAttributes [i];
\r
191 if (attr.NamespaceURI == msdataNS && attr.LocalName == "IsDataSet")
\r
192 return (attr.Value.ToLower (System.Globalization.CultureInfo.InvariantCulture) == "true");
\r
198 protected override void OnClear ()
\r
203 protected override void OnInsert (int index, object value)
\r
205 string ns = ((XmlSchema) value).TargetNamespace;
\r
206 if (ns == null) ns = "";
\r
208 if (table.Contains (ns))
\r
209 throw new InvalidOperationException ("A schema with the namespace '" + ns + "' has already been added.");
\r
211 table [ns] = value;
\r
214 protected override void OnRemove (int index, object value)
\r
216 table.Remove (value);
\r
219 protected override void OnSet (int index, object oldValue, object newValue)
\r
221 string ns = ((XmlSchema) oldValue).TargetNamespace;
\r
222 if (ns == null) ns = "";
\r
223 table [ns] = newValue;
\r
226 public void Remove (XmlSchema schema)
\r
228 List.Remove (schema);
\r
231 #endregion // Methods
\r