2 // System.Web.UI.BaseParser.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (C) 2002 Ximian, Inc. (http://www.ximian.com)
9 // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System
.Collections
;
33 using System
.Security
.Permissions
;
34 using System
.Web
.Compilation
;
35 using System
.Web
.Configuration
;
36 using System
.Globalization
;
37 using System
.Web
.Util
;
39 namespace System
.Web
.UI
42 [AspNetHostingPermission (SecurityAction
.LinkDemand
, Level
= AspNetHostingPermissionLevel
.Minimal
)]
43 [AspNetHostingPermission (SecurityAction
.InheritanceDemand
, Level
= AspNetHostingPermissionLevel
.Minimal
)]
44 public class BaseParser
51 internal string MapPath (string path
)
53 return MapPath (path
, true);
56 internal string MapPath (string path
, bool allowCrossAppMapping
)
59 throw new HttpException ("context is null!!");
61 return context
.Request
.MapPath (path
, BaseVirtualDir
, allowCrossAppMapping
);
64 internal string PhysicalPath (string path
)
66 if (Path
.DirectorySeparatorChar
!= '/')
67 path
= path
.Replace ('/', '\\');
69 return Path
.Combine (BaseDir
, path
);
72 internal bool GetBool (IDictionary hash
, string key
, bool deflt
)
74 string val
= hash
[key
] as string;
81 if (String
.Compare (val
, "true", true, Helpers
.InvariantCulture
) == 0)
83 else if (String
.Compare (val
, "false", true, Helpers
.InvariantCulture
) != 0)
84 ThrowParseException ("Invalid value for " + key
);
89 internal static string GetString (IDictionary hash
, string key
, string deflt
)
91 string val
= hash
[key
] as string;
99 internal static bool IsDirective (string value, char directiveChar
)
101 if (value == null || value == String
.Empty
)
104 value = value.Trim ();
105 if (!StrUtils
.StartsWith (value, "<%") || !StrUtils
.EndsWith (value, "%>"))
108 int dcIndex
= value.IndexOf (directiveChar
, 2);
116 while (dcIndex
>= 2) {
117 if (!Char
.IsWhiteSpace (value [dcIndex
]))
125 internal static bool IsDataBound (string value)
127 return IsDirective (value, '#');
130 internal static bool IsExpression (string value)
132 return IsDirective (value, '$');
135 internal void ThrowParseException (string message
, params object[] parms
)
138 throw new ParseException (location
, message
);
139 throw new ParseException (location
, String
.Format (message
, parms
));
142 internal void ThrowParseException (string message
, Exception inner
, params object[] parms
)
144 if (parms
== null || parms
.Length
== 0)
145 throw new ParseException (location
, message
, inner
);
147 throw new ParseException (location
, String
.Format (message
, parms
), inner
);
150 internal void ThrowParseFileNotFound (string path
, params object[] parms
)
152 ThrowParseException ("The file '" + path
+ "' does not exist", parms
);
155 internal ILocation Location
{
156 get { return location; }
157 set { location = value; }
160 internal HttpContext Context
{
161 get { return context; }
162 set { context = value; }
165 internal string BaseDir
{
168 baseDir
= MapPath (BaseVirtualDir
, false);
174 internal virtual string BaseVirtualDir
{
176 if (baseVDir
== null)
177 baseVDir
= VirtualPathUtility
.GetDirectory (context
.Request
.FilePath
);
183 if (VirtualPathUtility
.IsRooted (value))
184 baseVDir
= VirtualPathUtility
.ToAbsolute (value);
190 internal TSection GetConfigSection
<TSection
> (string section
) where TSection
: global::System
.Configuration
.ConfigurationSection
192 VirtualPath vpath
= VirtualPath
;
193 string vp
= vpath
!= null ? vpath
.Absolute
: null;
195 return WebConfigurationManager
.GetSection (section
) as TSection
;
197 return WebConfigurationManager
.GetSection (section
, vp
) as TSection
;
200 internal VirtualPath VirtualPath
{
205 internal CompilationSection CompilationConfig
{
206 get { return GetConfigSection <CompilationSection> ("system.web/compilation"); }