New test.
[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.Globalization;
36 using System.Runtime.Serialization;
37 using System.Collections;
38 using System.Xml;
39
40 namespace System.Configuration 
41 {
42 /* disable the obsolete warnings about ConfigurationException */
43 #pragma warning disable 618
44
45         [Serializable]
46         public class ConfigurationErrorsException : ConfigurationException
47         {
48                 //
49                 // Constructors
50                 //
51                 public ConfigurationErrorsException ()
52                 {
53                 }
54                 
55                 public ConfigurationErrorsException (string message)
56                         : base (message)
57                 {
58                         bareMessage = message;
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                         bareMessage = message;
102                         this.filename = filename;
103                         this.line = line;
104                 }
105                 
106                 //
107                 // Properties
108                 //
109                 public override string BareMessage
110                 {
111                         get  { return bareMessage; }
112                 }
113
114                 public ICollection Errors
115                 {
116                         get { throw new NotImplementedException (); }
117                 }
118
119                 public override string Filename
120                 {
121                         get { return filename; }
122                 }
123                 
124                 public override 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                 // Methods
141                 //
142                 public static string GetFilename (XmlReader reader)
143                 {
144                         if (reader is XmlTextReader)
145                                 return ((XmlTextReader)reader).BaseURI;
146                         else
147                                 return String.Empty;
148                 }
149
150                 public static int GetLineNumber (XmlReader reader)
151                 {
152                         if (reader is XmlTextReader)
153                                 return ((XmlTextReader)reader).LineNumber;
154                         else
155                                 return 0;
156                 }
157
158                 public static string GetFilename (XmlNode node)
159                 {
160                         if (!(node is IConfigXmlNode))
161                                 return String.Empty;
162
163                         return ((IConfigXmlNode) node).Filename;
164                 }
165
166                 public static int GetLineNumber (XmlNode node)
167                 {
168                         if (!(node is IConfigXmlNode))
169                                 return 0;
170
171                         return ((IConfigXmlNode) node).LineNumber;
172                 }
173                 
174                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
175                 {
176                         base.GetObjectData (info, context);
177                         info.AddValue ("ConfigurationErrors_Filename", filename);
178                         info.AddValue ("ConfigurationErrors_Line", line);
179                 }
180
181                 string bareMessage = "";
182                 string filename = "";
183                 int line = 0;
184         }
185 #pragma warning restore
186 }
187
188 #endif