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) {
316 ClientTargetSection sec = (ClientTargetSection)WebConfigurationManager.GetSection ("system.web/clientTarget");
317 if (sec.ClientTargets[clientTarget] == null) {
318 ThrowParseException (String.Format (
319 "ClientTarget '{0}' is an invalid alias. See the " +
320 "documentation for <clientTarget> config. section.",
323 clientTarget = sec.ClientTargets[clientTarget].UserAgent;
325 NameValueCollection coll;
326 coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
327 if (coll == null || coll [clientTarget] == null) {
328 ThrowParseException (String.Format (
329 "ClientTarget '{0}' is an invalid alias. See the " +
330 "documentation for <clientTarget> config. section.",
333 clientTarget = (string) coll [clientTarget];
337 notBuffer = !GetBool (atts, "Buffer", true);
340 masterPage = GetString (atts, "MasterPageFile", masterPage);
342 // Make sure the page exists
343 if (!String.IsNullOrEmpty (masterPage))
344 MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
346 title = GetString(atts, "Title", null);
348 theme = GetString (atts, "Theme", theme);
349 styleSheetTheme = GetString (atts, "StyleSheetTheme", styleSheetTheme);
350 enable_event_validation = GetBool (atts, "EnableEventValidation", true);
351 maintainScrollPositionOnPostBack = GetBool (atts, "MaintainScrollPositionOnPostBack", maintainScrollPositionOnPostBack);
354 GetString (atts, "EnableViewStateMac", null);
355 GetString (atts, "SmartNavigation", null);
357 base.ProcessMainAttributes (atts);
361 internal override void AddDirective (string directive, Hashtable atts)
363 if (String.Compare ("MasterType", directive, true) == 0) {
364 string type = GetString (atts, "TypeName", null);
366 masterType = LoadType (type);
367 if (masterType == null)
368 ThrowParseException ("Could not load type '" + type + "'.");
370 string path = GetString (atts, "VirtualPath", null);
372 masterType = MasterPageParser.GetCompiledMasterType (path, MapPath (path), HttpContext.Current);
374 ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute.");
376 AddAssembly (masterType.Assembly, true);
379 base.AddDirective (directive, atts);
383 static string SuggestCulture (string culture)
385 string retval = null;
386 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
387 if (ci.Name.StartsWith (culture))
388 retval += ci.Name + " ";
393 protected override Type CompileIntoType ()
395 AspGenerator generator = new AspGenerator (this);
396 return generator.GetCompiledType ();
399 internal bool EnableSessionState {
400 get { return enableSessionState; }
403 internal bool EnableViewStateMac {
404 get { return enableViewStateMac; }
407 internal bool SmartNavigation {
408 get { return smartNavigation; }
411 internal bool ReadOnlySessionState {
412 get { return readonlySessionState; }
415 internal bool HaveTrace {
416 get { return haveTrace; }
419 internal bool Trace {
420 get { return trace; }
423 internal TraceMode TraceMode {
424 get { return tracemode; }
427 internal override Type DefaultBaseType {
428 get { return baseType; }
431 internal override string DefaultBaseTypeName {
432 get { return "System.Web.UI.Page"; }
435 internal override string DefaultDirectiveName {
436 get { return "page"; }
439 internal string ResponseEncoding {
440 get { return responseEncoding; }
443 internal string ContentType {
444 get { return contentType; }
447 internal int CodePage {
448 get { return codepage; }
451 internal string Culture {
452 get { return culture; }
455 internal string UICulture {
456 get { return uiculture; }
463 internal string ErrorPage {
464 get { return errorPage; }
467 internal bool ValidateRequest {
468 get { return validateRequest; }
471 internal string ClientTarget {
472 get { return clientTarget; }
475 internal bool NotBuffer {
476 get { return notBuffer; }
480 internal string Theme {
481 get { return theme; }
484 internal string StyleSheetTheme {
485 get { return styleSheetTheme; }
488 internal string MasterPageFile {
489 get { return masterPage; }
492 internal Type MasterType {
493 get { return masterType; }
496 internal string Title {
497 get { return title; }
500 internal bool EnableEventValidation {
501 get { return enable_event_validation; }
504 internal bool MaintainScrollPositionOnPostBack {
505 get { return maintainScrollPositionOnPostBack; }
508 internal int MaxPageStateFieldLength {
509 get { return maxPageStateFieldLength; }
512 internal string PageParserFilterType {
513 get { return pageParserFilter; }