copied mono-api-diff.cs from mono-2-2 branch so new patch can be applied and history...
[mono.git] / mcs / class / System.Configuration / System.Configuration / ConfigurationLocation.cs
1 //
2 // System.Configuration.ConfigurationLocation.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //  Lluis Sanchez Gual (lluis@novell.com)
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
28 //
29
30 #if NET_2_0
31
32 using System.Xml;
33 using System.IO;
34
35 namespace System.Configuration {
36
37         public class ConfigurationLocation
38         {
39                 static readonly char[] pathTrimChars = { '/' };
40                 
41                 string path;
42                 Configuration configuration;
43                 Configuration parent;
44                 string xmlContent;
45                 bool parentResolved;
46                 bool allowOverride;
47                 
48                 internal ConfigurationLocation()
49                 {
50                 }
51                 
52                 internal ConfigurationLocation (string path, string xmlContent, Configuration parent, bool allowOverride)
53                 {
54                         if (!String.IsNullOrEmpty (path)) {
55                                 switch (path [0]) {
56                                         case ' ':
57                                         case '.':
58                                         case '/':
59                                         case '\\':
60                                                 throw new ConfigurationErrorsException ("<location> path attribute must be a relative virtual path.  It cannot start with any of ' ' '.' '/' or '\\'.");
61                                 }
62
63                                 path = path.TrimEnd (pathTrimChars);
64                         }
65                         
66                         this.path = path;
67                         this.xmlContent = xmlContent;
68                         this.parent = parent;
69                         this.allowOverride = allowOverride;
70                 }
71                 
72                 public string Path {
73                         get { return path; }
74                 }
75                 
76                 internal bool AllowOverride {
77                         get { return allowOverride; }
78                 }
79                 
80                 internal string XmlContent {
81                         get { return xmlContent; }
82                 }
83                 
84                 internal Configuration OpenedConfiguration {
85                         get { return configuration; }
86                 }
87
88                 public Configuration OpenConfiguration ()
89                 {
90                         if (configuration == null) {
91                                 if (!parentResolved) {
92                                         Configuration parentFile = parent.GetParentWithFile ();
93                                         if (parentFile != null) {
94                                                 string parentRelativePath = parent.ConfigHost.GetConfigPathFromLocationSubPath (parent.LocationConfigPath, path);
95                                                 parent = parentFile.FindLocationConfiguration (parentRelativePath, parent);
96                                         }
97                                 }
98                                 
99                                 configuration = new Configuration (parent, path);
100                                 using (XmlTextReader tr = new ConfigXmlTextReader (new StringReader (xmlContent), path))
101                                         configuration.ReadData (tr, allowOverride);
102
103                                 xmlContent = null;
104                         }
105                         return configuration;
106                 }
107                 
108                 internal void SetParentConfiguration (Configuration parent)
109                 {
110                         parentResolved = true;
111                         this.parent = parent;
112                         if (configuration != null)
113                                 configuration.Parent = parent;
114                 }
115         }
116 }
117
118 #endif