2003-10-14 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[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 \r
10 using System.Collections;\r
11 using System.Xml.Schema;\r
12 \r
13 namespace System.Xml.Serialization {\r
14         public class XmlSchemas : CollectionBase {\r
15 \r
16                 #region Fields\r
17                 private static string msdataNS = "urn:schemas-microsoft-com:xml-msdata";\r
18 \r
19                 Hashtable table = new Hashtable ();\r
20 \r
21                 #endregion\r
22 \r
23                 #region Constructors\r
24 \r
25                 public XmlSchemas ()\r
26                 {\r
27                 }\r
28 \r
29                 #endregion // Constructors\r
30 \r
31                 #region Properties\r
32 \r
33                 public XmlSchema this [int index] {\r
34                         get { \r
35                                 if (index < 0 || index > Count)\r
36                                         throw new ArgumentOutOfRangeException ();\r
37 \r
38                                 return (XmlSchema) List [index]; \r
39                         }\r
40                         set { List [index] = value; }\r
41                 }\r
42 \r
43                 public XmlSchema this [string ns] {\r
44                         get { return (XmlSchema) table[ns!=null?ns:""]; }\r
45                 }\r
46 \r
47                 #endregion // Properties\r
48 \r
49                 #region Methods\r
50 \r
51                 public int Add (XmlSchema schema)\r
52                 {\r
53                         Insert (Count, schema);\r
54                         return (Count - 1);\r
55                 }\r
56 \r
57                 public void Add (XmlSchemas schemas) \r
58                 {\r
59                         foreach (XmlSchema schema in schemas) \r
60                                 Add (schema);\r
61                 }\r
62 \r
63                 public bool Contains (XmlSchema schema)\r
64                 {\r
65                         return List.Contains (schema);\r
66                 }\r
67 \r
68                 public void CopyTo (XmlSchema[] array, int index) \r
69                 {\r
70                         List.CopyTo (array, index);\r
71                 }\r
72 \r
73                 public object Find (XmlQualifiedName name, Type type)\r
74                 {\r
75                         XmlSchema schema = table [name.Namespace] as XmlSchema;\r
76                         if (schema == null)\r
77                                 return null;\r
78 \r
79                         if (!schema.IsCompiled) {\r
80                                 schema.Compile (null);\r
81                         }\r
82 \r
83                         XmlSchemaObjectTable tbl = null;\r
84 \r
85                         if (type == typeof (XmlSchemaSimpleType) || type == typeof (XmlSchemaComplexType))\r
86                                 tbl = schema.SchemaTypes;\r
87                         else if (type == typeof (XmlSchemaAttribute))\r
88                                 tbl = schema.Attributes;\r
89                         else if (type == typeof (XmlSchemaAttributeGroup))\r
90                                 tbl = schema.AttributeGroups;\r
91                         else if (type == typeof (XmlSchemaElement))\r
92                                 tbl = schema.Elements;\r
93                         else if (type == typeof (XmlSchemaGroup))\r
94                                 tbl = schema.Groups;\r
95                         else if (type == typeof (XmlSchemaNotation))\r
96                                 tbl = schema.Notations;\r
97 \r
98                         object res = (tbl != null) ? tbl [name] : null;\r
99                         if (res != null && res.GetType () != type) return null;\r
100                         else return res;\r
101                 }\r
102 \r
103                 public int IndexOf (XmlSchema schema)\r
104                 {\r
105                         return List.IndexOf (schema);\r
106                 }\r
107 \r
108                 public void Insert (int index, XmlSchema schema)\r
109                 {\r
110                         List.Insert (index, schema);\r
111                 }\r
112 \r
113                 public static bool IsDataSet (XmlSchema schema)\r
114                 {\r
115                         XmlSchemaElement el = schema.Items.Count == 1 ?\r
116                                 schema.Items [0] as XmlSchemaElement : null;\r
117                         if (el != null && el.UnhandledAttributes.Length > 0) {\r
118                                 for (int i = 0; i < el.UnhandledAttributes.Length; i++) {\r
119                                         XmlAttribute attr = el.UnhandledAttributes [i];\r
120                                         if (attr.NamespaceURI == msdataNS && attr.LocalName == "IsDataSet")\r
121                                                 return (attr.Value.ToLower (System.Globalization.CultureInfo.InvariantCulture) == "true");\r
122                                 }\r
123                         }\r
124                         return false;\r
125                 }\r
126 \r
127                 protected override void OnClear ()\r
128                 {\r
129                         table.Clear ();\r
130                 }\r
131 \r
132                 protected override void OnInsert (int index, object value)\r
133                 {\r
134                         string ns = ((XmlSchema) value).TargetNamespace;\r
135                         if (ns == null) ns = "";\r
136                         table [ns] = value;\r
137                 }\r
138 \r
139                 protected override void OnRemove (int index, object value)\r
140                 {\r
141                         table.Remove (value);\r
142                 }\r
143 \r
144                 protected override void OnSet (int index, object oldValue, object newValue)\r
145                 {\r
146                         string ns = ((XmlSchema) oldValue).TargetNamespace;\r
147                         if (ns == null) ns = "";\r
148                         table [ns] = newValue;\r
149                 }\r
150         \r
151                 public void Remove (XmlSchema schema)\r
152                 {\r
153                         List.Remove (schema);\r
154                 }\r
155 \r
156                 #endregion // Methods\r
157         }\r
158 }