BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / System / System.Configuration / ConfigXmlDocument.cs
1 //
2 // System.Configuration.ConfigXmlDocument
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0 && CONFIGURATION_DEP
31 using System.Configuration.Internal;
32 #endif
33 using System.IO;
34 using System.Security;
35 using System.Security.Permissions;
36
37 #if (XML_DEP)
38 using System.Xml;
39
40 namespace System.Configuration
41 {
42         [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
43         public sealed class ConfigXmlDocument : XmlDocument, IConfigXmlNode
44 #if NET_2_0 && CONFIGURATION_DEP
45                 , IConfigErrorInfo
46 #endif
47         {
48                 XmlTextReader reader;
49                 string fileName;
50                 int lineNumber;
51
52                 public override XmlAttribute CreateAttribute (string prefix,
53                                                               string localName,
54                                                               string namespaceUri)
55                 {
56                         return new ConfigXmlAttribute (this, prefix, localName, namespaceUri);
57                 }
58
59                 public override XmlCDataSection CreateCDataSection (string data)
60                 {
61                         return new ConfigXmlCDataSection (this, data);
62                 }
63
64                 public override XmlComment CreateComment (string comment)
65                 {
66                         return new ConfigXmlComment (this, comment);
67                 }
68
69                 public override XmlElement CreateElement (string prefix, string localName, string namespaceUri)
70                 {
71                         return new ConfigXmlElement (this, prefix, localName, namespaceUri);
72                 }
73
74                 public override XmlSignificantWhitespace CreateSignificantWhitespace (string data)
75                 {
76                         return base.CreateSignificantWhitespace (data);
77                 }
78
79                 public override XmlText CreateTextNode (string text)
80                 {
81                         return new ConfigXmlText (this, text);
82                 }
83
84                 public override XmlWhitespace CreateWhitespace (string data)
85                 {
86                         return base.CreateWhitespace (data);
87                 }
88
89                 public override void Load (string filename)
90                 {
91                         XmlTextReader rd = new XmlTextReader (filename);
92                         try {
93                                 rd.MoveToContent ();
94                                 LoadSingleElement (filename, rd);
95                         } finally {
96                                 rd.Close ();
97                         }
98                 }
99
100                 public void LoadSingleElement (string filename, XmlTextReader sourceReader)
101                 {
102                         fileName = filename;
103                         lineNumber = sourceReader.LineNumber;
104                         string xml = sourceReader.ReadOuterXml();
105                         reader = new XmlTextReader (new StringReader (xml), sourceReader.NameTable);
106                         Load (reader);
107                         reader.Close ();
108                 }
109
110                 public string Filename
111                 {
112                         get {
113                                 if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
114                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
115                                 }
116                                 return fileName;
117                         }
118                 }
119
120                 public int LineNumber
121                 {
122                         get {
123                                 return lineNumber;
124                         }
125                 }
126
127 #if NET_2_0 && CONFIGURATION_DEP
128                 string System.Configuration.Internal.IConfigErrorInfo.Filename {
129                         get { return Filename; }
130                 }
131
132                 int System.Configuration.Internal.IConfigErrorInfo.LineNumber {
133                         get { return LineNumber; }
134                 }
135 #endif
136
137                 string IConfigXmlNode.Filename {
138                         get { return Filename; }
139                 }
140
141                 int IConfigXmlNode.LineNumber {
142                         get { return LineNumber; }
143                 }
144
145                 //
146                 // Wrappers for Xml* that just provide file name and line number addition
147                 //
148                 class ConfigXmlAttribute : XmlAttribute, IConfigXmlNode
149 #if NET_2_0 && CONFIGURATION_DEP
150                         , IConfigErrorInfo
151 #endif
152                 {
153                         string fileName;
154                         int lineNumber;
155
156                         public ConfigXmlAttribute (ConfigXmlDocument document,
157                                                    string prefix,
158                                                    string localName,
159                                                    string namespaceUri)
160                                 : base (prefix, localName, namespaceUri, document)
161                         {
162                                 fileName = document.fileName;
163                                 lineNumber = document.LineNumber;
164                         }
165
166                         public string Filename
167                         {
168                                 get {
169                                         if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
170                                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
171                                         }
172                                         return fileName;
173                                 }
174                         }
175
176                         public int LineNumber
177                         {
178                                 get {
179                                         return lineNumber;
180                                 }
181                         }
182                 }
183                 
184                 class ConfigXmlCDataSection : XmlCDataSection, IConfigXmlNode
185 #if NET_2_0 && CONFIGURATION_DEP
186                         , IConfigErrorInfo
187 #endif
188                 {
189                         string fileName;
190                         int lineNumber;
191
192                         public ConfigXmlCDataSection (ConfigXmlDocument document, string data)
193                                 : base (data, document)
194                         {
195                                 fileName = document.fileName;
196                                 lineNumber = document.LineNumber;
197                         }
198
199                         public string Filename
200                         {
201                                 get {
202                                         if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
203                                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
204                                         }
205                                         return fileName;
206                                 }
207                         }
208
209                         public int LineNumber
210                         {
211                                 get {
212                                         return lineNumber;
213                                 }
214                         }
215                 }
216                 
217                 class ConfigXmlComment : XmlComment, IConfigXmlNode
218                 {
219                         string fileName;
220                         int lineNumber;
221
222                         public ConfigXmlComment (ConfigXmlDocument document, string comment)
223                                 : base (comment, document)
224                         {
225                                 fileName = document.fileName;
226                                 lineNumber = document.LineNumber;
227                         }
228
229                         public string Filename
230                         {
231                                 get {
232                                         if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
233                                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
234                                         }
235                                         return fileName;
236                                 }
237                         }
238
239                         public int LineNumber
240                         {
241                                 get {
242                                         return lineNumber;
243                                 }
244                         }
245                 }
246         
247                 class ConfigXmlElement : XmlElement, IConfigXmlNode
248 #if NET_2_0 && CONFIGURATION_DEP
249                         , IConfigErrorInfo
250 #endif
251                 {
252                         string fileName;
253                         int lineNumber;
254
255                         public ConfigXmlElement (ConfigXmlDocument document,
256                                                  string prefix,
257                                                  string localName,
258                                                  string namespaceUri)
259                                 : base (prefix, localName, namespaceUri, document)
260                         {
261                                 fileName = document.fileName;
262                                 lineNumber = document.LineNumber;
263                         }
264
265                         public string Filename
266                         {
267                                 get {
268                                         if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
269                                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
270                                         }
271                                         return fileName;
272                                 }
273                         }
274
275                         public int LineNumber
276                         {
277                                 get {
278                                         return lineNumber;
279                                 }
280                         }
281                 }
282
283                 class ConfigXmlText : XmlText, IConfigXmlNode
284 #if NET_2_0 && CONFIGURATION_DEP
285                         , IConfigErrorInfo
286 #endif
287                 {
288                         string fileName;
289                         int lineNumber;
290
291                         public ConfigXmlText (ConfigXmlDocument document, string data)
292                                 : base (data, document)
293                         {
294                                 fileName = document.fileName;
295                                 lineNumber = document.LineNumber;
296                         }
297
298                         public string Filename
299                         {
300                                 get {
301                                         if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
302                                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
303                                         }
304                                         return fileName;
305                                 }
306                         }
307
308                         public int LineNumber
309                         {
310                                 get {
311                                         return lineNumber;
312                                 }
313                         }
314                 }
315         }
316 }
317
318 #endif