New test.
[mono.git] / mcs / class / System.Web / System.Web.UI / BaseParser.cs
1 //
2 // System.Web.UI.BaseParser.cs
3 //
4 // Authors:
5 //      Duncan Mak  (duncan@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) 2002 Ximian, Inc. (http://www.ximian.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections;
32 using System.IO;
33 using System.Security.Permissions;
34 using System.Web.Compilation;
35 using System.Web.Configuration;
36 using System.Web.Util;
37
38 namespace System.Web.UI
39 {
40         // CAS
41         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         public class BaseParser
44         {
45                 HttpContext context;
46                 string baseDir;
47                 string baseVDir;
48                 ILocation location;
49 #if !NET_2_0
50                 CompilationConfiguration compilationConfig;
51 #endif
52
53                 internal string MapPath (string path)
54                 {
55                         return MapPath (path, true);
56                 }
57
58                 internal string MapPath (string path, bool allowCrossAppMapping)
59                 {
60                         if (context == null)
61                                 throw new HttpException ("context is null!!");
62
63                         return context.Request.MapPath (path, BaseVirtualDir, allowCrossAppMapping);
64                 }
65
66                 internal string PhysicalPath (string path)
67                 {
68                         if (Path.DirectorySeparatorChar != '/')
69                                 path = path.Replace ('/', '\\');
70                                 
71                         return Path.Combine (BaseDir, path);
72                 }
73
74                 internal bool GetBool (Hashtable hash, string key, bool deflt)
75                 {
76                         string val = hash [key] as string;
77                         if (val == null)
78                                 return deflt;
79
80                         hash.Remove (key);
81
82                         bool result = false;
83                         if (String.Compare (val, "true", true) == 0)
84                                 result = true;
85                         else if (String.Compare (val, "false", true) != 0)
86                                 ThrowParseException ("Invalid value for " + key);
87
88                         return result;
89                 }
90
91                 internal static string GetString (Hashtable hash, string key, string deflt)
92                 {
93                         string val = hash [key] as string;
94                         if (val == null)
95                                 return deflt;
96
97                         hash.Remove (key);
98                         return val;
99                 }
100                 
101                 internal void ThrowParseException (string message)
102                 {
103                         throw new ParseException (location, message);
104                 }
105                 
106                 internal void ThrowParseException (string message, Exception inner)
107                 {
108                         throw new ParseException (location, message, inner);
109                 }
110                 
111                 internal ILocation Location {
112                         get { return location; }
113                         set { location = value; }
114                 }
115
116                 internal HttpContext Context {
117                         get { return context; }
118                         set { context = value; }
119                 }
120
121                 internal string BaseDir {
122                         get {
123                                 if (baseDir == null)
124                                         baseDir = MapPath (BaseVirtualDir, false);
125
126                                 return baseDir;
127                         }
128                 }
129
130                 internal virtual string BaseVirtualDir {
131                         get {
132                                 if (baseVDir == null)
133                                         baseVDir = UrlUtils.GetDirectory (context.Request.FilePath);
134
135                                 return baseVDir;
136                         }
137
138                         set { baseVDir = value; }
139                 }
140
141 #if NET_2_0
142                 internal CompilationSection CompilationConfig {
143                         get {
144                                 return WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
145                         }
146                 }
147
148 #else
149                 internal CompilationConfiguration CompilationConfig {
150                         get {
151                                 if (compilationConfig == null)
152                                         compilationConfig = CompilationConfiguration.GetInstance (context);
153
154                                 return compilationConfig;
155                         }
156                 }
157 #endif
158         }
159 }
160