PageCompiler.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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.Collections;
  32. using System.IO;
  33. using System.Reflection;
  34. using System.Text;
  35. using System.Web.Configuration;
  36. using System.Web.UI;
  37. using System.Web.SessionState;
  38. using System.Web.Util;
  39. #if NET_2_0
  40. using System.Web.Profile;
  41. #endif
  42. namespace System.Web.Compilation
  43. {
  44. class PageCompiler : TemplateControlCompiler
  45. {
  46. PageParser pageParser;
  47. static CodeTypeReference intRef = new CodeTypeReference (typeof (int));
  48. public PageCompiler (PageParser pageParser)
  49. : base (pageParser)
  50. {
  51. this.pageParser = pageParser;
  52. }
  53. #if NET_2_0
  54. protected override void CreateStaticFields ()
  55. {
  56. base.CreateStaticFields ();
  57. CodeMemberField fld = new CodeMemberField (typeof (object), "__fileDependencies");
  58. fld.Attributes = MemberAttributes.Private | MemberAttributes.Static;
  59. fld.InitExpression = new CodePrimitiveExpression (null);
  60. mainClass.Members.Add (fld);
  61. }
  62. #endif
  63. protected override void CreateConstructor (CodeStatementCollection localVars,
  64. CodeStatementCollection trueStmt)
  65. {
  66. if (pageParser.ClientTarget != null) {
  67. CodeExpression prop;
  68. prop = new CodePropertyReferenceExpression (thisRef, "ClientTarget");
  69. CodeExpression ct = new CodePrimitiveExpression (pageParser.ClientTarget);
  70. if (localVars == null)
  71. localVars = new CodeStatementCollection ();
  72. localVars.Add (new CodeAssignStatement (prop, ct));
  73. }
  74. #if NET_2_0
  75. ArrayList deps = pageParser.Dependencies;
  76. int depsCount = deps != null ? deps.Count : 0;
  77. if (depsCount > 0) {
  78. if (localVars == null)
  79. localVars = new CodeStatementCollection ();
  80. if (trueStmt == null)
  81. trueStmt = new CodeStatementCollection ();
  82. localVars.Add (
  83. new CodeVariableDeclarationStatement (
  84. typeof (string[]),
  85. "dependencies")
  86. );
  87. CodeVariableReferenceExpression dependencies = new CodeVariableReferenceExpression ("dependencies");
  88. trueStmt.Add (
  89. new CodeAssignStatement (dependencies, new CodeArrayCreateExpression (typeof (string), depsCount))
  90. );
  91. CodeArrayIndexerExpression arrayIndex;
  92. CodeAssignStatement assign;
  93. object o;
  94. for (int i = 0; i < depsCount; i++) {
  95. o = deps [i];
  96. arrayIndex = new CodeArrayIndexerExpression (dependencies, new CodeExpression[] {new CodePrimitiveExpression (i)});
  97. assign = new CodeAssignStatement (arrayIndex, new CodePrimitiveExpression (o));
  98. trueStmt.Add (assign);
  99. }
  100. CodeMethodInvokeExpression getDepsCall = new CodeMethodInvokeExpression (
  101. thisRef,
  102. "GetWrappedFileDependencies",
  103. new CodeExpression[] {dependencies}
  104. );
  105. assign = new CodeAssignStatement (GetMainClassFieldReferenceExpression ("__fileDependencies"), getDepsCall);
  106. trueStmt.Add (assign);
  107. }
  108. #endif
  109. base.CreateConstructor (localVars, trueStmt);
  110. }
  111. protected override void AddInterfaces ()
  112. {
  113. base.AddInterfaces ();
  114. CodeTypeReference cref;
  115. if (pageParser.EnableSessionState) {
  116. cref = new CodeTypeReference (typeof (IRequiresSessionState));
  117. #if NET_2_0
  118. if (partialClass != null)
  119. partialClass.BaseTypes.Add (cref);
  120. else
  121. #endif
  122. mainClass.BaseTypes.Add (cref);
  123. }
  124. if (pageParser.ReadOnlySessionState) {
  125. cref = new CodeTypeReference (typeof (IReadOnlySessionState));
  126. #if NET_2_0
  127. if (partialClass != null)
  128. partialClass.BaseTypes.Add (cref);
  129. else
  130. #endif
  131. mainClass.BaseTypes.Add (cref);
  132. }
  133. #if NET_2_0
  134. if (pageParser.Async)
  135. cref = new CodeTypeReference (typeof (System.Web.IHttpAsyncHandler));
  136. else
  137. cref = new CodeTypeReference (typeof (System.Web.IHttpHandler));
  138. mainClass.BaseTypes.Add (cref);
  139. #endif
  140. }
  141. void CreateGetTypeHashCode ()
  142. {
  143. CodeMemberMethod method = new CodeMemberMethod ();
  144. method.ReturnType = intRef;
  145. method.Name = "GetTypeHashCode";
  146. method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
  147. Random rnd = new Random (pageParser.InputFile.GetHashCode ());
  148. method.Statements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (rnd.Next ())));
  149. mainClass.Members.Add (method);
  150. }
  151. static CodeAssignStatement CreatePropertyAssign (CodeExpression expr, string name, object value)
  152. {
  153. CodePropertyReferenceExpression prop;
  154. prop = new CodePropertyReferenceExpression (expr, name);
  155. CodePrimitiveExpression prim;
  156. prim = new CodePrimitiveExpression (value);
  157. return new CodeAssignStatement (prop, prim);
  158. }
  159. static CodeAssignStatement CreatePropertyAssign (string name, object value)
  160. {
  161. return CreatePropertyAssign (thisRef, name, value);
  162. }
  163. protected override void AddStatementsToInitMethod (CodeMemberMethod method)
  164. {
  165. #if NET_2_0
  166. ILocation directiveLocation = pageParser.DirectiveLocation;
  167. CodeArgumentReferenceExpression ctrlVar = new CodeArgumentReferenceExpression("__ctrl");
  168. if (pageParser.Title != null)
  169. method.Statements.Add (AddLinePragma (CreatePropertyAssign (ctrlVar, "Title", pageParser.Title), directiveLocation));
  170. if (pageParser.MasterPageFile != null)
  171. method.Statements.Add (AddLinePragma (CreatePropertyAssign (ctrlVar, "MasterPageFile", pageParser.MasterPageFile), directiveLocation));
  172. if (pageParser.Theme != null)
  173. method.Statements.Add (AddLinePragma (CreatePropertyAssign (ctrlVar, "Theme", pageParser.Theme), directiveLocation));
  174. if (pageParser.StyleSheetTheme != null)
  175. method.Statements.Add (AddLinePragma (CreatePropertyAssign (ctrlVar, "StyleSheetTheme", pageParser.StyleSheetTheme), directiveLocation));
  176. if (pageParser.Async != false)
  177. method.Statements.Add (AddLinePragma (CreatePropertyAssign (ctrlVar, "AsyncMode", pageParser.Async), directiveLocation));
  178. if (pageParser.AsyncTimeout != -1)
  179. method.Statements.Add (AddLinePragma (CreatePropertyAssign (ctrlVar, "AsyncTimeout",
  180. TimeSpan.FromSeconds (pageParser.AsyncTimeout)), directiveLocation));
  181. CodeMethodInvokeExpression expr = new CodeMethodInvokeExpression (thisRef, "InitializeCulture");
  182. method.Statements.Add (AddLinePragma (new CodeExpressionStatement (expr), directiveLocation));
  183. #endif
  184. }
  185. protected override void PrependStatementsToFrameworkInitialize (CodeMemberMethod method)
  186. {
  187. base.PrependStatementsToFrameworkInitialize (method);
  188. #if NET_2_0
  189. if (pageParser.StyleSheetTheme != null)
  190. method.Statements.Add (CreatePropertyAssign ("StyleSheetTheme", pageParser.StyleSheetTheme));
  191. #endif
  192. }
  193. protected override void AppendStatementsToFrameworkInitialize (CodeMemberMethod method)
  194. {
  195. base.AppendStatementsToFrameworkInitialize (method);
  196. #if NET_2_0
  197. ArrayList deps = pageParser.Dependencies;
  198. int depsCount = deps != null ? deps.Count : 0;
  199. if (depsCount > 0) {
  200. CodeFieldReferenceExpression fileDependencies = GetMainClassFieldReferenceExpression ("__fileDependencies");
  201. method.Statements.Add (
  202. new CodeMethodInvokeExpression (
  203. thisRef,
  204. "AddWrappedFileDependencies",
  205. new CodeExpression[] {fileDependencies})
  206. );
  207. }
  208. #endif
  209. string responseEncoding = pageParser.ResponseEncoding;
  210. if (responseEncoding != null)
  211. method.Statements.Add (CreatePropertyAssign ("ResponseEncoding", responseEncoding));
  212. int codepage = pageParser.CodePage;
  213. if (codepage != -1)
  214. method.Statements.Add (CreatePropertyAssign ("CodePage", codepage));
  215. string contentType = pageParser.ContentType;
  216. if (contentType != null)
  217. method.Statements.Add (CreatePropertyAssign ("ContentType", contentType));
  218. if (pageParser.OutputCache) {
  219. CodeMethodReferenceExpression init = new CodeMethodReferenceExpression (null,
  220. "InitOutputCache");
  221. CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression (init,
  222. OutputCacheParams ());
  223. method.Statements.Add (invoke);
  224. }
  225. int lcid = pageParser.LCID;
  226. if (lcid != -1)
  227. method.Statements.Add (CreatePropertyAssign ("LCID", lcid));
  228. string culture = pageParser.Culture;
  229. if (culture != null)
  230. method.Statements.Add (CreatePropertyAssign ("Culture", culture));
  231. culture = pageParser.UICulture;
  232. if (culture != null)
  233. method.Statements.Add (CreatePropertyAssign ("UICulture", culture));
  234. string errorPage = pageParser.ErrorPage;
  235. if (errorPage != null)
  236. method.Statements.Add (CreatePropertyAssign ("ErrorPage", errorPage));
  237. if (pageParser.HaveTrace) {
  238. CodeAssignStatement stmt = new CodeAssignStatement ();
  239. stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceEnabled");
  240. stmt.Right = new CodePrimitiveExpression (pageParser.Trace);
  241. method.Statements.Add (stmt);
  242. }
  243. if (pageParser.TraceMode != TraceMode.Default) {
  244. CodeAssignStatement stmt = new CodeAssignStatement ();
  245. CodeTypeReferenceExpression tm = new CodeTypeReferenceExpression ("System.Web.TraceMode");
  246. stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceModeValue");
  247. stmt.Right = new CodeFieldReferenceExpression (tm, pageParser.TraceMode.ToString ());
  248. method.Statements.Add (stmt);
  249. }
  250. if (pageParser.NotBuffer) {
  251. CodeAssignStatement stmt = new CodeAssignStatement ();
  252. stmt.Left = new CodePropertyReferenceExpression (thisRef, "Buffer");
  253. stmt.Right = new CodePrimitiveExpression (false);
  254. method.Statements.Add (stmt);
  255. }
  256. #if NET_1_1
  257. if (pageParser.ValidateRequest) {
  258. CodeMethodInvokeExpression expr = new CodeMethodInvokeExpression ();
  259. CodePropertyReferenceExpression prop;
  260. prop = new CodePropertyReferenceExpression (thisRef, "Request");
  261. expr.Method = new CodeMethodReferenceExpression (prop, "ValidateInput");
  262. method.Statements.Add (expr);
  263. }
  264. #endif
  265. #if NET_2_0
  266. if (!pageParser.EnableEventValidation) {
  267. CodeAssignStatement stmt = new CodeAssignStatement ();
  268. CodePropertyReferenceExpression prop;
  269. prop = new CodePropertyReferenceExpression (thisRef, "EnableEventValidation");
  270. stmt.Left = prop;
  271. stmt.Right = new CodePrimitiveExpression (pageParser.EnableEventValidation);
  272. method.Statements.Add (stmt);
  273. }
  274. if (pageParser.MaintainScrollPositionOnPostBack) {
  275. CodeAssignStatement stmt = new CodeAssignStatement ();
  276. CodePropertyReferenceExpression prop;
  277. prop = new CodePropertyReferenceExpression (thisRef, "MaintainScrollPositionOnPostBack");
  278. stmt.Left = prop;
  279. stmt.Right = new CodePrimitiveExpression (pageParser.MaintainScrollPositionOnPostBack);
  280. method.Statements.Add (stmt);
  281. }
  282. #endif
  283. }
  284. private CodeExpression[] OutputCacheParams ()
  285. {
  286. return new CodeExpression [] {
  287. new CodePrimitiveExpression (pageParser.OutputCacheDuration),
  288. #if NET_2_0
  289. new CodePrimitiveExpression (pageParser.OutputCacheVaryByContentEncodings),
  290. #endif
  291. new CodePrimitiveExpression (pageParser.OutputCacheVaryByHeader),
  292. new CodePrimitiveExpression (pageParser.OutputCacheVaryByCustom),
  293. new CodeSnippetExpression (typeof (OutputCacheLocation).ToString () +
  294. "." + pageParser.OutputCacheLocation.ToString ()),
  295. new CodePrimitiveExpression (pageParser.OutputCacheVaryByParam)
  296. };
  297. }
  298. #if NET_2_0
  299. void CreateStronglyTypedProperty (Type type, string name)
  300. {
  301. if (type == null)
  302. return;
  303. CodeMemberProperty mprop = new CodeMemberProperty ();
  304. mprop.Name = name;
  305. mprop.Type = new CodeTypeReference (type);
  306. mprop.Attributes = MemberAttributes.Public | MemberAttributes.New;
  307. CodeExpression prop = new CodePropertyReferenceExpression (new CodeBaseReferenceExpression (), name);
  308. prop = new CodeCastExpression (type, prop);
  309. mprop.GetStatements.Add (new CodeMethodReturnStatement (prop));
  310. if (partialClass != null)
  311. partialClass.Members.Add (mprop);
  312. else
  313. mainClass.Members.Add (mprop);
  314. }
  315. #endif
  316. protected internal override void CreateMethods ()
  317. {
  318. base.CreateMethods ();
  319. #if NET_2_0
  320. CreateProfileProperty ();
  321. CreateStronglyTypedProperty (pageParser.MasterType, "Master");
  322. CreateStronglyTypedProperty (pageParser.PreviousPageType, "PreviousPage");
  323. #endif
  324. CreateGetTypeHashCode ();
  325. }
  326. public static Type CompilePageType (PageParser pageParser)
  327. {
  328. PageCompiler compiler = new PageCompiler (pageParser);
  329. return compiler.GetCompiledType ();
  330. }
  331. }
  332. }