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