File: ConfigurationSectionCollection.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 (211 lines) | stat: -rw-r--r-- 7,079 bytes parent folder | download | duplicates (7)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//------------------------------------------------------------------------------
// <copyright file="ConfigurationSectionCollection.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Configuration {
    using System.Collections;
    using System.Collections.Specialized;
    using System.Runtime.Serialization;
    using System.Security.Permissions;

    [Serializable()]
    public sealed class ConfigurationSectionCollection : NameObjectCollectionBase {

        private MgmtConfigurationRecord     _configRecord;
        private ConfigurationSectionGroup   _configSectionGroup;

        //
        // Create the collection of all sections in the section group.
        //
        internal ConfigurationSectionCollection(MgmtConfigurationRecord configRecord, ConfigurationSectionGroup configSectionGroup) :
                base(StringComparer.Ordinal) {

            _configRecord = configRecord;
            _configSectionGroup = configSectionGroup;

            foreach (DictionaryEntry de in _configRecord.SectionFactories) {
                FactoryId factoryId = (FactoryId) de.Value;
                if (factoryId.Group == _configSectionGroup.SectionGroupName) {
                    BaseAdd(factoryId.Name, factoryId.Name);
                }
            }
        }

        [SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context) {
            base.GetObjectData(info, context);
        }

        //
        // Remove the collection from configuration system, and remove all entries
        // in the base collection so that enumeration will return an empty collection.
        //
        internal void DetachFromConfigurationRecord() {
            _configRecord = null;
            BaseClear();
        }

        private void VerifyIsAttachedToConfigRecord() {
            if (_configRecord == null) {
                throw new InvalidOperationException(SR.GetString(SR.Config_cannot_edit_configurationsectiongroup_when_not_attached));
            }
        }

        //
        // Public Properties
        //

        // Indexer via name
        public ConfigurationSection this[string name] {
            get {
                return Get(name);
            }
        }

        // Indexer via integer index.
        public ConfigurationSection this[int index] {
            get {
                return Get(index);
            }
        }

        //
        // Public methods
        //

        //
        // Add a new section to the collection. This will result in a new declaration and definition.
        //
        // It is an error if the section already exists.
        //
        public void Add(string name, ConfigurationSection section) {
            VerifyIsAttachedToConfigRecord();

            _configRecord.AddConfigurationSection(_configSectionGroup.SectionGroupName, name, section);
            BaseAdd(name, name);
        }

        //
        // Remove all sections from the collection.
        //
        public void Clear() {
            VerifyIsAttachedToConfigRecord();

            string[] allKeys = BaseGetAllKeys();
            foreach (string key in allKeys) {
                Remove(key);
            }
        }

        //
        // Return the number of sections in the collection.
        //
        public override int Count {
            get {
                return base.Count;
            }
        }

        //
        // Copy all the sections to an array.
        //
        public void CopyTo(ConfigurationSection[] array, int index) {
            if (array == null) {
                throw new ArgumentNullException("array");
            }

            int c = Count;
            if (array.Length < c + index) {
                throw new ArgumentOutOfRangeException("index");
            }

            for (int i = 0, j = index; i < c; i++, j++) {
                array[j] = Get(i);
            }
        }

        //
        // Get the section at a given index.
        //
        public ConfigurationSection Get(int index) {
            return Get(GetKey(index));
        }

        //
        // Get the section with a given name.
        //
        public ConfigurationSection Get(string name) {
            VerifyIsAttachedToConfigRecord();

            // validate name
            if (String.IsNullOrEmpty(name))
                throw ExceptionUtil.ParameterNullOrEmpty("name");

            // prevent GetConfig from returning config not in this collection
            if (name.IndexOf('/') >= 0)
                return null;

            // get the section from the config record
            string configKey = BaseConfigurationRecord.CombineConfigKey(_configSectionGroup.SectionGroupName, name);
            return (ConfigurationSection)_configRecord.GetSection(configKey);
        }

        // Get an enumerator
        public override IEnumerator GetEnumerator() {
            int c = Count;
            for (int i = 0; i < c; i++) {
                yield return this[i];
            }
        }

        // Get the string key at a given index.
        public string GetKey(int index) {
            return BaseGetKey(index);
        }

        // Return the string keys of the collection.
        public override KeysCollection Keys {
            get {
                return base.Keys;
            }
        }

        //
        // Remove the declaration and definition of a section in this config file, including any 
        // location sections in the file.
        //
        // Note that if the section is declared in a parent, we still remove the declaration and
        // definition, and the instance of ConfigurationSection will be detached from the collection.
        // However, the collection will still have a ConfigurationSection of that name in the collection,
        // only it will have the value of the immediate parent.
        //
        public void Remove(string name) {
            VerifyIsAttachedToConfigRecord();

            //
            // Remove the factory and section from this record, so that when config is written,
            // it will contain neither a declaration or definition.
            //
            _configRecord.RemoveConfigurationSection(_configSectionGroup.SectionGroupName, name);

            //
            // Remove the section from the collection if it is no longer in the list of all SectionFactories.
            //
            string configKey = BaseConfigurationRecord.CombineConfigKey(_configSectionGroup.SectionGroupName, name);
            if (!_configRecord.SectionFactories.Contains(configKey)) {
                BaseRemove(name);
            }
        }

        //
        // Remove the section at that index.
        //
        public void RemoveAt(int index) {
            VerifyIsAttachedToConfigRecord();

            Remove(GetKey(index));
        }
    }
}