This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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 //
10
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 using System;
33 using System.Collections;
34 using System.IO;
35 using System.Web;
36 using System.Web.Compilation;
37 using System.Web.Configuration;
38 using System.Web.Util;
39
40 namespace System.Web.UI
41 {
42         public class BaseParser
43         {
44                 HttpContext context;
45                 string baseDir;
46                 string baseVDir;
47                 ILocation location;
48                 CompilationConfiguration compilationConfig;
49
50                 internal string MapPath (string path)
51                 {
52                         return MapPath (path, true);
53                 }
54
55                 internal string MapPath (string path, bool allowCrossAppMapping)
56                 {
57                         if (context == null)
58                                 throw new HttpException ("context is null!!");
59
60                         return context.Request.MapPath (path, BaseVirtualDir, allowCrossAppMapping);
61                 }
62
63                 internal string PhysicalPath (string path)
64                 {
65                         if (Path.DirectorySeparatorChar != '/')
66                                 path = path.Replace ('/', '\\');
67                                 
68                         return Path.Combine (BaseDir, path);
69                 }
70
71                 internal bool GetBool (Hashtable hash, string key, bool deflt)
72                 {
73                         string val = hash [key] as string;
74                         if (val == null)
75                                 return deflt;
76
77                         hash.Remove (key);
78
79                         bool result = false;
80                         if (String.Compare (val, "true", true) == 0)
81                                 result = true;
82                         else if (String.Compare (val, "false", true) != 0)
83                                 ThrowParseException ("Invalid value for " + key);
84
85                         return result;
86                 }
87
88                 internal static string GetString (Hashtable hash, string key, string deflt)
89                 {
90                         string val = hash [key] as string;
91                         if (val == null)
92                                 return deflt;
93
94                         hash.Remove (key);
95                         return val;
96                 }
97                 
98                 internal void ThrowParseException (string message)
99                 {
100                         throw new ParseException (location, message);
101                 }
102                 
103                 internal void ThrowParseException (string message, Exception inner)
104                 {
105                         throw new ParseException (location, message, inner);
106                 }
107                 
108                 internal ILocation Location {
109                         get { return location; }
110                         set { location = value; }
111                 }
112
113                 internal HttpContext Context {
114                         get { return context; }
115                         set { context = value; }
116                 }
117
118                 internal string BaseDir {
119                         get {
120                                 if (baseDir == null)
121                                         baseDir = MapPath (BaseVirtualDir, false);
122
123                                 return baseDir;
124                         }
125                 }
126
127                 internal virtual string BaseVirtualDir {
128                         get {
129                                 if (baseVDir == null)
130                                         baseVDir = UrlUtils.GetDirectory (context.Request.FilePath);
131
132                                 return baseVDir;
133                         }
134
135                         set { baseVDir = value; }
136                 }
137
138                 internal CompilationConfiguration CompilationConfig {
139                         get {
140                                 if (compilationConfig == null)
141                                         compilationConfig = CompilationConfiguration.GetInstance (context);
142
143                                 return compilationConfig;
144                         }
145                 }
146         }
147 }
148