PageCompiler.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. using System;
  10. using System.CodeDom;
  11. using System.IO;
  12. using System.Reflection;
  13. using System.Text;
  14. using System.Web.UI;
  15. using System.Web.SessionState;
  16. using System.Web.Util;
  17. namespace System.Web.Compilation
  18. {
  19. class PageCompiler : TemplateControlCompiler
  20. {
  21. PageParser pageParser;
  22. static CodeTypeReference intRef = new CodeTypeReference (typeof (int));
  23. public PageCompiler (PageParser pageParser)
  24. : base (pageParser)
  25. {
  26. this.pageParser = pageParser;
  27. }
  28. protected override void CreateConstructor (CodeStatementCollection localVars,
  29. CodeStatementCollection trueStmt)
  30. {
  31. if (pageParser.ClientTarget != null) {
  32. CodeExpression prop;
  33. prop = new CodePropertyReferenceExpression (thisRef, "ClientTarget");
  34. CodeExpression ct = new CodePrimitiveExpression (pageParser.ClientTarget);
  35. if (localVars == null)
  36. localVars = new CodeStatementCollection ();
  37. localVars.Add (new CodeAssignStatement (prop, ct));
  38. }
  39. base.CreateConstructor (localVars, trueStmt);
  40. }
  41. protected override void AddInterfaces ()
  42. {
  43. base.AddInterfaces ();
  44. if (pageParser.EnableSessionState)
  45. mainClass.BaseTypes.Add (new CodeTypeReference (typeof(IRequiresSessionState)));
  46. if (pageParser.ReadOnlySessionState)
  47. mainClass.BaseTypes.Add (new CodeTypeReference (typeof (IReadOnlySessionState)));
  48. }
  49. void CreateGetTypeHashCode ()
  50. {
  51. CodeMemberMethod method = new CodeMemberMethod ();
  52. method.ReturnType = intRef;
  53. method.Name = "GetTypeHashCode";
  54. method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
  55. Random rnd = new Random (pageParser.InputFile.GetHashCode ());
  56. method.Statements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (rnd.Next ())));
  57. mainClass.Members.Add (method);
  58. }
  59. static CodeAssignStatement CreatePropertyAssign (string name, object value)
  60. {
  61. CodePropertyReferenceExpression prop;
  62. prop = new CodePropertyReferenceExpression (thisRef, name);
  63. CodePrimitiveExpression prim;
  64. prim = new CodePrimitiveExpression (value);
  65. return new CodeAssignStatement (prop, prim);
  66. }
  67. protected override void AddStatementsToFrameworkInitialize (CodeMemberMethod method)
  68. {
  69. string responseEncoding = pageParser.ResponseEncoding;
  70. if (responseEncoding != null)
  71. method.Statements.Add (CreatePropertyAssign ("ResponseEncoding", responseEncoding));
  72. int codepage = pageParser.CodePage;
  73. if (codepage != -1)
  74. method.Statements.Add (CreatePropertyAssign ("CodePage", codepage));
  75. string contentType = pageParser.ContentType;
  76. if (contentType != null)
  77. method.Statements.Add (CreatePropertyAssign ("ContentType", contentType));
  78. if (pageParser.OutputCache) {
  79. CodeMethodReferenceExpression init = new CodeMethodReferenceExpression (null,
  80. "InitOutputCache");
  81. CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression (init,
  82. OutputCacheParams ());
  83. method.Statements.Add (invoke);
  84. }
  85. int lcid = pageParser.LCID;
  86. if (lcid != -1)
  87. method.Statements.Add (CreatePropertyAssign ("LCID", lcid));
  88. string culture = pageParser.Culture;
  89. if (culture != null)
  90. method.Statements.Add (CreatePropertyAssign ("Culture", culture));
  91. culture = pageParser.UICulture;
  92. if (culture != null)
  93. method.Statements.Add (CreatePropertyAssign ("UICulture", culture));
  94. string errorPage = pageParser.ErrorPage;
  95. if (errorPage != null)
  96. method.Statements.Add (CreatePropertyAssign ("ErrorPage", errorPage));
  97. if (pageParser.HaveTrace) {
  98. CodeAssignStatement stmt = new CodeAssignStatement ();
  99. stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceEnabled");
  100. stmt.Right = new CodePrimitiveExpression (pageParser.Trace);
  101. method.Statements.Add (stmt);
  102. }
  103. if (pageParser.TraceMode != TraceMode.Default) {
  104. CodeAssignStatement stmt = new CodeAssignStatement ();
  105. CodeTypeReferenceExpression tm = new CodeTypeReferenceExpression ("System.Web.TraceMode");
  106. stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceModeValue");
  107. stmt.Right = new CodeFieldReferenceExpression (tm, pageParser.TraceMode.ToString ());
  108. method.Statements.Add (stmt);
  109. }
  110. if (pageParser.NotBuffer) {
  111. CodeAssignStatement stmt = new CodeAssignStatement ();
  112. stmt.Left = new CodePropertyReferenceExpression (thisRef, "Buffer");
  113. stmt.Right = new CodePrimitiveExpression (false);
  114. method.Statements.Add (stmt);
  115. }
  116. #if NET_1_1
  117. if (pageParser.ValidateRequest) {
  118. CodeMethodInvokeExpression expr = new CodeMethodInvokeExpression ();
  119. CodePropertyReferenceExpression prop;
  120. prop = new CodePropertyReferenceExpression (thisRef, "Request");
  121. expr.Method = new CodeMethodReferenceExpression (prop, "ValidateInput");
  122. method.Statements.Add (expr);
  123. }
  124. #endif
  125. base.AddStatementsToFrameworkInitialize (method);
  126. }
  127. private CodeExpression[] OutputCacheParams ()
  128. {
  129. return new CodeExpression [] {
  130. new CodePrimitiveExpression (pageParser.OutputCacheDuration),
  131. new CodePrimitiveExpression (pageParser.OutputCacheVaryByHeader),
  132. new CodePrimitiveExpression (pageParser.OutputCacheVaryByCustom),
  133. new CodeSnippetExpression (typeof (OutputCacheLocation).ToString () +
  134. "." + pageParser.OutputCacheLocation.ToString ()),
  135. new CodePrimitiveExpression (pageParser.OutputCacheVaryByParam)
  136. };
  137. }
  138. protected override void CreateMethods ()
  139. {
  140. base.CreateMethods ();
  141. CreateGetTypeHashCode ();
  142. }
  143. public static Type CompilePageType (PageParser pageParser)
  144. {
  145. PageCompiler compiler = new PageCompiler (pageParser);
  146. return compiler.GetCompiledType ();
  147. }
  148. }
  149. }