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
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;
\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
68 #endregion // Properties
\r
72 public int Add (XmlSchema schema)
\r
74 Insert (Count, schema);
\r
78 public void Add (XmlSchemas schemas)
\r
80 foreach (XmlSchema schema in schemas)
\r
84 public bool Contains (XmlSchema schema)
\r
86 return List.Contains (schema);
\r
89 public void CopyTo (XmlSchema[] array, int index)
\r
91 List.CopyTo (array, index);
\r
94 public object Find (XmlQualifiedName name, Type type)
\r
96 XmlSchema schema = table [name.Namespace] as XmlSchema;
\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
104 foreach (XmlSchema s in this)
\r
106 object ob = Find (s, name, type);
\r
107 if (ob != null) return ob;
\r
112 return Find (schema, name, type);
\r
115 object Find (XmlSchema schema, XmlQualifiedName name, Type type)
\r
117 if (!schema.IsCompiled) {
\r
118 schema.Compile (null);
\r
121 XmlSchemaObjectTable tbl = null;
\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
136 object res = (tbl != null) ? tbl [name] : null;
\r
137 if (res != null && res.GetType () != type) return null;
\r
141 public int IndexOf (XmlSchema schema)
\r
143 return List.IndexOf (schema);
\r
146 public void Insert (int index, XmlSchema schema)
\r
148 List.Insert (index, schema);
\r
151 public static bool IsDataSet (XmlSchema schema)
\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
165 protected override void OnClear ()
\r
170 protected override void OnInsert (int index, object value)
\r
172 string ns = ((XmlSchema) value).TargetNamespace;
\r
173 if (ns == null) ns = "";
\r
174 table [ns] = value;
\r
177 protected override void OnRemove (int index, object value)
\r
179 table.Remove (value);
\r
182 protected override void OnSet (int index, object oldValue, object newValue)
\r
184 string ns = ((XmlSchema) oldValue).TargetNamespace;
\r
185 if (ns == null) ns = "";
\r
186 table [ns] = newValue;
\r
189 public void Remove (XmlSchema schema)
\r
191 List.Remove (schema);
\r
194 #endregion // Methods
\r