File: ConfigUtil.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 (148 lines) | stat: -rw-r--r-- 7,664 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
//------------------------------------------------------------------------------
// <copyright file="ConfigUtil.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.Configuration {
    using System;
    using System.Threading;
    using System.Configuration;
    using System.Xml;
    using System.Web.Compilation;
    using System.Web.Util;

    internal class ConfigUtil {
        private ConfigUtil() {
        }

        internal static void CheckBaseType(Type expectedBaseType, Type userBaseType, string propertyName, ConfigurationElement configElement) {
            // Make sure the base type is valid
            if (!expectedBaseType.IsAssignableFrom(userBaseType)) {
                throw new ConfigurationErrorsException(
                    SR.GetString(SR.Invalid_type_to_inherit_from,
                        userBaseType.FullName,
                        expectedBaseType.FullName), configElement.ElementInformation.Properties[propertyName].Source,
                        configElement.ElementInformation.Properties[propertyName].LineNumber);
            }
        }

        internal static Type GetType(string typeName, string propertyName, ConfigurationElement configElement,
            XmlNode node, bool checkAptcaBit, bool ignoreCase) {

            // We should get either a propertyName/configElement or node, but not both.
            // They are used only for error reporting.
            Debug.Assert((propertyName != null) != (node != null));

            Type val;
            try {
                val = BuildManager.GetType(typeName, true /*throwOnError*/, ignoreCase);
            }
            catch (Exception e) {
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
                    throw;
                }

                if (node != null) {
                    throw new ConfigurationErrorsException(e.Message, e, node);
                }
                else {
                    if (configElement != null) {
                        throw new ConfigurationErrorsException(e.Message, e,
                                                               configElement.ElementInformation.Properties[propertyName].Source,
                                                               configElement.ElementInformation.Properties[propertyName].LineNumber);
                    }
                    else {
                        throw new ConfigurationErrorsException(e.Message, e);
                    }
                }
            }

            // If we're not in full trust, only allow types that have the APTCA bit (ASURT 139687),
            // unless the checkAptcaBit flag is false
            if (checkAptcaBit) {
                if (node != null) {
                    HttpRuntime.FailIfNoAPTCABit(val, node);
                }
                else {
                    HttpRuntime.FailIfNoAPTCABit(val,
                        configElement != null ? configElement.ElementInformation : null,
                        propertyName);
                }
            }

            return val;
        }

        internal static Type GetType(string typeName, string propertyName, ConfigurationElement configElement) {
            return GetType(typeName, propertyName, configElement, true /*checkAptcaBit*/);
        }

        internal static Type GetType(string typeName, string propertyName, ConfigurationElement configElement, bool checkAptcaBit) {
            return GetType(typeName, propertyName, configElement, checkAptcaBit, false);
        }

        internal static Type GetType(string typeName, string propertyName, ConfigurationElement configElement, bool checkAptcaBit, bool ignoreCase) {
            return GetType(typeName, propertyName, configElement, null /*node*/, checkAptcaBit, ignoreCase);
        }

        internal static Type GetType(string typeName, XmlNode node) {
            return GetType(typeName, node, false /*ignoreCase*/);
        }

        internal static Type GetType(string typeName, XmlNode node, bool ignoreCase) {
            return GetType(typeName, null, null, node, true /*checkAptcaBit*/, ignoreCase);
        }

        internal static void CheckAssignableType(Type baseType, Type type, ConfigurationElement configElement, string propertyName) {
            if (!baseType.IsAssignableFrom(type)) {
                throw new ConfigurationErrorsException(
                                SR.GetString(SR.Type_doesnt_inherit_from_type, type.FullName, baseType.FullName),
                                configElement.ElementInformation.Properties[propertyName].Source, configElement.ElementInformation.Properties[propertyName].LineNumber);
            }
        }

        internal static void CheckAssignableType(Type baseType, Type baseType2, Type type, ConfigurationElement configElement, string propertyName) {
            if (!baseType.IsAssignableFrom(type) && !baseType2.IsAssignableFrom(type)) {
                throw new ConfigurationErrorsException(
                                SR.GetString(SR.Type_doesnt_inherit_from_type, type.FullName, baseType.FullName),
                                configElement.ElementInformation.Properties[propertyName].Source,
                                configElement.ElementInformation.Properties[propertyName].LineNumber);
            }
        }

        internal static bool IsTypeHandlerOrFactory(Type t) {
            return typeof(IHttpHandler).IsAssignableFrom(t)
                || typeof(IHttpHandlerFactory).IsAssignableFrom(t);
        }

        internal static ConfigurationErrorsException MakeConfigurationErrorsException(string message, Exception innerException = null, PropertyInformation configProperty = null) {
            return new ConfigurationErrorsException(message, innerException, (configProperty != null) ? configProperty.Source : null, (configProperty != null) ? configProperty.LineNumber : 0);
        }

        // If a configuration section has a default value coming from 4.0 config and the AppDomain is opted in to auto-config upgrade,
        // set the configuration section element explicitly to its new 4.5 value. If the property has been explicitly set, no change
        // will be made.
        internal static void SetFX45DefaultValue(ConfigurationSection configSection, ConfigurationProperty property, object newDefaultValue) {
            if (BinaryCompatibility.Current.TargetsAtLeastFramework45 && !configSection.IsReadOnly()) {
                PropertyInformation propInfo = configSection.ElementInformation.Properties[property.Name];
                Debug.Assert(propInfo != null);
                Debug.Assert(propInfo.Type.IsInstanceOfType(newDefaultValue));

                if (propInfo.ValueOrigin == PropertyValueOrigin.Default) {
                    try {
                        propInfo.Value = newDefaultValue;
                    }
                    catch (ConfigurationErrorsException) {
                        // Calling the Value setter might throw if the configuration element is locked.
                        // We can technically override the "is locked?" check by calling the appropriate
                        // method, but for now let's just honor the locks and ignore these errors. The
                        // config sections we're touching shouldn't really ever be locked anyway, so
                        // nobody should ever run into this in practice.
                    }
                }
            }
        }

    }
}