2
0

PageCompiler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. if (pageParser.EnableSessionState)
  69. mainClass.BaseTypes.Add (new CodeTypeReference (typeof(IRequiresSessionState)));
  70. if (pageParser.ReadOnlySessionState)
  71. mainClass.BaseTypes.Add (new CodeTypeReference (typeof (IReadOnlySessionState)));
  72. }
  73. void CreateGetTypeHashCode ()
  74. {
  75. CodeMemberMethod method = new CodeMemberMethod ();
  76. method.ReturnType = intRef;
  77. method.Name = "GetTypeHashCode";
  78. method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
  79. Random rnd = new Random (pageParser.InputFile.GetHashCode ());
  80. method.Statements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (rnd.Next ())));
  81. mainClass.Members.Add (method);
  82. }
  83. void InternalCreatePageProperty (string retType, string name, string contextProperty)
  84. {
  85. CodeMemberProperty property = new CodeMemberProperty ();
  86. property.Name = name;
  87. property.Type = new CodeTypeReference (retType);
  88. property.Attributes = MemberAttributes.Family | MemberAttributes.Final;
  89. CodeMethodReturnStatement ret = new CodeMethodReturnStatement ();
  90. CodeCastExpression cast = new CodeCastExpression ();
  91. ret.Expression = cast;
  92. CodePropertyReferenceExpression refexp = new CodePropertyReferenceExpression ();
  93. refexp.TargetObject = new CodePropertyReferenceExpression (new CodeThisReferenceExpression (), "Context");
  94. refexp.PropertyName = contextProperty;
  95. cast.TargetType = new CodeTypeReference (retType);
  96. cast.Expression = refexp;
  97. property.GetStatements.Add (ret);
  98. mainClass.Members.Add (property);
  99. }
  100. #if NET_2_0
  101. void CreateProfileProperty ()
  102. {
  103. string retType;
  104. ProfileSection ps = WebConfigurationManager.GetSection ("system.web/profile") as ProfileSection;
  105. if (ps != null && ps.PropertySettings.Count > 0)
  106. retType = "ProfileCommon";
  107. else
  108. retType = "System.Web.Profile.DefaultProfile";
  109. InternalCreatePageProperty (retType, "Profile", "Profile");
  110. }
  111. #endif
  112. static CodeAssignStatement CreatePropertyAssign (CodeExpression expr, string name, object value)
  113. {
  114. CodePropertyReferenceExpression prop;
  115. prop = new CodePropertyReferenceExpression (expr, name);
  116. CodePrimitiveExpression prim;
  117. prim = new CodePrimitiveExpression (value);
  118. return new CodeAssignStatement (prop, prim);
  119. }
  120. static CodeAssignStatement CreatePropertyAssign (string name, object value)
  121. {
  122. return CreatePropertyAssign (thisRef, name, value);
  123. }
  124. protected override void AddStatementsToInitMethod (CodeMemberMethod method)
  125. {
  126. #if NET_2_0
  127. CodeMethodInvokeExpression expr = new CodeMethodInvokeExpression (thisRef, "InitializeCulture");
  128. method.Statements.Add (new CodeExpressionStatement (expr));
  129. CodeArgumentReferenceExpression ctrlVar = new CodeArgumentReferenceExpression("__ctrl");
  130. if (pageParser.Title != null)
  131. method.Statements.Add (CreatePropertyAssign (ctrlVar, "Title", pageParser.Title));
  132. if (pageParser.MasterPageFile != null)
  133. method.Statements.Add (CreatePropertyAssign (ctrlVar, "MasterPageFile", pageParser.MasterPageFile));
  134. if (pageParser.Theme != null)
  135. method.Statements.Add (CreatePropertyAssign (ctrlVar, "Theme", pageParser.Theme));
  136. if (pageParser.StyleSheetTheme != null)
  137. method.Statements.Add (CreatePropertyAssign (ctrlVar, "StyleSheetTheme", pageParser.StyleSheetTheme));
  138. #endif
  139. }
  140. protected override void PrependStatementsToFrameworkInitialize (CodeMemberMethod method)
  141. {
  142. base.PrependStatementsToFrameworkInitialize (method);
  143. #if NET_2_0
  144. if (pageParser.StyleSheetTheme != null)
  145. method.Statements.Add (CreatePropertyAssign ("StyleSheetTheme", pageParser.StyleSheetTheme));
  146. #endif
  147. }
  148. protected override void AppendStatementsToFrameworkInitialize (CodeMemberMethod method)
  149. {
  150. string responseEncoding = pageParser.ResponseEncoding;
  151. if (responseEncoding != null)
  152. method.Statements.Add (CreatePropertyAssign ("ResponseEncoding", responseEncoding));
  153. int codepage = pageParser.CodePage;
  154. if (codepage != -1)
  155. method.Statements.Add (CreatePropertyAssign ("CodePage", codepage));
  156. string contentType = pageParser.ContentType;
  157. if (contentType != null)
  158. method.Statements.Add (CreatePropertyAssign ("ContentType", contentType));
  159. if (pageParser.OutputCache) {
  160. CodeMethodReferenceExpression init = new CodeMethodReferenceExpression (null,
  161. "InitOutputCache");
  162. CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression (init,
  163. OutputCacheParams ());
  164. method.Statements.Add (invoke);
  165. }
  166. int lcid = pageParser.LCID;
  167. if (lcid != -1)
  168. method.Statements.Add (CreatePropertyAssign ("LCID", lcid));
  169. string culture = pageParser.Culture;
  170. if (culture != null)
  171. method.Statements.Add (CreatePropertyAssign ("Culture", culture));
  172. culture = pageParser.UICulture;
  173. if (culture != null)
  174. method.Statements.Add (CreatePropertyAssign ("UICulture", culture));
  175. string errorPage = pageParser.ErrorPage;
  176. if (errorPage != null)
  177. method.Statements.Add (CreatePropertyAssign ("ErrorPage", errorPage));
  178. if (pageParser.HaveTrace) {
  179. CodeAssignStatement stmt = new CodeAssignStatement ();
  180. stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceEnabled");
  181. stmt.Right = new CodePrimitiveExpression (pageParser.Trace);
  182. method.Statements.Add (stmt);
  183. }
  184. if (pageParser.TraceMode != TraceMode.Default) {
  185. CodeAssignStatement stmt = new CodeAssignStatement ();
  186. CodeTypeReferenceExpression tm = new CodeTypeReferenceExpression ("System.Web.TraceMode");
  187. stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceModeValue");
  188. stmt.Right = new CodeFieldReferenceExpression (tm, pageParser.TraceMode.ToString ());
  189. method.Statements.Add (stmt);
  190. }
  191. if (pageParser.NotBuffer) {
  192. CodeAssignStatement stmt = new CodeAssignStatement ();
  193. stmt.Left = new CodePropertyReferenceExpression (thisRef, "Buffer");
  194. stmt.Right = new CodePrimitiveExpression (false);
  195. method.Statements.Add (stmt);
  196. }
  197. #if NET_1_1
  198. if (pageParser.ValidateRequest) {
  199. CodeMethodInvokeExpression expr = new CodeMethodInvokeExpression ();
  200. CodePropertyReferenceExpression prop;
  201. prop = new CodePropertyReferenceExpression (thisRef, "Request");
  202. expr.Method = new CodeMethodReferenceExpression (prop, "ValidateInput");
  203. method.Statements.Add (expr);
  204. }
  205. #endif
  206. #if NET_2_0
  207. if (!pageParser.EnableEventValidation) {
  208. CodeAssignStatement stmt = new CodeAssignStatement ();
  209. CodePropertyReferenceExpression prop;
  210. prop = new CodePropertyReferenceExpression (thisRef, "EnableEventValidation");
  211. stmt.Left = prop;
  212. stmt.Right = new CodePrimitiveExpression (pageParser.EnableEventValidation);
  213. method.Statements.Add (stmt);
  214. }
  215. #endif
  216. base.AppendStatementsToFrameworkInitialize (method);
  217. }
  218. private CodeExpression[] OutputCacheParams ()
  219. {
  220. return new CodeExpression [] {
  221. new CodePrimitiveExpression (pageParser.OutputCacheDuration),
  222. new CodePrimitiveExpression (pageParser.OutputCacheVaryByHeader),
  223. new CodePrimitiveExpression (pageParser.OutputCacheVaryByCustom),
  224. new CodeSnippetExpression (typeof (OutputCacheLocation).ToString () +
  225. "." + pageParser.OutputCacheLocation.ToString ()),
  226. new CodePrimitiveExpression (pageParser.OutputCacheVaryByParam)
  227. };
  228. }
  229. protected internal override void CreateMethods ()
  230. {
  231. base.CreateMethods ();
  232. #if NET_2_0
  233. CreateProfileProperty ();
  234. if (pageParser.MasterType != null) {
  235. CodeMemberProperty mprop = new CodeMemberProperty ();
  236. mprop.Name = "Master";
  237. mprop.Type = new CodeTypeReference (pageParser.MasterType);
  238. mprop.Attributes = MemberAttributes.Public | MemberAttributes.New;
  239. CodeExpression prop = new CodePropertyReferenceExpression (new CodeBaseReferenceExpression (), "Master");
  240. prop = new CodeCastExpression (pageParser.MasterType, prop);
  241. mprop.GetStatements.Add (new CodeMethodReturnStatement (prop));
  242. mainClass.Members.Add (mprop);
  243. }
  244. #endif
  245. CreateGetTypeHashCode ();
  246. }
  247. public static Type CompilePageType (PageParser pageParser)
  248. {
  249. PageCompiler compiler = new PageCompiler (pageParser);
  250. return compiler.GetCompiledType ();
  251. }
  252. }
  253. }