2003-04-30 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.Util;
16
17 namespace System.Web.UI
18 {
19         public class BaseParser
20         {
21                 private HttpContext context;
22                 private string baseDir;
23                 private string baseVDir;
24
25                 internal string MapPath (string path)
26                 {
27                         return MapPath (path, true);
28                 }
29
30                 internal string MapPath (string path, bool allowCrossAppMapping)
31                 {
32                         if (context == null)
33                                 throw new HttpException ("context is null!!");
34
35                         return context.Request.MapPath (path, context.Request.ApplicationPath, allowCrossAppMapping);
36                 }
37
38                 internal string PhysicalPath (string path)
39                 {
40                         if (Path.DirectorySeparatorChar != '/')
41                                 path = path.Replace ('/', '\\');
42                                 
43                         return Path.Combine (BaseDir, path);
44                 }
45
46                 internal bool GetBool (Hashtable hash, string key, bool deflt)
47                 {
48                         string val = hash [key] as string;
49                         if (val == null)
50                                 return deflt;
51
52                         hash.Remove (key);
53
54                         bool result;
55                         if (String.Compare (val, "true", true) == 0)
56                                 result = true;
57                         else if (String.Compare (val, "false", true) == 0)
58                                 result = false;
59                         else
60                                 throw new HttpException ("Invalid value for " + key);
61
62                         return result;
63                 }
64
65                 internal string GetString (Hashtable hash, string key, string deflt)
66                 {
67                         string val = hash [key] as string;
68                         if (val == null)
69                                 return deflt;
70
71                         hash.Remove (key);
72                         return val;
73                 }
74                 
75                 //
76                 
77                 internal HttpContext Context {
78                         get { return context; }
79                         set { context = value; }
80                 }
81
82                 internal string BaseDir {
83                         get {
84                                 if (baseDir == null)
85                                         baseDir = MapPath (BaseVirtualDir, false);
86
87                                 return baseDir;
88                         }
89                 }
90
91                 internal virtual string BaseVirtualDir {
92                         get {
93                                 if (baseVDir == null)
94                                         baseVDir = UrlUtils.GetDirectory (context.Request.FilePath);
95
96                                 return baseVDir;
97                         }
98                 }
99         }
100 }
101