2 // System.Web.UI.PageParser
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
31 using System.Collections.Specialized;
32 using System.Globalization;
33 using System.Security.Permissions;
35 using System.Web.Compilation;
36 using System.Web.Configuration;
37 using System.Web.Util;
40 namespace System.Web.UI
42 // CAS - no InheritanceDemand here as the class is sealed
43 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44 public sealed class PageParser : TemplateControlParser
46 bool enableSessionState = true;
51 bool readonlySessionState;
52 string responseEncoding;
61 Type baseType = typeof (Page);
68 string styleSheetTheme;
69 bool enable_event_validation;
76 internal PageParser (string virtualPath, string inputFile, HttpContext context)
79 BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
80 InputFile = inputFile;
81 SetBaseType (PagesConfig.PageBaseType);
82 AddApplicationAssembly ();
86 internal PageParser (string virtualPath, TextReader reader, HttpContext context)
89 BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
91 SetBaseType (PagesConfig.PageBaseType);
92 AddApplicationAssembly ();
96 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
100 PageParser pp = new PageParser (virtualPath, inputFile, context);
101 IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
105 internal override void ProcessMainAttributes (Hashtable atts)
107 string enabless = GetString (atts, "EnableSessionState",
109 PagesConfig.EnableSessionState.ToString()
111 PagesConfig.EnableSessionState
114 if (enabless != null) {
115 readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
116 if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
117 enableSessionState = true;
118 } else if (String.Compare (enabless, "false", true) == 0) {
119 enableSessionState = false;
121 ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
125 string cp = GetString (atts, "CodePage", null);
127 if (responseEncoding != null)
128 ThrowParseException ("CodePage and ResponseEncoding are " +
129 "mutually exclusive.");
133 codepage = (int) UInt32.Parse (cp);
135 ThrowParseException ("Invalid value for CodePage: " + cp);
139 Encoding.GetEncoding (codepage);
141 ThrowParseException ("Unsupported codepage: " + cp);
145 responseEncoding = GetString (atts, "ResponseEncoding", null);
146 if (responseEncoding != null) {
148 ThrowParseException ("CodePage and ResponseEncoding are " +
149 "mutually exclusive.");
152 Encoding.GetEncoding (responseEncoding);
154 ThrowParseException ("Unsupported encoding: " + responseEncoding);
158 contentType = GetString (atts, "ContentType", null);
160 string lcidStr = GetString (atts, "LCID", null);
161 if (lcidStr != null) {
163 lcid = (int) UInt32.Parse (lcidStr);
165 ThrowParseException ("Invalid value for LCID: " + lcid);
168 CultureInfo ci = null;
170 ci = new CultureInfo (lcid);
172 ThrowParseException ("Unsupported LCID: " + lcid);
175 if (ci.IsNeutralCulture) {
176 string suggestedCulture = SuggestCulture (ci.Name);
177 string fmt = "LCID attribute must be set to a non-neutral Culture.";
178 if (suggestedCulture != null) {
179 ThrowParseException (fmt + " Please try one of these: " +
182 ThrowParseException (fmt);
187 culture = GetString (atts, "Culture", null);
188 if (culture != null) {
190 ThrowParseException ("Culture and LCID are mutually exclusive.");
192 CultureInfo ci = null;
194 ci = new CultureInfo (culture);
196 ThrowParseException ("Unsupported Culture: " + culture);
199 if (ci.IsNeutralCulture) {
200 string suggestedCulture = SuggestCulture (culture);
201 string fmt = "Culture attribute must be set to a non-neutral Culture.";
202 if (suggestedCulture != null)
203 ThrowParseException (fmt +
204 " Please try one of these: " + suggestedCulture);
206 ThrowParseException (fmt);
210 uiculture = GetString (atts, "UICulture", null);
211 if (uiculture != null) {
212 CultureInfo ci = null;
214 ci = new CultureInfo (uiculture);
216 ThrowParseException ("Unsupported Culture: " + uiculture);
219 if (ci.IsNeutralCulture) {
220 string suggestedCulture = SuggestCulture (uiculture);
221 string fmt = "UICulture attribute must be set to a non-neutral Culture.";
222 if (suggestedCulture != null)
223 ThrowParseException (fmt +
224 " Please try one of these: " + suggestedCulture);
226 ThrowParseException (fmt);
230 string tracestr = GetString (atts, "Trace", null);
231 if (tracestr != null) {
233 atts ["Trace"] = tracestr;
234 trace = GetBool (atts, "Trace", false);
237 string tracemodes = GetString (atts, "TraceMode", null);
238 if (tracemodes != null) {
241 tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
246 if (!valid || tracemode == TraceMode.Default)
247 ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
248 "one of the following values: SortByTime, SortByCategory.");
251 errorPage = GetString (atts, "ErrorPage", null);
252 validateRequest = GetBool (atts, "ValidateRequest", PagesConfig.ValidateRequest);
253 clientTarget = GetString (atts, "ClientTarget", null);
254 if (clientTarget != null) {
256 ClientTargetSection sec = (ClientTargetSection)WebConfigurationManager.GetSection ("system.web/clientTarget");
257 if (sec.ClientTargets[clientTarget] == null) {
258 ThrowParseException (String.Format (
259 "ClientTarget '{0}' is an invalid alias. See the " +
260 "documentation for <clientTarget> config. section.",
263 clientTarget = sec.ClientTargets[clientTarget].UserAgent;
265 NameValueCollection coll;
266 coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
267 if (coll == null || coll [clientTarget] == null) {
268 ThrowParseException (String.Format (
269 "ClientTarget '{0}' is an invalid alias. See the " +
270 "documentation for <clientTarget> config. section.",
273 clientTarget = (string) coll [clientTarget];
277 notBuffer = !GetBool (atts, "Buffer", true);
280 masterPage = GetString (atts, "MasterPageFile", null);
282 // Make sure the page exists
283 if (masterPage != null)
284 MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
286 title = GetString(atts, "Title", null);
288 theme = GetString (atts, "Theme", null);
289 styleSheetTheme = GetString (atts, "StyleSheetTheme", null);
290 enable_event_validation = GetBool (atts, "EnableEventValidation", true);
293 GetString (atts, "EnableViewStateMac", null);
294 GetString (atts, "SmartNavigation", null);
296 base.ProcessMainAttributes (atts);
300 internal override void AddDirective (string directive, Hashtable atts)
302 if (String.Compare ("MasterType", directive, true) == 0) {
303 string type = GetString (atts, "TypeName", null);
305 masterType = LoadType (type);
306 if (masterType == null)
307 ThrowParseException ("Could not load type '" + type + "'.");
309 string path = GetString (atts, "VirtualPath", null);
311 masterType = MasterPageParser.GetCompiledMasterType (path, MapPath (path), HttpContext.Current);
313 ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute.");
315 AddAssembly (masterType.Assembly, true);
318 base.AddDirective (directive, atts);
322 static string SuggestCulture (string culture)
324 string retval = null;
325 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
326 if (ci.Name.StartsWith (culture))
327 retval += ci.Name + " ";
332 protected override Type CompileIntoType ()
334 AspGenerator generator = new AspGenerator (this);
335 return generator.GetCompiledType ();
338 internal bool EnableSessionState {
339 get { return enableSessionState; }
342 internal bool ReadOnlySessionState {
343 get { return readonlySessionState; }
346 internal bool HaveTrace {
347 get { return haveTrace; }
350 internal bool Trace {
351 get { return trace; }
354 internal TraceMode TraceMode {
355 get { return tracemode; }
358 internal override Type DefaultBaseType {
359 get { return baseType; }
362 internal override string DefaultBaseTypeName {
363 get { return "System.Web.UI.Page"; }
366 internal override string DefaultDirectiveName {
367 get { return "page"; }
370 internal string ResponseEncoding {
371 get { return responseEncoding; }
374 internal string ContentType {
375 get { return contentType; }
378 internal int CodePage {
379 get { return codepage; }
382 internal string Culture {
383 get { return culture; }
386 internal string UICulture {
387 get { return uiculture; }
394 internal string ErrorPage {
395 get { return errorPage; }
398 internal bool ValidateRequest {
399 get { return validateRequest; }
402 internal string ClientTarget {
403 get { return clientTarget; }
406 internal bool NotBuffer {
407 get { return notBuffer; }
411 internal string Theme {
412 get { return theme; }
415 internal string StyleSheetTheme {
416 get { return styleSheetTheme; }
419 internal string MasterPageFile {
420 get { return masterPage; }
423 internal Type MasterType {
424 get { return masterType; }
427 internal string Title {
428 get { return title; }
431 internal bool EnableEventValidation {
432 get { return enable_event_validation; }