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