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 PagesEnableSessionState enableSessionState = PagesEnableSessionState.True;
47 bool enableViewStateMac = true;
53 string responseEncoding;
62 Type baseType = typeof (Page);
69 string styleSheetTheme;
70 bool enable_event_validation;
71 bool maintainScrollPositionOnPostBack;
72 int maxPageStateFieldLength = -1;
73 string pageParserFilter = String.Empty;
74 Type previousPageType;
79 LoadPagesConfigDefaults ();
82 internal PageParser (string virtualPath, string inputFile, HttpContext context)
85 BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
86 InputFile = inputFile;
87 SetBaseType (PagesConfig.PageBaseType);
88 AddApplicationAssembly ();
89 LoadPagesConfigDefaults ();
93 internal PageParser (string virtualPath, TextReader reader, HttpContext context)
96 BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
98 SetBaseType (PagesConfig.PageBaseType);
99 AddApplicationAssembly ();
100 LoadPagesConfigDefaults ();
104 internal override void LoadPagesConfigDefaults ()
106 base.LoadPagesConfigDefaults ();
108 PagesSection ps = PagesConfig;
110 PagesConfiguration ps = PagesConfig;
113 notBuffer = !ps.Buffer;
114 enableSessionState = ps.EnableSessionState;
115 enableViewStateMac = ps.EnableViewStateMac;
116 smartNavigation = ps.SmartNavigation;
117 validateRequest = ps.ValidateRequest;
119 masterPage = ps.MasterPageFile;
120 if (masterPage.Length == 0)
123 enable_event_validation = ps.EnableEventValidation;
124 maxPageStateFieldLength = ps.MaxPageStateFieldLength;
125 pageParserFilter = ps.PageParserFilterType;
127 if (theme.Length == 0)
129 styleSheetTheme = ps.StyleSheetTheme;
130 if (styleSheetTheme.Length == 0)
131 styleSheetTheme = null;
132 maintainScrollPositionOnPostBack = ps.MaintainScrollPositionOnPostBack;
136 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
140 PageParser pp = new PageParser (virtualPath, inputFile, context);
141 IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
145 internal override void ProcessMainAttributes (Hashtable atts)
147 // note: the 'enableSessionState' configuration property is
148 // processed in a case-sensitive manner while the page-level
149 // attribute is processed case-insensitive
150 string enabless = GetString (atts, "EnableSessionState", null);
151 if (enabless != null) {
152 if (String.Compare (enabless, "readonly", true) == 0)
153 enableSessionState = PagesEnableSessionState.ReadOnly;
154 else if (String.Compare (enabless, "true", true) == 0)
155 enableSessionState = PagesEnableSessionState.True;
156 else if (String.Compare (enabless, "false", true) == 0)
157 enableSessionState = PagesEnableSessionState.False;
159 ThrowParseException ("Invalid value for enableSessionState: " + enabless);
162 string cp = GetString (atts, "CodePage", null);
164 if (responseEncoding != null)
165 ThrowParseException ("CodePage and ResponseEncoding are " +
166 "mutually exclusive.");
170 codepage = (int) UInt32.Parse (cp);
172 ThrowParseException ("Invalid value for CodePage: " + cp);
176 Encoding.GetEncoding (codepage);
178 ThrowParseException ("Unsupported codepage: " + cp);
182 responseEncoding = GetString (atts, "ResponseEncoding", null);
183 if (responseEncoding != null) {
185 ThrowParseException ("CodePage and ResponseEncoding are " +
186 "mutually exclusive.");
189 Encoding.GetEncoding (responseEncoding);
191 ThrowParseException ("Unsupported encoding: " + responseEncoding);
195 contentType = GetString (atts, "ContentType", null);
197 string lcidStr = GetString (atts, "LCID", null);
198 if (lcidStr != null) {
200 lcid = (int) UInt32.Parse (lcidStr);
202 ThrowParseException ("Invalid value for LCID: " + lcid);
205 CultureInfo ci = null;
207 ci = new CultureInfo (lcid);
209 ThrowParseException ("Unsupported LCID: " + lcid);
212 if (ci.IsNeutralCulture) {
213 string suggestedCulture = SuggestCulture (ci.Name);
214 string fmt = "LCID attribute must be set to a non-neutral Culture.";
215 if (suggestedCulture != null) {
216 ThrowParseException (fmt + " Please try one of these: " +
219 ThrowParseException (fmt);
224 culture = GetString (atts, "Culture", null);
225 if (culture != null) {
227 ThrowParseException ("Culture and LCID are mutually exclusive.");
229 CultureInfo ci = null;
232 if (!culture.StartsWith ("auto"))
234 ci = new CultureInfo (culture);
236 ThrowParseException ("Unsupported Culture: " + culture);
239 if (ci != null && ci.IsNeutralCulture) {
240 string suggestedCulture = SuggestCulture (culture);
241 string fmt = "Culture attribute must be set to a non-neutral Culture.";
242 if (suggestedCulture != null)
243 ThrowParseException (fmt +
244 " Please try one of these: " + suggestedCulture);
246 ThrowParseException (fmt);
250 uiculture = GetString (atts, "UICulture", null);
251 if (uiculture != null) {
252 CultureInfo ci = null;
255 if (!uiculture.StartsWith ("auto"))
257 ci = new CultureInfo (uiculture);
259 ThrowParseException ("Unsupported Culture: " + uiculture);
262 if (ci != null && ci.IsNeutralCulture) {
263 string suggestedCulture = SuggestCulture (uiculture);
264 string fmt = "UICulture attribute must be set to a non-neutral Culture.";
265 if (suggestedCulture != null)
266 ThrowParseException (fmt +
267 " Please try one of these: " + suggestedCulture);
269 ThrowParseException (fmt);
273 string tracestr = GetString (atts, "Trace", null);
274 if (tracestr != null) {
276 atts ["Trace"] = tracestr;
277 trace = GetBool (atts, "Trace", false);
280 string tracemodes = GetString (atts, "TraceMode", null);
281 if (tracemodes != null) {
284 tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
289 if (!valid || tracemode == TraceMode.Default)
290 ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
291 "one of the following values: SortByTime, SortByCategory.");
294 errorPage = GetString (atts, "ErrorPage", null);
295 validateRequest = GetBool (atts, "ValidateRequest", validateRequest);
296 clientTarget = GetString (atts, "ClientTarget", null);
297 if (clientTarget != null) {
298 clientTarget = clientTarget.Trim ();
300 ClientTargetSection sec = (ClientTargetSection)WebConfigurationManager.GetSection ("system.web/clientTarget");
301 ClientTarget ct = null;
303 if ((ct = sec.ClientTargets [clientTarget]) == null)
304 clientTarget = clientTarget.ToLower (CultureInfo.InvariantCulture);
306 if (ct == null && (ct = sec.ClientTargets [clientTarget]) == null) {
307 ThrowParseException (String.Format (
308 "ClientTarget '{0}' is an invalid alias. See the " +
309 "documentation for <clientTarget> config. section.",
312 clientTarget = ct.UserAgent;
314 NameValueCollection coll;
315 coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
319 ct = coll [clientTarget];
321 ct = coll [clientTarget.ToLower ()];
325 ThrowParseException (String.Format (
326 "ClientTarget '{0}' is an invalid alias. See the " +
327 "documentation for <clientTarget> config. section.",
330 clientTarget = (string) ct;
334 notBuffer = !GetBool (atts, "Buffer", true);
337 masterPage = GetString (atts, "MasterPageFile", masterPage);
339 // Make sure the page exists
340 if (!String.IsNullOrEmpty (masterPage))
341 MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
343 title = GetString(atts, "Title", null);
345 theme = GetString (atts, "Theme", theme);
346 styleSheetTheme = GetString (atts, "StyleSheetTheme", styleSheetTheme);
347 enable_event_validation = GetBool (atts, "EnableEventValidation", true);
348 maintainScrollPositionOnPostBack = GetBool (atts, "MaintainScrollPositionOnPostBack", maintainScrollPositionOnPostBack);
351 GetString (atts, "EnableViewStateMac", null);
352 GetString (atts, "SmartNavigation", null);
354 base.ProcessMainAttributes (atts);
358 internal override void AddDirective (string directive, Hashtable atts)
360 bool isMasterType = String.Compare ("MasterType", directive, StringComparison.OrdinalIgnoreCase) == 0;
361 bool isPreviousPageType = isMasterType ? false : String.Compare ("PreviousPageType", directive,
362 StringComparison.OrdinalIgnoreCase) == 0;
364 string typeName = null;
365 string virtualPath = null;
368 if (isMasterType || isPreviousPageType) {
369 typeName = GetString (atts, "TypeName", null);
370 virtualPath = GetString (atts, "VirtualPath", null);
372 if (typeName != null && virtualPath != null)
373 ThrowParseException (
374 String.Format ("The '{0}' directive must have exactly one attribute: TypeName or VirtualPath", directive));
375 if (typeName != null) {
376 type = LoadType (typeName);
378 ThrowParseException (String.Format ("Could not load type '{0}'.", typeName));
379 } else if (virtualPath != null) {
380 string mappedPath = MapPath (virtualPath);
382 type = masterType = MasterPageParser.GetCompiledMasterType (virtualPath,
384 HttpContext.Current);
386 type = previousPageType = GetCompiledPageType (virtualPath, mappedPath,
387 HttpContext.Current);
389 ThrowParseException (
390 String.Format ("The {0} directive must have either a TypeName or a VirtualPath attribute.", directive));
392 AddAssembly (type.Assembly, true);
394 base.AddDirective (directive, atts);
398 static string SuggestCulture (string culture)
400 string retval = null;
401 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
402 if (ci.Name.StartsWith (culture))
403 retval += ci.Name + " ";
408 public static Type GetCompiledPageType (string virtualPath, string inputFile, HttpContext context)
410 PageParser pp = new PageParser (virtualPath, inputFile, context);
411 return pp.CompileIntoType ();
414 protected override Type CompileIntoType ()
416 AspGenerator generator = new AspGenerator (this);
417 return generator.GetCompiledType ();
420 internal bool EnableSessionState {
422 return enableSessionState == PagesEnableSessionState.True ||
423 ReadOnlySessionState;
427 internal bool EnableViewStateMac {
428 get { return enableViewStateMac; }
431 internal bool SmartNavigation {
432 get { return smartNavigation; }
435 internal bool ReadOnlySessionState {
437 return enableSessionState == PagesEnableSessionState.ReadOnly;
441 internal bool HaveTrace {
442 get { return haveTrace; }
445 internal bool Trace {
446 get { return trace; }
449 internal TraceMode TraceMode {
450 get { return tracemode; }
453 internal override Type DefaultBaseType {
454 get { return baseType; }
457 internal override string DefaultBaseTypeName {
458 get { return "System.Web.UI.Page"; }
461 internal override string DefaultDirectiveName {
462 get { return "page"; }
465 internal string ResponseEncoding {
466 get { return responseEncoding; }
469 internal string ContentType {
470 get { return contentType; }
473 internal int CodePage {
474 get { return codepage; }
477 internal string Culture {
478 get { return culture; }
481 internal string UICulture {
482 get { return uiculture; }
489 internal string ErrorPage {
490 get { return errorPage; }
493 internal bool ValidateRequest {
494 get { return validateRequest; }
497 internal string ClientTarget {
498 get { return clientTarget; }
501 internal bool NotBuffer {
502 get { return notBuffer; }
506 internal string Theme {
507 get { return theme; }
510 internal string StyleSheetTheme {
511 get { return styleSheetTheme; }
514 internal string MasterPageFile {
515 get { return masterPage; }
518 internal Type MasterType {
519 get { return masterType; }
522 internal string Title {
523 get { return title; }
526 internal bool EnableEventValidation {
527 get { return enable_event_validation; }
530 internal bool MaintainScrollPositionOnPostBack {
531 get { return maintainScrollPositionOnPostBack; }
534 internal int MaxPageStateFieldLength {
535 get { return maxPageStateFieldLength; }
538 internal string PageParserFilterType {
539 get { return pageParserFilter; }
542 internal Type PreviousPageType {
543 get { return previousPageType; }