PageCompiler.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #if NET_2_0
  60. if (pageParser.MasterPageFile != null) {
  61. CodeExpression prop;
  62. prop = new CodePropertyReferenceExpression (thisRef, "MasterPageFile");
  63. CodeExpression ct = new CodePrimitiveExpression (pageParser.MasterPageFile);
  64. if (localVars == null)
  65. localVars = new CodeStatementCollection ();
  66. localVars.Add (new CodeAssignStatement (prop, ct));
  67. }
  68. #endif
  69. base.CreateConstructor (localVars, trueStmt);
  70. }
  71. protected override void AddInterfaces ()
  72. {
  73. base.AddInterfaces ();
  74. if (pageParser.EnableSessionState)
  75. mainClass.BaseTypes.Add (new CodeTypeReference (typeof(IRequiresSessionState)));
  76. if (pageParser.ReadOnlySessionState)
  77. mainClass.BaseTypes.Add (new CodeTypeReference (typeof (IReadOnlySessionState)));
  78. }
  79. void CreateGetTypeHashCode ()
  80. {
  81. CodeMemberMethod method = new CodeMemberMethod ();
  82. method.ReturnType = intRef;
  83. method.Name = "GetTypeHashCode";
  84. method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
  85. Random rnd = new Random (pageParser.InputFile.GetHashCode ());
  86. method.Statements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (rnd.Next ())));
  87. mainClass.Members.Add (method);
  88. }
  89. static CodeAssignStatement CreatePropertyAssign (string name, object value)
  90. {
  91. CodePropertyReferenceExpression prop;
  92. prop = new CodePropertyReferenceExpression (thisRef, name);
  93. CodePrimitiveExpression prim;
  94. prim = new CodePrimitiveExpression (value);
  95. return new CodeAssignStatement (prop, prim);
  96. }
  97. protected override void AddStatementsToFrameworkInitialize (CodeMemberMethod method)
  98. {
  99. string responseEncoding = pageParser.ResponseEncoding;
  100. if (responseEncoding != null)
  101. method.Statements.Add (CreatePropertyAssign ("ResponseEncoding", responseEncoding));
  102. int codepage = pageParser.CodePage;
  103. if (codepage != -1)
  104. method.Statements.Add (CreatePropertyAssign ("CodePage", codepage));
  105. string contentType = pageParser.ContentType;
  106. if (contentType != null)
  107. method.Statements.Add (CreatePropertyAssign ("ContentType", contentType));
  108. if (pageParser.OutputCache) {
  109. CodeMethodReferenceExpression init = new CodeMethodReferenceExpression (null,
  110. "InitOutputCache");
  111. CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression (init,
  112. OutputCacheParams ());
  113. method.Statements.Add (invoke);
  114. }
  115. int lcid = pageParser.LCID;
  116. if (lcid != -1)
  117. method.Statements.Add (CreatePropertyAssign ("LCID", lcid));
  118. string culture = pageParser.Culture;
  119. if (culture != null)
  120. method.Statements.Add (CreatePropertyAssign ("Culture", culture));
  121. culture = pageParser.UICulture;
  122. if (culture != null)
  123. method.Statements.Add (CreatePropertyAssign ("UICulture", culture));
  124. string errorPage = pageParser.ErrorPage;
  125. if (errorPage != null)
  126. method.Statements.Add (CreatePropertyAssign ("ErrorPage", errorPage));
  127. if (pageParser.HaveTrace) {
  128. CodeAssignStatement stmt = new CodeAssignStatement ();
  129. stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceEnabled");
  130. stmt.Right = new CodePrimitiveExpression (pageParser.Trace);
  131. method.Statements.Add (stmt);
  132. }
  133. if (pageParser.TraceMode != TraceMode.Default) {
  134. CodeAssignStatement stmt = new CodeAssignStatement ();
  135. CodeTypeReferenceExpression tm = new CodeTypeReferenceExpression ("System.Web.TraceMode");
  136. stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceModeValue");
  137. stmt.Right = new CodeFieldReferenceExpression (tm, pageParser.TraceMode.ToString ());
  138. method.Statements.Add (stmt);
  139. }
  140. if (pageParser.NotBuffer) {
  141. CodeAssignStatement stmt = new CodeAssignStatement ();
  142. stmt.Left = new CodePropertyReferenceExpression (thisRef, "Buffer");
  143. stmt.Right = new CodePrimitiveExpression (false);
  144. method.Statements.Add (stmt);
  145. }
  146. #if NET_1_1
  147. if (pageParser.ValidateRequest) {
  148. CodeMethodInvokeExpression expr = new CodeMethodInvokeExpression ();
  149. CodePropertyReferenceExpression prop;
  150. prop = new CodePropertyReferenceExpression (thisRef, "Request");
  151. expr.Method = new CodeMethodReferenceExpression (prop, "ValidateInput");
  152. method.Statements.Add (expr);
  153. }
  154. #endif
  155. base.AddStatementsToFrameworkInitialize (method);
  156. }
  157. private CodeExpression[] OutputCacheParams ()
  158. {
  159. return new CodeExpression [] {
  160. new CodePrimitiveExpression (pageParser.OutputCacheDuration),
  161. new CodePrimitiveExpression (pageParser.OutputCacheVaryByHeader),
  162. new CodePrimitiveExpression (pageParser.OutputCacheVaryByCustom),
  163. new CodeSnippetExpression (typeof (OutputCacheLocation).ToString () +
  164. "." + pageParser.OutputCacheLocation.ToString ()),
  165. new CodePrimitiveExpression (pageParser.OutputCacheVaryByParam)
  166. };
  167. }
  168. protected override void CreateMethods ()
  169. {
  170. base.CreateMethods ();
  171. CreateGetTypeHashCode ();
  172. }
  173. public static Type CompilePageType (PageParser pageParser)
  174. {
  175. PageCompiler compiler = new PageCompiler (pageParser);
  176. return compiler.GetCompiledType ();
  177. }
  178. }
  179. }