[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / System.Web / System.Web.UI / BaseParser.cs
blobbf78b68bf5cfbd33b43b0f13bb45cf21e1adb428
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)
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.
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;
39 namespace System.Web.UI
41 // CAS
42 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44 public class BaseParser
46 HttpContext context;
47 string baseDir;
48 string baseVDir;
49 ILocation location;
51 internal string MapPath (string path)
53 return MapPath (path, true);
56 internal string MapPath (string path, bool allowCrossAppMapping)
58 if (context == null)
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;
75 if (val == null)
76 return deflt;
78 hash.Remove (key);
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);
86 return result;
89 internal static string GetString (IDictionary hash, string key, string deflt)
91 string val = hash [key] as string;
92 if (val == null)
93 return deflt;
95 hash.Remove (key);
96 return val;
99 internal static bool IsDirective (string value, char directiveChar)
101 if (value == null || value == String.Empty)
102 return false;
104 value = value.Trim ();
105 if (!StrUtils.StartsWith (value, "<%") || !StrUtils.EndsWith (value, "%>"))
106 return false;
108 int dcIndex = value.IndexOf (directiveChar, 2);
109 if (dcIndex == -1)
110 return false;
112 if (dcIndex == 2)
113 return true;
114 dcIndex--;
116 while (dcIndex >= 2) {
117 if (!Char.IsWhiteSpace (value [dcIndex]))
118 return false;
119 dcIndex--;
122 return true;
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)
137 if (parms == null)
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);
146 else
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 {
166 get {
167 if (baseDir == null)
168 baseDir = MapPath (BaseVirtualDir, false);
170 return baseDir;
174 internal virtual string BaseVirtualDir {
175 get {
176 if (baseVDir == null)
177 baseVDir = VirtualPathUtility.GetDirectory (context.Request.FilePath);
179 return baseVDir;
182 set {
183 if (VirtualPathUtility.IsRooted (value))
184 baseVDir = VirtualPathUtility.ToAbsolute (value);
185 else
186 baseVDir = 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;
194 if (vp == null)
195 return WebConfigurationManager.GetSection (section) as TSection;
196 else
197 return WebConfigurationManager.GetSection (section, vp) as TSection;
200 internal VirtualPath VirtualPath {
201 get;
202 set;
205 internal CompilationSection CompilationConfig {
206 get { return GetConfigSection <CompilationSection> ("system.web/compilation"); }