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;
75 Type previousPageType;
80 LoadPagesConfigDefaults ();
83 internal PageParser (string virtualPath, string inputFile, HttpContext context)
86 BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
87 InputFile = inputFile;
88 SetBaseType (PagesConfig.PageBaseType);
89 AddApplicationAssembly ();
90 LoadPagesConfigDefaults ();
94 internal PageParser (string virtualPath, TextReader reader, HttpContext context)
97 BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
99 SetBaseType (PagesConfig.PageBaseType);
100 AddApplicationAssembly ();
101 LoadPagesConfigDefaults ();
105 internal override void LoadPagesConfigDefaults ()
107 base.LoadPagesConfigDefaults ();
109 PagesSection ps = PagesConfig;
111 PagesConfiguration ps = PagesConfig;
114 notBuffer = !ps.Buffer;
116 switch (ps.EnableSessionState) {
117 case PagesEnableSessionState.True:
118 case PagesEnableSessionState.ReadOnly:
119 enableSessionState = true;
123 enableSessionState = false;
127 if (String.Compare (ps.EnableSessionState, "true", true, CultureInfo.InvariantCulture) == 0)
128 enableSessionState = true;
130 enableSessionState = false;
133 enableViewStateMac = ps.EnableViewStateMac;
134 smartNavigation = ps.SmartNavigation;
135 validateRequest = ps.ValidateRequest;
137 masterPage = ps.MasterPageFile;
138 if (masterPage.Length == 0)
141 enable_event_validation = ps.EnableEventValidation;
142 maxPageStateFieldLength = ps.MaxPageStateFieldLength;
143 pageParserFilter = ps.PageParserFilterType;
145 if (theme.Length == 0)
147 styleSheetTheme = ps.StyleSheetTheme;
148 if (styleSheetTheme.Length == 0)
149 styleSheetTheme = null;
150 maintainScrollPositionOnPostBack = ps.MaintainScrollPositionOnPostBack;
154 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
158 PageParser pp = new PageParser (virtualPath, inputFile, context);
159 IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
163 internal override void ProcessMainAttributes (Hashtable atts)
165 // note: the 'enableSessionState' configuration property is
166 // processed in a case-sensitive manner while the page-level
167 // attribute is processed case-insensitive
168 string enabless = GetString (atts, "EnableSessionState", enableSessionState.ToString ());
169 if (enabless != null) {
170 readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
171 if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
172 enableSessionState = true;
173 } else if (String.Compare (enabless, "false", true) == 0) {
174 enableSessionState = false;
176 ThrowParseException ("Invalid value for enableSessionState: " + enabless);
180 string cp = GetString (atts, "CodePage", null);
182 if (responseEncoding != null)
183 ThrowParseException ("CodePage and ResponseEncoding are " +
184 "mutually exclusive.");
188 codepage = (int) UInt32.Parse (cp);
190 ThrowParseException ("Invalid value for CodePage: " + cp);
194 Encoding.GetEncoding (codepage);
196 ThrowParseException ("Unsupported codepage: " + cp);
200 responseEncoding = GetString (atts, "ResponseEncoding", null);
201 if (responseEncoding != null) {
203 ThrowParseException ("CodePage and ResponseEncoding are " +
204 "mutually exclusive.");
207 Encoding.GetEncoding (responseEncoding);
209 ThrowParseException ("Unsupported encoding: " + responseEncoding);
213 contentType = GetString (atts, "ContentType", null);
215 string lcidStr = GetString (atts, "LCID", null);
216 if (lcidStr != null) {
218 lcid = (int) UInt32.Parse (lcidStr);
220 ThrowParseException ("Invalid value for LCID: " + lcid);
223 CultureInfo ci = null;
225 ci = new CultureInfo (lcid);
227 ThrowParseException ("Unsupported LCID: " + lcid);
230 if (ci.IsNeutralCulture) {
231 string suggestedCulture = SuggestCulture (ci.Name);
232 string fmt = "LCID attribute must be set to a non-neutral Culture.";
233 if (suggestedCulture != null) {
234 ThrowParseException (fmt + " Please try one of these: " +
237 ThrowParseException (fmt);
242 culture = GetString (atts, "Culture", null);
243 if (culture != null) {
245 ThrowParseException ("Culture and LCID are mutually exclusive.");
247 CultureInfo ci = null;
250 if (!culture.StartsWith ("auto"))
252 ci = new CultureInfo (culture);
254 ThrowParseException ("Unsupported Culture: " + culture);
257 if (ci != null && ci.IsNeutralCulture) {
258 string suggestedCulture = SuggestCulture (culture);
259 string fmt = "Culture attribute must be set to a non-neutral Culture.";
260 if (suggestedCulture != null)
261 ThrowParseException (fmt +
262 " Please try one of these: " + suggestedCulture);
264 ThrowParseException (fmt);
268 uiculture = GetString (atts, "UICulture", null);
269 if (uiculture != null) {
270 CultureInfo ci = null;
273 if (!uiculture.StartsWith ("auto"))
275 ci = new CultureInfo (uiculture);
277 ThrowParseException ("Unsupported Culture: " + uiculture);
280 if (ci != null && ci.IsNeutralCulture) {
281 string suggestedCulture = SuggestCulture (uiculture);
282 string fmt = "UICulture attribute must be set to a non-neutral Culture.";
283 if (suggestedCulture != null)
284 ThrowParseException (fmt +
285 " Please try one of these: " + suggestedCulture);
287 ThrowParseException (fmt);
291 string tracestr = GetString (atts, "Trace", null);
292 if (tracestr != null) {
294 atts ["Trace"] = tracestr;
295 trace = GetBool (atts, "Trace", false);
298 string tracemodes = GetString (atts, "TraceMode", null);
299 if (tracemodes != null) {
302 tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
307 if (!valid || tracemode == TraceMode.Default)
308 ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
309 "one of the following values: SortByTime, SortByCategory.");
312 errorPage = GetString (atts, "ErrorPage", null);
313 validateRequest = GetBool (atts, "ValidateRequest", validateRequest);
314 clientTarget = GetString (atts, "ClientTarget", null);
315 if (clientTarget != null) {
316 clientTarget = clientTarget.Trim ();
318 ClientTargetSection sec = (ClientTargetSection)WebConfigurationManager.GetSection ("system.web/clientTarget");
319 ClientTarget ct = null;
321 if ((ct = sec.ClientTargets [clientTarget]) == null)
322 clientTarget = clientTarget.ToLower (CultureInfo.InvariantCulture);
324 if (ct == null && (ct = sec.ClientTargets [clientTarget]) == null) {
325 ThrowParseException (String.Format (
326 "ClientTarget '{0}' is an invalid alias. See the " +
327 "documentation for <clientTarget> config. section.",
330 clientTarget = ct.UserAgent;
332 NameValueCollection coll;
333 coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
337 ct = coll [clientTarget];
339 ct = coll [clientTarget.ToLower ()];
343 ThrowParseException (String.Format (
344 "ClientTarget '{0}' is an invalid alias. See the " +
345 "documentation for <clientTarget> config. section.",
348 clientTarget = (string) ct;
352 notBuffer = !GetBool (atts, "Buffer", true);
355 masterPage = GetString (atts, "MasterPageFile", masterPage);
357 // Make sure the page exists
358 if (!String.IsNullOrEmpty (masterPage))
359 MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
361 title = GetString(atts, "Title", null);
363 theme = GetString (atts, "Theme", theme);
364 styleSheetTheme = GetString (atts, "StyleSheetTheme", styleSheetTheme);
365 enable_event_validation = GetBool (atts, "EnableEventValidation", true);
366 maintainScrollPositionOnPostBack = GetBool (atts, "MaintainScrollPositionOnPostBack", maintainScrollPositionOnPostBack);
369 GetString (atts, "EnableViewStateMac", null);
370 GetString (atts, "SmartNavigation", null);
372 base.ProcessMainAttributes (atts);
376 internal override void AddDirective (string directive, Hashtable atts)
378 bool isMasterType = String.Compare ("MasterType", directive, StringComparison.OrdinalIgnoreCase) == 0;
379 bool isPreviousPageType = isMasterType ? false : String.Compare ("PreviousPageType", directive,
380 StringComparison.OrdinalIgnoreCase) == 0;
382 string typeName = null;
383 string virtualPath = null;
386 if (isMasterType || isPreviousPageType) {
387 typeName = GetString (atts, "TypeName", null);
388 virtualPath = GetString (atts, "VirtualPath", null);
390 if (typeName != null && virtualPath != null)
391 ThrowParseException (
392 String.Format ("The '{0}' directive must have exactly one attribute: TypeName or VirtualPath", directive));
393 if (typeName != null) {
394 type = LoadType (typeName);
396 ThrowParseException (String.Format ("Could not load type '{0}'.", typeName));
397 } else if (virtualPath != null) {
398 string mappedPath = MapPath (virtualPath);
400 type = masterType = MasterPageParser.GetCompiledMasterType (virtualPath,
402 HttpContext.Current);
404 type = previousPageType = GetCompiledPageType (virtualPath, mappedPath,
405 HttpContext.Current);
407 ThrowParseException (
408 String.Format ("The {0} directive must have either a TypeName or a VirtualPath attribute.", directive));
410 AddAssembly (type.Assembly, true);
412 base.AddDirective (directive, atts);
416 static string SuggestCulture (string culture)
418 string retval = null;
419 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
420 if (ci.Name.StartsWith (culture))
421 retval += ci.Name + " ";
426 public static Type GetCompiledPageType (string virtualPath, string inputFile, HttpContext context)
428 PageParser pp = new PageParser (virtualPath, inputFile, context);
429 return pp.CompileIntoType ();
432 protected override Type CompileIntoType ()
434 AspGenerator generator = new AspGenerator (this);
435 return generator.GetCompiledType ();
438 internal bool EnableSessionState {
439 get { return enableSessionState; }
442 internal bool EnableViewStateMac {
443 get { return enableViewStateMac; }
446 internal bool SmartNavigation {
447 get { return smartNavigation; }
450 internal bool ReadOnlySessionState {
451 get { return readonlySessionState; }
454 internal bool HaveTrace {
455 get { return haveTrace; }
458 internal bool Trace {
459 get { return trace; }
462 internal TraceMode TraceMode {
463 get { return tracemode; }
466 internal override Type DefaultBaseType {
467 get { return baseType; }
470 internal override string DefaultBaseTypeName {
471 get { return "System.Web.UI.Page"; }
474 internal override string DefaultDirectiveName {
475 get { return "page"; }
478 internal string ResponseEncoding {
479 get { return responseEncoding; }
482 internal string ContentType {
483 get { return contentType; }
486 internal int CodePage {
487 get { return codepage; }
490 internal string Culture {
491 get { return culture; }
494 internal string UICulture {
495 get { return uiculture; }
502 internal string ErrorPage {
503 get { return errorPage; }
506 internal bool ValidateRequest {
507 get { return validateRequest; }
510 internal string ClientTarget {
511 get { return clientTarget; }
514 internal bool NotBuffer {
515 get { return notBuffer; }
519 internal string Theme {
520 get { return theme; }
523 internal string StyleSheetTheme {
524 get { return styleSheetTheme; }
527 internal string MasterPageFile {
528 get { return masterPage; }
531 internal Type MasterType {
532 get { return masterType; }
535 internal string Title {
536 get { return title; }
539 internal bool EnableEventValidation {
540 get { return enable_event_validation; }
543 internal bool MaintainScrollPositionOnPostBack {
544 get { return maintainScrollPositionOnPostBack; }
547 internal int MaxPageStateFieldLength {
548 get { return maxPageStateFieldLength; }
551 internal string PageParserFilterType {
552 get { return pageParserFilter; }
555 internal Type PreviousPageType {
556 get { return previousPageType; }