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;
47 bool enableViewStateMac = true;
48 bool smartNavigation = false;
53 bool readonlySessionState;
54 string responseEncoding;
63 Type baseType = typeof (Page);
70 string styleSheetTheme;
71 bool enable_event_validation;
72 bool maintainScrollPositionOnPostBack;
73 int maxPageStateFieldLength = -1;
74 string pageParserFilter = String.Empty;
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;
115 switch (ps.EnableSessionState) {
116 case PagesEnableSessionState.True:
117 case PagesEnableSessionState.ReadOnly:
118 enableSessionState = true;
122 enableSessionState = false;
126 if (String.Compare (ps.EnableSessionState, "true", true, CultureInfo.InvariantCulture) == 0)
127 enableSessionState = true;
129 enableSessionState = false;
132 enableViewStateMac = ps.EnableViewStateMac;
133 smartNavigation = ps.SmartNavigation;
134 validateRequest = ps.ValidateRequest;
136 masterPage = ps.MasterPageFile;
137 if (masterPage.Length == 0)
140 enable_event_validation = ps.EnableEventValidation;
141 maxPageStateFieldLength = ps.MaxPageStateFieldLength;
142 pageParserFilter = ps.PageParserFilterType;
144 if (theme.Length == 0)
146 styleSheetTheme = ps.StyleSheetTheme;
147 if (styleSheetTheme.Length == 0)
148 styleSheetTheme = null;
149 maintainScrollPositionOnPostBack = ps.MaintainScrollPositionOnPostBack;
153 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
157 PageParser pp = new PageParser (virtualPath, inputFile, context);
158 IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
162 internal override void ProcessMainAttributes (Hashtable atts)
164 // note: the 'enableSessionState' configuration property is
165 // processed in a case-sensitive manner while the page-level
166 // attribute is processed case-insensitive
167 string enabless = GetString (atts, "EnableSessionState", enableSessionState.ToString ());
168 if (enabless != null) {
169 readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
170 if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
171 enableSessionState = true;
172 } else if (String.Compare (enabless, "false", true) == 0) {
173 enableSessionState = false;
175 ThrowParseException ("Invalid value for enableSessionState: " + enabless);
179 string cp = GetString (atts, "CodePage", null);
181 if (responseEncoding != null)
182 ThrowParseException ("CodePage and ResponseEncoding are " +
183 "mutually exclusive.");
187 codepage = (int) UInt32.Parse (cp);
189 ThrowParseException ("Invalid value for CodePage: " + cp);
193 Encoding.GetEncoding (codepage);
195 ThrowParseException ("Unsupported codepage: " + cp);
199 responseEncoding = GetString (atts, "ResponseEncoding", null);
200 if (responseEncoding != null) {
202 ThrowParseException ("CodePage and ResponseEncoding are " +
203 "mutually exclusive.");
206 Encoding.GetEncoding (responseEncoding);
208 ThrowParseException ("Unsupported encoding: " + responseEncoding);
212 contentType = GetString (atts, "ContentType", null);
214 string lcidStr = GetString (atts, "LCID", null);
215 if (lcidStr != null) {
217 lcid = (int) UInt32.Parse (lcidStr);
219 ThrowParseException ("Invalid value for LCID: " + lcid);
222 CultureInfo ci = null;
224 ci = new CultureInfo (lcid);
226 ThrowParseException ("Unsupported LCID: " + lcid);
229 if (ci.IsNeutralCulture) {
230 string suggestedCulture = SuggestCulture (ci.Name);
231 string fmt = "LCID attribute must be set to a non-neutral Culture.";
232 if (suggestedCulture != null) {
233 ThrowParseException (fmt + " Please try one of these: " +
236 ThrowParseException (fmt);
241 culture = GetString (atts, "Culture", null);
242 if (culture != null) {
244 ThrowParseException ("Culture and LCID are mutually exclusive.");
246 CultureInfo ci = null;
249 if (!culture.StartsWith ("auto"))
251 ci = new CultureInfo (culture);
253 ThrowParseException ("Unsupported Culture: " + culture);
256 if (ci != null && ci.IsNeutralCulture) {
257 string suggestedCulture = SuggestCulture (culture);
258 string fmt = "Culture attribute must be set to a non-neutral Culture.";
259 if (suggestedCulture != null)
260 ThrowParseException (fmt +
261 " Please try one of these: " + suggestedCulture);
263 ThrowParseException (fmt);
267 uiculture = GetString (atts, "UICulture", null);
268 if (uiculture != null) {
269 CultureInfo ci = null;
272 if (!uiculture.StartsWith ("auto"))
274 ci = new CultureInfo (uiculture);
276 ThrowParseException ("Unsupported Culture: " + uiculture);
279 if (ci != null && ci.IsNeutralCulture) {
280 string suggestedCulture = SuggestCulture (uiculture);
281 string fmt = "UICulture attribute must be set to a non-neutral Culture.";
282 if (suggestedCulture != null)
283 ThrowParseException (fmt +
284 " Please try one of these: " + suggestedCulture);
286 ThrowParseException (fmt);
290 string tracestr = GetString (atts, "Trace", null);
291 if (tracestr != null) {
293 atts ["Trace"] = tracestr;
294 trace = GetBool (atts, "Trace", false);
297 string tracemodes = GetString (atts, "TraceMode", null);
298 if (tracemodes != null) {
301 tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
306 if (!valid || tracemode == TraceMode.Default)
307 ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
308 "one of the following values: SortByTime, SortByCategory.");
311 errorPage = GetString (atts, "ErrorPage", null);
312 validateRequest = GetBool (atts, "ValidateRequest", validateRequest);
313 clientTarget = GetString (atts, "ClientTarget", null);
314 if (clientTarget != null) {
315 clientTarget = clientTarget.Trim ();
317 ClientTargetSection sec = (ClientTargetSection)WebConfigurationManager.GetSection ("system.web/clientTarget");
318 ClientTarget ct = null;
320 if ((ct = sec.ClientTargets [clientTarget]) == null)
321 clientTarget = clientTarget.ToLower (CultureInfo.InvariantCulture);
323 if (ct == null && (ct = sec.ClientTargets [clientTarget]) == null) {
324 ThrowParseException (String.Format (
325 "ClientTarget '{0}' is an invalid alias. See the " +
326 "documentation for <clientTarget> config. section.",
329 clientTarget = ct.UserAgent;
331 NameValueCollection coll;
332 coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
336 ct = coll [clientTarget];
338 ct = coll [clientTarget.ToLower ()];
342 ThrowParseException (String.Format (
343 "ClientTarget '{0}' is an invalid alias. See the " +
344 "documentation for <clientTarget> config. section.",
347 clientTarget = (string) ct;
351 notBuffer = !GetBool (atts, "Buffer", true);
354 masterPage = GetString (atts, "MasterPageFile", masterPage);
356 // Make sure the page exists
357 if (!String.IsNullOrEmpty (masterPage))
358 MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
360 title = GetString(atts, "Title", null);
362 theme = GetString (atts, "Theme", theme);
363 styleSheetTheme = GetString (atts, "StyleSheetTheme", styleSheetTheme);
364 enable_event_validation = GetBool (atts, "EnableEventValidation", true);
365 maintainScrollPositionOnPostBack = GetBool (atts, "MaintainScrollPositionOnPostBack", maintainScrollPositionOnPostBack);
368 GetString (atts, "EnableViewStateMac", null);
369 GetString (atts, "SmartNavigation", null);
371 base.ProcessMainAttributes (atts);
375 internal override void AddDirective (string directive, Hashtable atts)
377 if (String.Compare ("MasterType", directive, true) == 0) {
378 string type = GetString (atts, "TypeName", null);
380 masterType = LoadType (type);
381 if (masterType == null)
382 ThrowParseException ("Could not load type '" + type + "'.");
384 string path = GetString (atts, "VirtualPath", null);
386 masterType = MasterPageParser.GetCompiledMasterType (path, MapPath (path), HttpContext.Current);
388 ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute.");
390 AddAssembly (masterType.Assembly, true);
393 base.AddDirective (directive, atts);
397 static string SuggestCulture (string culture)
399 string retval = null;
400 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
401 if (ci.Name.StartsWith (culture))
402 retval += ci.Name + " ";
407 protected override Type CompileIntoType ()
409 AspGenerator generator = new AspGenerator (this);
410 return generator.GetCompiledType ();
413 internal bool EnableSessionState {
414 get { return enableSessionState; }
417 internal bool EnableViewStateMac {
418 get { return enableViewStateMac; }
421 internal bool SmartNavigation {
422 get { return smartNavigation; }
425 internal bool ReadOnlySessionState {
426 get { return readonlySessionState; }
429 internal bool HaveTrace {
430 get { return haveTrace; }
433 internal bool Trace {
434 get { return trace; }
437 internal TraceMode TraceMode {
438 get { return tracemode; }
441 internal override Type DefaultBaseType {
442 get { return baseType; }
445 internal override string DefaultBaseTypeName {
446 get { return "System.Web.UI.Page"; }
449 internal override string DefaultDirectiveName {
450 get { return "page"; }
453 internal string ResponseEncoding {
454 get { return responseEncoding; }
457 internal string ContentType {
458 get { return contentType; }
461 internal int CodePage {
462 get { return codepage; }
465 internal string Culture {
466 get { return culture; }
469 internal string UICulture {
470 get { return uiculture; }
477 internal string ErrorPage {
478 get { return errorPage; }
481 internal bool ValidateRequest {
482 get { return validateRequest; }
485 internal string ClientTarget {
486 get { return clientTarget; }
489 internal bool NotBuffer {
490 get { return notBuffer; }
494 internal string Theme {
495 get { return theme; }
498 internal string StyleSheetTheme {
499 get { return styleSheetTheme; }
502 internal string MasterPageFile {
503 get { return masterPage; }
506 internal Type MasterType {
507 get { return masterType; }
510 internal string Title {
511 get { return title; }
514 internal bool EnableEventValidation {
515 get { return enable_event_validation; }
518 internal bool MaintainScrollPositionOnPostBack {
519 get { return maintainScrollPositionOnPostBack; }
522 internal int MaxPageStateFieldLength {
523 get { return maxPageStateFieldLength; }
526 internal string PageParserFilterType {
527 get { return pageParserFilter; }