2 // System.Web.UI.PageParser
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Globalization;
36 using System.Web.Compilation;
37 using System.Web.Configuration;
38 using System.Web.Util;
40 namespace System.Web.UI
42 public sealed class PageParser : TemplateControlParser
44 bool enableSessionState = true;
49 bool readonlySessionState;
50 string responseEncoding;
59 Type baseType = typeof (Page);
70 internal PageParser (string virtualPath, string inputFile, HttpContext context)
73 BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
74 InputFile = inputFile;
75 SetBaseType (PagesConfig.PageBaseType);
76 AddApplicationAssembly ();
79 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
83 PageParser pp = new PageParser (virtualPath, inputFile, context);
84 IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
88 internal override void ProcessMainAttributes (Hashtable atts)
90 string enabless = GetString (atts, "EnableSessionState", PagesConfig.EnableSessionState);
91 if (enabless != null) {
92 readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
93 if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
94 enableSessionState = true;
95 } else if (String.Compare (enabless, "false", true) == 0) {
96 enableSessionState = false;
98 ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
102 string cp = GetString (atts, "CodePage", null);
104 if (responseEncoding != null)
105 ThrowParseException ("CodePage and ResponseEncoding are " +
106 "mutually exclusive.");
110 codepage = (int) UInt32.Parse (cp);
112 ThrowParseException ("Invalid value for CodePage: " + cp);
116 Encoding.GetEncoding (codepage);
118 ThrowParseException ("Unsupported codepage: " + cp);
122 responseEncoding = GetString (atts, "ResponseEncoding", null);
123 if (responseEncoding != null) {
125 ThrowParseException ("CodePage and ResponseEncoding are " +
126 "mutually exclusive.");
129 Encoding.GetEncoding (responseEncoding);
131 ThrowParseException ("Unsupported encoding: " + responseEncoding);
135 contentType = GetString (atts, "ContentType", null);
137 string lcidStr = GetString (atts, "LCID", null);
138 if (lcidStr != null) {
140 lcid = (int) UInt32.Parse (lcidStr);
142 ThrowParseException ("Invalid value for LCID: " + lcid);
145 CultureInfo ci = null;
147 ci = new CultureInfo (lcid);
149 ThrowParseException ("Unsupported LCID: " + lcid);
152 if (ci.IsNeutralCulture) {
153 string suggestedCulture = SuggestCulture (ci.Name);
154 string fmt = "LCID attribute must be set to a non-neutral Culture.";
155 if (suggestedCulture != null) {
156 ThrowParseException (fmt + " Please try one of these: " +
159 ThrowParseException (fmt);
164 culture = GetString (atts, "Culture", null);
165 if (culture != null) {
167 ThrowParseException ("Culture and LCID are mutually exclusive.");
169 CultureInfo ci = null;
171 ci = new CultureInfo (culture);
173 ThrowParseException ("Unsupported Culture: " + culture);
176 if (ci.IsNeutralCulture) {
177 string suggestedCulture = SuggestCulture (culture);
178 string fmt = "Culture attribute must be set to a non-neutral Culture.";
179 if (suggestedCulture != null)
180 ThrowParseException (fmt +
181 " Please try one of these: " + suggestedCulture);
183 ThrowParseException (fmt);
187 uiculture = GetString (atts, "UICulture", null);
188 if (uiculture != null) {
189 CultureInfo ci = null;
191 ci = new CultureInfo (uiculture);
193 ThrowParseException ("Unsupported Culture: " + uiculture);
196 if (ci.IsNeutralCulture) {
197 string suggestedCulture = SuggestCulture (uiculture);
198 string fmt = "UICulture attribute must be set to a non-neutral Culture.";
199 if (suggestedCulture != null)
200 ThrowParseException (fmt +
201 " Please try one of these: " + suggestedCulture);
203 ThrowParseException (fmt);
207 string tracestr = GetString (atts, "Trace", null);
208 if (tracestr != null) {
210 atts ["Trace"] = tracestr;
211 trace = GetBool (atts, "Trace", false);
214 string tracemodes = GetString (atts, "TraceMode", null);
215 if (tracemodes != null) {
218 tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
223 if (!valid || tracemode == TraceMode.Default)
224 ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
225 "one of the following values: SortByTime, SortByCategory.");
228 errorPage = GetString (atts, "ErrorPage", null);
229 validateRequest = GetBool (atts, "ValidateRequest", PagesConfig.ValidateRequest);
230 clientTarget = GetString (atts, "ClientTarget", null);
231 if (clientTarget != null) {
232 NameValueCollection coll;
233 coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
234 if (coll == null || coll [clientTarget] == null) {
235 ThrowParseException (String.Format (
236 "ClientTarget '{0}' is an invalid alias. See the " +
237 "documentation for <clientTarget> config. section.",
240 clientTarget = (string) coll [clientTarget];
243 notBuffer = !GetBool (atts, "Buffer", true);
246 masterPage = GetString (atts, "MasterPageFile", null);
248 // Make sure the page exists
249 if (masterPage != null)
250 MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
253 GetString (atts, "EnableViewStateMac", null);
254 GetString (atts, "SmartNavigation", null);
256 base.ProcessMainAttributes (atts);
260 internal override void AddDirective (string directive, Hashtable atts)
262 if (String.Compare ("MasterType", directive, true) == 0) {
263 string type = GetString (atts, "TypeName", null);
265 masterType = LoadType (type);
266 if (masterType == null)
267 ThrowParseException ("Could not load type '" + type + "'.");
269 string path = GetString (atts, "VirtualPath", null);
271 masterType = MasterPageParser.GetCompiledMasterType (path, MapPath (path), HttpContext.Current);
273 ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute.");
275 AddAssembly (masterType.Assembly, true);
278 base.AddDirective (directive, atts);
282 static string SuggestCulture (string culture)
284 string retval = null;
285 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
286 if (ci.Name.StartsWith (culture))
287 retval += ci.Name + " ";
292 protected override Type CompileIntoType ()
294 AspGenerator generator = new AspGenerator (this);
295 return generator.GetCompiledType ();
298 internal bool EnableSessionState {
299 get { return enableSessionState; }
302 internal bool ReadOnlySessionState {
303 get { return readonlySessionState; }
306 internal bool HaveTrace {
307 get { return haveTrace; }
310 internal bool Trace {
311 get { return trace; }
314 internal TraceMode TraceMode {
315 get { return tracemode; }
318 internal override Type DefaultBaseType {
319 get { return baseType; }
322 internal override string DefaultBaseTypeName {
323 get { return "System.Web.UI.Page"; }
326 internal override string DefaultDirectiveName {
327 get { return "page"; }
330 internal string ResponseEncoding {
331 get { return responseEncoding; }
334 internal string ContentType {
335 get { return contentType; }
338 internal int CodePage {
339 get { return codepage; }
342 internal string Culture {
343 get { return culture; }
346 internal string UICulture {
347 get { return uiculture; }
354 internal string ErrorPage {
355 get { return errorPage; }
358 internal bool ValidateRequest {
359 get { return validateRequest; }
362 internal string ClientTarget {
363 get { return clientTarget; }
366 internal bool NotBuffer {
367 get { return notBuffer; }
371 internal string MasterPageFile {
372 get { return masterPage; }
375 internal Type MasterType {
376 get { return masterType; }