2005-04-12 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / System / System.Configuration / ConfigurationException.cs
1 //
2 // System.Configuration.ConfigurationException.cs
3 //
4 // Author:
5 //   Duncan Mak (duncan@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Globalization;
33 using System.Runtime.Serialization;
34
35 #if (XML_DEP)
36 using System.Xml;
37 #endif
38
39 namespace System.Configuration 
40 {
41         [Serializable]
42         public class ConfigurationException : SystemException
43         {
44                 // Fields               
45                 string bareMessage;
46                 string filename;
47                 int line;
48
49                 //
50                 // Constructors
51                 //
52                 public ConfigurationException ()
53                         : base (Locale.GetText ("There is an error in a configuration setting."))
54                 {
55                         filename = null;
56                         bareMessage = Locale.GetText ("There is an error in a configuration setting.");
57                         line = 0;
58                 }
59                 
60                 public ConfigurationException (string message)
61                         : base (message)
62                 {
63                         bareMessage = message;
64                 }
65
66                 protected ConfigurationException (SerializationInfo info, StreamingContext context)
67                         : base (info, context)
68                 {
69                         filename = info.GetString ("filename");
70                         line = info.GetInt32 ("line");
71                 }
72
73                 public ConfigurationException (string message, Exception inner)
74                         : base (message, inner)
75                 {
76                         bareMessage = message;
77                 }
78
79 #if (XML_DEP)
80                 public ConfigurationException (string message, XmlNode node)
81                         : base (message)
82                 {
83                         filename = GetXmlNodeFilename(node);
84                         line = GetXmlNodeLineNumber(node);
85                         bareMessage = message;
86                 }
87
88                 public ConfigurationException (string message, Exception inner, XmlNode node)
89                         : base (message, inner)
90                 {
91                         filename = GetXmlNodeFilename (node);
92                         line = GetXmlNodeLineNumber (node);
93                         bareMessage = message;
94                 }
95 #endif
96                 public ConfigurationException (string message, string filename, int line)
97                         : base (message)
98                 {
99                         bareMessage = message;
100                         this.filename = filename;
101                         this.line= line;
102                 }
103
104                 public ConfigurationException (string message, Exception inner, string filename, int line)
105                         : base (message)
106                 {
107                         bareMessage = message;
108                         this.filename = filename;
109                         this.line = line;
110                 }
111                 //
112                 // Properties
113                 //
114                 public string BareMessage
115                 {
116                         get  { return bareMessage; }
117                 }
118
119                 public string Filename
120                 {
121                         get { return filename; }
122                 }
123                 
124                 public int Line
125                 {
126                         get { return line; }
127                 }
128
129                 public override string Message
130                 {
131                         get {
132                                 string baseMsg = base.Message;
133                                 string f = (filename == null) ? String.Empty : filename;
134                                 string l = (line == 0) ? String.Empty : (" line " + line);
135
136                                 return baseMsg + " (" + f + l + ")";
137                         }
138                 }
139
140                 //
141                 // Methods
142                 //
143 #if (XML_DEP)
144                 public static string GetXmlNodeFilename (XmlNode node)
145                 {
146                         if (!(node is IConfigXmlNode))
147                                 return String.Empty;
148
149                         return ((IConfigXmlNode) node).Filename;
150                 }
151
152                 public static int GetXmlNodeLineNumber (XmlNode node)
153                 {
154                         if (!(node is IConfigXmlNode))
155                                 return 0;
156
157                         return ((IConfigXmlNode) node).LineNumber;
158                 }
159 #endif
160                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
161                 {
162                         base.GetObjectData (info, context);
163                         info.AddValue ("filename", filename);
164                         info.AddValue ("line", line);
165                 }
166         }
167 }