b1574d476b3d5f0662f88bd60a9a7dd1d0e1d5a1
[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
39 namespace System.Web.UI
40 {
41         // CAS - no InheritanceDemand here as the class is sealed
42         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         public sealed class PageParser : TemplateControlParser
44         {
45                 bool enableSessionState = true;
46                 bool haveTrace;
47                 bool trace;
48                 bool notBuffer;
49                 TraceMode tracemode;
50                 bool readonlySessionState;
51                 string responseEncoding;
52                 string contentType;
53                 int codepage = -1;
54                 int lcid = -1;
55                 string culture;
56                 string uiculture;
57                 string errorPage;
58                 bool validateRequest;
59                 string clientTarget;
60                 Type baseType = typeof (Page);
61
62 #if NET_2_0
63                 string masterPage;
64                 Type masterType;
65 #endif
66
67                 public PageParser ()
68                 {
69                 }
70                 
71                 internal PageParser (string virtualPath, string inputFile, HttpContext context)
72                 {
73                         Context = context;
74                         BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
75                         InputFile = inputFile;
76                         SetBaseType (PagesConfig.PageBaseType);
77                         AddApplicationAssembly ();
78                 }
79
80                 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
81                                                                     string inputFile, 
82                                                                     HttpContext context)
83                 {
84                         PageParser pp = new PageParser (virtualPath, inputFile, context);
85                         IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
86                         return h;
87                 }
88
89                 internal override void ProcessMainAttributes (Hashtable atts)
90                 {
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;
98                                 } else {
99                                         ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
100                                 }
101                         }
102
103                         string cp = GetString (atts, "CodePage", null);
104                         if (cp != null) {
105                                 if (responseEncoding != null)
106                                         ThrowParseException ("CodePage and ResponseEncoding are " +
107                                                              "mutually exclusive.");
108
109                                 int codepage = 0;
110                                 try {
111                                         codepage = (int) UInt32.Parse (cp);
112                                 } catch {
113                                         ThrowParseException ("Invalid value for CodePage: " + cp);
114                                 }
115
116                                 try {
117                                         Encoding.GetEncoding (codepage);
118                                 } catch {
119                                         ThrowParseException ("Unsupported codepage: " + cp);
120                                 }
121                         }
122                         
123                         responseEncoding = GetString (atts, "ResponseEncoding", null);
124                         if (responseEncoding != null) {
125                                 if (codepage != -1)
126                                         ThrowParseException ("CodePage and ResponseEncoding are " +
127                                                              "mutually exclusive.");
128
129                                 try {
130                                         Encoding.GetEncoding (responseEncoding);
131                                 } catch {
132                                         ThrowParseException ("Unsupported encoding: " + responseEncoding);
133                                 }
134                         }
135                         
136                         contentType = GetString (atts, "ContentType", null);
137
138                         string lcidStr = GetString (atts, "LCID", null);
139                         if (lcidStr != null) {
140                                 try {
141                                         lcid = (int) UInt32.Parse (lcidStr);
142                                 } catch {
143                                         ThrowParseException ("Invalid value for LCID: " + lcid);
144                                 }
145
146                                 CultureInfo ci = null;
147                                 try {
148                                         ci = new CultureInfo (lcid);
149                                 } catch {
150                                         ThrowParseException ("Unsupported LCID: " + lcid);
151                                 }
152
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: " +
158                                                                      suggestedCulture);
159                                         } else {
160                                                 ThrowParseException (fmt);
161                                         }
162                                 }
163                         }
164
165                         culture = GetString (atts, "Culture", null);
166                         if (culture != null) {
167                                 if (lcidStr != null) 
168                                         ThrowParseException ("Culture and LCID are mutually exclusive.");
169                                 
170                                 CultureInfo ci = null;
171                                 try {
172                                         ci = new CultureInfo (culture);                                 
173                                 } catch {
174                                         ThrowParseException ("Unsupported Culture: " + culture);
175                                 }
176
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);
183                                         else
184                                                 ThrowParseException (fmt);
185                                 }
186                         }
187
188                         uiculture = GetString (atts, "UICulture", null);
189                         if (uiculture != null) {
190                                 CultureInfo ci = null;
191                                 try {
192                                         ci = new CultureInfo (uiculture);                                       
193                                 } catch {
194                                         ThrowParseException ("Unsupported Culture: " + uiculture);
195                                 }
196
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);
203                                         else
204                                                 ThrowParseException (fmt);
205                                 }
206                         }
207
208                         string tracestr = GetString (atts, "Trace", null);
209                         if (tracestr != null) {
210                                 haveTrace = true;
211                                 atts ["Trace"] = tracestr;
212                                 trace = GetBool (atts, "Trace", false);
213                         }
214
215                         string tracemodes = GetString (atts, "TraceMode", null);
216                         if (tracemodes != null) {
217                                 bool valid = true;
218                                 try {
219                                         tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
220                                 } catch {
221                                         valid = false;
222                                 }
223
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.");
227                         }
228
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.",
239                                                         clientTarget));
240                                 }
241                                 clientTarget = (string) coll [clientTarget];
242                         }
243
244                         notBuffer = !GetBool (atts, "Buffer", true);
245                         
246 #if NET_2_0
247                         masterPage = GetString (atts, "MasterPageFile", null);
248                         
249                         // Make sure the page exists
250                         if (masterPage != null)
251                                 MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
252 #endif
253                         // Ignored by now
254                         GetString (atts, "EnableViewStateMac", null);
255                         GetString (atts, "SmartNavigation", null);
256
257                         base.ProcessMainAttributes (atts);
258                 }
259                 
260 #if NET_2_0
261                 internal override void AddDirective (string directive, Hashtable atts)
262                 {
263                         if (String.Compare ("MasterType", directive, true) == 0) {
264                                 string type = GetString (atts, "TypeName", null);
265                                 if (type != null) {
266                                         masterType = LoadType (type);
267                                         if (masterType == null)
268                                                 ThrowParseException ("Could not load type '" + type + "'.");
269                                 } else {
270                                         string path = GetString (atts, "VirtualPath", null);
271                                         if (path != null)
272                                                 masterType = MasterPageParser.GetCompiledMasterType (path, MapPath (path), HttpContext.Current);
273                                         else
274                                                 ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute.");
275                                 }
276                                 AddAssembly (masterType.Assembly, true);
277                         }
278                         else
279                                 base.AddDirective (directive, atts);
280                 }
281 #endif
282                 
283                 static string SuggestCulture (string culture)
284                 {
285                         string retval = null;
286                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
287                                 if (ci.Name.StartsWith (culture))
288                                         retval += ci.Name + " ";
289                         }
290                         return retval;
291                 }
292
293                 protected override Type CompileIntoType ()
294                 {
295                         AspGenerator generator = new AspGenerator (this);
296                         return generator.GetCompiledType ();
297                 }
298
299                 internal bool EnableSessionState {
300                         get { return enableSessionState; }
301                 }
302                 
303                 internal bool ReadOnlySessionState {
304                         get { return readonlySessionState; }
305                 }
306
307                 internal bool HaveTrace {
308                         get { return haveTrace; }
309                 }
310
311                 internal bool Trace {
312                         get { return trace; }
313                 }
314
315                 internal TraceMode TraceMode {
316                         get { return tracemode; }
317                 }
318                 
319                 internal override Type DefaultBaseType {
320                         get { return baseType; }
321                 }
322
323                 internal override string DefaultBaseTypeName {
324                         get { return "System.Web.UI.Page"; }
325                 }
326
327                 internal override string DefaultDirectiveName {
328                         get { return "page"; }
329                 }
330
331                 internal string ResponseEncoding {
332                         get { return responseEncoding; }
333                 }
334
335                 internal string ContentType {
336                         get { return contentType; }
337                 }
338
339                 internal int CodePage {
340                         get { return codepage; }
341                 }
342
343                 internal string Culture {
344                         get { return culture; }
345                 }
346
347                 internal string UICulture {
348                         get { return uiculture; }
349                 }
350
351                 internal int LCID {
352                         get { return lcid; }
353                 }
354
355                 internal string ErrorPage {
356                         get { return errorPage; }
357                 }
358
359                 internal bool ValidateRequest {
360                         get { return validateRequest; }
361                 }
362
363                 internal string ClientTarget {
364                         get { return clientTarget; }
365                 }
366
367                 internal bool NotBuffer {
368                         get { return notBuffer; }
369                 }
370
371 #if NET_2_0
372                 internal string MasterPageFile {
373                         get { return masterPage; }
374                 }
375                 
376                 internal Type MasterType {
377                         get { return masterType; }
378                 }
379 #endif
380         }
381 }
382