PageParser.cs 6.7 KB

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