Moved TestConfiguration.cs to Npgsql.
[mono.git] / mcs / class / System.Web / System.Web / VirtualPathUtility.cs
1 //
2 // System.Web.VirtualPathUtility.cs
3 //
4 // Author:
5 //      Chris Toshok (toshok@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8
9 //
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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.Web.Util;
35
36 namespace System.Web {
37
38         public static class VirtualPathUtility
39         {
40                 public static string AppendTrailingSlash (string virtualPath)
41                 {
42                         if (virtualPath == null)
43                                 return virtualPath;
44
45                         int length = virtualPath.Length;
46                         if (length == 0 || virtualPath [length - 1] == '/')
47                                 return virtualPath;
48
49                         return virtualPath + "/";
50                 }
51
52                 public static string Combine (string basePath, string relativePath)
53                 {
54                         if (basePath == null || basePath == "")
55                                 throw new ArgumentNullException ("basePath");
56
57                         if (relativePath == null || relativePath == "")
58                                 throw new ArgumentNullException ("relativePath");
59
60                         if (basePath [0] != '/')
61                                 throw new ArgumentException ("basePath is not an absolute path", "basePath");
62
63                         if (relativePath [0] != '/')
64                                 return "/" + relativePath;
65
66                         return UrlUtils.Combine (basePath, relativePath);
67                 }
68
69                 public static string GetDirectory (string virtualPath)
70                 {
71                         if (virtualPath == null || virtualPath == "") // Yes, "" throws an ArgumentNullException
72                                 throw new ArgumentNullException ("virtualPath");
73
74                         if (virtualPath [0] != '/')
75                                 throw new ArgumentException ("The virtual path is not rooted", "virtualPath");
76
77                         string result = UrlUtils.GetDirectory (virtualPath);
78                         return AppendTrailingSlash (result);
79                 }
80
81                 public static string GetExtension (string virtualPath)
82                 {
83                         string filename = GetFileName (virtualPath);
84                         int dot = filename.LastIndexOf ('.');
85                         if (dot == -1 || dot == filename.Length + 1)
86                                 return "";
87
88                         return filename.Substring (dot);
89                 }
90
91                 public static string GetFileName (string virtualPath)
92                 {
93                         if (virtualPath == null || virtualPath == "") // Yes, "" throws an ArgumentNullException
94                                 throw new ArgumentNullException ("virtualPath");
95                         
96                         return UrlUtils.GetFile (RemoveTrailingSlash (virtualPath));
97                 }
98
99                 public static bool IsAbsolute (string virtualPath)
100                 {
101                         if (virtualPath == "" || virtualPath == null)
102                                 throw new ArgumentNullException ("virtualPath");
103
104                         return (virtualPath [0] == '/');
105                 }
106
107                 public static bool IsAppRelative (string virtualPath)
108                 {
109                         if (virtualPath == null || virtualPath == "")
110                                 throw new ArgumentNullException ("virtualPath");
111
112                         string vpath = HttpRuntime.AppDomainAppVirtualPath;
113                         if (vpath == null)
114                                 return false;
115
116                         return virtualPath.StartsWith (AppendTrailingSlash (vpath));
117                 }
118
119                 public static string MakeRelative (string fromPath, string toPath)
120                 {
121                         if (fromPath == null || toPath == null)
122                                 throw new NullReferenceException (); // yeah!
123
124                         if (toPath == "")
125                                 return toPath;
126
127                         if (toPath [0] != '/')
128                                 throw new ArgumentOutOfRangeException (); // This is what MS does.
129
130                         if (fromPath.Length > 0 && fromPath [0] != '/')
131                                 throw new ArgumentOutOfRangeException (); // This is what MS does.
132
133                         Uri from = new Uri ("http://nothing" + fromPath);
134                         return from.MakeRelativeUri (new Uri ("http://nothing" + toPath)).AbsolutePath;
135                 }
136
137                 public static string RemoveTrailingSlash (string virtualPath)
138                 {
139                         if (virtualPath == null || virtualPath == "")
140                                 return null;
141
142                         int last = virtualPath.Length - 1;
143                         if (last == 0 || virtualPath [last] != '/')
144                                 return virtualPath;
145
146                         return virtualPath.Substring (0, last);
147                 }
148
149                 public static string ToAbsolute (string virtualPath)
150                 {
151                         string apppath = HttpRuntime.AppDomainAppVirtualPath;
152                         if (apppath == null)
153                                 throw new HttpException ("The path to the application is not known");
154
155                         return ToAbsolute (virtualPath,apppath);
156                 }
157
158                 public static string ToAbsolute (string virtualPath, string applicationPath)
159                 {
160                         if (applicationPath == null || applicationPath == "")
161                                 throw new ArgumentNullException ("applicationPath");
162
163                         if (virtualPath == null || virtualPath == "")
164                                 throw new ArgumentNullException ("virtualPath");
165
166                         if (virtualPath.StartsWith (".."))
167                                 throw new ArgumentException (String.Format ("Relative path not allowed: '{0}'", virtualPath));
168
169                         if (applicationPath [0] != '/')
170                                 throw new ArgumentOutOfRangeException ("appPath is not rooted", "applicationPath");
171
172                         return UrlUtils.Combine (applicationPath, virtualPath);
173
174                 }
175
176                 public static string ToAppRelative (string virtualPath)
177                 {
178                         string apppath = HttpRuntime.AppDomainAppVirtualPath;
179                         if (apppath == null)
180                                 throw new HttpException ("The path to the application is not known");
181
182                         return ToAppRelative (apppath, virtualPath);
183                 }
184
185                 public static string ToAppRelative (string virtualPath, string applicationPath)
186                 {
187                         if (applicationPath == null || applicationPath == "")
188                                 throw new ArgumentNullException ("applicationPath");
189
190                         if (virtualPath == null || virtualPath == "")
191                                 throw new ArgumentNullException ("virtualPath");
192
193                         if (virtualPath.StartsWith (".."))
194                                 throw new ArgumentException (String.Format ("Relative path not allowed: '{0}'", virtualPath));
195
196                         if (applicationPath [0] != '/')
197                                 throw new ArgumentOutOfRangeException ("appPath is not rooted", "applicationPath");
198
199                         return MakeRelative (applicationPath, virtualPath);
200                 }
201         }
202 }
203
204 #endif
205