Add new test CallBackTest.cs and 2 new resources for this test CallbackTest1.aspx...
[mono.git] / mcs / class / System.Web / System.Web.Compilation / PageCompiler.cs
1 //
2 // System.Web.Compilation.PageCompiler
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.CodeDom;
32 using System.IO;
33 using System.Reflection;
34 using System.Text;
35 using System.Web.UI;
36 using System.Web.SessionState;
37 using System.Web.Util;
38
39 namespace System.Web.Compilation
40 {
41         class PageCompiler : TemplateControlCompiler
42         {
43                 PageParser pageParser;
44                 static CodeTypeReference intRef = new CodeTypeReference (typeof (int));
45
46                 public PageCompiler (PageParser pageParser)
47                         : base (pageParser)
48                 {
49                         this.pageParser = pageParser;
50                 }
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
64                         base.CreateConstructor (localVars, trueStmt);
65                 }
66                 
67                 protected override void AddInterfaces () 
68                 {
69                         base.AddInterfaces ();
70                         if (pageParser.EnableSessionState)
71                                 mainClass.BaseTypes.Add (new CodeTypeReference (typeof(IRequiresSessionState)));
72
73                         if (pageParser.ReadOnlySessionState)
74                                 mainClass.BaseTypes.Add (new CodeTypeReference (typeof (IReadOnlySessionState)));
75                 }
76
77                 void CreateGetTypeHashCode () 
78                 {
79                         CodeMemberMethod method = new CodeMemberMethod ();
80                         method.ReturnType = intRef;
81                         method.Name = "GetTypeHashCode";
82                         method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
83                         Random rnd = new Random (pageParser.InputFile.GetHashCode ());
84                         method.Statements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (rnd.Next ())));
85                         mainClass.Members.Add (method);
86                 }
87
88                 static CodeAssignStatement CreatePropertyAssign (CodeExpression expr, string name, object value)
89                 {
90                         CodePropertyReferenceExpression prop;
91                         prop = new CodePropertyReferenceExpression (expr, name);
92                         CodePrimitiveExpression prim;
93                         prim = new CodePrimitiveExpression (value);
94                         return new CodeAssignStatement (prop, prim);
95                 }
96
97                 static CodeAssignStatement CreatePropertyAssign (string name, object value)
98                 {
99                         return CreatePropertyAssign (thisRef, name, value);
100                 }
101
102                 protected override void AddStatementsToInitMethod (CodeMemberMethod method)
103                 {
104 #if NET_2_0
105                         CodeArgumentReferenceExpression ctrlVar = new CodeArgumentReferenceExpression("__ctrl");
106                         if (pageParser.Title != null)
107                                 method.Statements.Add (CreatePropertyAssign (ctrlVar, "Title", pageParser.Title));
108
109                         if (pageParser.MasterPageFile != null)
110                                 method.Statements.Add (CreatePropertyAssign (ctrlVar, "MasterPageFile", pageParser.MasterPageFile));
111
112                         if (pageParser.Theme != null)
113                                 method.Statements.Add (CreatePropertyAssign (ctrlVar, "Theme", pageParser.Theme));
114
115                         if (pageParser.StyleSheetTheme != null)
116                                 method.Statements.Add (CreatePropertyAssign (ctrlVar, "StyleSheetTheme", pageParser.StyleSheetTheme));
117 #endif
118                 }
119
120                 protected override void PrependStatementsToFrameworkInitialize (CodeMemberMethod method)
121                 {
122                         base.PrependStatementsToFrameworkInitialize (method);
123 #if NET_2_0
124                         if (pageParser.StyleSheetTheme != null)
125                                 method.Statements.Add (CreatePropertyAssign ("StyleSheetTheme", pageParser.StyleSheetTheme));
126 #endif
127                 }
128
129                 protected override void AppendStatementsToFrameworkInitialize (CodeMemberMethod method)
130                 {
131                         string responseEncoding = pageParser.ResponseEncoding;
132                         if (responseEncoding != null)
133                                 method.Statements.Add (CreatePropertyAssign ("ResponseEncoding", responseEncoding));
134                         
135                         int codepage = pageParser.CodePage;
136                         if (codepage != -1)
137                                 method.Statements.Add (CreatePropertyAssign ("CodePage", codepage));
138
139                         string contentType = pageParser.ContentType;
140                         if (contentType != null)
141                                 method.Statements.Add (CreatePropertyAssign ("ContentType", contentType));
142
143                         if (pageParser.OutputCache) {
144                                 CodeMethodReferenceExpression init = new CodeMethodReferenceExpression (null,
145                                                 "InitOutputCache");
146                                 CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression (init,
147                                                 OutputCacheParams ());
148                                 method.Statements.Add (invoke);
149                         }
150                         
151                         int lcid = pageParser.LCID;
152                         if (lcid != -1)
153                                 method.Statements.Add (CreatePropertyAssign ("LCID", lcid));
154
155                         string culture = pageParser.Culture;
156                         if (culture != null)
157                                 method.Statements.Add (CreatePropertyAssign ("Culture", culture));
158
159                         culture = pageParser.UICulture;
160                         if (culture != null)
161                                 method.Statements.Add (CreatePropertyAssign ("UICulture", culture));
162
163                         string errorPage = pageParser.ErrorPage;
164                         if (errorPage != null)
165                                 method.Statements.Add (CreatePropertyAssign ("ErrorPage", errorPage));
166
167                         if (pageParser.HaveTrace) {
168                                 CodeAssignStatement stmt = new CodeAssignStatement ();
169                                 stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceEnabled");
170                                 stmt.Right = new CodePrimitiveExpression (pageParser.Trace);
171                                 method.Statements.Add (stmt);
172                         }
173
174                         if (pageParser.TraceMode != TraceMode.Default) {
175                                 CodeAssignStatement stmt = new CodeAssignStatement ();
176                                 CodeTypeReferenceExpression tm = new CodeTypeReferenceExpression ("System.Web.TraceMode");
177                                 stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceModeValue");
178                                 stmt.Right = new CodeFieldReferenceExpression (tm, pageParser.TraceMode.ToString ());
179                                 method.Statements.Add (stmt);
180                         }
181
182                         if (pageParser.NotBuffer) {
183                                 CodeAssignStatement stmt = new CodeAssignStatement ();
184                                 stmt.Left = new CodePropertyReferenceExpression (thisRef, "Buffer");
185                                 stmt.Right = new CodePrimitiveExpression (false);
186                                 method.Statements.Add (stmt);
187                         }
188
189 #if NET_1_1
190                         if (pageParser.ValidateRequest) {
191                                 CodeMethodInvokeExpression expr = new CodeMethodInvokeExpression ();
192                                 CodePropertyReferenceExpression prop;
193                                 prop = new CodePropertyReferenceExpression (thisRef, "Request");
194                                 expr.Method = new CodeMethodReferenceExpression (prop, "ValidateInput");
195                                 method.Statements.Add (expr);
196                         }
197 #endif
198                         
199                         base.AppendStatementsToFrameworkInitialize (method);
200                 }
201
202                 private CodeExpression[] OutputCacheParams ()
203                 {
204                         return new CodeExpression [] {
205                                 new CodePrimitiveExpression (pageParser.OutputCacheDuration),
206                                 new CodePrimitiveExpression (pageParser.OutputCacheVaryByHeader),
207                                 new CodePrimitiveExpression (pageParser.OutputCacheVaryByCustom),
208                                 new CodeSnippetExpression (typeof (OutputCacheLocation).ToString () +
209                                                 "." + pageParser.OutputCacheLocation.ToString ()),
210                                 new CodePrimitiveExpression (pageParser.OutputCacheVaryByParam)
211                                 };
212                 }
213                 
214                 protected internal override void CreateMethods ()
215                 {
216                         base.CreateMethods ();
217
218 #if NET_2_0
219                         if (pageParser.MasterType != null) {
220                                 CodeMemberProperty mprop = new CodeMemberProperty ();
221                                 mprop.Name = "Master";
222                                 mprop.Type = new CodeTypeReference (pageParser.MasterType);
223                                 mprop.Attributes = MemberAttributes.Public | MemberAttributes.New;
224                                 CodeExpression prop = new CodePropertyReferenceExpression (new CodeBaseReferenceExpression (), "Master");
225                                 prop = new CodeCastExpression (pageParser.MasterType, prop);
226                                 mprop.GetStatements.Add (new CodeMethodReturnStatement (prop));
227                                 mainClass.Members.Add (mprop);
228                         }
229 #endif
230                         
231                         CreateGetTypeHashCode ();
232                 }
233
234                 public static Type CompilePageType (PageParser pageParser)
235                 {
236                         PageCompiler compiler = new PageCompiler (pageParser);
237                         return compiler.GetCompiledType ();
238                 }
239         }
240 }
241