fixed tests
[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 #endif
71
72                 public PageParser ()
73                 {
74                 }
75                 
76                 internal PageParser (string virtualPath, string inputFile, HttpContext context)
77                 {
78                         Context = context;
79                         BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
80                         InputFile = inputFile;
81                         SetBaseType (PagesConfig.PageBaseType);
82                         AddApplicationAssembly ();
83                 }
84
85 #if NET_2_0
86                 internal PageParser (string virtualPath, TextReader reader, HttpContext context)
87                 {
88                         Context = context;
89                         BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
90                         Reader = reader;
91                         SetBaseType (PagesConfig.PageBaseType);
92                         AddApplicationAssembly ();
93                 }
94 #endif
95
96                 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
97                                                                     string inputFile, 
98                                                                     HttpContext context)
99                 {
100                         PageParser pp = new PageParser (virtualPath, inputFile, context);
101                         IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
102                         return h;
103                 }
104                 
105                 internal override void ProcessMainAttributes (Hashtable atts)
106                 {
107                         string enabless = GetString (atts, "EnableSessionState",
108 #if NET_2_0
109                                                      PagesConfig.EnableSessionState.ToString()
110 #else
111                                                      PagesConfig.EnableSessionState
112 #endif
113                                                      );
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;
120                                 } else {
121                                         ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
122                                 }
123                         }
124
125                         string cp = GetString (atts, "CodePage", null);
126                         if (cp != null) {
127                                 if (responseEncoding != null)
128                                         ThrowParseException ("CodePage and ResponseEncoding are " +
129                                                              "mutually exclusive.");
130
131                                 int codepage = 0;
132                                 try {
133                                         codepage = (int) UInt32.Parse (cp);
134                                 } catch {
135                                         ThrowParseException ("Invalid value for CodePage: " + cp);
136                                 }
137
138                                 try {
139                                         Encoding.GetEncoding (codepage);
140                                 } catch {
141                                         ThrowParseException ("Unsupported codepage: " + cp);
142                                 }
143                         }
144                         
145                         responseEncoding = GetString (atts, "ResponseEncoding", null);
146                         if (responseEncoding != null) {
147                                 if (codepage != -1)
148                                         ThrowParseException ("CodePage and ResponseEncoding are " +
149                                                              "mutually exclusive.");
150
151                                 try {
152                                         Encoding.GetEncoding (responseEncoding);
153                                 } catch {
154                                         ThrowParseException ("Unsupported encoding: " + responseEncoding);
155                                 }
156                         }
157                         
158                         contentType = GetString (atts, "ContentType", null);
159
160                         string lcidStr = GetString (atts, "LCID", null);
161                         if (lcidStr != null) {
162                                 try {
163                                         lcid = (int) UInt32.Parse (lcidStr);
164                                 } catch {
165                                         ThrowParseException ("Invalid value for LCID: " + lcid);
166                                 }
167
168                                 CultureInfo ci = null;
169                                 try {
170                                         ci = new CultureInfo (lcid);
171                                 } catch {
172                                         ThrowParseException ("Unsupported LCID: " + lcid);
173                                 }
174
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: " +
180                                                                      suggestedCulture);
181                                         } else {
182                                                 ThrowParseException (fmt);
183                                         }
184                                 }
185                         }
186
187                         culture = GetString (atts, "Culture", null);
188                         if (culture != null) {
189                                 if (lcidStr != null) 
190                                         ThrowParseException ("Culture and LCID are mutually exclusive.");
191                                 
192                                 CultureInfo ci = null;
193                                 try {
194 #if NET_2_0
195                                         if (!culture.StartsWith ("auto"))
196 #endif
197                                                 ci = new CultureInfo (culture);
198                                 } catch {
199                                         ThrowParseException ("Unsupported Culture: " + culture);
200                                 }
201
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);
208                                         else
209                                                 ThrowParseException (fmt);
210                                 }
211                         }
212
213                         uiculture = GetString (atts, "UICulture", null);
214                         if (uiculture != null) {
215                                 CultureInfo ci = null;
216                                 try {
217 #if NET_2_0
218                                         if (!uiculture.StartsWith ("auto"))
219 #endif
220                                                 ci = new CultureInfo (uiculture);
221                                 } catch {
222                                         ThrowParseException ("Unsupported Culture: " + uiculture);
223                                 }
224
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);
231                                         else
232                                                 ThrowParseException (fmt);
233                                 }
234                         }
235
236                         string tracestr = GetString (atts, "Trace", null);
237                         if (tracestr != null) {
238                                 haveTrace = true;
239                                 atts ["Trace"] = tracestr;
240                                 trace = GetBool (atts, "Trace", false);
241                         }
242
243                         string tracemodes = GetString (atts, "TraceMode", null);
244                         if (tracemodes != null) {
245                                 bool valid = true;
246                                 try {
247                                         tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
248                                 } catch {
249                                         valid = false;
250                                 }
251
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.");
255                         }
256
257                         errorPage = GetString (atts, "ErrorPage", null);
258                         validateRequest = GetBool (atts, "ValidateRequest", PagesConfig.ValidateRequest);
259                         clientTarget = GetString (atts, "ClientTarget", null);
260                         if (clientTarget != null) {
261 #if NET_2_0
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.",
267                                                         clientTarget));
268                                 }
269                                 clientTarget = sec.ClientTargets[clientTarget].UserAgent;
270 #else
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.",
277                                                         clientTarget));
278                                 }
279                                 clientTarget = (string) coll [clientTarget];
280 #endif
281                         }
282
283                         notBuffer = !GetBool (atts, "Buffer", true);
284                         
285 #if NET_2_0
286                         masterPage = GetString (atts, "MasterPageFile", null);
287                         
288                         // Make sure the page exists
289                         if (masterPage != null)
290                                 MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
291
292                         title = GetString(atts, "Title", null);
293
294                         theme = GetString (atts, "Theme", null);
295                         styleSheetTheme = GetString (atts, "StyleSheetTheme", null);
296                         enable_event_validation = GetBool (atts, "EnableEventValidation", true);
297 #endif
298                         // Ignored by now
299                         GetString (atts, "EnableViewStateMac", null);
300                         GetString (atts, "SmartNavigation", null);
301
302                         base.ProcessMainAttributes (atts);
303                 }
304                 
305 #if NET_2_0
306                 internal override void AddDirective (string directive, Hashtable atts)
307                 {
308                         if (String.Compare ("MasterType", directive, true) == 0) {
309                                 string type = GetString (atts, "TypeName", null);
310                                 if (type != null) {
311                                         masterType = LoadType (type);
312                                         if (masterType == null)
313                                                 ThrowParseException ("Could not load type '" + type + "'.");
314                                 } else {
315                                         string path = GetString (atts, "VirtualPath", null);
316                                         if (path != null)
317                                                 masterType = MasterPageParser.GetCompiledMasterType (path, MapPath (path), HttpContext.Current);
318                                         else
319                                                 ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute.");
320                                 }
321                                 AddAssembly (masterType.Assembly, true);
322                         }
323                         else
324                                 base.AddDirective (directive, atts);
325                 }
326 #endif
327                 
328                 static string SuggestCulture (string culture)
329                 {
330                         string retval = null;
331                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
332                                 if (ci.Name.StartsWith (culture))
333                                         retval += ci.Name + " ";
334                         }
335                         return retval;
336                 }
337
338                 protected override Type CompileIntoType ()
339                 {
340                         AspGenerator generator = new AspGenerator (this);
341                         return generator.GetCompiledType ();
342                 }
343
344                 internal bool EnableSessionState {
345                         get { return enableSessionState; }
346                 }
347                 
348                 internal bool ReadOnlySessionState {
349                         get { return readonlySessionState; }
350                 }
351
352                 internal bool HaveTrace {
353                         get { return haveTrace; }
354                 }
355
356                 internal bool Trace {
357                         get { return trace; }
358                 }
359
360                 internal TraceMode TraceMode {
361                         get { return tracemode; }
362                 }
363                 
364                 internal override Type DefaultBaseType {
365                         get { return baseType; }
366                 }
367
368                 internal override string DefaultBaseTypeName {
369                         get { return "System.Web.UI.Page"; }
370                 }
371
372                 internal override string DefaultDirectiveName {
373                         get { return "page"; }
374                 }
375
376                 internal string ResponseEncoding {
377                         get { return responseEncoding; }
378                 }
379
380                 internal string ContentType {
381                         get { return contentType; }
382                 }
383
384                 internal int CodePage {
385                         get { return codepage; }
386                 }
387
388                 internal string Culture {
389                         get { return culture; }
390                 }
391
392                 internal string UICulture {
393                         get { return uiculture; }
394                 }
395
396                 internal int LCID {
397                         get { return lcid; }
398                 }
399
400                 internal string ErrorPage {
401                         get { return errorPage; }
402                 }
403
404                 internal bool ValidateRequest {
405                         get { return validateRequest; }
406                 }
407
408                 internal string ClientTarget {
409                         get { return clientTarget; }
410                 }
411
412                 internal bool NotBuffer {
413                         get { return notBuffer; }
414                 }
415
416 #if NET_2_0
417                 internal string Theme {
418                         get { return theme; }
419                 }
420
421                 internal string StyleSheetTheme {
422                         get { return styleSheetTheme; }
423                 }
424
425                 internal string MasterPageFile {
426                         get { return masterPage; }
427                 }
428                 
429                 internal Type MasterType {
430                         get { return masterType; }
431                 }
432
433                 internal string Title {
434                         get { return title; }
435                 }
436
437                 internal bool EnableEventValidation {
438                         get { return enable_event_validation; }
439                 }
440 #endif
441         }
442 }
443