File: ConfigurationException.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 (225 lines) | stat: -rw-r--r-- 8,921 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//------------------------------------------------------------------------------
// <copyright file="ConfigurationException.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Configuration {
    using System.Configuration.Internal;
    using System.Globalization;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Security;
    using System.Security.Permissions;
    using System.Xml;
    using System.Collections;
    using System.Runtime.Versioning;

    // A config exception can contain a filename (of a config file)
    // and a line number (of the location in the file in which a problem was
    // encountered).
    // 
    // Section handlers should throw this exception (or subclasses)
    // together with filename and line nubmer information where possible.
    [Serializable]
    public class ConfigurationException : SystemException {
        private const string    HTTP_PREFIX = "http:";

        private string          _filename; 
        private int             _line;     

        void Init(string filename, int line) {
            HResult = HResults.Configuration;
            _filename = filename;
            _line = line;
        }

        // Default ctor is required for serialization.
        protected ConfigurationException(SerializationInfo info, StreamingContext context) : base(info, context) { 
            Init(info.GetString("filename"), info.GetInt32("line"));
        }

        [Obsolete("This class is obsolete, to create a new exception create a System." +
                  "Configuration!System.Configuration.ConfigurationErrorsException")]
        public ConfigurationException() : 
                this(null, null, null, 0) {}

        [Obsolete("This class is obsolete, to create a new exception create a System." +
                  "Configuration!System.Configuration.ConfigurationErrorsException")]
        public ConfigurationException(string message) : 
                this(message, null, null, 0) {}

        [Obsolete("This class is obsolete, to create a new exception create a System." +
                  "Configuration!System.Configuration.ConfigurationErrorsException")]
        public ConfigurationException(string message, Exception inner) : 
                this(message, inner, null, 0) {}


        [Obsolete("This class is obsolete, to create a new exception create a System." +
                  "Configuration!System.Configuration.ConfigurationErrorsException")]
        public ConfigurationException(string message, XmlNode node) : 
                this(message, null, GetUnsafeXmlNodeFilename(node), GetXmlNodeLineNumber(node)) {}

        [Obsolete("This class is obsolete, to create a new exception create a System." +
                  "Configuration!System.Configuration.ConfigurationErrorsException")]
        public ConfigurationException(string message, Exception inner, XmlNode node) : 
                this(message, inner, GetUnsafeXmlNodeFilename(node), GetXmlNodeLineNumber(node)) {}


        [Obsolete("This class is obsolete, to create a new exception create a System." +
                  "Configuration!System.Configuration.ConfigurationErrorsException")]
        public ConfigurationException(string message, string filename, int line) :
                this(message, null, filename, line) {}

        [Obsolete("This class is obsolete, to create a new exception create a System." +
                  "Configuration!System.Configuration.ConfigurationErrorsException")]
        public ConfigurationException(string message, Exception inner, string filename, int line) : base(message, inner) {
            Init(filename, line);
        }

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

        // The message includes the file/line number information.  
        // To get the message without the extra information, use BareMessage.
        public override string Message {
            get {
                string file = Filename;
                if (!string.IsNullOrEmpty(file)) {
                    if (Line != 0) {
                        return BareMessage + " (" + file + " line " + Line.ToString(CultureInfo.InvariantCulture) + ")";
                    }
                    else {
                        return BareMessage + " (" + file + ")";
                    }
                }
                else if (Line != 0) {
                    return BareMessage + " (line " + Line.ToString("G", CultureInfo.InvariantCulture) + ")";
                }
                else {
                    return BareMessage;
                }
            }
        }

        public virtual string BareMessage {
            get {
                return base.Message;
            }
        }

        public virtual string Filename {
            [ResourceExposure(ResourceScope.Machine)]
            [ResourceConsumption(ResourceScope.Machine)]
            get {
                return SafeFilename(_filename);
            }
        }

        public virtual int Line {
            get {
                return _line;
            }
        }

        [Obsolete("This class is obsolete, use System.Configuration!System.Configuration." +
                  "ConfigurationErrorsException.GetFilename instead")]
        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)]
        public static string GetXmlNodeFilename(XmlNode node) {
            return SafeFilename(GetUnsafeXmlNodeFilename(node));
        }

        [Obsolete("This class is obsolete, use System.Configuration!System.Configuration." +
                  "ConfigurationErrorsException.GetLinenumber instead")]
        public static int GetXmlNodeLineNumber(XmlNode node) {
            IConfigErrorInfo configNode = node as IConfigErrorInfo;

            if (configNode != null) {
                return configNode.LineNumber;
            }
            return 0;
        }

        [FileIOPermission(SecurityAction.Assert, AllFiles=FileIOPermissionAccess.PathDiscovery)]
        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)]
        private static string FullPathWithAssert(string filename) {
            string fullPath = null;

            try {
                fullPath = Path.GetFullPath(filename);
            }
            catch {
            }

            return fullPath;
        }

        // 
        // Internal Helper to strip a full path to just filename.ext when caller 
        // does not have path discovery to the path (used for sane error handling).
        //
        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)]
        internal static string SafeFilename(string filename) {
            if (string.IsNullOrEmpty(filename)) {
                return filename;
            }

            // configuration file can be an http URL in IE
            if (filename.StartsWith(HTTP_PREFIX, StringComparison.OrdinalIgnoreCase)) {
                return filename;
            }

            //
            // If it is a relative path, return it as is. 
            // This could happen if the exception was constructed from the serialization constructor,
            // and the caller did not have PathDiscoveryPermission for the file.
            //
            try {
                if (!Path.IsPathRooted(filename)) {
                    return filename;
                }
            }
            catch {
                return null;
            }

            try {
                // Confirm that it is a full path.
                // GetFullPath will also Demand PathDiscovery for the resulting path
                string fullPath = Path.GetFullPath(filename);
            } 
            catch (SecurityException) {
                // Get just the name of the file without the directory part.
                try {
                    string fullPath = FullPathWithAssert(filename);
                    filename = Path.GetFileName(fullPath);
                }
                catch {
                    filename = null;
                }
            }
            catch {
                filename = null;
            }

            return filename;
        }

        private static string GetUnsafeXmlNodeFilename(XmlNode node) {
            IConfigErrorInfo configNode = node as IConfigErrorInfo;

            if (configNode != null) {
                return configNode.Filename;
            }

            return string.Empty;
        }
    }
}