Merge pull request #225 from mistoll/master
[mono.git] / mcs / class / System.Configuration / System.Configuration / ConfigurationErrorsException.cs
1 //
2 // System.Configuration.ConfigurationErrorsException.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //      Chris Toshok (toshok@ximian.com)
7 //
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 #if NET_2_0
33
34 using System;
35 using System.Configuration.Internal;
36 using System.Globalization;
37 using System.Runtime.Serialization;
38 using System.Collections;
39 using System.Xml;
40
41 namespace System.Configuration 
42 {
43 /* disable the obsolete warnings about ConfigurationException */
44 #pragma warning disable 618
45
46         [Serializable]
47         public class ConfigurationErrorsException : ConfigurationException
48         {
49                 //
50                 // Constructors
51                 //
52                 public ConfigurationErrorsException ()
53                 {
54                 }
55                 
56                 public ConfigurationErrorsException (string message)
57                         : base (message)
58                 {
59                 }
60
61                 protected ConfigurationErrorsException (SerializationInfo info, StreamingContext context)
62                         : base (info, context)
63                 {
64                         filename = info.GetString ("ConfigurationErrors_Filename");
65                         line = info.GetInt32 ("ConfigurationErrors_Line");
66                 }
67
68                 public ConfigurationErrorsException (string message, Exception inner)
69                         : base (message, inner)
70                 {
71                 }
72
73                 public ConfigurationErrorsException (string message, XmlNode node)
74                         : this (message, null, GetFilename (node), GetLineNumber (node))
75                 {
76                 }
77
78                 public ConfigurationErrorsException (string message, Exception inner, XmlNode node)
79                         : this (message, inner, GetFilename (node), GetLineNumber (node))
80                 {
81                 }
82                 
83                 public ConfigurationErrorsException (string message, XmlReader reader)
84                         : this (message, null, GetFilename (reader), GetLineNumber (reader))
85                 {
86                 }
87
88                 public ConfigurationErrorsException (string message, Exception inner, XmlReader reader)
89                         : this (message, inner, GetFilename (reader), GetLineNumber (reader))
90                 {
91                 }
92                 
93                 public ConfigurationErrorsException (string message, string filename, int line)
94                         : this (message, null, filename, line)
95                 {
96                 }
97
98                 public ConfigurationErrorsException (string message, Exception inner, string filename, int line)
99                         : base (message, inner)
100                 {
101                         this.filename = filename;
102                         this.line = line;
103                 }
104                 
105                 //
106                 // Properties
107                 //
108                 public override string BareMessage {
109                         get  { return base.BareMessage; }
110                 }
111
112                 public ICollection Errors {
113                         get { throw new NotImplementedException (); }
114                 }
115
116                 public override string Filename {
117                         get { return filename; }
118                 }
119                 
120                 public override int Line {
121                         get { return line; }
122                 }
123
124                 public override string Message {
125                         get {
126                                 string msg;
127                                 if (!String.IsNullOrEmpty (filename)) {
128                                         if (line != 0)
129                                                 msg = BareMessage + " (" + filename + " line " + line + ")";
130                                         else
131                                                 msg = BareMessage + " (" + filename + ")";
132                                 } else {
133                                         if (line != 0)
134                                                 msg = BareMessage + " (line " + line + ")";
135                                         else
136                                                 msg = BareMessage;
137                                 }
138                                 return msg;
139                         }
140                 }
141                 //
142                 // Methods
143                 //
144                 public static string GetFilename (XmlReader reader)
145                 {
146                         // FIXME: eliminate this silly compatibility.
147                         if (reader is IConfigErrorInfo)
148                                 return ((IConfigErrorInfo) reader).Filename;
149
150                         return reader != null ? reader.BaseURI : null;
151                 }
152
153                 public static int GetLineNumber (XmlReader reader)
154                 {
155                         // FIXME: eliminate this silly compatibility.
156                         if (reader is IConfigErrorInfo)
157                                 return ((IConfigErrorInfo) reader).LineNumber;
158
159                         IXmlLineInfo li = reader as IXmlLineInfo;
160                         return li != null ? li.LineNumber : 0;
161                 }
162
163                 public static string GetFilename (XmlNode node)
164                 {
165                         if (!(node is IConfigErrorInfo))
166                                 return null;
167
168                         return ((IConfigErrorInfo) node).Filename;
169                 }
170
171                 public static int GetLineNumber (XmlNode node)
172                 {
173                         if (!(node is IConfigErrorInfo))
174                                 return 0;
175
176                         return ((IConfigErrorInfo) node).LineNumber;
177                 }
178                 
179                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
180                 {
181                         base.GetObjectData (info, context);
182                         info.AddValue ("ConfigurationErrors_Filename", filename);
183                         info.AddValue ("ConfigurationErrors_Line", line);
184                 }
185
186                 readonly string filename;
187                 readonly int line;
188         }
189 #pragma warning restore
190 }
191
192 #endif