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;
32 // FIXME: this is here just for DesignTimeTemplateParser. Anything to do?
33 internal PageParser ()
37 internal PageParser (string virtualPath, string inputFile, HttpContext context)
40 BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
41 InputFile = inputFile;
42 AddApplicationAssembly ();
45 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
49 PageParser pp = new PageParser (virtualPath, inputFile, context);
50 IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
54 internal override void ProcessMainAttributes (Hashtable atts)
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;
64 ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
68 string cp = GetString (atts, "CodePage", null);
70 if (responseEncoding != null)
71 ThrowParseException ("CodePage and ResponseEncoding are " +
72 "mutually exclusive.");
76 codepage = (int) UInt32.Parse (cp);
78 ThrowParseException ("Invalid value for CodePage: " + cp);
82 Encoding.GetEncoding (codepage);
84 ThrowParseException ("Unsupported codepage: " + cp);
88 responseEncoding = GetString (atts, "ResponseEncoding", null);
89 if (responseEncoding != null) {
91 ThrowParseException ("CodePage and ResponseEncoding are " +
92 "mutually exclusive.");
95 Encoding.GetEncoding (responseEncoding);
97 ThrowParseException ("Unsupported encoding: " + responseEncoding);
101 contentType = GetString (atts, "ContentType", null);
103 string lcidStr = GetString (atts, "LCID", null);
104 if (lcidStr != null) {
106 lcid = (int) UInt32.Parse (lcidStr);
108 ThrowParseException ("Invalid value for LCID: " + lcid);
111 CultureInfo ci = null;
113 ci = new CultureInfo (lcid);
115 ThrowParseException ("Unsupported LCID: " + lcid);
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: " +
125 ThrowParseException (fmt);
130 culture = GetString (atts, "Culture", null);
131 if (culture != null) {
133 ThrowParseException ("Culture and LCID are mutually exclusive.");
135 CultureInfo ci = null;
137 ci = new CultureInfo (culture);
139 ThrowParseException ("Unsupported Culture: " + culture);
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);
149 ThrowParseException (fmt);
153 uiculture = GetString (atts, "UICulture", null);
154 if (uiculture != null) {
155 CultureInfo ci = null;
157 ci = new CultureInfo (uiculture);
159 ThrowParseException ("Unsupported Culture: " + uiculture);
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);
169 ThrowParseException (fmt);
173 trace = GetBool (atts, "Trace", false);
175 string tracemodes = GetString (atts, "TraceMode", null);
176 if (tracemodes != null) {
179 tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
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.");
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);
199 base.ProcessMainAttributes (atts);
202 static string SuggestCulture (string culture)
204 string retval = null;
205 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
206 if (ci.Name.StartsWith (culture))
207 retval += ci.Name + " ";
212 protected override Type CompileIntoType ()
214 AspGenerator generator = new AspGenerator (this);
215 return generator.GetCompiledType ();
218 internal bool EnableSessionState {
219 get { return enableSessionState; }
222 internal bool ReadOnlySessionState {
223 get { return readonlySessionState; }
226 internal bool Trace {
227 get { return trace; }
230 internal TraceMode TraceMode {
231 get { return tracemode; }
234 internal override Type DefaultBaseType {
235 get { return typeof (Page); }
238 internal override string DefaultDirectiveName {
239 get { return "page"; }
242 internal string ResponseEncoding {
243 get { return responseEncoding; }
246 internal string ContentType {
247 get { return contentType; }
250 internal int CodePage {
251 get { return codepage; }
254 internal string Culture {
255 get { return culture; }
258 internal string UICulture {
259 get { return uiculture; }