* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Data / Mainsoft.Data.Configuration.jvm / ProvidersSectionHandler.cs
1 //
2 // System.Data.OleDb.OleDbConnection
3 //
4 // Authors:
5 //      Konstantin Triger <kostat@mainsoft.com>
6 //      Boris Kirzner <borisk@mainsoft.com>
7 //      
8 // (C) 2006 Mainsoft Corporation (http://www.mainsoft.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32
33 using System;
34 using System.Configuration;
35 using System.Xml;
36 using System.Collections;
37 using System.Collections.Specialized;
38
39 namespace Mainsoft.Data.Configuration
40 {
41         class ProvidersSectionHandler : IConfigurationSectionHandler
42         {
43                 public virtual object Create (object parent, object configContext, XmlNode section) {
44                         if (section.Attributes != null && section.Attributes.Count != 0)
45                                 ThrowException ("Unrecognized attribute", section);
46
47                         ArrayList providers = new ArrayList();
48                         
49                         XmlNodeList providerElements = section.SelectNodes ("provider");
50                         foreach (XmlNode child in providerElements) {
51                                 XmlNodeType ntype = child.NodeType;
52                                 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
53                                         continue;
54
55                                 if (ntype != XmlNodeType.Element)
56                                         ThrowException ("Only elements allowed", child);
57
58                                 object parentProvider = null;
59                                 XmlAttribute extends = child.Attributes["parent"];
60                                 if (extends != null) {
61                                         string super = extends.Value;
62                                         string sectionName = super.Substring(0, super.IndexOf('#'));
63                                         string id = super.Substring(sectionName.Length+1);
64                                         IList supers = (IList) ConfigurationSettings.GetConfig(sectionName);
65                                         for (int i = 0; i < supers.Count; i++) {
66                                                 Hashtable ht = (Hashtable)supers[i];
67                                                 string curId = (string)ht["id"];
68                                                 if (String.CompareOrdinal(id,curId) == 0) {
69                                                         parentProvider = ht;
70                                                         break;
71                                                 }
72                                         }
73                                         child.Attributes.Remove(extends);
74                                 }
75
76                                 DictionarySectionHandler handler = new DictionarySectionHandler();
77                                 Hashtable col = (Hashtable) handler.Create (parentProvider, null, child);
78                                 providers.Add(col);
79                         }       
80                         if (parent != null && parent is IList) {
81                                 IList parentList = (IList)parent;
82                                 for (int i = 0; i < parentList.Count; i++) {
83                                         Hashtable htParent = (Hashtable)parentList[i];
84                                         string parentId = (string)htParent["id"];
85                                         bool overriden = false;
86                                         for (int y = 0; y < providers.Count; y++) {
87                                                 Hashtable htMe = (Hashtable)providers[y];
88                                                 string myId = (string)htMe["id"];
89                                                 if (String.CompareOrdinal(parentId,myId) == 0) {
90                                                         overriden = true;
91                                                         foreach (object key in htParent.Keys)
92                                                                 if (!htMe.ContainsKey(key))
93                                                                         htMe[key] = htParent[key];
94                                                         break;
95                                                 }
96                                         }
97
98                                         if (!overriden)
99                                                 providers.Add(htParent);
100                                 }
101                         }
102
103                         return providers;
104                 }
105                 
106                 static internal void ThrowException (string msg, XmlNode node) 
107                 {
108                         if (node != null && node.Name != String.Empty)
109                                 msg = msg + " (node name: " + node.Name + ") ";
110                         throw new ConfigurationException (msg, node);
111                 }
112         }
113 }