PageCompiler.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 (string name, object value)
  80. {
  81. CodePropertyReferenceExpression prop;
  82. prop = new CodePropertyReferenceExpression (thisRef, name);
  83. CodePrimitiveExpression prim;
  84. prim = new CodePrimitiveExpression (value);
  85. return new CodeAssignStatement (prop, prim);
  86. }
  87. protected override void AddStatementsToFrameworkInitialize (CodeMemberMethod method)
  88. {
  89. string responseEncoding = pageParser.ResponseEncoding;
  90. if (responseEncoding != null)
  91. method.Statements.Add (CreatePropertyAssign ("ResponseEncoding", responseEncoding));
  92. int codepage = pageParser.CodePage;
  93. if (codepage != -1)
  94. method.Statements.Add (CreatePropertyAssign ("CodePage", codepage));
  95. string contentType = pageParser.ContentType;
  96. if (contentType != null)
  97. method.Statements.Add (CreatePropertyAssign ("ContentType", contentType));
  98. if (pageParser.OutputCache) {
  99. CodeMethodReferenceExpression init = new CodeMethodReferenceExpression (null,
  100. "InitOutputCache");
  101. CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression (init,
  102. OutputCacheParams ());
  103. method.Statements.Add (invoke);
  104. }
  105. int lcid = pageParser.LCID;
  106. if (lcid != -1)
  107. method.Statements.Add (CreatePropertyAssign ("LCID", lcid));
  108. string culture = pageParser.Culture;
  109. if (culture != null)
  110. method.Statements.Add (CreatePropertyAssign ("Culture", culture));
  111. culture = pageParser.UICulture;
  112. if (culture != null)
  113. method.Statements.Add (CreatePropertyAssign ("UICulture", culture));
  114. string errorPage = pageParser.ErrorPage;
  115. if (errorPage != null)
  116. method.Statements.Add (CreatePropertyAssign ("ErrorPage", errorPage));
  117. if (pageParser.HaveTrace) {
  118. CodeAssignStatement stmt = new CodeAssignStatement ();
  119. stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceEnabled");
  120. stmt.Right = new CodePrimitiveExpression (pageParser.Trace);
  121. method.Statements.Add (stmt);
  122. }
  123. if (pageParser.TraceMode != TraceMode.Default) {
  124. CodeAssignStatement stmt = new CodeAssignStatement ();
  125. CodeTypeReferenceExpression tm = new CodeTypeReferenceExpression ("System.Web.TraceMode");
  126. stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceModeValue");
  127. stmt.Right = new CodeFieldReferenceExpression (tm, pageParser.TraceMode.ToString ());
  128. method.Statements.Add (stmt);
  129. }
  130. if (pageParser.NotBuffer) {
  131. CodeAssignStatement stmt = new CodeAssignStatement ();
  132. stmt.Left = new CodePropertyReferenceExpression (thisRef, "Buffer");
  133. stmt.Right = new CodePrimitiveExpression (false);
  134. method.Statements.Add (stmt);
  135. }
  136. #if NET_1_1
  137. if (pageParser.ValidateRequest) {
  138. CodeMethodInvokeExpression expr = new CodeMethodInvokeExpression ();
  139. CodePropertyReferenceExpression prop;
  140. prop = new CodePropertyReferenceExpression (thisRef, "Request");
  141. expr.Method = new CodeMethodReferenceExpression (prop, "ValidateInput");
  142. method.Statements.Add (expr);
  143. }
  144. #endif
  145. base.AddStatementsToFrameworkInitialize (method);
  146. }
  147. private CodeExpression[] OutputCacheParams ()
  148. {
  149. return new CodeExpression [] {
  150. new CodePrimitiveExpression (pageParser.OutputCacheDuration),
  151. new CodePrimitiveExpression (pageParser.OutputCacheVaryByHeader),
  152. new CodePrimitiveExpression (pageParser.OutputCacheVaryByCustom),
  153. new CodeSnippetExpression (typeof (OutputCacheLocation).ToString () +
  154. "." + pageParser.OutputCacheLocation.ToString ()),
  155. new CodePrimitiveExpression (pageParser.OutputCacheVaryByParam)
  156. };
  157. }
  158. protected override void CreateMethods ()
  159. {
  160. base.CreateMethods ();
  161. CreateGetTypeHashCode ();
  162. }
  163. public static Type CompilePageType (PageParser pageParser)
  164. {
  165. PageCompiler compiler = new PageCompiler (pageParser);
  166. return compiler.GetCompiledType ();
  167. }
  168. }
  169. }