File: SectionInformation.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 (840 lines) | stat: -rw-r--r-- 32,832 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
//------------------------------------------------------------------------------
// <copyright file="SectionInformation.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Configuration {
    using System;
    using System.Collections.Specialized;
    using System.Configuration.Internal;
    using System.IO;
    using System.Reflection;
    using System.Security;
    using System.Text;
    using System.Xml;
    using System.Globalization;

    public sealed class SectionInformation {
        // Flags
        private const int Flag_Attached                     = 0x00000001;
        private const int Flag_Declared                     = 0x00000002;
        private const int Flag_DeclarationRequired          = 0x00000004;
        private const int Flag_AllowLocation                = 0x00000008;
        private const int Flag_RestartOnExternalChanges     = 0x00000010;
        private const int Flag_RequirePermission            = 0x00000020;                                 
        private const int Flag_LocationLocked               = 0x00000040;
        private const int Flag_ChildrenLocked               = 0x00000080;
        private const int Flag_InheritInChildApps           = 0x00000100;                                 
        private const int Flag_IsParentSection              = 0x00000200;                                 
        private const int Flag_Removed                      = 0x00000400;                                 
        private const int Flag_ProtectionProviderDetermined = 0x00000800;                                 
        private const int Flag_ForceSave                    = 0x00001000;
        private const int Flag_IsUndeclared                 = 0x00002000;        
        private const int Flag_ChildrenLockWithoutFileInput         = 0x00004000;

        private const int Flag_AllowExeDefinitionModified   = 0x00010000;
        private const int Flag_AllowDefinitionModified      = 0x00020000;
        private const int Flag_ConfigSourceModified         = 0x00040000;
        private const int Flag_ProtectionProviderModified   = 0x00080000;
        private const int Flag_OverrideModeDefaultModified  = 0x00100000;
        private const int Flag_OverrideModeModified         = 0x00200000;   // Used only for modified tracking        
        private const int Flag_ConfigBuilderDetermined       = 0x00400000;
        private const int Flag_ConfigBuilderModified         = 0x00800000;

        private ConfigurationSection            _configurationSection;
        private SafeBitVector32                 _flags;
        private SimpleBitVector32               _modifiedFlags;
        private ConfigurationAllowDefinition    _allowDefinition;
        private ConfigurationAllowExeDefinition _allowExeDefinition;
        private MgmtConfigurationRecord         _configRecord;
        private string                          _configKey;
        private string                          _group;
        private string                          _name;
        private string                          _typeName;
        private string                          _rawXml;
        private string                          _configSource;
        private string                          _configSourceStreamName;
        private ProtectedConfigurationProvider  _protectionProvider;
        private string                          _protectionProviderName;
        private ConfigurationBuilder             _configBuilder;
        private string                          _configBuilderName;
        private OverrideModeSetting             _overrideModeDefault;       // The default mode for the section in _configurationSection
        private OverrideModeSetting             _overrideMode;              // The override mode at the current config path

        //
        // Constructor
        //
        internal SectionInformation(ConfigurationSection associatedConfigurationSection) {
            _configKey = String.Empty;
            _group = String.Empty;
            _name = String.Empty;

            _configurationSection   = associatedConfigurationSection;
            _allowDefinition        = ConfigurationAllowDefinition.Everywhere;
            _allowExeDefinition     = ConfigurationAllowExeDefinition.MachineToApplication;
            _overrideModeDefault    = OverrideModeSetting.SectionDefault;
            _overrideMode           = OverrideModeSetting.LocationDefault;

            _flags[ Flag_AllowLocation            ] = true;
            _flags[ Flag_RestartOnExternalChanges ] = true;
            _flags[ Flag_RequirePermission        ] = true;
            _flags[ Flag_InheritInChildApps       ] = true;
            _flags[ Flag_ForceSave                ] = false;

            _modifiedFlags = new SimpleBitVector32();
        }

        internal void ResetModifiedFlags() {
            _modifiedFlags = new SimpleBitVector32();
        }

        internal bool IsModifiedFlags() {
            return (_modifiedFlags.Data != 0);
        }
        // for instantiation of a ConfigurationSection from GetConfig
        internal void AttachToConfigurationRecord(MgmtConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord) {
            SetRuntimeConfigurationInformation(configRecord, factoryRecord, sectionRecord);
            _configRecord = configRecord;
        }

        internal void SetRuntimeConfigurationInformation(BaseConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord) {
            _flags[ Flag_Attached ] = true;

            // factory info
            _configKey          = factoryRecord.ConfigKey;
            _group              = factoryRecord.Group;
            _name               = factoryRecord.Name;
            _typeName           = factoryRecord.FactoryTypeName;
            _allowDefinition    = factoryRecord.AllowDefinition;
            _allowExeDefinition = factoryRecord.AllowExeDefinition;
            _flags[ Flag_AllowLocation            ] = factoryRecord.AllowLocation;
            _flags[ Flag_RestartOnExternalChanges ] = factoryRecord.RestartOnExternalChanges;
            _flags[ Flag_RequirePermission        ] = factoryRecord.RequirePermission;
            _overrideModeDefault                    = factoryRecord.OverrideModeDefault;

            if (factoryRecord.IsUndeclared) {
                _flags[ Flag_IsUndeclared             ] = true;
                _flags[ Flag_Declared                 ] = false;
                _flags[ Flag_DeclarationRequired      ] = false;
            }
            else {
                _flags[ Flag_IsUndeclared             ] = false;
                _flags[ Flag_Declared                 ] = configRecord.GetFactoryRecord(factoryRecord.ConfigKey, false) != null;
                _flags[ Flag_DeclarationRequired      ] = configRecord.IsRootDeclaration(factoryRecord.ConfigKey, false);
            }

            // section info
            _flags[ Flag_LocationLocked ]               = sectionRecord.Locked;
            _flags[ Flag_ChildrenLocked ]               = sectionRecord.LockChildren;
            _flags[ Flag_ChildrenLockWithoutFileInput]  = sectionRecord.LockChildrenWithoutFileInput;

            if (sectionRecord.HasFileInput) {
                SectionInput fileInput = sectionRecord.FileInput;

                _flags[Flag_ConfigBuilderDetermined] = fileInput.IsConfigBuilderDetermined;
                _configBuilder                       = fileInput.ConfigBuilder;
                _flags[Flag_ProtectionProviderDetermined] = fileInput.IsProtectionProviderDetermined;
                _protectionProvider                 = fileInput.ProtectionProvider;
                
                SectionXmlInfo sectionXmlInfo = fileInput.SectionXmlInfo;

                _configSource                       = sectionXmlInfo.ConfigSource;
                _configSourceStreamName             = sectionXmlInfo.ConfigSourceStreamName;
                _overrideMode                       = sectionXmlInfo.OverrideModeSetting;
                _flags[ Flag_InheritInChildApps ]   = !sectionXmlInfo.SkipInChildApps;
                _configBuilderName             = sectionXmlInfo.ConfigBuilderName;
                _protectionProviderName             = sectionXmlInfo.ProtectionProviderName;
            }
            else {
                _flags[Flag_ConfigBuilderDetermined] = false;
                _configBuilder = null;
                _flags[Flag_ProtectionProviderDetermined] = false;
                _protectionProvider = null;
            }

            // element context information
            _configurationSection.AssociateContext( configRecord );
        }

        internal void DetachFromConfigurationRecord() {
            RevertToParent();
            _flags[ Flag_Attached ] = false;
            _configRecord = null;
        }

        private bool IsRuntime {
            get {
                return ( _flags[ Flag_Attached ] ) && 
                       ( _configRecord == null   );
            }
        }

        internal bool Attached {
            get {return _flags[ Flag_Attached ];}
        }

        private void VerifyDesigntime() {
            if (IsRuntime) {
                throw new InvalidOperationException(SR.GetString(SR.Config_operation_not_runtime));
            }
        }

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

        // VerifyIsEditable 
        //
        // Verify the section is Editable.  
        // It may not be editable for the following reasons:
        //   - We are in Runtime mode, not Design time
        //   - The section is not attached to a _configRecord.
        //   - We are locked (ie. allowOveride = false )
        //   - We are a parent section (ie. Retrieved from GetParentSection)
        //
        internal void VerifyIsEditable() {
            VerifyDesigntime();

            if (IsLocked) {
                throw new InvalidOperationException(SR.GetString(SR.Config_cannot_edit_configurationsection_when_locked));
            }

            if (_flags[Flag_IsParentSection]) {
                throw new InvalidOperationException(SR.GetString(SR.Config_cannot_edit_configurationsection_parentsection));
            }

            if ( !_flags[ Flag_AllowLocation ] &&
                 ( _configRecord != null ) && 
                 ( _configRecord.IsLocationConfig ) ) {
                throw new InvalidOperationException(SR.GetString(SR.Config_cannot_edit_configurationsection_when_location_locked));
            }
        }

        // VerifyNotParentSection
        //
        // Verify that this is not a parent section
        //
        private void VerifyNotParentSection() {
            if ( _flags[Flag_IsParentSection] ) {
                throw new InvalidOperationException(SR.GetString(SR.Config_configsection_parentnotvalid));
           }
        }
        
        // VerifySupportsLocation
        //
        // Verify that we support the location tag.  This is true either
        // in machine.config, or in any place for the web config system
        //
        private void VerifySupportsLocation() {
            if ( ( _configRecord != null ) &&
                 !_configRecord.RecordSupportsLocation ) {
                throw new InvalidOperationException(SR.GetString(SR.Config_cannot_edit_locationattriubtes));
            }
        }

        internal void VerifyIsEditableFactory() {
            if (_configRecord != null && _configRecord.IsLocationConfig) {
                throw new InvalidOperationException(SR.GetString(SR.Config_cannot_edit_configurationsection_in_location_config));
            }

            // Can't edit factory if the section is built-in
            if (BaseConfigurationRecord.IsImplicitSection(ConfigKey)) {
                throw new InvalidOperationException(SR.GetString(SR.Config_cannot_edit_configurationsection_when_it_is_implicit));
            }

            // Can't edit undeclared section
            if (_flags[Flag_IsUndeclared]) {
                throw new InvalidOperationException(SR.GetString(SR.Config_cannot_edit_configurationsection_when_it_is_undeclared));
            }
        }

        internal string ConfigKey {
            get {return _configKey;}
        }

        internal bool Removed {
            get {
                return _flags[ Flag_Removed ];
            }
            set {
                _flags[ Flag_Removed ] = value;
            }
        }

        private FactoryRecord FindParentFactoryRecord(bool permitErrors) {
            FactoryRecord factoryRecord = null;

            if (_configRecord != null && !_configRecord.Parent.IsRootConfig) {
                factoryRecord = _configRecord.Parent.FindFactoryRecord(_configKey, permitErrors);
            }

            return factoryRecord;
        }

        //
        // public properties and methods
        //
        public string SectionName {
            get {return _configKey;}
        }

        public string Name {
            get {return _name;}
        }

        public ConfigurationAllowDefinition AllowDefinition {
            get {return _allowDefinition;}
            set {
                VerifyIsEditable();
                VerifyIsEditableFactory();

                // allow AllowDefinition to be different from current type,
                // so long as it doesn't conflict with a type already defined
                FactoryRecord factoryRecord = FindParentFactoryRecord(false);
                if (factoryRecord != null && factoryRecord.AllowDefinition != value) {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Config_tag_name_already_defined, _configKey));
                }

                _allowDefinition = value;
                _modifiedFlags[ Flag_AllowDefinitionModified       ] = true;
            }
        }

        internal bool AllowDefinitionModified {
            get {
                return _modifiedFlags[ Flag_AllowDefinitionModified ];
            }
        }

        public ConfigurationAllowExeDefinition AllowExeDefinition {
            get {return _allowExeDefinition;}
            set {
                VerifyIsEditable();
                VerifyIsEditableFactory();

                // allow AllowDefinition to be different from current type,
                // so long as it doesn't conflict with a type already defined
                FactoryRecord factoryRecord = FindParentFactoryRecord(false);
                if (factoryRecord != null && factoryRecord.AllowExeDefinition != value) {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Config_tag_name_already_defined, _configKey));
                }

                _allowExeDefinition = value;
                _modifiedFlags[ Flag_AllowExeDefinitionModified       ] = true;
            }
        }

        internal bool AllowExeDefinitionModified {
            get {
                return _modifiedFlags[ Flag_AllowExeDefinitionModified ];
            }
        }

        public OverrideMode OverrideModeDefault {
            get {return _overrideModeDefault.OverrideMode;}
            set {
                VerifyIsEditable();
                VerifyIsEditableFactory();

                // allow OverrideModeDefault to be different from current value,
                // so long as it doesn't conflict with a type already defined
                FactoryRecord factoryRecord = FindParentFactoryRecord(false);
                if (factoryRecord != null && factoryRecord.OverrideModeDefault.OverrideMode != value) {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Config_tag_name_already_defined, _configKey));
                }

                // Threat "Inherit" as "Allow" as "Inherit" does not make sense as a default
                if (value == OverrideMode.Inherit) {
                    value = OverrideMode.Allow;
                }

                _overrideModeDefault.OverrideMode = value;
                _modifiedFlags[ Flag_OverrideModeDefaultModified       ] = true;
            }
        }

        internal OverrideModeSetting OverrideModeDefaultSetting {
            get { return _overrideModeDefault; }
        }

        internal bool OverrideModeDefaultModified {
            get {
                return _modifiedFlags[ Flag_OverrideModeDefaultModified ];
            }
        }
            

        public bool AllowLocation {
            get {
                return _flags[ Flag_AllowLocation ];
            }
            set {
                VerifyIsEditable();
                VerifyIsEditableFactory();

                // allow AllowLocation to be different from current type,
                // so long as it doesn't conflict with a type already defined
                FactoryRecord factoryRecord = FindParentFactoryRecord(false);
                if (factoryRecord != null && factoryRecord.AllowLocation != value) {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Config_tag_name_already_defined, _configKey));
                }

                _flags[ Flag_AllowLocation ] = value;
                _modifiedFlags[ Flag_AllowLocation ] = true;
            }
        }

        internal bool AllowLocationModified {
            get {
                return _modifiedFlags[ Flag_AllowLocation ];
            }
        }

        public bool AllowOverride {
            get {
                return _overrideMode.AllowOverride;
            }
            set {
                VerifyIsEditable();
                VerifySupportsLocation();
                _overrideMode.AllowOverride = value;
                _modifiedFlags[ Flag_OverrideModeModified] = true;
            }
        }

        public OverrideMode OverrideMode {
            get {
                return _overrideMode.OverrideMode;
            }
            set {
                VerifyIsEditable();
                VerifySupportsLocation();
                _overrideMode.OverrideMode = value;
                _modifiedFlags[ Flag_OverrideModeModified] = true;

                // Modify the state of OverrideModeEffective according to the changes to the current mode
                switch(value) {
                    case OverrideMode.Inherit:
                        // Wehen the state is changing to inherit - apply the value from parent which does not
                        // include the file input lock mode
                        _flags[Flag_ChildrenLocked] = _flags[Flag_ChildrenLockWithoutFileInput];
                        break;

                    case OverrideMode.Allow:
                        _flags[Flag_ChildrenLocked] = false;
                        break;

                    case OverrideMode.Deny:
                        _flags[Flag_ChildrenLocked] = true;
                        break;

                    default:
                        Debug.Assert(false,"Unexpected value for OverrideMode");
                        break;                        
                }
            }
        }

        public OverrideMode OverrideModeEffective {
            get { 
                return (_flags[Flag_ChildrenLocked] ? OverrideMode.Deny : OverrideMode.Allow);
            }
        }

        internal OverrideModeSetting OverrideModeSetting {
            get { return _overrideMode; }
        }


        // LocationAttributesAreDefault
        //
        // Are the location attributes for this section set to the
        // default settings?
        //
        internal bool LocationAttributesAreDefault {
            get {
                return ( _overrideMode.IsDefaultForLocationTag &&
                         ( _flags[ Flag_InheritInChildApps ] == true ) );
            }
        }
        
        public string ConfigSource {
            get {
               if (_configSource != null) {
                   return _configSource;
               }
               else {
                   return String.Empty;
               }
            }

            set {
                string configSource;
                
                VerifyIsEditable();

                if (!String.IsNullOrEmpty(value)) {
                    configSource = BaseConfigurationRecord.NormalizeConfigSource(value, null);
                }
                else {
                    configSource = null;
                }
                
                // return early if there is no change
                if (configSource == _configSource)
                    return;

                if (_configRecord != null) {
                    _configRecord.ChangeConfigSource(this, _configSource, _configSourceStreamName, configSource);
                }

                _configSource = configSource;
                _modifiedFlags[Flag_ConfigSourceModified] = true;
            }
        }

        internal bool ConfigSourceModified {
            get {
                return _modifiedFlags[Flag_ConfigSourceModified];
            }
        }

        internal string ConfigSourceStreamName {
            get { 
                return _configSourceStreamName;
            }

            set {
                _configSourceStreamName = value;
            }
        }

        public bool InheritInChildApplications {
            get {
                return _flags[ Flag_InheritInChildApps ];
            }
            set {
                VerifyIsEditable();
                VerifySupportsLocation();
                _flags[ Flag_InheritInChildApps ] = value;
            }
        }

        // True if the section is declared at the current level
        public bool IsDeclared {
            get {
                VerifyNotParentSection();
                
                return _flags[ Flag_Declared ];
            }
        }

        // IsDeclarationRequired
        //
        // Is the Declaration Required.  It is required if it is not set
        // in a parent, or the parent entry does not have the type
        //
        public bool IsDeclarationRequired {
            get { 
                VerifyNotParentSection();

                return _flags[Flag_DeclarationRequired]; 
            }
        }

        // Force the section declaration to be written out during Save.
        public void ForceDeclaration() {
            ForceDeclaration( true );
        }

        // If force==false, it actually means don't declare it at
        // the current level.
        public void ForceDeclaration(bool force) {
            VerifyIsEditable();

            if ( ( force == false ) &&
                 _flags[ Flag_DeclarationRequired ] )
            {
                // Since it is required, we can not remove it
            }
            else
            {
                //
                // 




                if (force == true && BaseConfigurationRecord.IsImplicitSection(SectionName)) {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Cannot_declare_or_remove_implicit_section));
                }

                if (force == true && _flags[ Flag_IsUndeclared ] ) {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Config_cannot_edit_configurationsection_when_it_is_undeclared));
                }

                _flags[ Flag_Declared ] = force;
            }
        }

        // IsDefinitionAllowed
        //
        // Is the Definition Allowed at this point.  This is all depending
        // on the Definition that is allowed, and what context we are 
        // writing the file
        //
        private bool IsDefinitionAllowed {
            get {
                if (_configRecord == null) {
                    return true;
                }
                else {
                    return _configRecord.IsDefinitionAllowed(_allowDefinition, _allowExeDefinition);
                }
            }
        }

        public bool IsLocked {
            get {
                return _flags[Flag_LocationLocked] || !IsDefinitionAllowed || 
                    _configurationSection.ElementInformation.IsLocked;  // See VSWhidbey 484189
            }
        }

        public bool IsProtected {
            get {return (ProtectionProvider != null);}
        }

        internal string ConfigBuilderName {
            get { return _configBuilderName; }
        }

        public ConfigurationBuilder ConfigurationBuilder {
            get {
                if (!_flags[Flag_ConfigBuilderDetermined] && _configRecord != null) {
                    _configBuilder = _configRecord.GetConfigBuilderFromName(_configBuilderName);
                    _flags[Flag_ConfigBuilderDetermined] = true;
                }

                return _configBuilder;
            }
        }

        public ProtectedConfigurationProvider ProtectionProvider {
            get {
                if (!_flags[ Flag_ProtectionProviderDetermined] && _configRecord != null) {
                    _protectionProvider = _configRecord.GetProtectionProviderFromName(_protectionProviderName, false);
                    _flags[ Flag_ProtectionProviderDetermined ] = true;
                }
                
                return _protectionProvider;
            }
        }

        // method to cause a section to be protected using the specified provider
        public void ProtectSection(string protectionProvider) {
            ProtectedConfigurationProvider protectedConfigurationProvider = null;

            VerifyIsEditable();

            // Do not encrypt sections that will be read by a native reader.
            // These sections are be marked with allowLocation=false.
            // Also, the configProtectedData section cannot be encrypted!
            if (!AllowLocation || _configKey == BaseConfigurationRecord.RESERVED_SECTION_PROTECTED_CONFIGURATION) {
                throw new InvalidOperationException(SR.GetString(SR.Config_not_allowed_to_encrypt_this_section));
            }

            if (_configRecord != null) {
                if (String.IsNullOrEmpty(protectionProvider)) {
                    protectionProvider = _configRecord.DefaultProviderName;
                }

                protectedConfigurationProvider = _configRecord.GetProtectionProviderFromName(protectionProvider, true);
            }
            else {
                throw new InvalidOperationException(SR.GetString(SR.Must_add_to_config_before_protecting_it));
            }

            _protectionProviderName = protectionProvider;
            _protectionProvider = protectedConfigurationProvider;

            _flags[ Flag_ProtectionProviderDetermined ] = true;
            _modifiedFlags[ Flag_ProtectionProviderModified ] = true;
        }

        public void UnprotectSection() {
            VerifyIsEditable();

            _protectionProvider = null;
            _protectionProviderName = null;
            _flags[ Flag_ProtectionProviderDetermined ] = true;
            _modifiedFlags[ Flag_ProtectionProviderModified ] = true;
        }

        internal string ProtectionProviderName {
            get { return _protectionProviderName; }
        }

        public bool RestartOnExternalChanges {
            get {
                return _flags[ Flag_RestartOnExternalChanges ];
            }
            set {
                VerifyIsEditable();
                VerifyIsEditableFactory();

                // allow RestartOnExternalChanges to be different from current type,
                // so long as it doesn't conflict with a type already defined
                FactoryRecord factoryRecord = FindParentFactoryRecord(false);
                if (factoryRecord != null && factoryRecord.RestartOnExternalChanges != value) {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Config_tag_name_already_defined, _configKey));
                }

                _flags[ Flag_RestartOnExternalChanges ] = value;
                _modifiedFlags[ Flag_RestartOnExternalChanges ] = true;
            }
        }

        internal bool RestartOnExternalChangesModified {
            get {
                return _modifiedFlags[ Flag_RestartOnExternalChanges ];
            }
        }

        public bool RequirePermission {
            get {
                return _flags[ Flag_RequirePermission ];
            }
            set {
                VerifyIsEditable();
                VerifyIsEditableFactory();

                // allow RequirePermission to be different from current type,
                // so long as it doesn't conflict with a type already defined
                FactoryRecord factoryRecord = FindParentFactoryRecord(false);
                if (factoryRecord != null && factoryRecord.RequirePermission != value) {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Config_tag_name_already_defined, _configKey));
                }

                _flags[ Flag_RequirePermission ] = value;
                _modifiedFlags[ Flag_RequirePermission ] = true;
            }
        }

        internal bool RequirePermissionModified {
            get {
                return _modifiedFlags[ Flag_RequirePermission ];
            }
        }


        public string Type {
            get {return _typeName;}
            set {
                if (String.IsNullOrEmpty(value)) {
                    throw ExceptionUtil.PropertyNullOrEmpty("Type");
                }

                VerifyIsEditable();
                VerifyIsEditableFactory();

                // allow type to be different from current type,
                // so long as it doesn't conflict with a type already defined
                FactoryRecord factoryRecord = FindParentFactoryRecord(false);
                if (factoryRecord != null) {
                    IInternalConfigHost host = null;
                    if (_configRecord != null) {
                        host = _configRecord.Host;
                    }

                    if (!factoryRecord.IsEquivalentType(host, value)) {
                        throw new ConfigurationErrorsException(SR.GetString(SR.Config_tag_name_already_defined, _configKey));
                    }
                }

                _typeName = value;
            }
        }

        public ConfigurationSection GetParentSection() {
            VerifyDesigntime();

            if ( _flags[ Flag_IsParentSection ] ) {
                throw new InvalidOperationException(SR.GetString(SR.Config_getparentconfigurationsection_first_instance));
            }

            // if a users create a configsection with : sectionType sec  = new sectionType();
            // the config record will be null.  Return null for the parent in this case.
            ConfigurationSection ancestor = null;
            if (_configRecord != null) {  
                ancestor = _configRecord.FindAndCloneImmediateParentSection(_configurationSection);
                if (ancestor != null) {
                    ancestor.SectionInformation._flags[Flag_IsParentSection]     = true;
                    ancestor.SetReadOnly();
                }
            }

            return ancestor;
        }

        public string GetRawXml() {
            VerifyDesigntime();
            VerifyNotParentSection();

            if (RawXml != null) {
                return RawXml;
            }
            else if (_configRecord != null) {
                return _configRecord.GetRawXml(_configKey);
            }
            else {
                return null;
            }
        }

        public void SetRawXml(string rawXml) {
            VerifyIsEditable();

            if (_configRecord != null) {
                _configRecord.SetRawXml(_configurationSection, rawXml);
            }
            else {
                RawXml = (String.IsNullOrEmpty(rawXml)) ? null : rawXml;
            }
        }

        internal string RawXml {
            get {
                return _rawXml;
            }

            set {
                _rawXml = value;
            }
        }

        // True if the section will be serialized to the current hierarchy level, regardless of 
        // ConfigurationSaveMode.
        public bool ForceSave
        {
            get { 
                return _flags[ Flag_ForceSave ];
            }
            set { 
                VerifyIsEditable();

                _flags[ Flag_ForceSave ] = value; 
            }
        }

        public void RevertToParent() {
            VerifyIsEditable();
            VerifyIsAttachedToConfigRecord();

            _configRecord.RevertToParent(_configurationSection);
        }
    }
}