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