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;
70 bool maintainScrollPositionOnPostBack;
77 internal PageParser (string virtualPath, string inputFile, HttpContext context)
80 BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
81 InputFile = inputFile;
82 SetBaseType (PagesConfig.PageBaseType);
83 AddApplicationAssembly ();
87 internal PageParser (string virtualPath, TextReader reader, HttpContext context)
90 BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
92 SetBaseType (PagesConfig.PageBaseType);
93 AddApplicationAssembly ();
97 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
101 PageParser pp = new PageParser (virtualPath, inputFile, context);
102 IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
106 internal override void ProcessMainAttributes (Hashtable atts)
108 string enabless = GetString (atts, "EnableSessionState",
110 PagesConfig.EnableSessionState.ToString()
112 PagesConfig.EnableSessionState
115 if (enabless != null) {
116 readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
117 if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
118 enableSessionState = true;
119 } else if (String.Compare (enabless, "false", true) == 0) {
120 enableSessionState = false;
122 ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
126 string cp = GetString (atts, "CodePage", null);
128 if (responseEncoding != null)
129 ThrowParseException ("CodePage and ResponseEncoding are " +
130 "mutually exclusive.");
134 codepage = (int) UInt32.Parse (cp);
136 ThrowParseException ("Invalid value for CodePage: " + cp);
140 Encoding.GetEncoding (codepage);
142 ThrowParseException ("Unsupported codepage: " + cp);
146 responseEncoding = GetString (atts, "ResponseEncoding", null);
147 if (responseEncoding != null) {
149 ThrowParseException ("CodePage and ResponseEncoding are " +
150 "mutually exclusive.");
153 Encoding.GetEncoding (responseEncoding);
155 ThrowParseException ("Unsupported encoding: " + responseEncoding);
159 contentType = GetString (atts, "ContentType", null);
161 string lcidStr = GetString (atts, "LCID", null);
162 if (lcidStr != null) {
164 lcid = (int) UInt32.Parse (lcidStr);
166 ThrowParseException ("Invalid value for LCID: " + lcid);
169 CultureInfo ci = null;
171 ci = new CultureInfo (lcid);
173 ThrowParseException ("Unsupported LCID: " + lcid);
176 if (ci.IsNeutralCulture) {
177 string suggestedCulture = SuggestCulture (ci.Name);
178 string fmt = "LCID attribute must be set to a non-neutral Culture.";
179 if (suggestedCulture != null) {
180 ThrowParseException (fmt + " Please try one of these: " +
183 ThrowParseException (fmt);
188 culture = GetString (atts, "Culture", null);
189 if (culture != null) {
191 ThrowParseException ("Culture and LCID are mutually exclusive.");
193 CultureInfo ci = null;
196 if (!culture.StartsWith ("auto"))
198 ci = new CultureInfo (culture);
200 ThrowParseException ("Unsupported Culture: " + culture);
203 if (ci != null && ci.IsNeutralCulture) {
204 string suggestedCulture = SuggestCulture (culture);
205 string fmt = "Culture attribute must be set to a non-neutral Culture.";
206 if (suggestedCulture != null)
207 ThrowParseException (fmt +
208 " Please try one of these: " + suggestedCulture);
210 ThrowParseException (fmt);
214 uiculture = GetString (atts, "UICulture", null);
215 if (uiculture != null) {
216 CultureInfo ci = null;
219 if (!uiculture.StartsWith ("auto"))
221 ci = new CultureInfo (uiculture);
223 ThrowParseException ("Unsupported Culture: " + uiculture);
226 if (ci != null && ci.IsNeutralCulture) {
227 string suggestedCulture = SuggestCulture (uiculture);
228 string fmt = "UICulture attribute must be set to a non-neutral Culture.";
229 if (suggestedCulture != null)
230 ThrowParseException (fmt +
231 " Please try one of these: " + suggestedCulture);
233 ThrowParseException (fmt);
237 string tracestr = GetString (atts, "Trace", null);
238 if (tracestr != null) {
240 atts ["Trace"] = tracestr;
241 trace = GetBool (atts, "Trace", false);
244 string tracemodes = GetString (atts, "TraceMode", null);
245 if (tracemodes != null) {
248 tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
253 if (!valid || tracemode == TraceMode.Default)
254 ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
255 "one of the following values: SortByTime, SortByCategory.");
258 errorPage = GetString (atts, "ErrorPage", null);
259 validateRequest = GetBool (atts, "ValidateRequest", PagesConfig.ValidateRequest);
260 clientTarget = GetString (atts, "ClientTarget", null);
261 if (clientTarget != null) {
263 ClientTargetSection sec = (ClientTargetSection)WebConfigurationManager.GetSection ("system.web/clientTarget");
264 if (sec.ClientTargets[clientTarget] == null) {
265 ThrowParseException (String.Format (
266 "ClientTarget '{0}' is an invalid alias. See the " +
267 "documentation for <clientTarget> config. section.",
270 clientTarget = sec.ClientTargets[clientTarget].UserAgent;
272 NameValueCollection coll;
273 coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
274 if (coll == null || coll [clientTarget] == null) {
275 ThrowParseException (String.Format (
276 "ClientTarget '{0}' is an invalid alias. See the " +
277 "documentation for <clientTarget> config. section.",
280 clientTarget = (string) coll [clientTarget];
284 notBuffer = !GetBool (atts, "Buffer", true);
287 masterPage = GetString (atts, "MasterPageFile", null);
289 // Make sure the page exists
290 if (masterPage != null)
291 MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
293 title = GetString(atts, "Title", null);
295 theme = GetString (atts, "Theme", null);
296 styleSheetTheme = GetString (atts, "StyleSheetTheme", null);
297 enable_event_validation = GetBool (atts, "EnableEventValidation", true);
298 maintainScrollPositionOnPostBack = GetBool (atts, "MaintainScrollPositionOnPostBack", true);
301 GetString (atts, "EnableViewStateMac", null);
302 GetString (atts, "SmartNavigation", null);
304 base.ProcessMainAttributes (atts);
308 internal override void AddDirective (string directive, Hashtable atts)
310 if (String.Compare ("MasterType", directive, true) == 0) {
311 string type = GetString (atts, "TypeName", null);
313 masterType = LoadType (type);
314 if (masterType == null)
315 ThrowParseException ("Could not load type '" + type + "'.");
317 string path = GetString (atts, "VirtualPath", null);
319 masterType = MasterPageParser.GetCompiledMasterType (path, MapPath (path), HttpContext.Current);
321 ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute.");
323 AddAssembly (masterType.Assembly, true);
326 base.AddDirective (directive, atts);
330 static string SuggestCulture (string culture)
332 string retval = null;
333 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
334 if (ci.Name.StartsWith (culture))
335 retval += ci.Name + " ";
340 protected override Type CompileIntoType ()
342 AspGenerator generator = new AspGenerator (this);
343 return generator.GetCompiledType ();
346 internal bool EnableSessionState {
347 get { return enableSessionState; }
350 internal bool ReadOnlySessionState {
351 get { return readonlySessionState; }
354 internal bool HaveTrace {
355 get { return haveTrace; }
358 internal bool Trace {
359 get { return trace; }
362 internal TraceMode TraceMode {
363 get { return tracemode; }
366 internal override Type DefaultBaseType {
367 get { return baseType; }
370 internal override string DefaultBaseTypeName {
371 get { return "System.Web.UI.Page"; }
374 internal override string DefaultDirectiveName {
375 get { return "page"; }
378 internal string ResponseEncoding {
379 get { return responseEncoding; }
382 internal string ContentType {
383 get { return contentType; }
386 internal int CodePage {
387 get { return codepage; }
390 internal string Culture {
391 get { return culture; }
394 internal string UICulture {
395 get { return uiculture; }
402 internal string ErrorPage {
403 get { return errorPage; }
406 internal bool ValidateRequest {
407 get { return validateRequest; }
410 internal string ClientTarget {
411 get { return clientTarget; }
414 internal bool NotBuffer {
415 get { return notBuffer; }
419 internal string Theme {
420 get { return theme; }
423 internal string StyleSheetTheme {
424 get { return styleSheetTheme; }
427 internal string MasterPageFile {
428 get { return masterPage; }
431 internal Type MasterType {
432 get { return masterType; }
435 internal string Title {
436 get { return title; }
439 internal bool EnableEventValidation {
440 get { return enable_event_validation; }
443 internal bool MaintainScrollPositionOnPostBack {
444 get { return maintainScrollPositionOnPostBack; }