Merge pull request #4453 from lambdageek/bug-49721
[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 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 #pragma warning disable 618
41
42 namespace System.Configuration
43 {
44         [PermissionSet (SecurityAction.LinkDemand, Unrestricted = true)]
45         public sealed class ConfigXmlDocument : XmlDocument, IConfigXmlNode
46 #if CONFIGURATION_DEP
47                 , IConfigErrorInfo
48 #endif
49         {
50                 XmlTextReader reader;
51                 string fileName;
52                 int lineNumber;
53
54                 public override XmlAttribute CreateAttribute (string prefix,
55                                                               string localName,
56                                                               string namespaceUri)
57                 {
58                         return new ConfigXmlAttribute (this, prefix, localName, namespaceUri);
59                 }
60
61                 public override XmlCDataSection CreateCDataSection (string data)
62                 {
63                         return new ConfigXmlCDataSection (this, data);
64                 }
65
66                 public override XmlComment CreateComment (string data)
67                 {
68                         return new ConfigXmlComment (this, data);
69                 }
70
71                 public override XmlElement CreateElement (string prefix, string localName, string namespaceUri)
72                 {
73                         return new ConfigXmlElement (this, prefix, localName, namespaceUri);
74                 }
75
76                 public override XmlSignificantWhitespace CreateSignificantWhitespace (string data)
77                 {
78                         return base.CreateSignificantWhitespace (data);
79                 }
80
81                 public override XmlText CreateTextNode (string text)
82                 {
83                         return new ConfigXmlText (this, text);
84                 }
85
86                 public override XmlWhitespace CreateWhitespace (string data)
87                 {
88                         return base.CreateWhitespace (data);
89                 }
90
91                 public override void Load (string filename)
92                 {
93                         XmlTextReader rd = new XmlTextReader (filename);
94                         try {
95                                 rd.MoveToContent ();
96                                 LoadSingleElement (filename, rd);
97                         } finally {
98                                 rd.Close ();
99                         }
100                 }
101
102                 public void LoadSingleElement (string filename, XmlTextReader sourceReader)
103                 {
104                         fileName = filename;
105                         lineNumber = sourceReader.LineNumber;
106                         string xml = sourceReader.ReadOuterXml();
107                         reader = new XmlTextReader (new StringReader (xml), sourceReader.NameTable);
108                         Load (reader);
109                         reader.Close ();
110                 }
111
112                 public string Filename
113                 {
114                         get {
115                                 if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
116                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
117                                 }
118                                 return fileName;
119                         }
120                 }
121
122                 public int LineNumber
123                 {
124                         get {
125                                 return lineNumber;
126                         }
127                 }
128
129 #if CONFIGURATION_DEP
130                 string System.Configuration.Internal.IConfigErrorInfo.Filename {
131                         get { return Filename; }
132                 }
133
134                 int System.Configuration.Internal.IConfigErrorInfo.LineNumber {
135                         get { return LineNumber; }
136                 }
137 #endif
138
139                 string IConfigXmlNode.Filename {
140                         get { return Filename; }
141                 }
142
143                 int IConfigXmlNode.LineNumber {
144                         get { return LineNumber; }
145                 }
146
147                 //
148                 // Wrappers for Xml* that just provide file name and line number addition
149                 //
150                 class ConfigXmlAttribute : XmlAttribute, IConfigXmlNode
151 #if CONFIGURATION_DEP
152                         , IConfigErrorInfo
153 #endif
154                 {
155                         string fileName;
156                         int lineNumber;
157
158                         public ConfigXmlAttribute (ConfigXmlDocument document,
159                                                    string prefix,
160                                                    string localName,
161                                                    string namespaceUri)
162                                 : base (prefix, localName, namespaceUri, document)
163                         {
164                                 fileName = document.fileName;
165                                 lineNumber = document.LineNumber;
166                         }
167
168                         public string Filename
169                         {
170                                 get {
171                                         if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
172                                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
173                                         }
174                                         return fileName;
175                                 }
176                         }
177
178                         public int LineNumber
179                         {
180                                 get {
181                                         return lineNumber;
182                                 }
183                         }
184                 }
185                 
186                 class ConfigXmlCDataSection : XmlCDataSection, IConfigXmlNode
187 #if CONFIGURATION_DEP
188                         , IConfigErrorInfo
189 #endif
190                 {
191                         string fileName;
192                         int lineNumber;
193
194                         public ConfigXmlCDataSection (ConfigXmlDocument document, string data)
195                                 : base (data, document)
196                         {
197                                 fileName = document.fileName;
198                                 lineNumber = document.LineNumber;
199                         }
200
201                         public string Filename
202                         {
203                                 get {
204                                         if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
205                                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
206                                         }
207                                         return fileName;
208                                 }
209                         }
210
211                         public int LineNumber
212                         {
213                                 get {
214                                         return lineNumber;
215                                 }
216                         }
217                 }
218                 
219                 class ConfigXmlComment : XmlComment, IConfigXmlNode
220                 {
221                         string fileName;
222                         int lineNumber;
223
224                         public ConfigXmlComment (ConfigXmlDocument document, string comment)
225                                 : base (comment, document)
226                         {
227                                 fileName = document.fileName;
228                                 lineNumber = document.LineNumber;
229                         }
230
231                         public string Filename
232                         {
233                                 get {
234                                         if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
235                                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
236                                         }
237                                         return fileName;
238                                 }
239                         }
240
241                         public int LineNumber
242                         {
243                                 get {
244                                         return lineNumber;
245                                 }
246                         }
247                 }
248         
249                 class ConfigXmlElement : XmlElement, IConfigXmlNode
250 #if CONFIGURATION_DEP
251                         , IConfigErrorInfo
252 #endif
253                 {
254                         string fileName;
255                         int lineNumber;
256
257                         public ConfigXmlElement (ConfigXmlDocument document,
258                                                  string prefix,
259                                                  string localName,
260                                                  string namespaceUri)
261                                 : base (prefix, localName, namespaceUri, document)
262                         {
263                                 fileName = document.fileName;
264                                 lineNumber = document.LineNumber;
265                         }
266
267                         public string Filename
268                         {
269                                 get {
270                                         if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
271                                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
272                                         }
273                                         return fileName;
274                                 }
275                         }
276
277                         public int LineNumber
278                         {
279                                 get {
280                                         return lineNumber;
281                                 }
282                         }
283                 }
284
285                 class ConfigXmlText : XmlText, IConfigXmlNode
286 #if CONFIGURATION_DEP
287                         , IConfigErrorInfo
288 #endif
289                 {
290                         string fileName;
291                         int lineNumber;
292
293                         public ConfigXmlText (ConfigXmlDocument document, string data)
294                                 : base (data, document)
295                         {
296                                 fileName = document.fileName;
297                                 lineNumber = document.LineNumber;
298                         }
299
300                         public string Filename
301                         {
302                                 get {
303                                         if ((fileName != null) && (fileName.Length > 0) && SecurityManager.SecurityEnabled) {
304                                                 new FileIOPermission (FileIOPermissionAccess.PathDiscovery, fileName).Demand ();
305                                         }
306                                         return fileName;
307                                 }
308                         }
309
310                         public int LineNumber
311                         {
312                                 get {
313                                         return lineNumber;
314                                 }
315                         }
316                 }
317         }
318 }
319
320 #endif