Support a few more Page directive attributes
[mono.git] / mcs / class / System.Web / System.Web.UI / PageParser.cs
1 //
2 // System.Web.UI.PageParser
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System.Collections;
31 using System.Collections.Specialized;
32 using System.Globalization;
33 using System.Security.Permissions;
34 using System.Text;
35 using System.Web.Compilation;
36 using System.Web.Configuration;
37 using System.Web.Util;
38 using System.IO;
39
40 namespace System.Web.UI
41 {
42         // CAS - no InheritanceDemand here as the class is sealed
43         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         public sealed class PageParser : TemplateControlParser
45         {
46                 bool enableSessionState = true;
47                 bool haveTrace;
48                 bool trace;
49                 bool notBuffer;
50                 TraceMode tracemode;
51                 bool readonlySessionState;
52                 string responseEncoding;
53                 string contentType;
54                 int codepage = -1;
55                 int lcid = -1;
56                 string culture;
57                 string uiculture;
58                 string errorPage;
59                 bool validateRequest;
60                 string clientTarget;
61                 Type baseType = typeof (Page);
62
63 #if NET_2_0
64                 string masterPage;
65                 Type masterType;
66                 string title;
67                 string theme;
68                 string styleSheetTheme;
69                 bool enable_event_validation;
70                 bool maintainScrollPositionOnPostBack;
71 #endif
72
73                 public PageParser ()
74                 {
75                 }
76                 
77                 internal PageParser (string virtualPath, string inputFile, HttpContext context)
78                 {
79                         Context = context;
80                         BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
81                         InputFile = inputFile;
82                         SetBaseType (PagesConfig.PageBaseType);
83                         AddApplicationAssembly ();
84                 }
85
86 #if NET_2_0
87                 internal PageParser (string virtualPath, TextReader reader, HttpContext context)
88                 {
89                         Context = context;
90                         BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
91                         Reader = reader;
92                         SetBaseType (PagesConfig.PageBaseType);
93                         AddApplicationAssembly ();
94                 }
95 #endif
96
97                 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
98                                                                     string inputFile, 
99                                                                     HttpContext context)
100                 {
101                         PageParser pp = new PageParser (virtualPath, inputFile, context);
102                         IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
103                         return h;
104                 }
105                 
106                 internal override void ProcessMainAttributes (Hashtable atts)
107                 {
108                         string enabless = GetString (atts, "EnableSessionState",
109 #if NET_2_0
110                                                      PagesConfig.EnableSessionState.ToString()
111 #else
112                                                      PagesConfig.EnableSessionState
113 #endif
114                                                      );
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;
121                                 } else {
122                                         ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
123                                 }
124                         }
125
126                         string cp = GetString (atts, "CodePage", null);
127                         if (cp != null) {
128                                 if (responseEncoding != null)
129                                         ThrowParseException ("CodePage and ResponseEncoding are " +
130                                                              "mutually exclusive.");
131
132                                 int codepage = 0;
133                                 try {
134                                         codepage = (int) UInt32.Parse (cp);
135                                 } catch {
136                                         ThrowParseException ("Invalid value for CodePage: " + cp);
137                                 }
138
139                                 try {
140                                         Encoding.GetEncoding (codepage);
141                                 } catch {
142                                         ThrowParseException ("Unsupported codepage: " + cp);
143                                 }
144                         }
145                         
146                         responseEncoding = GetString (atts, "ResponseEncoding", null);
147                         if (responseEncoding != null) {
148                                 if (codepage != -1)
149                                         ThrowParseException ("CodePage and ResponseEncoding are " +
150                                                              "mutually exclusive.");
151
152                                 try {
153                                         Encoding.GetEncoding (responseEncoding);
154                                 } catch {
155                                         ThrowParseException ("Unsupported encoding: " + responseEncoding);
156                                 }
157                         }
158                         
159                         contentType = GetString (atts, "ContentType", null);
160
161                         string lcidStr = GetString (atts, "LCID", null);
162                         if (lcidStr != null) {
163                                 try {
164                                         lcid = (int) UInt32.Parse (lcidStr);
165                                 } catch {
166                                         ThrowParseException ("Invalid value for LCID: " + lcid);
167                                 }
168
169                                 CultureInfo ci = null;
170                                 try {
171                                         ci = new CultureInfo (lcid);
172                                 } catch {
173                                         ThrowParseException ("Unsupported LCID: " + lcid);
174                                 }
175
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: " +
181                                                                      suggestedCulture);
182                                         } else {
183                                                 ThrowParseException (fmt);
184                                         }
185                                 }
186                         }
187
188                         culture = GetString (atts, "Culture", null);
189                         if (culture != null) {
190                                 if (lcidStr != null) 
191                                         ThrowParseException ("Culture and LCID are mutually exclusive.");
192                                 
193                                 CultureInfo ci = null;
194                                 try {
195 #if NET_2_0
196                                         if (!culture.StartsWith ("auto"))
197 #endif
198                                                 ci = new CultureInfo (culture);
199                                 } catch {
200                                         ThrowParseException ("Unsupported Culture: " + culture);
201                                 }
202
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);
209                                         else
210                                                 ThrowParseException (fmt);
211                                 }
212                         }
213
214                         uiculture = GetString (atts, "UICulture", null);
215                         if (uiculture != null) {
216                                 CultureInfo ci = null;
217                                 try {
218 #if NET_2_0
219                                         if (!uiculture.StartsWith ("auto"))
220 #endif
221                                                 ci = new CultureInfo (uiculture);
222                                 } catch {
223                                         ThrowParseException ("Unsupported Culture: " + uiculture);
224                                 }
225
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);
232                                         else
233                                                 ThrowParseException (fmt);
234                                 }
235                         }
236
237                         string tracestr = GetString (atts, "Trace", null);
238                         if (tracestr != null) {
239                                 haveTrace = true;
240                                 atts ["Trace"] = tracestr;
241                                 trace = GetBool (atts, "Trace", false);
242                         }
243
244                         string tracemodes = GetString (atts, "TraceMode", null);
245                         if (tracemodes != null) {
246                                 bool valid = true;
247                                 try {
248                                         tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
249                                 } catch {
250                                         valid = false;
251                                 }
252
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.");
256                         }
257
258                         errorPage = GetString (atts, "ErrorPage", null);
259                         validateRequest = GetBool (atts, "ValidateRequest", PagesConfig.ValidateRequest);
260                         clientTarget = GetString (atts, "ClientTarget", null);
261                         if (clientTarget != null) {
262 #if NET_2_0
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.",
268                                                         clientTarget));
269                                 }
270                                 clientTarget = sec.ClientTargets[clientTarget].UserAgent;
271 #else
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.",
278                                                         clientTarget));
279                                 }
280                                 clientTarget = (string) coll [clientTarget];
281 #endif
282                         }
283
284                         notBuffer = !GetBool (atts, "Buffer", true);
285                         
286 #if NET_2_0
287                         masterPage = GetString (atts, "MasterPageFile", null);
288                         
289                         // Make sure the page exists
290                         if (masterPage != null)
291                                 MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
292
293                         title = GetString(atts, "Title", null);
294
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);
299 #endif
300                         // Ignored by now
301                         GetString (atts, "EnableViewStateMac", null);
302                         GetString (atts, "SmartNavigation", null);
303
304                         base.ProcessMainAttributes (atts);
305                 }
306                 
307 #if NET_2_0
308                 internal override void AddDirective (string directive, Hashtable atts)
309                 {
310                         if (String.Compare ("MasterType", directive, true) == 0) {
311                                 string type = GetString (atts, "TypeName", null);
312                                 if (type != null) {
313                                         masterType = LoadType (type);
314                                         if (masterType == null)
315                                                 ThrowParseException ("Could not load type '" + type + "'.");
316                                 } else {
317                                         string path = GetString (atts, "VirtualPath", null);
318                                         if (path != null)
319                                                 masterType = MasterPageParser.GetCompiledMasterType (path, MapPath (path), HttpContext.Current);
320                                         else
321                                                 ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute.");
322                                 }
323                                 AddAssembly (masterType.Assembly, true);
324                         }
325                         else
326                                 base.AddDirective (directive, atts);
327                 }
328 #endif
329                 
330                 static string SuggestCulture (string culture)
331                 {
332                         string retval = null;
333                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
334                                 if (ci.Name.StartsWith (culture))
335                                         retval += ci.Name + " ";
336                         }
337                         return retval;
338                 }
339
340                 protected override Type CompileIntoType ()
341                 {
342                         AspGenerator generator = new AspGenerator (this);
343                         return generator.GetCompiledType ();
344                 }
345
346                 internal bool EnableSessionState {
347                         get { return enableSessionState; }
348                 }
349                 
350                 internal bool ReadOnlySessionState {
351                         get { return readonlySessionState; }
352                 }
353
354                 internal bool HaveTrace {
355                         get { return haveTrace; }
356                 }
357
358                 internal bool Trace {
359                         get { return trace; }
360                 }
361
362                 internal TraceMode TraceMode {
363                         get { return tracemode; }
364                 }
365                 
366                 internal override Type DefaultBaseType {
367                         get { return baseType; }
368                 }
369
370                 internal override string DefaultBaseTypeName {
371                         get { return "System.Web.UI.Page"; }
372                 }
373
374                 internal override string DefaultDirectiveName {
375                         get { return "page"; }
376                 }
377
378                 internal string ResponseEncoding {
379                         get { return responseEncoding; }
380                 }
381
382                 internal string ContentType {
383                         get { return contentType; }
384                 }
385
386                 internal int CodePage {
387                         get { return codepage; }
388                 }
389
390                 internal string Culture {
391                         get { return culture; }
392                 }
393
394                 internal string UICulture {
395                         get { return uiculture; }
396                 }
397
398                 internal int LCID {
399                         get { return lcid; }
400                 }
401
402                 internal string ErrorPage {
403                         get { return errorPage; }
404                 }
405
406                 internal bool ValidateRequest {
407                         get { return validateRequest; }
408                 }
409
410                 internal string ClientTarget {
411                         get { return clientTarget; }
412                 }
413
414                 internal bool NotBuffer {
415                         get { return notBuffer; }
416                 }
417
418 #if NET_2_0
419                 internal string Theme {
420                         get { return theme; }
421                 }
422
423                 internal string StyleSheetTheme {
424                         get { return styleSheetTheme; }
425                 }
426
427                 internal string MasterPageFile {
428                         get { return masterPage; }
429                 }
430                 
431                 internal Type MasterType {
432                         get { return masterType; }
433                 }
434
435                 internal string Title {
436                         get { return title; }
437                 }
438
439                 internal bool EnableEventValidation {
440                         get { return enable_event_validation; }
441                 }
442
443                 internal bool MaintainScrollPositionOnPostBack {
444                         get { return maintainScrollPositionOnPostBack; }
445                 }
446 #endif
447         }
448 }
449