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