PageParser.cs 6.8 KB

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