Merge pull request #2059 from ermshiperete/Xamarin-33968
[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-2010 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.Globalization;
37 using System.Web.Util;
38
39 namespace System.Web.UI
40 {
41         // CAS
42         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         public class BaseParser
45         {
46                 HttpContext context;
47                 string baseDir;
48                 string baseVDir;
49                 ILocation location;
50
51                 internal string MapPath (string path)
52                 {
53                         return MapPath (path, true);
54                 }
55
56                 internal string MapPath (string path, bool allowCrossAppMapping)
57                 {
58                         if (context == null)
59                                 throw new HttpException ("context is null!!");
60
61                         return context.Request.MapPath (path, BaseVirtualDir, allowCrossAppMapping);
62                 }
63
64                 internal string PhysicalPath (string path)
65                 {
66                         if (Path.DirectorySeparatorChar != '/')
67                                 path = path.Replace ('/', '\\');
68                                 
69                         return Path.Combine (BaseDir, path);
70                 }
71
72                 internal bool GetBool (IDictionary hash, string key, bool deflt)
73                 {
74                         string val = hash [key] as string;
75                         if (val == null)
76                                 return deflt;
77
78                         hash.Remove (key);
79
80                         bool result = false;
81                         if (String.Compare (val, "true", true, Helpers.InvariantCulture) == 0)
82                                 result = true;
83                         else if (String.Compare (val, "false", true, Helpers.InvariantCulture) != 0)
84                                 ThrowParseException ("Invalid value for " + key);
85
86                         return result;
87                 }
88
89                 internal static string GetString (IDictionary hash, string key, string deflt)
90                 {
91                         string val = hash [key] as string;
92                         if (val == null)
93                                 return deflt;
94
95                         hash.Remove (key);
96                         return val;
97                 }
98
99                 internal static bool IsDirective (string value, char directiveChar)
100                 {
101                         if (value == null || value == String.Empty)
102                                 return false;
103                         
104                         value = value.Trim ();
105                         if (!StrUtils.StartsWith (value, "<%") || !StrUtils.EndsWith (value, "%>"))
106                                 return false;
107
108                         int dcIndex = value.IndexOf (directiveChar, 2);
109                         if (dcIndex == -1)
110                                 return false;
111
112                         if (dcIndex == 2)
113                                 return true;
114                         dcIndex--;
115                         
116                         while (dcIndex >= 2) {
117                                 if (!Char.IsWhiteSpace (value [dcIndex]))
118                                         return false;
119                                 dcIndex--;
120                         }
121
122                         return true;
123                 }
124                 
125                 internal static bool IsDataBound (string value)
126                 {
127                         return IsDirective (value, '#');
128                 }
129
130                 internal static bool IsExpression (string value)
131                 {
132                         return IsDirective (value, '$');
133                 }
134                 
135                 internal void ThrowParseException (string message, params object[] parms)
136                 {
137                         if (parms == null)
138                                 throw new ParseException (location, message);
139                         throw new ParseException (location, String.Format (message, parms));
140                 }
141                 
142                 internal void ThrowParseException (string message, Exception inner, params object[] parms)
143                 {
144                         if (parms == null || parms.Length == 0)
145                                 throw new ParseException (location, message, inner);
146                         else
147                                 throw new ParseException (location, String.Format (message, parms), inner);
148                 }
149
150                 internal void ThrowParseFileNotFound (string path, params object[] parms)
151                 {
152                         ThrowParseException ("The file '" + path + "' does not exist", parms);
153                 }
154                 
155                 internal ILocation Location {
156                         get { return location; }
157                         set { location = value; }
158                 }
159
160                 internal HttpContext Context {
161                         get { return context; }
162                         set { context = value; }
163                 }
164
165                 internal string BaseDir {
166                         get {
167                                 if (baseDir == null)
168                                         baseDir = MapPath (BaseVirtualDir, false);
169
170                                 return baseDir;
171                         }
172                 }
173
174                 internal virtual string BaseVirtualDir {
175                         get {
176                                 if (baseVDir == null)
177                                         baseVDir = VirtualPathUtility.GetDirectory (context.Request.FilePath);
178                                 
179                                 return baseVDir;
180                         }
181
182                         set {
183                                 if (VirtualPathUtility.IsRooted (value))
184                                         baseVDir = VirtualPathUtility.ToAbsolute (value);
185                                 else
186                                         baseVDir = value; 
187                         }
188                 }
189
190                 internal TSection GetConfigSection <TSection> (string section) where TSection: global::System.Configuration.ConfigurationSection
191                 {
192                         VirtualPath vpath = VirtualPath;
193                         string vp = vpath != null ? vpath.Absolute : null;
194                         if (vp == null)
195                                 return WebConfigurationManager.GetSection (section) as TSection;
196                         else
197                                 return WebConfigurationManager.GetSection (section, vp) as TSection;
198                 }
199                 
200                 internal VirtualPath VirtualPath {
201                         get;
202                         set;
203                 }
204
205                 internal CompilationSection CompilationConfig {
206                         get { return GetConfigSection <CompilationSection> ("system.web/compilation"); }
207                 }
208         }
209 }
210