2005-06-05 Peter Bartok <pbartok@novell.com>
[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 #if NET_2_0
65                         if (pageParser.MasterPageFile != null) {
66                                 CodeExpression prop;
67                                 prop = new CodePropertyReferenceExpression (thisRef, "MasterPageFile");
68                                 CodeExpression ct = new CodePrimitiveExpression (pageParser.MasterPageFile);
69                                 if (localVars == null)
70                                         localVars = new CodeStatementCollection ();
71                                 localVars.Add (new CodeAssignStatement (prop, ct));
72                         }
73 #endif
74
75                         base.CreateConstructor (localVars, trueStmt);
76                 }
77                 
78                 protected override void AddInterfaces () 
79                 {
80                         base.AddInterfaces ();
81                         if (pageParser.EnableSessionState)
82                                 mainClass.BaseTypes.Add (new CodeTypeReference (typeof(IRequiresSessionState)));
83
84                         if (pageParser.ReadOnlySessionState)
85                                 mainClass.BaseTypes.Add (new CodeTypeReference (typeof (IReadOnlySessionState)));
86                 }
87
88                 void CreateGetTypeHashCode () 
89                 {
90                         CodeMemberMethod method = new CodeMemberMethod ();
91                         method.ReturnType = intRef;
92                         method.Name = "GetTypeHashCode";
93                         method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
94                         Random rnd = new Random (pageParser.InputFile.GetHashCode ());
95                         method.Statements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (rnd.Next ())));
96                         mainClass.Members.Add (method);
97                 }
98
99                 static CodeAssignStatement CreatePropertyAssign (string name, object value)
100                 {
101                         CodePropertyReferenceExpression prop;
102                         prop = new CodePropertyReferenceExpression (thisRef, name);
103                         CodePrimitiveExpression prim;
104                         prim = new CodePrimitiveExpression (value);
105                         return new CodeAssignStatement (prop, prim);
106                 }
107
108                 protected override void AddStatementsToFrameworkInitialize (CodeMemberMethod method)
109                 {
110                         string responseEncoding = pageParser.ResponseEncoding;
111                         if (responseEncoding != null)
112                                 method.Statements.Add (CreatePropertyAssign ("ResponseEncoding", responseEncoding));
113                         
114                         int codepage = pageParser.CodePage;
115                         if (codepage != -1)
116                                 method.Statements.Add (CreatePropertyAssign ("CodePage", codepage));
117
118                         string contentType = pageParser.ContentType;
119                         if (contentType != null)
120                                 method.Statements.Add (CreatePropertyAssign ("ContentType", contentType));
121
122                         if (pageParser.OutputCache) {
123                                 CodeMethodReferenceExpression init = new CodeMethodReferenceExpression (null,
124                                                 "InitOutputCache");
125                                 CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression (init,
126                                                 OutputCacheParams ());
127                                 method.Statements.Add (invoke);
128                         }
129                         
130                         int lcid = pageParser.LCID;
131                         if (lcid != -1)
132                                 method.Statements.Add (CreatePropertyAssign ("LCID", lcid));
133
134                         string culture = pageParser.Culture;
135                         if (culture != null)
136                                 method.Statements.Add (CreatePropertyAssign ("Culture", culture));
137
138                         culture = pageParser.UICulture;
139                         if (culture != null)
140                                 method.Statements.Add (CreatePropertyAssign ("UICulture", culture));
141
142                         string errorPage = pageParser.ErrorPage;
143                         if (errorPage != null)
144                                 method.Statements.Add (CreatePropertyAssign ("ErrorPage", errorPage));
145
146                         if (pageParser.HaveTrace) {
147                                 CodeAssignStatement stmt = new CodeAssignStatement ();
148                                 stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceEnabled");
149                                 stmt.Right = new CodePrimitiveExpression (pageParser.Trace);
150                                 method.Statements.Add (stmt);
151                         }
152
153                         if (pageParser.TraceMode != TraceMode.Default) {
154                                 CodeAssignStatement stmt = new CodeAssignStatement ();
155                                 CodeTypeReferenceExpression tm = new CodeTypeReferenceExpression ("System.Web.TraceMode");
156                                 stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceModeValue");
157                                 stmt.Right = new CodeFieldReferenceExpression (tm, pageParser.TraceMode.ToString ());
158                                 method.Statements.Add (stmt);
159                         }
160
161                         if (pageParser.NotBuffer) {
162                                 CodeAssignStatement stmt = new CodeAssignStatement ();
163                                 stmt.Left = new CodePropertyReferenceExpression (thisRef, "Buffer");
164                                 stmt.Right = new CodePrimitiveExpression (false);
165                                 method.Statements.Add (stmt);
166                         }
167
168 #if NET_1_1
169                         if (pageParser.ValidateRequest) {
170                                 CodeMethodInvokeExpression expr = new CodeMethodInvokeExpression ();
171                                 CodePropertyReferenceExpression prop;
172                                 prop = new CodePropertyReferenceExpression (thisRef, "Request");
173                                 expr.Method = new CodeMethodReferenceExpression (prop, "ValidateInput");
174                                 method.Statements.Add (expr);
175                         }
176 #endif
177                         
178                         base.AddStatementsToFrameworkInitialize (method);
179                 }
180
181                 private CodeExpression[] OutputCacheParams ()
182                 {
183                         return new CodeExpression [] {
184                                 new CodePrimitiveExpression (pageParser.OutputCacheDuration),
185                                 new CodePrimitiveExpression (pageParser.OutputCacheVaryByHeader),
186                                 new CodePrimitiveExpression (pageParser.OutputCacheVaryByCustom),
187                                 new CodeSnippetExpression (typeof (OutputCacheLocation).ToString () +
188                                                 "." + pageParser.OutputCacheLocation.ToString ()),
189                                 new CodePrimitiveExpression (pageParser.OutputCacheVaryByParam)
190                                 };
191                 }
192                 
193                 protected override void CreateMethods ()
194                 {
195                         base.CreateMethods ();
196
197                         CreateGetTypeHashCode ();
198                 }
199
200                 public static Type CompilePageType (PageParser pageParser)
201                 {
202                         PageCompiler compiler = new PageCompiler (pageParser);
203                         return compiler.GetCompiledType ();
204                 }
205         }
206 }
207