2004-07-02 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30 using System;
31 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Globalization;
34 using System.Text;
35 using System.Web;
36 using System.Web.Compilation;
37 using System.Web.Configuration;
38 using System.Web.Util;
39
40 namespace System.Web.UI
41 {
42         public sealed class PageParser : TemplateControlParser
43         {
44                 bool enableSessionState = true;
45                 bool haveTrace;
46                 bool trace;
47                 bool notBuffer;
48                 TraceMode tracemode;
49                 bool readonlySessionState;
50                 string responseEncoding;
51                 string contentType;
52                 int codepage = -1;
53                 int lcid = -1;
54                 string culture;
55                 string uiculture;
56                 string errorPage;
57                 bool validateRequest;
58                 string clientTarget;
59                 Type baseType = typeof (Page);
60
61                 public PageParser ()
62                 {
63                 }
64                 
65                 internal PageParser (string virtualPath, string inputFile, HttpContext context)
66                 {
67                         Context = context;
68                         BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
69                         InputFile = inputFile;
70                         SetBaseType (PagesConfig.PageBaseType);
71                         AddApplicationAssembly ();
72                 }
73
74                 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
75                                                                     string inputFile, 
76                                                                     HttpContext context)
77                 {
78                         PageParser pp = new PageParser (virtualPath, inputFile, context);
79                         IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
80                         return h;
81                 }
82
83                 internal override void ProcessMainAttributes (Hashtable atts)
84                 {
85                         string enabless = GetString (atts, "EnableSessionState", PagesConfig.EnableSessionState);
86                         if (enabless != null) {
87                                 readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
88                                 if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
89                                         enableSessionState = true;
90                                 } else if (String.Compare (enabless, "false", true) == 0) {
91                                         enableSessionState = false;
92                                 } else {
93                                         ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
94                                 }
95                         }
96
97                         string cp = GetString (atts, "CodePage", null);
98                         if (cp != null) {
99                                 if (responseEncoding != null)
100                                         ThrowParseException ("CodePage and ResponseEncoding are " +
101                                                              "mutually exclusive.");
102
103                                 int codepage = 0;
104                                 try {
105                                         codepage = (int) UInt32.Parse (cp);
106                                 } catch {
107                                         ThrowParseException ("Invalid value for CodePage: " + cp);
108                                 }
109
110                                 try {
111                                         Encoding.GetEncoding (codepage);
112                                 } catch {
113                                         ThrowParseException ("Unsupported codepage: " + cp);
114                                 }
115                         }
116                         
117                         responseEncoding = GetString (atts, "ResponseEncoding", null);
118                         if (responseEncoding != null) {
119                                 if (codepage != -1)
120                                         ThrowParseException ("CodePage and ResponseEncoding are " +
121                                                              "mutually exclusive.");
122
123                                 try {
124                                         Encoding.GetEncoding (responseEncoding);
125                                 } catch {
126                                         ThrowParseException ("Unsupported encoding: " + responseEncoding);
127                                 }
128                         }
129                         
130                         contentType = GetString (atts, "ContentType", null);
131
132                         string lcidStr = GetString (atts, "LCID", null);
133                         if (lcidStr != null) {
134                                 try {
135                                         lcid = (int) UInt32.Parse (lcidStr);
136                                 } catch {
137                                         ThrowParseException ("Invalid value for LCID: " + lcid);
138                                 }
139
140                                 CultureInfo ci = null;
141                                 try {
142                                         ci = new CultureInfo (lcid);
143                                 } catch {
144                                         ThrowParseException ("Unsupported LCID: " + lcid);
145                                 }
146
147                                 if (ci.IsNeutralCulture) {
148                                         string suggestedCulture = SuggestCulture (ci.Name);
149                                         string fmt = "LCID attribute must be set to a non-neutral Culture.";
150                                         if (suggestedCulture != null) {
151                                                 ThrowParseException (fmt + " Please try one of these: " +
152                                                                      suggestedCulture);
153                                         } else {
154                                                 ThrowParseException (fmt);
155                                         }
156                                 }
157                         }
158
159                         culture = GetString (atts, "Culture", null);
160                         if (culture != null) {
161                                 if (lcidStr != null) 
162                                         ThrowParseException ("Culture and LCID are mutually exclusive.");
163                                 
164                                 CultureInfo ci = null;
165                                 try {
166                                         ci = new CultureInfo (culture);                                 
167                                 } catch {
168                                         ThrowParseException ("Unsupported Culture: " + culture);
169                                 }
170
171                                 if (ci.IsNeutralCulture) {
172                                         string suggestedCulture = SuggestCulture (culture);
173                                         string fmt = "Culture attribute must be set to a non-neutral Culture.";
174                                         if (suggestedCulture != null)
175                                                 ThrowParseException (fmt +
176                                                                 " Please try one of these: " + suggestedCulture);
177                                         else
178                                                 ThrowParseException (fmt);
179                                 }
180                         }
181
182                         uiculture = GetString (atts, "UICulture", null);
183                         if (uiculture != null) {
184                                 CultureInfo ci = null;
185                                 try {
186                                         ci = new CultureInfo (uiculture);                                       
187                                 } catch {
188                                         ThrowParseException ("Unsupported Culture: " + uiculture);
189                                 }
190
191                                 if (ci.IsNeutralCulture) {
192                                         string suggestedCulture = SuggestCulture (uiculture);
193                                         string fmt = "UICulture attribute must be set to a non-neutral Culture.";
194                                         if (suggestedCulture != null)
195                                                 ThrowParseException (fmt +
196                                                                 " Please try one of these: " + suggestedCulture);
197                                         else
198                                                 ThrowParseException (fmt);
199                                 }
200                         }
201
202                         string tracestr = GetString (atts, "Trace", null);
203                         if (tracestr != null) {
204                                 haveTrace = true;
205                                 atts ["Trace"] = tracestr;
206                                 trace = GetBool (atts, "Trace", false);
207                         }
208
209                         string tracemodes = GetString (atts, "TraceMode", null);
210                         if (tracemodes != null) {
211                                 bool valid = true;
212                                 try {
213                                         tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
214                                 } catch {
215                                         valid = false;
216                                 }
217
218                                 if (!valid || tracemode == TraceMode.Default)
219                                         ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
220                                                         "one of the following values: SortByTime, SortByCategory.");
221                         }
222
223                         errorPage = GetString (atts, "ErrorPage", null);
224                         validateRequest = GetBool (atts, "ValidateRequest", PagesConfig.ValidateRequest);
225                         clientTarget = GetString (atts, "ClientTarget", null);
226                         if (clientTarget != null) {
227                                 NameValueCollection coll;
228                                 coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
229                                 if (coll == null || coll [clientTarget] == null) {
230                                         ThrowParseException (String.Format (
231                                                         "ClientTarget '{0}' is an invalid alias. See the " +
232                                                         "documentation for <clientTarget> config. section.",
233                                                         clientTarget));
234                                 }
235                                 clientTarget = (string) coll [clientTarget];
236                         }
237
238                         notBuffer = !GetBool (atts, "Buffer", true);
239
240                         // Ignored by now
241                         GetString (atts, "EnableViewStateMac", null);
242                         GetString (atts, "SmartNavigation", null);
243
244                         base.ProcessMainAttributes (atts);
245                 }
246                 
247                 static string SuggestCulture (string culture)
248                 {
249                         string retval = null;
250                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
251                                 if (ci.Name.StartsWith (culture))
252                                         retval += ci.Name + " ";
253                         }
254                         return retval;
255                 }
256
257                 protected override Type CompileIntoType ()
258                 {
259                         AspGenerator generator = new AspGenerator (this);
260                         return generator.GetCompiledType ();
261                 }
262
263                 internal bool EnableSessionState {
264                         get { return enableSessionState; }
265                 }
266                 
267                 internal bool ReadOnlySessionState {
268                         get { return readonlySessionState; }
269                 }
270
271                 internal bool HaveTrace {
272                         get { return haveTrace; }
273                 }
274
275                 internal bool Trace {
276                         get { return trace; }
277                 }
278
279                 internal TraceMode TraceMode {
280                         get { return tracemode; }
281                 }
282                 
283                 internal override Type DefaultBaseType {
284                         get { return baseType; }
285                 }
286
287                 internal override string DefaultBaseTypeName {
288                         get { return "System.Web.UI.Page"; }
289                 }
290
291                 internal override string DefaultDirectiveName {
292                         get { return "page"; }
293                 }
294
295                 internal string ResponseEncoding {
296                         get { return responseEncoding; }
297                 }
298
299                 internal string ContentType {
300                         get { return contentType; }
301                 }
302
303                 internal int CodePage {
304                         get { return codepage; }
305                 }
306
307                 internal string Culture {
308                         get { return culture; }
309                 }
310
311                 internal string UICulture {
312                         get { return uiculture; }
313                 }
314
315                 internal int LCID {
316                         get { return lcid; }
317                 }
318
319                 internal string ErrorPage {
320                         get { return errorPage; }
321                 }
322
323                 internal bool ValidateRequest {
324                         get { return validateRequest; }
325                 }
326
327                 internal string ClientTarget {
328                         get { return clientTarget; }
329                 }
330
331                 internal bool NotBuffer {
332                         get { return notBuffer; }
333                 }
334         }
335 }
336