* Page.cs: Don't tell the response to cache anymore. This is done
[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 //
9 using System;
10 using System.Collections;
11 using System.Globalization;
12 using System.Text;
13 using System.Web;
14 using System.Web.Compilation;
15 using System.Web.Util;
16
17 namespace System.Web.UI
18 {
19         public sealed class PageParser : TemplateControlParser
20         {
21                 bool enableSessionState = true;
22                 bool trace;
23                 TraceMode tracemode;
24                 bool readonlySessionState;
25                 string responseEncoding;
26                 string contentType;
27                 int codepage = -1;
28                 int lcid = -1;
29                 string culture;
30                 string uiculture;
31                 string errorPage;
32                 bool validateRequest;
33                 Type baseType = typeof (Page);
34
35                 public PageParser ()
36                 {
37                 }
38                 
39                 internal PageParser (string virtualPath, string inputFile, HttpContext context)
40                 {
41                         Context = context;
42                         BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
43                         InputFile = inputFile;
44                         AddApplicationAssembly ();
45                 }
46
47                 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
48                                                                     string inputFile, 
49                                                                     HttpContext context)
50                 {
51                         PageParser pp = new PageParser (virtualPath, inputFile, context);
52                         IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
53                         return h;
54                 }
55
56                 internal override void ProcessMainAttributes (Hashtable atts)
57                 {
58                         SetBaseType (PagesConfig.PageBaseType);
59                         string enabless = GetString (atts, "EnableSessionState", PagesConfig.EnableSessionState);
60                         if (enabless != null) {
61                                 readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
62                                 if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
63                                         enableSessionState = true;
64                                 } else if (String.Compare (enabless, "false", true) == 0) {
65                                         enableSessionState = false;
66                                 } else {
67                                         ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
68                                 }
69                         }
70
71                         string cp = GetString (atts, "CodePage", null);
72                         if (cp != null) {
73                                 if (responseEncoding != null)
74                                         ThrowParseException ("CodePage and ResponseEncoding are " +
75                                                              "mutually exclusive.");
76
77                                 int codepage = 0;
78                                 try {
79                                         codepage = (int) UInt32.Parse (cp);
80                                 } catch {
81                                         ThrowParseException ("Invalid value for CodePage: " + cp);
82                                 }
83
84                                 try {
85                                         Encoding.GetEncoding (codepage);
86                                 } catch {
87                                         ThrowParseException ("Unsupported codepage: " + cp);
88                                 }
89                         }
90                         
91                         responseEncoding = GetString (atts, "ResponseEncoding", null);
92                         if (responseEncoding != null) {
93                                 if (codepage != -1)
94                                         ThrowParseException ("CodePage and ResponseEncoding are " +
95                                                              "mutually exclusive.");
96
97                                 try {
98                                         Encoding.GetEncoding (responseEncoding);
99                                 } catch {
100                                         ThrowParseException ("Unsupported encoding: " + responseEncoding);
101                                 }
102                         }
103                         
104                         contentType = GetString (atts, "ContentType", null);
105
106                         string lcidStr = GetString (atts, "LCID", null);
107                         if (lcidStr != null) {
108                                 try {
109                                         lcid = (int) UInt32.Parse (lcidStr);
110                                 } catch {
111                                         ThrowParseException ("Invalid value for LCID: " + lcid);
112                                 }
113
114                                 CultureInfo ci = null;
115                                 try {
116                                         ci = new CultureInfo (lcid);
117                                 } catch {
118                                         ThrowParseException ("Unsupported LCID: " + lcid);
119                                 }
120
121                                 if (ci.IsNeutralCulture) {
122                                         string suggestedCulture = SuggestCulture (ci.Name);
123                                         string fmt = "LCID attribute must be set to a non-neutral Culture.";
124                                         if (suggestedCulture != null) {
125                                                 ThrowParseException (fmt + " Please try one of these: " +
126                                                                      suggestedCulture);
127                                         } else {
128                                                 ThrowParseException (fmt);
129                                         }
130                                 }
131                         }
132
133                         culture = GetString (atts, "Culture", null);
134                         if (culture != null) {
135                                 if (lcidStr != null) 
136                                         ThrowParseException ("Culture and LCID are mutually exclusive.");
137                                 
138                                 CultureInfo ci = null;
139                                 try {
140                                         ci = new CultureInfo (culture);                                 
141                                 } catch {
142                                         ThrowParseException ("Unsupported Culture: " + culture);
143                                 }
144
145                                 if (ci.IsNeutralCulture) {
146                                         string suggestedCulture = SuggestCulture (culture);
147                                         string fmt = "Culture attribute must be set to a non-neutral Culture.";
148                                         if (suggestedCulture != null)
149                                                 ThrowParseException (fmt +
150                                                                 " Please try one of these: " + suggestedCulture);
151                                         else
152                                                 ThrowParseException (fmt);
153                                 }
154                         }
155
156                         uiculture = GetString (atts, "UICulture", null);
157                         if (uiculture != null) {
158                                 CultureInfo ci = null;
159                                 try {
160                                         ci = new CultureInfo (uiculture);                                       
161                                 } catch {
162                                         ThrowParseException ("Unsupported Culture: " + uiculture);
163                                 }
164
165                                 if (ci.IsNeutralCulture) {
166                                         string suggestedCulture = SuggestCulture (uiculture);
167                                         string fmt = "UICulture attribute must be set to a non-neutral Culture.";
168                                         if (suggestedCulture != null)
169                                                 ThrowParseException (fmt +
170                                                                 " Please try one of these: " + suggestedCulture);
171                                         else
172                                                 ThrowParseException (fmt);
173                                 }
174                         }
175
176                         trace = GetBool (atts, "Trace", false);
177
178                         string tracemodes = GetString (atts, "TraceMode", null);
179                         if (tracemodes != null) {
180                                 bool valid = true;
181                                 try {
182                                         tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
183                                 } catch {
184                                         valid = false;
185                                 }
186
187                                 if (!valid || tracemode == TraceMode.Default)
188                                         ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
189                                                         "one of the following values: SortByTime, SortByCategory.");
190                         }
191                         
192                         errorPage = GetString (atts, "ErrorPage", null);
193                         validateRequest = GetBool (atts, "ValidateRequest", PagesConfig.ValidateRequest);
194
195                         // Ignored by now
196                         GetString (atts, "Buffer", null);
197                         GetString (atts, "ClientTarget", null);
198                         GetString (atts, "EnableViewStateMac", null);
199                         GetString (atts, "SmartNavigation", null);
200
201                         base.ProcessMainAttributes (atts);
202                 }
203                 
204                 static string SuggestCulture (string culture)
205                 {
206                         string retval = null;
207                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
208                                 if (ci.Name.StartsWith (culture))
209                                         retval += ci.Name + " ";
210                         }
211                         return retval;
212                 }
213
214                 protected override Type CompileIntoType ()
215                 {
216                         AspGenerator generator = new AspGenerator (this);
217                         return generator.GetCompiledType ();
218                 }
219
220                 internal bool EnableSessionState {
221                         get { return enableSessionState; }
222                 }
223                 
224                 internal bool ReadOnlySessionState {
225                         get { return readonlySessionState; }
226                 }
227
228                 internal bool Trace {
229                         get { return trace; } 
230                 }
231
232                 internal TraceMode TraceMode {
233                         get { return tracemode; }
234                 }
235                 
236                 internal override Type DefaultBaseType {
237                         get { return baseType; }
238                 }
239
240                 internal override string DefaultBaseTypeName {
241                         get { return "System.Web.UI.Page"; }
242                 }
243
244                 internal override string DefaultDirectiveName {
245                         get { return "page"; }
246                 }
247
248                 internal string ResponseEncoding {
249                         get { return responseEncoding; }
250                 }
251
252                 internal string ContentType {
253                         get { return contentType; }
254                 }
255
256                 internal int CodePage {
257                         get { return codepage; }
258                 }
259
260                 internal string Culture {
261                         get { return culture; }
262                 }
263
264                 internal string UICulture {
265                         get { return uiculture; }
266                 }
267
268                 internal int LCID {
269                         get { return lcid; }
270                 }
271
272                 internal string ErrorPage {
273                         get { return errorPage; }
274                 }
275
276                 internal bool ValidateRequest {
277                         get { return validateRequest; }
278                 }
279         }
280 }
281