2003-10-14 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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 using System;
12 using System.Collections;
13 using System.IO;
14 using System.Web;
15 using System.Web.Compilation;
16 using System.Web.Configuration;
17 using System.Web.Util;
18
19 namespace System.Web.UI
20 {
21         public class BaseParser
22         {
23                 HttpContext context;
24                 string baseDir;
25                 string baseVDir;
26                 ILocation location;
27                 CompilationConfiguration compilationConfig;
28
29                 internal string MapPath (string path)
30                 {
31                         return MapPath (path, true);
32                 }
33
34                 internal string MapPath (string path, bool allowCrossAppMapping)
35                 {
36                         if (context == null)
37                                 throw new HttpException ("context is null!!");
38
39                         return context.Request.MapPath (path, BaseVirtualDir, allowCrossAppMapping);
40                 }
41
42                 internal string PhysicalPath (string path)
43                 {
44                         if (Path.DirectorySeparatorChar != '/')
45                                 path = path.Replace ('/', '\\');
46                                 
47                         return Path.Combine (BaseDir, path);
48                 }
49
50                 internal bool GetBool (Hashtable hash, string key, bool deflt)
51                 {
52                         string val = hash [key] as string;
53                         if (val == null)
54                                 return deflt;
55
56                         hash.Remove (key);
57
58                         bool result = false;
59                         if (String.Compare (val, "true", true) == 0)
60                                 result = true;
61                         else if (String.Compare (val, "false", true) != 0)
62                                 ThrowParseException ("Invalid value for " + key);
63
64                         return result;
65                 }
66
67                 internal static string GetString (Hashtable hash, string key, string deflt)
68                 {
69                         string val = hash [key] as string;
70                         if (val == null)
71                                 return deflt;
72
73                         hash.Remove (key);
74                         return val;
75                 }
76                 
77                 internal void ThrowParseException (string message)
78                 {
79                         throw new ParseException (location, message);
80                 }
81                 
82                 internal void ThrowParseException (string message, Exception inner)
83                 {
84                         throw new ParseException (location, message, inner);
85                 }
86                 
87                 internal ILocation Location {
88                         get { return location; }
89                         set { location = value; }
90                 }
91
92                 internal HttpContext Context {
93                         get { return context; }
94                         set { context = value; }
95                 }
96
97                 internal string BaseDir {
98                         get {
99                                 if (baseDir == null)
100                                         baseDir = MapPath (BaseVirtualDir, false);
101
102                                 return baseDir;
103                         }
104                 }
105
106                 internal virtual string BaseVirtualDir {
107                         get {
108                                 if (baseVDir == null)
109                                         baseVDir = UrlUtils.GetDirectory (context.Request.FilePath);
110
111                                 return baseVDir;
112                         }
113
114                         set { baseVDir = value; }
115                 }
116
117                 internal CompilationConfiguration CompilationConfig {
118                         get {
119                                 if (compilationConfig == null)
120                                         compilationConfig = CompilationConfiguration.GetInstance (context);
121
122                                 return compilationConfig;
123                         }
124                 }
125         }
126 }
127