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);
65 internal PageParser (string virtualPath, string inputFile, HttpContext context)
68 BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
69 InputFile = inputFile;
70 SetBaseType (PagesConfig.PageBaseType);
71 AddApplicationAssembly ();
74 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
78 PageParser pp = new PageParser (virtualPath, inputFile, context);
79 IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
83 internal override void ProcessMainAttributes (Hashtable atts)
85 string enabless = GetString (atts, "EnableSessionState", PagesConfig.EnableSessionState);
86 if (enabless != null) {
87 readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
88 if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
89 enableSessionState = true;
90 } else if (String.Compare (enabless, "false", true) == 0) {
91 enableSessionState = false;
93 ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
97 string cp = GetString (atts, "CodePage", null);
99 if (responseEncoding != null)
100 ThrowParseException ("CodePage and ResponseEncoding are " +
101 "mutually exclusive.");
105 codepage = (int) UInt32.Parse (cp);
107 ThrowParseException ("Invalid value for CodePage: " + cp);
111 Encoding.GetEncoding (codepage);
113 ThrowParseException ("Unsupported codepage: " + cp);
117 responseEncoding = GetString (atts, "ResponseEncoding", null);
118 if (responseEncoding != null) {
120 ThrowParseException ("CodePage and ResponseEncoding are " +
121 "mutually exclusive.");
124 Encoding.GetEncoding (responseEncoding);
126 ThrowParseException ("Unsupported encoding: " + responseEncoding);
130 contentType = GetString (atts, "ContentType", null);
132 string lcidStr = GetString (atts, "LCID", null);
133 if (lcidStr != null) {
135 lcid = (int) UInt32.Parse (lcidStr);
137 ThrowParseException ("Invalid value for LCID: " + lcid);
140 CultureInfo ci = null;
142 ci = new CultureInfo (lcid);
144 ThrowParseException ("Unsupported LCID: " + lcid);
147 if (ci.IsNeutralCulture) {
148 string suggestedCulture = SuggestCulture (ci.Name);
149 string fmt = "LCID attribute must be set to a non-neutral Culture.";
150 if (suggestedCulture != null) {
151 ThrowParseException (fmt + " Please try one of these: " +
154 ThrowParseException (fmt);
159 culture = GetString (atts, "Culture", null);
160 if (culture != null) {
162 ThrowParseException ("Culture and LCID are mutually exclusive.");
164 CultureInfo ci = null;
166 ci = new CultureInfo (culture);
168 ThrowParseException ("Unsupported Culture: " + culture);
171 if (ci.IsNeutralCulture) {
172 string suggestedCulture = SuggestCulture (culture);
173 string fmt = "Culture attribute must be set to a non-neutral Culture.";
174 if (suggestedCulture != null)
175 ThrowParseException (fmt +
176 " Please try one of these: " + suggestedCulture);
178 ThrowParseException (fmt);
182 uiculture = GetString (atts, "UICulture", null);
183 if (uiculture != null) {
184 CultureInfo ci = null;
186 ci = new CultureInfo (uiculture);
188 ThrowParseException ("Unsupported Culture: " + uiculture);
191 if (ci.IsNeutralCulture) {
192 string suggestedCulture = SuggestCulture (uiculture);
193 string fmt = "UICulture attribute must be set to a non-neutral Culture.";
194 if (suggestedCulture != null)
195 ThrowParseException (fmt +
196 " Please try one of these: " + suggestedCulture);
198 ThrowParseException (fmt);
202 string tracestr = GetString (atts, "Trace", null);
203 if (tracestr != null) {
205 atts ["Trace"] = tracestr;
206 trace = GetBool (atts, "Trace", false);
209 string tracemodes = GetString (atts, "TraceMode", null);
210 if (tracemodes != null) {
213 tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
218 if (!valid || tracemode == TraceMode.Default)
219 ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
220 "one of the following values: SortByTime, SortByCategory.");
223 errorPage = GetString (atts, "ErrorPage", null);
224 validateRequest = GetBool (atts, "ValidateRequest", PagesConfig.ValidateRequest);
225 clientTarget = GetString (atts, "ClientTarget", null);
226 if (clientTarget != null) {
227 NameValueCollection coll;
228 coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
229 if (coll == null || coll [clientTarget] == null) {
230 ThrowParseException (String.Format (
231 "ClientTarget '{0}' is an invalid alias. See the " +
232 "documentation for <clientTarget> config. section.",
235 clientTarget = (string) coll [clientTarget];
238 notBuffer = !GetBool (atts, "Buffer", true);
241 GetString (atts, "EnableViewStateMac", null);
242 GetString (atts, "SmartNavigation", null);
244 base.ProcessMainAttributes (atts);
247 static string SuggestCulture (string culture)
249 string retval = null;
250 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
251 if (ci.Name.StartsWith (culture))
252 retval += ci.Name + " ";
257 protected override Type CompileIntoType ()
259 AspGenerator generator = new AspGenerator (this);
260 return generator.GetCompiledType ();
263 internal bool EnableSessionState {
264 get { return enableSessionState; }
267 internal bool ReadOnlySessionState {
268 get { return readonlySessionState; }
271 internal bool HaveTrace {
272 get { return haveTrace; }
275 internal bool Trace {
276 get { return trace; }
279 internal TraceMode TraceMode {
280 get { return tracemode; }
283 internal override Type DefaultBaseType {
284 get { return baseType; }
287 internal override string DefaultBaseTypeName {
288 get { return "System.Web.UI.Page"; }
291 internal override string DefaultDirectiveName {
292 get { return "page"; }
295 internal string ResponseEncoding {
296 get { return responseEncoding; }
299 internal string ContentType {
300 get { return contentType; }
303 internal int CodePage {
304 get { return codepage; }
307 internal string Culture {
308 get { return culture; }
311 internal string UICulture {
312 get { return uiculture; }
319 internal string ErrorPage {
320 get { return errorPage; }
323 internal bool ValidateRequest {
324 get { return validateRequest; }
327 internal string ClientTarget {
328 get { return clientTarget; }
331 internal bool NotBuffer {
332 get { return notBuffer; }