2 // System.Web.UI.PageParser
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
10 using System.Collections;
11 using System.Globalization;
14 using System.Web.Compilation;
15 using System.Web.Util;
17 namespace System.Web.UI
19 public sealed class PageParser : TemplateControlParser
21 bool enableSessionState = true;
24 bool readonlySessionState;
25 string responseEncoding;
33 Type baseType = typeof (Page);
39 internal PageParser (string virtualPath, string inputFile, HttpContext context)
42 BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
43 InputFile = inputFile;
44 AddApplicationAssembly ();
47 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
51 PageParser pp = new PageParser (virtualPath, inputFile, context);
52 IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
56 internal override void ProcessMainAttributes (Hashtable atts)
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;
67 ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
71 string cp = GetString (atts, "CodePage", null);
73 if (responseEncoding != null)
74 ThrowParseException ("CodePage and ResponseEncoding are " +
75 "mutually exclusive.");
79 codepage = (int) UInt32.Parse (cp);
81 ThrowParseException ("Invalid value for CodePage: " + cp);
85 Encoding.GetEncoding (codepage);
87 ThrowParseException ("Unsupported codepage: " + cp);
91 responseEncoding = GetString (atts, "ResponseEncoding", null);
92 if (responseEncoding != null) {
94 ThrowParseException ("CodePage and ResponseEncoding are " +
95 "mutually exclusive.");
98 Encoding.GetEncoding (responseEncoding);
100 ThrowParseException ("Unsupported encoding: " + responseEncoding);
104 contentType = GetString (atts, "ContentType", null);
106 string lcidStr = GetString (atts, "LCID", null);
107 if (lcidStr != null) {
109 lcid = (int) UInt32.Parse (lcidStr);
111 ThrowParseException ("Invalid value for LCID: " + lcid);
114 CultureInfo ci = null;
116 ci = new CultureInfo (lcid);
118 ThrowParseException ("Unsupported LCID: " + lcid);
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: " +
128 ThrowParseException (fmt);
133 culture = GetString (atts, "Culture", null);
134 if (culture != null) {
136 ThrowParseException ("Culture and LCID are mutually exclusive.");
138 CultureInfo ci = null;
140 ci = new CultureInfo (culture);
142 ThrowParseException ("Unsupported Culture: " + culture);
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);
152 ThrowParseException (fmt);
156 uiculture = GetString (atts, "UICulture", null);
157 if (uiculture != null) {
158 CultureInfo ci = null;
160 ci = new CultureInfo (uiculture);
162 ThrowParseException ("Unsupported Culture: " + uiculture);
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);
172 ThrowParseException (fmt);
176 trace = GetBool (atts, "Trace", false);
178 string tracemodes = GetString (atts, "TraceMode", null);
179 if (tracemodes != null) {
182 tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
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.");
192 errorPage = GetString (atts, "ErrorPage", null);
193 validateRequest = GetBool (atts, "ValidateRequest", PagesConfig.ValidateRequest);
196 GetString (atts, "Buffer", null);
197 GetString (atts, "ClientTarget", null);
198 GetString (atts, "EnableViewStateMac", null);
199 GetString (atts, "SmartNavigation", null);
201 base.ProcessMainAttributes (atts);
204 static string SuggestCulture (string culture)
206 string retval = null;
207 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
208 if (ci.Name.StartsWith (culture))
209 retval += ci.Name + " ";
214 protected override Type CompileIntoType ()
216 AspGenerator generator = new AspGenerator (this);
217 return generator.GetCompiledType ();
220 internal bool EnableSessionState {
221 get { return enableSessionState; }
224 internal bool ReadOnlySessionState {
225 get { return readonlySessionState; }
228 internal bool Trace {
229 get { return trace; }
232 internal TraceMode TraceMode {
233 get { return tracemode; }
236 internal override Type DefaultBaseType {
237 get { return baseType; }
240 internal override string DefaultBaseTypeName {
241 get { return "System.Web.UI.Page"; }
244 internal override string DefaultDirectiveName {
245 get { return "page"; }
248 internal string ResponseEncoding {
249 get { return responseEncoding; }
252 internal string ContentType {
253 get { return contentType; }
256 internal int CodePage {
257 get { return codepage; }
260 internal string Culture {
261 get { return culture; }
264 internal string UICulture {
265 get { return uiculture; }
272 internal string ErrorPage {
273 get { return errorPage; }
276 internal bool ValidateRequest {
277 get { return validateRequest; }