PageParser.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // System.Web.UI.PageParser
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  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. namespace System.Web.UI
  17. {
  18. public sealed class PageParser : TemplateControlParser
  19. {
  20. bool enableSessionState = true;
  21. bool readonlySessionState;
  22. string responseEncoding;
  23. string contentType;
  24. int codepage = -1;
  25. int lcid = -1;
  26. string culture;
  27. string uiculture;
  28. // FIXME: this is here just for DesignTimeTemplateParser. Anything to do?
  29. internal PageParser ()
  30. {
  31. }
  32. internal PageParser (string virtualPath, string inputFile, HttpContext context)
  33. {
  34. Context = context;
  35. BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
  36. InputFile = inputFile;
  37. AddApplicationAssembly ();
  38. }
  39. public static IHttpHandler GetCompiledPageInstance (string virtualPath,
  40. string inputFile,
  41. HttpContext context)
  42. {
  43. PageParser pp = new PageParser (virtualPath, inputFile, context);
  44. IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
  45. return h;
  46. }
  47. internal override void ProcessMainAttributes (Hashtable atts)
  48. {
  49. string enabless = GetString (atts, "EnableSessionState", null);
  50. if (enabless != null) {
  51. readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
  52. if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
  53. enableSessionState = true;
  54. } else if (String.Compare (enabless, "false", true) == 0) {
  55. enableSessionState = false;
  56. } else {
  57. ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
  58. }
  59. }
  60. string cp = GetString (atts, "CodePage", null);
  61. if (cp != null) {
  62. if (responseEncoding != null)
  63. ThrowParseException ("CodePage and ResponseEncoding are " +
  64. "mutually exclusive.");
  65. int codepage = 0;
  66. try {
  67. codepage = (int) UInt32.Parse (cp);
  68. } catch {
  69. ThrowParseException ("Invalid value for CodePage: " + cp);
  70. }
  71. try {
  72. Encoding.GetEncoding (codepage);
  73. } catch {
  74. ThrowParseException ("Unsupported codepage: " + cp);
  75. }
  76. }
  77. responseEncoding = GetString (atts, "ResponseEncoding", null);
  78. if (responseEncoding != null) {
  79. if (codepage != -1)
  80. ThrowParseException ("CodePage and ResponseEncoding are " +
  81. "mutually exclusive.");
  82. try {
  83. Encoding.GetEncoding (responseEncoding);
  84. } catch {
  85. ThrowParseException ("Unsupported encoding: " + responseEncoding);
  86. }
  87. }
  88. contentType = GetString (atts, "ContentType", null);
  89. string lcidStr = GetString (atts, "LCID", null);
  90. if (lcidStr != null) {
  91. try {
  92. lcid = (int) UInt32.Parse (lcidStr);
  93. } catch {
  94. ThrowParseException ("Invalid value for LCID: " + lcid);
  95. }
  96. CultureInfo ci = null;
  97. try {
  98. ci = new CultureInfo (lcid);
  99. } catch {
  100. ThrowParseException ("Unsupported LCID: " + lcid);
  101. }
  102. if (ci.IsNeutralCulture) {
  103. string suggestedCulture = SuggestCulture (ci.Name);
  104. string fmt = "LCID attribute must be set to a non-neutral Culture.";
  105. if (suggestedCulture != null) {
  106. ThrowParseException (fmt + " Please try one of these: " +
  107. suggestedCulture);
  108. } else {
  109. ThrowParseException (fmt);
  110. }
  111. }
  112. }
  113. culture = GetString (atts, "Culture", null);
  114. if (culture != null) {
  115. if (lcidStr != null)
  116. ThrowParseException ("Culture and LCID are mutually exclusive.");
  117. CultureInfo ci = null;
  118. try {
  119. ci = new CultureInfo (culture);
  120. } catch {
  121. ThrowParseException ("Unsupported Culture: " + culture);
  122. }
  123. if (ci.IsNeutralCulture) {
  124. string suggestedCulture = SuggestCulture (culture);
  125. string fmt = "Culture attribute must be set to a non-neutral Culture.";
  126. if (suggestedCulture != null)
  127. ThrowParseException (fmt +
  128. " Please try one of these: " + suggestedCulture);
  129. else
  130. ThrowParseException (fmt);
  131. }
  132. }
  133. uiculture = GetString (atts, "UICulture", null);
  134. if (uiculture != null) {
  135. CultureInfo ci = null;
  136. try {
  137. ci = new CultureInfo (uiculture);
  138. } catch {
  139. ThrowParseException ("Unsupported Culture: " + uiculture);
  140. }
  141. if (ci.IsNeutralCulture) {
  142. string suggestedCulture = SuggestCulture (uiculture);
  143. string fmt = "UICulture attribute must be set to a non-neutral Culture.";
  144. if (suggestedCulture != null)
  145. ThrowParseException (fmt +
  146. " Please try one of these: " + suggestedCulture);
  147. else
  148. ThrowParseException (fmt);
  149. }
  150. }
  151. // Ignored by now
  152. GetString (atts, "Buffer", null);
  153. GetString (atts, "ClientTarget", null);
  154. GetString (atts, "EnableViewStateMac", null);
  155. GetString (atts, "ErrorPage", null);
  156. GetString (atts, "Trace", null);
  157. GetString (atts, "TraceMode", null);
  158. GetString (atts, "SmartNavigation", null);
  159. GetBool (atts, "ValidateRequest", true);
  160. base.ProcessMainAttributes (atts);
  161. }
  162. static string SuggestCulture (string culture)
  163. {
  164. string retval = null;
  165. foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
  166. if (ci.Name.StartsWith (culture))
  167. retval += ci.Name + " ";
  168. }
  169. return retval;
  170. }
  171. protected override Type CompileIntoType ()
  172. {
  173. AspGenerator generator = new AspGenerator (this);
  174. return generator.GetCompiledType ();
  175. }
  176. internal bool EnableSessionState {
  177. get { return enableSessionState; }
  178. }
  179. internal bool ReadOnlySessionState {
  180. get { return readonlySessionState; }
  181. }
  182. internal override Type DefaultBaseType {
  183. get { return typeof (Page); }
  184. }
  185. internal override string DefaultDirectiveName {
  186. get { return "page"; }
  187. }
  188. internal string ResponseEncoding {
  189. get { return responseEncoding; }
  190. }
  191. internal string ContentType {
  192. get { return contentType; }
  193. }
  194. internal int CodePage {
  195. get { return codepage; }
  196. }
  197. internal string Culture {
  198. get { return culture; }
  199. }
  200. internal string UICulture {
  201. get { return uiculture; }
  202. }
  203. internal int LCID {
  204. get { return lcid; }
  205. }
  206. }
  207. }