2 // System.Web.Compilation.PageCompiler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.Reflection;
36 using System.Web.SessionState;
37 using System.Web.Util;
39 namespace System.Web.Compilation
41 class PageCompiler : TemplateControlCompiler
43 PageParser pageParser;
44 static CodeTypeReference intRef = new CodeTypeReference (typeof (int));
46 public PageCompiler (PageParser pageParser)
49 this.pageParser = pageParser;
52 protected override void CreateConstructor (CodeStatementCollection localVars,
53 CodeStatementCollection trueStmt)
55 if (pageParser.ClientTarget != null) {
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));
65 if (pageParser.MasterPageFile != null) {
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));
75 base.CreateConstructor (localVars, trueStmt);
78 protected override void AddInterfaces ()
80 base.AddInterfaces ();
81 if (pageParser.EnableSessionState)
82 mainClass.BaseTypes.Add (new CodeTypeReference (typeof(IRequiresSessionState)));
84 if (pageParser.ReadOnlySessionState)
85 mainClass.BaseTypes.Add (new CodeTypeReference (typeof (IReadOnlySessionState)));
88 void CreateGetTypeHashCode ()
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);
99 static CodeAssignStatement CreatePropertyAssign (string name, object value)
101 CodePropertyReferenceExpression prop;
102 prop = new CodePropertyReferenceExpression (thisRef, name);
103 CodePrimitiveExpression prim;
104 prim = new CodePrimitiveExpression (value);
105 return new CodeAssignStatement (prop, prim);
108 protected override void AddStatementsToFrameworkInitialize (CodeMemberMethod method)
110 string responseEncoding = pageParser.ResponseEncoding;
111 if (responseEncoding != null)
112 method.Statements.Add (CreatePropertyAssign ("ResponseEncoding", responseEncoding));
114 int codepage = pageParser.CodePage;
116 method.Statements.Add (CreatePropertyAssign ("CodePage", codepage));
118 string contentType = pageParser.ContentType;
119 if (contentType != null)
120 method.Statements.Add (CreatePropertyAssign ("ContentType", contentType));
122 if (pageParser.OutputCache) {
123 CodeMethodReferenceExpression init = new CodeMethodReferenceExpression (null,
125 CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression (init,
126 OutputCacheParams ());
127 method.Statements.Add (invoke);
130 int lcid = pageParser.LCID;
132 method.Statements.Add (CreatePropertyAssign ("LCID", lcid));
134 string culture = pageParser.Culture;
136 method.Statements.Add (CreatePropertyAssign ("Culture", culture));
138 culture = pageParser.UICulture;
140 method.Statements.Add (CreatePropertyAssign ("UICulture", culture));
142 string errorPage = pageParser.ErrorPage;
143 if (errorPage != null)
144 method.Statements.Add (CreatePropertyAssign ("ErrorPage", errorPage));
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);
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);
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);
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);
178 base.AddStatementsToFrameworkInitialize (method);
181 private CodeExpression[] OutputCacheParams ()
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)
193 protected override void CreateMethods ()
195 base.CreateMethods ();
197 CreateGetTypeHashCode ();
200 public static Type CompilePageType (PageParser pageParser)
202 PageCompiler compiler = new PageCompiler (pageParser);
203 return compiler.GetCompiledType ();