PageParser.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Specialized;
  32. using System.Globalization;
  33. using System.Text;
  34. using System.Web;
  35. using System.Web.Compilation;
  36. using System.Web.Configuration;
  37. using System.Web.Util;
  38. namespace System.Web.UI
  39. {
  40. public sealed class PageParser : TemplateControlParser
  41. {
  42. bool enableSessionState = true;
  43. bool haveTrace;
  44. bool trace;
  45. bool notBuffer;
  46. TraceMode tracemode;
  47. bool readonlySessionState;
  48. string responseEncoding;
  49. string contentType;
  50. int codepage = -1;
  51. int lcid = -1;
  52. string culture;
  53. string uiculture;
  54. string errorPage;
  55. bool validateRequest;
  56. string clientTarget;
  57. Type baseType = typeof (Page);
  58. public PageParser ()
  59. {
  60. }
  61. internal PageParser (string virtualPath, string inputFile, HttpContext context)
  62. {
  63. Context = context;
  64. BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
  65. InputFile = inputFile;
  66. SetBaseType (PagesConfig.PageBaseType);
  67. AddApplicationAssembly ();
  68. }
  69. public static IHttpHandler GetCompiledPageInstance (string virtualPath,
  70. string inputFile,
  71. HttpContext context)
  72. {
  73. PageParser pp = new PageParser (virtualPath, inputFile, context);
  74. IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
  75. return h;
  76. }
  77. internal override void ProcessMainAttributes (Hashtable atts)
  78. {
  79. string enabless = GetString (atts, "EnableSessionState", PagesConfig.EnableSessionState);
  80. if (enabless != null) {
  81. readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
  82. if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
  83. enableSessionState = true;
  84. } else if (String.Compare (enabless, "false", true) == 0) {
  85. enableSessionState = false;
  86. } else {
  87. ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
  88. }
  89. }
  90. string cp = GetString (atts, "CodePage", null);
  91. if (cp != null) {
  92. if (responseEncoding != null)
  93. ThrowParseException ("CodePage and ResponseEncoding are " +
  94. "mutually exclusive.");
  95. int codepage = 0;
  96. try {
  97. codepage = (int) UInt32.Parse (cp);
  98. } catch {
  99. ThrowParseException ("Invalid value for CodePage: " + cp);
  100. }
  101. try {
  102. Encoding.GetEncoding (codepage);
  103. } catch {
  104. ThrowParseException ("Unsupported codepage: " + cp);
  105. }
  106. }
  107. responseEncoding = GetString (atts, "ResponseEncoding", null);
  108. if (responseEncoding != null) {
  109. if (codepage != -1)
  110. ThrowParseException ("CodePage and ResponseEncoding are " +
  111. "mutually exclusive.");
  112. try {
  113. Encoding.GetEncoding (responseEncoding);
  114. } catch {
  115. ThrowParseException ("Unsupported encoding: " + responseEncoding);
  116. }
  117. }
  118. contentType = GetString (atts, "ContentType", null);
  119. string lcidStr = GetString (atts, "LCID", null);
  120. if (lcidStr != null) {
  121. try {
  122. lcid = (int) UInt32.Parse (lcidStr);
  123. } catch {
  124. ThrowParseException ("Invalid value for LCID: " + lcid);
  125. }
  126. CultureInfo ci = null;
  127. try {
  128. ci = new CultureInfo (lcid);
  129. } catch {
  130. ThrowParseException ("Unsupported LCID: " + lcid);
  131. }
  132. if (ci.IsNeutralCulture) {
  133. string suggestedCulture = SuggestCulture (ci.Name);
  134. string fmt = "LCID attribute must be set to a non-neutral Culture.";
  135. if (suggestedCulture != null) {
  136. ThrowParseException (fmt + " Please try one of these: " +
  137. suggestedCulture);
  138. } else {
  139. ThrowParseException (fmt);
  140. }
  141. }
  142. }
  143. culture = GetString (atts, "Culture", null);
  144. if (culture != null) {
  145. if (lcidStr != null)
  146. ThrowParseException ("Culture and LCID are mutually exclusive.");
  147. CultureInfo ci = null;
  148. try {
  149. ci = new CultureInfo (culture);
  150. } catch {
  151. ThrowParseException ("Unsupported Culture: " + culture);
  152. }
  153. if (ci.IsNeutralCulture) {
  154. string suggestedCulture = SuggestCulture (culture);
  155. string fmt = "Culture attribute must be set to a non-neutral Culture.";
  156. if (suggestedCulture != null)
  157. ThrowParseException (fmt +
  158. " Please try one of these: " + suggestedCulture);
  159. else
  160. ThrowParseException (fmt);
  161. }
  162. }
  163. uiculture = GetString (atts, "UICulture", null);
  164. if (uiculture != null) {
  165. CultureInfo ci = null;
  166. try {
  167. ci = new CultureInfo (uiculture);
  168. } catch {
  169. ThrowParseException ("Unsupported Culture: " + uiculture);
  170. }
  171. if (ci.IsNeutralCulture) {
  172. string suggestedCulture = SuggestCulture (uiculture);
  173. string fmt = "UICulture attribute must be set to a non-neutral Culture.";
  174. if (suggestedCulture != null)
  175. ThrowParseException (fmt +
  176. " Please try one of these: " + suggestedCulture);
  177. else
  178. ThrowParseException (fmt);
  179. }
  180. }
  181. string tracestr = GetString (atts, "Trace", null);
  182. if (tracestr != null) {
  183. haveTrace = true;
  184. atts ["Trace"] = tracestr;
  185. trace = GetBool (atts, "Trace", false);
  186. }
  187. string tracemodes = GetString (atts, "TraceMode", null);
  188. if (tracemodes != null) {
  189. bool valid = true;
  190. try {
  191. tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
  192. } catch {
  193. valid = false;
  194. }
  195. if (!valid || tracemode == TraceMode.Default)
  196. ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
  197. "one of the following values: SortByTime, SortByCategory.");
  198. }
  199. errorPage = GetString (atts, "ErrorPage", null);
  200. validateRequest = GetBool (atts, "ValidateRequest", PagesConfig.ValidateRequest);
  201. clientTarget = GetString (atts, "ClientTarget", null);
  202. if (clientTarget != null) {
  203. NameValueCollection coll;
  204. coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
  205. if (coll == null || coll [clientTarget] == null) {
  206. ThrowParseException (String.Format (
  207. "ClientTarget '{0}' is an invalid alias. See the " +
  208. "documentation for <clientTarget> config. section.",
  209. clientTarget));
  210. }
  211. clientTarget = (string) coll [clientTarget];
  212. }
  213. notBuffer = !GetBool (atts, "Buffer", true);
  214. // Ignored by now
  215. GetString (atts, "EnableViewStateMac", null);
  216. GetString (atts, "SmartNavigation", null);
  217. base.ProcessMainAttributes (atts);
  218. }
  219. static string SuggestCulture (string culture)
  220. {
  221. string retval = null;
  222. foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
  223. if (ci.Name.StartsWith (culture))
  224. retval += ci.Name + " ";
  225. }
  226. return retval;
  227. }
  228. protected override Type CompileIntoType ()
  229. {
  230. AspGenerator generator = new AspGenerator (this);
  231. return generator.GetCompiledType ();
  232. }
  233. internal bool EnableSessionState {
  234. get { return enableSessionState; }
  235. }
  236. internal bool ReadOnlySessionState {
  237. get { return readonlySessionState; }
  238. }
  239. internal bool HaveTrace {
  240. get { return haveTrace; }
  241. }
  242. internal bool Trace {
  243. get { return trace; }
  244. }
  245. internal TraceMode TraceMode {
  246. get { return tracemode; }
  247. }
  248. internal override Type DefaultBaseType {
  249. get { return baseType; }
  250. }
  251. internal override string DefaultBaseTypeName {
  252. get { return "System.Web.UI.Page"; }
  253. }
  254. internal override string DefaultDirectiveName {
  255. get { return "page"; }
  256. }
  257. internal string ResponseEncoding {
  258. get { return responseEncoding; }
  259. }
  260. internal string ContentType {
  261. get { return contentType; }
  262. }
  263. internal int CodePage {
  264. get { return codepage; }
  265. }
  266. internal string Culture {
  267. get { return culture; }
  268. }
  269. internal string UICulture {
  270. get { return uiculture; }
  271. }
  272. internal int LCID {
  273. get { return lcid; }
  274. }
  275. internal string ErrorPage {
  276. get { return errorPage; }
  277. }
  278. internal bool ValidateRequest {
  279. get { return validateRequest; }
  280. }
  281. internal string ClientTarget {
  282. get { return clientTarget; }
  283. }
  284. internal bool NotBuffer {
  285. get { return notBuffer; }
  286. }
  287. }
  288. }