2 // System.Web.UI.PageParser
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
32 using System.Collections.Specialized;
33 using System.Globalization;
36 using System.Web.Compilation;
37 using System.Web.Configuration;
38 using System.Web.Util;
40 namespace System.Web.UI
42 public sealed class PageParser : TemplateControlParser
44 bool enableSessionState = true;
49 bool readonlySessionState;
50 string responseEncoding;
59 Type baseType = typeof (Page);
69 internal PageParser (string virtualPath, string inputFile, HttpContext context)
72 BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
73 InputFile = inputFile;
74 SetBaseType (PagesConfig.PageBaseType);
75 AddApplicationAssembly ();
78 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
82 PageParser pp = new PageParser (virtualPath, inputFile, context);
83 IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
87 internal override void ProcessMainAttributes (Hashtable atts)
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;
97 ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
101 string cp = GetString (atts, "CodePage", null);
103 if (responseEncoding != null)
104 ThrowParseException ("CodePage and ResponseEncoding are " +
105 "mutually exclusive.");
109 codepage = (int) UInt32.Parse (cp);
111 ThrowParseException ("Invalid value for CodePage: " + cp);
115 Encoding.GetEncoding (codepage);
117 ThrowParseException ("Unsupported codepage: " + cp);
121 responseEncoding = GetString (atts, "ResponseEncoding", null);
122 if (responseEncoding != null) {
124 ThrowParseException ("CodePage and ResponseEncoding are " +
125 "mutually exclusive.");
128 Encoding.GetEncoding (responseEncoding);
130 ThrowParseException ("Unsupported encoding: " + responseEncoding);
134 contentType = GetString (atts, "ContentType", null);
136 string lcidStr = GetString (atts, "LCID", null);
137 if (lcidStr != null) {
139 lcid = (int) UInt32.Parse (lcidStr);
141 ThrowParseException ("Invalid value for LCID: " + lcid);
144 CultureInfo ci = null;
146 ci = new CultureInfo (lcid);
148 ThrowParseException ("Unsupported LCID: " + lcid);
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: " +
158 ThrowParseException (fmt);
163 culture = GetString (atts, "Culture", null);
164 if (culture != null) {
166 ThrowParseException ("Culture and LCID are mutually exclusive.");
168 CultureInfo ci = null;
170 ci = new CultureInfo (culture);
172 ThrowParseException ("Unsupported Culture: " + culture);
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);
182 ThrowParseException (fmt);
186 uiculture = GetString (atts, "UICulture", null);
187 if (uiculture != null) {
188 CultureInfo ci = null;
190 ci = new CultureInfo (uiculture);
192 ThrowParseException ("Unsupported Culture: " + uiculture);
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);
202 ThrowParseException (fmt);
206 string tracestr = GetString (atts, "Trace", null);
207 if (tracestr != null) {
209 atts ["Trace"] = tracestr;
210 trace = GetBool (atts, "Trace", false);
213 string tracemodes = GetString (atts, "TraceMode", null);
214 if (tracemodes != null) {
217 tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
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.");
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.",
239 clientTarget = (string) coll [clientTarget];
242 notBuffer = !GetBool (atts, "Buffer", true);
245 masterPage = GetString (atts, "MasterPageFile", null);
248 GetString (atts, "EnableViewStateMac", null);
249 GetString (atts, "SmartNavigation", null);
251 base.ProcessMainAttributes (atts);
254 static string SuggestCulture (string culture)
256 string retval = null;
257 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
258 if (ci.Name.StartsWith (culture))
259 retval += ci.Name + " ";
264 protected override Type CompileIntoType ()
266 AspGenerator generator = new AspGenerator (this);
267 return generator.GetCompiledType ();
270 internal bool EnableSessionState {
271 get { return enableSessionState; }
274 internal bool ReadOnlySessionState {
275 get { return readonlySessionState; }
278 internal bool HaveTrace {
279 get { return haveTrace; }
282 internal bool Trace {
283 get { return trace; }
286 internal TraceMode TraceMode {
287 get { return tracemode; }
290 internal override Type DefaultBaseType {
291 get { return baseType; }
294 internal override string DefaultBaseTypeName {
295 get { return "System.Web.UI.Page"; }
298 internal override string DefaultDirectiveName {
299 get { return "page"; }
302 internal string ResponseEncoding {
303 get { return responseEncoding; }
306 internal string ContentType {
307 get { return contentType; }
310 internal int CodePage {
311 get { return codepage; }
314 internal string Culture {
315 get { return culture; }
318 internal string UICulture {
319 get { return uiculture; }
326 internal string ErrorPage {
327 get { return errorPage; }
330 internal bool ValidateRequest {
331 get { return validateRequest; }
334 internal string ClientTarget {
335 get { return clientTarget; }
338 internal bool NotBuffer {
339 get { return notBuffer; }
343 internal string MasterPageFile {
344 get { return masterPage; }