File: DbProviderConfigurationHandler.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (189 lines) | stat: -rw-r--r-- 7,774 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//------------------------------------------------------------------------------
// <copyright file="DbProviderConfigurationHandler.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------

namespace System.Data.Common {

    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.Data;
    using System.Diagnostics;
    using System.Globalization;
    using System.Xml;

    // this calss can be used by any provider to support a provider specific configuration section. The configutation
    // Object is a NameValueCollection
   
    // <configSections>
    //     <section name="system.data.<provider>" type="System.data.common.DbProviderConfigurationHandler, System.Data, Version=%ASSEMBLY_VERSION%, Culture=neutral, PublicKeyToken=%ECMA_PUBLICKEY%" />
    // </configSections>
    // <system.data.<provider>
    //     <settings>
    //         <add name="<provider setting" value = "<value of setting>"  />
    //     </settings>
    // </system.data.<provider>
    // this class is delayed created, use ConfigurationManager.GetSection("system.data.<provider>") to obtain
    
    public class DbProviderConfigurationHandler : IConfigurationSectionHandler { // V1.2.3300
        internal const string settings = "settings";
      

        public DbProviderConfigurationHandler() { // V1.2.3300
        }
/*
        static internal void CheckForChildNodes(XmlNode node) {
            if (node.HasChildNodes) {
                throw ADP.ConfigBaseNoChildNodes(node.FirstChild);
            }
        }

        static private void CheckForNonElement(XmlNode node) {
            if (XmlNodeType.Element != node.NodeType) {
                throw ADP.ConfigBaseElementsOnly(node);
            }
        }
        
        static internal void CheckForUnrecognizedAttributes(XmlNode node) {
            if (0 != node.Attributes.Count) {
                throw ADP.ConfigUnrecognizedAttributes(node);
            }
        }
*/
        static internal NameValueCollection CloneParent(NameValueCollection parentConfig) {
            if (null == parentConfig) {
                parentConfig = new NameValueCollection();
            }
            else {
                parentConfig = new NameValueCollection(parentConfig);
            }
            return parentConfig;
        }
        
        virtual public object Create(object parent, object configContext, XmlNode section) { // V1.2.3300
#if DEBUG
            try {
#endif
                return CreateStatic(parent, configContext, section);
#if DEBUG
            }
            catch(Exception e) {
                // 
                if (ADP.IsCatchableExceptionType(e)) {
                    ADP.TraceExceptionWithoutRethrow(e); // it will be rethrown
                }
                throw;
            }
#endif
        }

        static internal object CreateStatic(object parent, object configContext, XmlNode section) {            
            object config = parent;
            if (null != section) {
                config = CloneParent(parent as NameValueCollection);
                bool foundSettings = false;

                HandlerBase.CheckForUnrecognizedAttributes(section);
                foreach (XmlNode child in section.ChildNodes) {
                    if (HandlerBase.IsIgnorableAlsoCheckForNonElement(child)) {
                        continue;
                    }
                    string sectionGroup = child.Name;
                    switch(sectionGroup) {
                    case DbProviderConfigurationHandler.settings:
                        if (foundSettings) {
                            throw ADP.ConfigSectionsUnique(DbProviderConfigurationHandler.settings);
                        }
                        foundSettings= true;
                        DbProviderDictionarySectionHandler.CreateStatic(config as NameValueCollection, configContext, child);
                        break;
                    default:
                        throw ADP.ConfigUnrecognizedElement(child);
                    }
                }
            }
            return config;
        }
/*
        // skip whitespace and comments, throws if non-element
        static internal bool IsIgnorableAlsoCheckForNonElement(XmlNode node) {
            if ((XmlNodeType.Comment == node.NodeType) || (XmlNodeType.Whitespace == node.NodeType)) {
                return true;
            }
            HandlerBase.CheckForNonElement(node);
            return false;
        }
  */      
        static internal string RemoveAttribute(XmlNode node, string name) {
            XmlNode attribute = node.Attributes.RemoveNamedItem(name);
            if (null == attribute) {
                throw ADP.ConfigRequiredAttributeMissing(name, node);
            }
            string value = attribute.Value;
            if (0 == value.Length) {
                throw ADP.ConfigRequiredAttributeEmpty(name, node);
            }
            return value;
        }
        
        // based off of DictionarySectionHandler
        sealed private class DbProviderDictionarySectionHandler/* : IConfigurationSectionHandler*/ {
            
            static internal NameValueCollection CreateStatic(NameValueCollection config, Object context, XmlNode section) {
                if (null != section) {
                    HandlerBase.CheckForUnrecognizedAttributes(section);
                    }
                    
                    foreach (XmlNode child in section.ChildNodes) {
                        if (HandlerBase.IsIgnorableAlsoCheckForNonElement(child)) {
                            continue;
                        }
                        switch(child.Name) {
                        case "add":
                            HandleAdd(child, config);
                            break;
                        case "remove":
                            HandleRemove(child, config);
                            break;
                        case "clear":
                            HandleClear(child, config);
                            break;
                        default:
                            throw ADP.ConfigUnrecognizedElement(child);
                        }
                    }
                return config;
                
            }
            static private void HandleAdd(XmlNode child, NameValueCollection config) {

                // should add vaildate that setting is a known supported setting 
                // (i.e. that the value of the name attribute is is good)
                HandlerBase.CheckForChildNodes(child);
                string name = RemoveAttribute(child, "name");
                string value = RemoveAttribute(child, "value");
                HandlerBase.CheckForUnrecognizedAttributes(child);
                config.Add(name,value);
                
            }
            static private void HandleRemove(XmlNode child, NameValueCollection config) {
                HandlerBase.CheckForChildNodes(child);                
                String name = RemoveAttribute(child, "name");                
                HandlerBase.CheckForUnrecognizedAttributes(child);
                config.Remove(name);
            }
            static private void HandleClear(XmlNode child, NameValueCollection config) {
                HandlerBase.CheckForChildNodes(child);
                HandlerBase.CheckForUnrecognizedAttributes(child);
                config.Clear();
            }
        }
        
    }
}