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;
39 namespace System.Web.UI
41 // CAS - no InheritanceDemand here as the class is sealed
42 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 public sealed class PageParser : TemplateControlParser
45 bool enableSessionState = true;
50 bool readonlySessionState;
51 string responseEncoding;
60 Type baseType = typeof (Page);
71 internal PageParser (string virtualPath, string inputFile, HttpContext context)
74 BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
75 InputFile = inputFile;
76 SetBaseType (PagesConfig.PageBaseType);
77 AddApplicationAssembly ();
80 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
84 PageParser pp = new PageParser (virtualPath, inputFile, context);
85 IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
89 internal override void ProcessMainAttributes (Hashtable atts)
91 string enabless = GetString (atts, "EnableSessionState", PagesConfig.EnableSessionState);
92 if (enabless != null) {
93 readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
94 if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
95 enableSessionState = true;
96 } else if (String.Compare (enabless, "false", true) == 0) {
97 enableSessionState = false;
99 ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
103 string cp = GetString (atts, "CodePage", null);
105 if (responseEncoding != null)
106 ThrowParseException ("CodePage and ResponseEncoding are " +
107 "mutually exclusive.");
111 codepage = (int) UInt32.Parse (cp);
113 ThrowParseException ("Invalid value for CodePage: " + cp);
117 Encoding.GetEncoding (codepage);
119 ThrowParseException ("Unsupported codepage: " + cp);
123 responseEncoding = GetString (atts, "ResponseEncoding", null);
124 if (responseEncoding != null) {
126 ThrowParseException ("CodePage and ResponseEncoding are " +
127 "mutually exclusive.");
130 Encoding.GetEncoding (responseEncoding);
132 ThrowParseException ("Unsupported encoding: " + responseEncoding);
136 contentType = GetString (atts, "ContentType", null);
138 string lcidStr = GetString (atts, "LCID", null);
139 if (lcidStr != null) {
141 lcid = (int) UInt32.Parse (lcidStr);
143 ThrowParseException ("Invalid value for LCID: " + lcid);
146 CultureInfo ci = null;
148 ci = new CultureInfo (lcid);
150 ThrowParseException ("Unsupported LCID: " + lcid);
153 if (ci.IsNeutralCulture) {
154 string suggestedCulture = SuggestCulture (ci.Name);
155 string fmt = "LCID attribute must be set to a non-neutral Culture.";
156 if (suggestedCulture != null) {
157 ThrowParseException (fmt + " Please try one of these: " +
160 ThrowParseException (fmt);
165 culture = GetString (atts, "Culture", null);
166 if (culture != null) {
168 ThrowParseException ("Culture and LCID are mutually exclusive.");
170 CultureInfo ci = null;
172 ci = new CultureInfo (culture);
174 ThrowParseException ("Unsupported Culture: " + culture);
177 if (ci.IsNeutralCulture) {
178 string suggestedCulture = SuggestCulture (culture);
179 string fmt = "Culture attribute must be set to a non-neutral Culture.";
180 if (suggestedCulture != null)
181 ThrowParseException (fmt +
182 " Please try one of these: " + suggestedCulture);
184 ThrowParseException (fmt);
188 uiculture = GetString (atts, "UICulture", null);
189 if (uiculture != null) {
190 CultureInfo ci = null;
192 ci = new CultureInfo (uiculture);
194 ThrowParseException ("Unsupported Culture: " + uiculture);
197 if (ci.IsNeutralCulture) {
198 string suggestedCulture = SuggestCulture (uiculture);
199 string fmt = "UICulture attribute must be set to a non-neutral Culture.";
200 if (suggestedCulture != null)
201 ThrowParseException (fmt +
202 " Please try one of these: " + suggestedCulture);
204 ThrowParseException (fmt);
208 string tracestr = GetString (atts, "Trace", null);
209 if (tracestr != null) {
211 atts ["Trace"] = tracestr;
212 trace = GetBool (atts, "Trace", false);
215 string tracemodes = GetString (atts, "TraceMode", null);
216 if (tracemodes != null) {
219 tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
224 if (!valid || tracemode == TraceMode.Default)
225 ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
226 "one of the following values: SortByTime, SortByCategory.");
229 errorPage = GetString (atts, "ErrorPage", null);
230 validateRequest = GetBool (atts, "ValidateRequest", PagesConfig.ValidateRequest);
231 clientTarget = GetString (atts, "ClientTarget", null);
232 if (clientTarget != null) {
233 NameValueCollection coll;
234 coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
235 if (coll == null || coll [clientTarget] == null) {
236 ThrowParseException (String.Format (
237 "ClientTarget '{0}' is an invalid alias. See the " +
238 "documentation for <clientTarget> config. section.",
241 clientTarget = (string) coll [clientTarget];
244 notBuffer = !GetBool (atts, "Buffer", true);
247 masterPage = GetString (atts, "MasterPageFile", null);
249 // Make sure the page exists
250 if (masterPage != null)
251 MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
254 GetString (atts, "EnableViewStateMac", null);
255 GetString (atts, "SmartNavigation", null);
257 base.ProcessMainAttributes (atts);
261 internal override void AddDirective (string directive, Hashtable atts)
263 if (String.Compare ("MasterType", directive, true) == 0) {
264 string type = GetString (atts, "TypeName", null);
266 masterType = LoadType (type);
267 if (masterType == null)
268 ThrowParseException ("Could not load type '" + type + "'.");
270 string path = GetString (atts, "VirtualPath", null);
272 masterType = MasterPageParser.GetCompiledMasterType (path, MapPath (path), HttpContext.Current);
274 ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute.");
276 AddAssembly (masterType.Assembly, true);
279 base.AddDirective (directive, atts);
283 static string SuggestCulture (string culture)
285 string retval = null;
286 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
287 if (ci.Name.StartsWith (culture))
288 retval += ci.Name + " ";
293 protected override Type CompileIntoType ()
295 AspGenerator generator = new AspGenerator (this);
296 return generator.GetCompiledType ();
299 internal bool EnableSessionState {
300 get { return enableSessionState; }
303 internal bool ReadOnlySessionState {
304 get { return readonlySessionState; }
307 internal bool HaveTrace {
308 get { return haveTrace; }
311 internal bool Trace {
312 get { return trace; }
315 internal TraceMode TraceMode {
316 get { return tracemode; }
319 internal override Type DefaultBaseType {
320 get { return baseType; }
323 internal override string DefaultBaseTypeName {
324 get { return "System.Web.UI.Page"; }
327 internal override string DefaultDirectiveName {
328 get { return "page"; }
331 internal string ResponseEncoding {
332 get { return responseEncoding; }
335 internal string ContentType {
336 get { return contentType; }
339 internal int CodePage {
340 get { return codepage; }
343 internal string Culture {
344 get { return culture; }
347 internal string UICulture {
348 get { return uiculture; }
355 internal string ErrorPage {
356 get { return errorPage; }
359 internal bool ValidateRequest {
360 get { return validateRequest; }
363 internal string ClientTarget {
364 get { return clientTarget; }
367 internal bool NotBuffer {
368 get { return notBuffer; }
372 internal string MasterPageFile {
373 get { return masterPage; }
376 internal Type MasterType {
377 get { return masterType; }