File: ConfigurationElementInterceptor.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (92 lines) | stat: -rw-r--r-- 3,532 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
using System.Configuration;
using System.Text;
using System.Xml;

namespace System.IdentityModel.Configuration
{
#pragma warning disable 1591
    public partial class ConfigurationElementInterceptor : ConfigurationElement
    {
        private XmlDocument elementXml;

        protected override void DeserializeElement( XmlReader reader, bool serializeCollectionKey )
        {
            elementXml = new XmlDocument();
            elementXml.LoadXml( reader.ReadOuterXml() );

            // Create a new XmlTextReader so this element can be loaded
            // by the framework.
            using ( XmlReader newReader = XmlDictionaryReader.CreateTextReader( Encoding.UTF8.GetBytes( elementXml.DocumentElement.OuterXml ), XmlDictionaryReaderQuotas.Max ) )
            {
                newReader.Read();
                base.DeserializeElement( newReader, serializeCollectionKey );
            }
        }

        // There are parts in the configuration where users can specify arbitrary elements and attributes.
        // For example, when loading a custom token handler. The interceptor is implemented to 
        // specifically handle these cases. So return true when the Framework detects a unrecognized element
        // or attribute to keep the parser running.
        protected override bool OnDeserializeUnrecognizedAttribute( string name, string value )
        {
            return true;
        }

        protected override bool OnDeserializeUnrecognizedElement( string elementName, XmlReader reader )
        {
            return true;
        }

        //
        // The Reset method is called in the nested vdir scenario,
        // where the child inherits the parent's config section.
        // The sequence of calls is as follows:
        // 1. Application accesses the section in the child app
        // 2. The config system walks up the inheritance chain and finds that it can instantiate the section at the parent level.
        // 3. The config system populates the section with the values from the parent, including setting the custom XML property.
        // 4. Now, the config system tries to instantiage the section at the child level. It creates a brand new instance of the section.
        // 5. The config system takes the parent section as a template and uses it to initialize the child (by calling this Reset method).
        // 6. Then the config system populates the child with values that were overwritten at the child level.
        //
        protected override void Reset( ConfigurationElement parentElement )
        {
            base.Reset( parentElement );
            Reset( (ConfigurationElementInterceptor)parentElement );
        }

        public XmlElement ElementAsXml
        {
            get
            {
                if ( elementXml != null )
                {
                    return elementXml.DocumentElement;
                }

                return null;
            }
        }

        public XmlNodeList ChildNodes
        {
            get
            {
                if ( ( elementXml != null ) && ( ElementAsXml.ChildNodes.Count != 0 ) )
                {
                    return ElementAsXml.ChildNodes;
                }

                return null;
            }
        }

        //
        // Copy custom properties from parent level.
        //
        private void Reset( ConfigurationElementInterceptor parentElement )
        {
            this.elementXml = parentElement.elementXml;
        }
    }
#pragma warning restore 1591
}