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;
76 internal PageParser (string virtualPath, string inputFile, HttpContext context)
79 BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
80 InputFile = inputFile;
81 SetBaseType (PagesConfig.PageBaseType);
82 AddApplicationAssembly ();
86 internal PageParser (string virtualPath, TextReader reader, HttpContext context)
89 BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
91 SetBaseType (PagesConfig.PageBaseType);
92 AddApplicationAssembly ();
96 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
100 PageParser pp = new PageParser (virtualPath, inputFile, context);
101 IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
105 internal override void ProcessMainAttributes (Hashtable atts)
107 string enabless = GetString (atts, "EnableSessionState",
109 PagesConfig.EnableSessionState.ToString()
111 PagesConfig.EnableSessionState
114 if (enabless != null) {
115 readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
116 if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
117 enableSessionState = true;
118 } else if (String.Compare (enabless, "false", true) == 0) {
119 enableSessionState = false;
121 ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
125 string cp = GetString (atts, "CodePage", null);
127 if (responseEncoding != null)
128 ThrowParseException ("CodePage and ResponseEncoding are " +
129 "mutually exclusive.");
133 codepage = (int) UInt32.Parse (cp);
135 ThrowParseException ("Invalid value for CodePage: " + cp);
139 Encoding.GetEncoding (codepage);
141 ThrowParseException ("Unsupported codepage: " + cp);
145 responseEncoding = GetString (atts, "ResponseEncoding", null);
146 if (responseEncoding != null) {
148 ThrowParseException ("CodePage and ResponseEncoding are " +
149 "mutually exclusive.");
152 Encoding.GetEncoding (responseEncoding);
154 ThrowParseException ("Unsupported encoding: " + responseEncoding);
158 contentType = GetString (atts, "ContentType", null);
160 string lcidStr = GetString (atts, "LCID", null);
161 if (lcidStr != null) {
163 lcid = (int) UInt32.Parse (lcidStr);
165 ThrowParseException ("Invalid value for LCID: " + lcid);
168 CultureInfo ci = null;
170 ci = new CultureInfo (lcid);
172 ThrowParseException ("Unsupported LCID: " + lcid);
175 if (ci.IsNeutralCulture) {
176 string suggestedCulture = SuggestCulture (ci.Name);
177 string fmt = "LCID attribute must be set to a non-neutral Culture.";
178 if (suggestedCulture != null) {
179 ThrowParseException (fmt + " Please try one of these: " +
182 ThrowParseException (fmt);
187 culture = GetString (atts, "Culture", null);
188 if (culture != null) {
190 ThrowParseException ("Culture and LCID are mutually exclusive.");
192 CultureInfo ci = null;
195 if (!culture.StartsWith ("auto"))
197 ci = new CultureInfo (culture);
199 ThrowParseException ("Unsupported Culture: " + culture);
202 if (ci != null && ci.IsNeutralCulture) {
203 string suggestedCulture = SuggestCulture (culture);
204 string fmt = "Culture attribute must be set to a non-neutral Culture.";
205 if (suggestedCulture != null)
206 ThrowParseException (fmt +
207 " Please try one of these: " + suggestedCulture);
209 ThrowParseException (fmt);
213 uiculture = GetString (atts, "UICulture", null);
214 if (uiculture != null) {
215 CultureInfo ci = null;
218 if (!uiculture.StartsWith ("auto"))
220 ci = new CultureInfo (uiculture);
222 ThrowParseException ("Unsupported Culture: " + uiculture);
225 if (ci != null && ci.IsNeutralCulture) {
226 string suggestedCulture = SuggestCulture (uiculture);
227 string fmt = "UICulture attribute must be set to a non-neutral Culture.";
228 if (suggestedCulture != null)
229 ThrowParseException (fmt +
230 " Please try one of these: " + suggestedCulture);
232 ThrowParseException (fmt);
236 string tracestr = GetString (atts, "Trace", null);
237 if (tracestr != null) {
239 atts ["Trace"] = tracestr;
240 trace = GetBool (atts, "Trace", false);
243 string tracemodes = GetString (atts, "TraceMode", null);
244 if (tracemodes != null) {
247 tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
252 if (!valid || tracemode == TraceMode.Default)
253 ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
254 "one of the following values: SortByTime, SortByCategory.");
257 errorPage = GetString (atts, "ErrorPage", null);
258 validateRequest = GetBool (atts, "ValidateRequest", PagesConfig.ValidateRequest);
259 clientTarget = GetString (atts, "ClientTarget", null);
260 if (clientTarget != null) {
262 ClientTargetSection sec = (ClientTargetSection)WebConfigurationManager.GetSection ("system.web/clientTarget");
263 if (sec.ClientTargets[clientTarget] == null) {
264 ThrowParseException (String.Format (
265 "ClientTarget '{0}' is an invalid alias. See the " +
266 "documentation for <clientTarget> config. section.",
269 clientTarget = sec.ClientTargets[clientTarget].UserAgent;
271 NameValueCollection coll;
272 coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
273 if (coll == null || coll [clientTarget] == null) {
274 ThrowParseException (String.Format (
275 "ClientTarget '{0}' is an invalid alias. See the " +
276 "documentation for <clientTarget> config. section.",
279 clientTarget = (string) coll [clientTarget];
283 notBuffer = !GetBool (atts, "Buffer", true);
286 masterPage = GetString (atts, "MasterPageFile", null);
288 // Make sure the page exists
289 if (masterPage != null)
290 MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
292 title = GetString(atts, "Title", null);
294 theme = GetString (atts, "Theme", null);
295 styleSheetTheme = GetString (atts, "StyleSheetTheme", null);
296 enable_event_validation = GetBool (atts, "EnableEventValidation", true);
299 GetString (atts, "EnableViewStateMac", null);
300 GetString (atts, "SmartNavigation", null);
302 base.ProcessMainAttributes (atts);
306 internal override void AddDirective (string directive, Hashtable atts)
308 if (String.Compare ("MasterType", directive, true) == 0) {
309 string type = GetString (atts, "TypeName", null);
311 masterType = LoadType (type);
312 if (masterType == null)
313 ThrowParseException ("Could not load type '" + type + "'.");
315 string path = GetString (atts, "VirtualPath", null);
317 masterType = MasterPageParser.GetCompiledMasterType (path, MapPath (path), HttpContext.Current);
319 ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute.");
321 AddAssembly (masterType.Assembly, true);
324 base.AddDirective (directive, atts);
328 static string SuggestCulture (string culture)
330 string retval = null;
331 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
332 if (ci.Name.StartsWith (culture))
333 retval += ci.Name + " ";
338 protected override Type CompileIntoType ()
340 AspGenerator generator = new AspGenerator (this);
341 return generator.GetCompiledType ();
344 internal bool EnableSessionState {
345 get { return enableSessionState; }
348 internal bool ReadOnlySessionState {
349 get { return readonlySessionState; }
352 internal bool HaveTrace {
353 get { return haveTrace; }
356 internal bool Trace {
357 get { return trace; }
360 internal TraceMode TraceMode {
361 get { return tracemode; }
364 internal override Type DefaultBaseType {
365 get { return baseType; }
368 internal override string DefaultBaseTypeName {
369 get { return "System.Web.UI.Page"; }
372 internal override string DefaultDirectiveName {
373 get { return "page"; }
376 internal string ResponseEncoding {
377 get { return responseEncoding; }
380 internal string ContentType {
381 get { return contentType; }
384 internal int CodePage {
385 get { return codepage; }
388 internal string Culture {
389 get { return culture; }
392 internal string UICulture {
393 get { return uiculture; }
400 internal string ErrorPage {
401 get { return errorPage; }
404 internal bool ValidateRequest {
405 get { return validateRequest; }
408 internal string ClientTarget {
409 get { return clientTarget; }
412 internal bool NotBuffer {
413 get { return notBuffer; }
417 internal string Theme {
418 get { return theme; }
421 internal string StyleSheetTheme {
422 get { return styleSheetTheme; }
425 internal string MasterPageFile {
426 get { return masterPage; }
429 internal Type MasterType {
430 get { return masterType; }
433 internal string Title {
434 get { return title; }
437 internal bool EnableEventValidation {
438 get { return enable_event_validation; }