PageCompiler.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // System.Web.Compilation.PageCompiler
  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.CodeDom;
  31. using System.IO;
  32. using System.Reflection;
  33. using System.Text;
  34. using System.Web.UI;
  35. using System.Web.SessionState;
  36. using System.Web.Util;
  37. namespace System.Web.Compilation
  38. {
  39. class PageCompiler : TemplateControlCompiler
  40. {
  41. PageParser pageParser;
  42. static CodeTypeReference intRef = new CodeTypeReference (typeof (int));
  43. public PageCompiler (PageParser pageParser)
  44. : base (pageParser)
  45. {
  46. this.pageParser = pageParser;
  47. }
  48. protected override void CreateConstructor (CodeStatementCollection localVars,
  49. CodeStatementCollection trueStmt)
  50. {
  51. if (pageParser.ClientTarget != null) {
  52. CodeExpression prop;
  53. prop = new CodePropertyReferenceExpression (thisRef, "ClientTarget");
  54. CodeExpression ct = new CodePrimitiveExpression (pageParser.ClientTarget);
  55. if (localVars == null)
  56. localVars = new CodeStatementCollection ();
  57. localVars.Add (new CodeAssignStatement (prop, ct));
  58. }
  59. base.CreateConstructor (localVars, trueStmt);
  60. }
  61. protected override void AddInterfaces ()
  62. {
  63. base.AddInterfaces ();
  64. if (pageParser.EnableSessionState)
  65. mainClass.BaseTypes.Add (new CodeTypeReference (typeof(IRequiresSessionState)));
  66. if (pageParser.ReadOnlySessionState)
  67. mainClass.BaseTypes.Add (new CodeTypeReference (typeof (IReadOnlySessionState)));
  68. }
  69. void CreateGetTypeHashCode ()
  70. {
  71. CodeMemberMethod method = new CodeMemberMethod ();
  72. method.ReturnType = intRef;
  73. method.Name = "GetTypeHashCode";
  74. method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
  75. Random rnd = new Random (pageParser.InputFile.GetHashCode ());
  76. method.Statements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (rnd.Next ())));
  77. mainClass.Members.Add (method);
  78. }
  79. static CodeAssignStatement CreatePropertyAssign (CodeExpression expr, string name, object value)
  80. {
  81. CodePropertyReferenceExpression prop;
  82. prop = new CodePropertyReferenceExpression (expr, name);
  83. CodePrimitiveExpression prim;
  84. prim = new CodePrimitiveExpression (value);
  85. return new CodeAssignStatement (prop, prim);
  86. }
  87. static CodeAssignStatement CreatePropertyAssign (string name, object value)
  88. {
  89. return CreatePropertyAssign (thisRef, name, value);
  90. }
  91. protected override void AddStatementsToInitMethod (CodeMemberMethod method)
  92. {
  93. #if NET_2_0
  94. CodeArgumentReferenceExpression ctrlVar = new CodeArgumentReferenceExpression("__ctrl");
  95. if (pageParser.Title != null)
  96. method.Statements.Add (CreatePropertyAssign (ctrlVar, "Title", pageParser.Title));
  97. if (pageParser.MasterPageFile != null)
  98. method.Statements.Add (CreatePropertyAssign (ctrlVar, "MasterPageFile", pageParser.MasterPageFile));
  99. if (pageParser.Theme != null)
  100. method.Statements.Add (CreatePropertyAssign (ctrlVar, "Theme", pageParser.Theme));
  101. if (pageParser.StyleSheetTheme != null)
  102. method.Statements.Add (CreatePropertyAssign (ctrlVar, "StyleSheetTheme", pageParser.StyleSheetTheme));
  103. #endif
  104. }
  105. protected override void PrependStatementsToFrameworkInitialize (CodeMemberMethod method)
  106. {
  107. base.PrependStatementsToFrameworkInitialize (method);
  108. #if NET_2_0
  109. if (pageParser.StyleSheetTheme != null)
  110. method.Statements.Add (CreatePropertyAssign ("StyleSheetTheme", pageParser.StyleSheetTheme));
  111. #endif
  112. }
  113. protected override void AppendStatementsToFrameworkInitialize (CodeMemberMethod method)
  114. {
  115. string responseEncoding = pageParser.ResponseEncoding;
  116. if (responseEncoding != null)
  117. method.Statements.Add (CreatePropertyAssign ("ResponseEncoding", responseEncoding));
  118. int codepage = pageParser.CodePage;
  119. if (codepage != -1)
  120. method.Statements.Add (CreatePropertyAssign ("CodePage", codepage));
  121. string contentType = pageParser.ContentType;
  122. if (contentType != null)
  123. method.Statements.Add (CreatePropertyAssign ("ContentType", contentType));
  124. if (pageParser.OutputCache) {
  125. CodeMethodReferenceExpression init = new CodeMethodReferenceExpression (null,
  126. "InitOutputCache");
  127. CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression (init,
  128. OutputCacheParams ());
  129. method.Statements.Add (invoke);
  130. }
  131. int lcid = pageParser.LCID;
  132. if (lcid != -1)
  133. method.Statements.Add (CreatePropertyAssign ("LCID", lcid));
  134. string culture = pageParser.Culture;
  135. if (culture != null)
  136. method.Statements.Add (CreatePropertyAssign ("Culture", culture));
  137. culture = pageParser.UICulture;
  138. if (culture != null)
  139. method.Statements.Add (CreatePropertyAssign ("UICulture", culture));
  140. string errorPage = pageParser.ErrorPage;
  141. if (errorPage != null)
  142. method.Statements.Add (CreatePropertyAssign ("ErrorPage", errorPage));
  143. if (pageParser.HaveTrace) {
  144. CodeAssignStatement stmt = new CodeAssignStatement ();
  145. stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceEnabled");
  146. stmt.Right = new CodePrimitiveExpression (pageParser.Trace);
  147. method.Statements.Add (stmt);
  148. }
  149. if (pageParser.TraceMode != TraceMode.Default) {
  150. CodeAssignStatement stmt = new CodeAssignStatement ();
  151. CodeTypeReferenceExpression tm = new CodeTypeReferenceExpression ("System.Web.TraceMode");
  152. stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceModeValue");
  153. stmt.Right = new CodeFieldReferenceExpression (tm, pageParser.TraceMode.ToString ());
  154. method.Statements.Add (stmt);
  155. }
  156. if (pageParser.NotBuffer) {
  157. CodeAssignStatement stmt = new CodeAssignStatement ();
  158. stmt.Left = new CodePropertyReferenceExpression (thisRef, "Buffer");
  159. stmt.Right = new CodePrimitiveExpression (false);
  160. method.Statements.Add (stmt);
  161. }
  162. #if NET_1_1
  163. if (pageParser.ValidateRequest) {
  164. CodeMethodInvokeExpression expr = new CodeMethodInvokeExpression ();
  165. CodePropertyReferenceExpression prop;
  166. prop = new CodePropertyReferenceExpression (thisRef, "Request");
  167. expr.Method = new CodeMethodReferenceExpression (prop, "ValidateInput");
  168. method.Statements.Add (expr);
  169. }
  170. #endif
  171. #if NET_2_0
  172. if (!pageParser.EnableEventValidation) {
  173. CodeAssignStatement stmt = new CodeAssignStatement ();
  174. CodePropertyReferenceExpression prop;
  175. prop = new CodePropertyReferenceExpression (thisRef, "EnableEventValidation");
  176. stmt.Left = prop;
  177. stmt.Right = new CodePrimitiveExpression (pageParser.EnableEventValidation);
  178. method.Statements.Add (stmt);
  179. }
  180. #endif
  181. base.AppendStatementsToFrameworkInitialize (method);
  182. }
  183. private CodeExpression[] OutputCacheParams ()
  184. {
  185. return new CodeExpression [] {
  186. new CodePrimitiveExpression (pageParser.OutputCacheDuration),
  187. new CodePrimitiveExpression (pageParser.OutputCacheVaryByHeader),
  188. new CodePrimitiveExpression (pageParser.OutputCacheVaryByCustom),
  189. new CodeSnippetExpression (typeof (OutputCacheLocation).ToString () +
  190. "." + pageParser.OutputCacheLocation.ToString ()),
  191. new CodePrimitiveExpression (pageParser.OutputCacheVaryByParam)
  192. };
  193. }
  194. protected internal override void CreateMethods ()
  195. {
  196. base.CreateMethods ();
  197. #if NET_2_0
  198. if (pageParser.MasterType != null) {
  199. CodeMemberProperty mprop = new CodeMemberProperty ();
  200. mprop.Name = "Master";
  201. mprop.Type = new CodeTypeReference (pageParser.MasterType);
  202. mprop.Attributes = MemberAttributes.Public | MemberAttributes.New;
  203. CodeExpression prop = new CodePropertyReferenceExpression (new CodeBaseReferenceExpression (), "Master");
  204. prop = new CodeCastExpression (pageParser.MasterType, prop);
  205. mprop.GetStatements.Add (new CodeMethodReturnStatement (prop));
  206. mainClass.Members.Add (mprop);
  207. }
  208. #endif
  209. CreateGetTypeHashCode ();
  210. }
  211. public static Type CompilePageType (PageParser pageParser)
  212. {
  213. PageCompiler compiler = new PageCompiler (pageParser);
  214. return compiler.GetCompiledType ();
  215. }
  216. }
  217. }