PageCompiler.cs 11 KB

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