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));
64 base.CreateConstructor (localVars, trueStmt);
67 protected override void AddInterfaces ()
69 base.AddInterfaces ();
70 if (pageParser.EnableSessionState)
71 mainClass.BaseTypes.Add (new CodeTypeReference (typeof(IRequiresSessionState)));
73 if (pageParser.ReadOnlySessionState)
74 mainClass.BaseTypes.Add (new CodeTypeReference (typeof (IReadOnlySessionState)));
77 void CreateGetTypeHashCode ()
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);
88 static CodeAssignStatement CreatePropertyAssign (CodeExpression expr, string name, object value)
90 CodePropertyReferenceExpression prop;
91 prop = new CodePropertyReferenceExpression (expr, name);
92 CodePrimitiveExpression prim;
93 prim = new CodePrimitiveExpression (value);
94 return new CodeAssignStatement (prop, prim);
97 static CodeAssignStatement CreatePropertyAssign (string name, object value)
99 return CreatePropertyAssign (thisRef, name, value);
102 protected override void AddStatementsToInitMethod (CodeMemberMethod method)
105 CodeArgumentReferenceExpression ctrlVar = new CodeArgumentReferenceExpression("__ctrl");
106 if (pageParser.Title != null)
107 method.Statements.Add (CreatePropertyAssign (ctrlVar, "Title", pageParser.Title));
109 if (pageParser.MasterPageFile != null)
110 method.Statements.Add (CreatePropertyAssign (ctrlVar, "MasterPageFile", pageParser.MasterPageFile));
112 if (pageParser.Theme != null)
113 method.Statements.Add (CreatePropertyAssign (ctrlVar, "Theme", pageParser.Theme));
115 if (pageParser.StyleSheetTheme != null)
116 method.Statements.Add (CreatePropertyAssign (ctrlVar, "StyleSheetTheme", pageParser.StyleSheetTheme));
120 protected override void PrependStatementsToFrameworkInitialize (CodeMemberMethod method)
122 base.PrependStatementsToFrameworkInitialize (method);
124 if (pageParser.StyleSheetTheme != null)
125 method.Statements.Add (CreatePropertyAssign ("StyleSheetTheme", pageParser.StyleSheetTheme));
129 protected override void AppendStatementsToFrameworkInitialize (CodeMemberMethod method)
131 string responseEncoding = pageParser.ResponseEncoding;
132 if (responseEncoding != null)
133 method.Statements.Add (CreatePropertyAssign ("ResponseEncoding", responseEncoding));
135 int codepage = pageParser.CodePage;
137 method.Statements.Add (CreatePropertyAssign ("CodePage", codepage));
139 string contentType = pageParser.ContentType;
140 if (contentType != null)
141 method.Statements.Add (CreatePropertyAssign ("ContentType", contentType));
143 if (pageParser.OutputCache) {
144 CodeMethodReferenceExpression init = new CodeMethodReferenceExpression (null,
146 CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression (init,
147 OutputCacheParams ());
148 method.Statements.Add (invoke);
151 int lcid = pageParser.LCID;
153 method.Statements.Add (CreatePropertyAssign ("LCID", lcid));
155 string culture = pageParser.Culture;
157 method.Statements.Add (CreatePropertyAssign ("Culture", culture));
159 culture = pageParser.UICulture;
161 method.Statements.Add (CreatePropertyAssign ("UICulture", culture));
163 string errorPage = pageParser.ErrorPage;
164 if (errorPage != null)
165 method.Statements.Add (CreatePropertyAssign ("ErrorPage", errorPage));
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);
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);
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);
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);
199 base.AppendStatementsToFrameworkInitialize (method);
202 private CodeExpression[] OutputCacheParams ()
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)
214 protected internal override void CreateMethods ()
216 base.CreateMethods ();
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);
231 CreateGetTypeHashCode ();
234 public static Type CompilePageType (PageParser pageParser)
236 PageCompiler compiler = new PageCompiler (pageParser);
237 return compiler.GetCompiledType ();