2 // System.Web.Compilation.PageCompiler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
12 using System.Reflection;
15 using System.Web.SessionState;
16 using System.Web.Util;
18 namespace System.Web.Compilation
20 class PageCompiler : TemplateControlCompiler
22 PageParser pageParser;
23 static CodeTypeReference intRef = new CodeTypeReference (typeof (int));
25 public PageCompiler (PageParser pageParser)
28 this.pageParser = pageParser;
31 protected override void AddInterfaces ()
33 base.AddInterfaces ();
34 if (pageParser.EnableSessionState)
35 mainClass.BaseTypes.Add (new CodeTypeReference (typeof(IRequiresSessionState)));
37 if (pageParser.ReadOnlySessionState)
38 mainClass.BaseTypes.Add (new CodeTypeReference (typeof (IReadOnlySessionState)));
41 void CreateGetTypeHashCode ()
43 CodeMemberMethod method = new CodeMemberMethod ();
44 method.ReturnType = intRef;
45 method.Name = "GetTypeHashCode";
46 method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
47 Random rnd = new Random (pageParser.InputFile.GetHashCode ());
48 method.Statements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (rnd.Next ())));
49 mainClass.Members.Add (method);
52 static CodeAssignStatement CreatePropertyAssign (string name, object value)
54 CodePropertyReferenceExpression prop;
55 prop = new CodePropertyReferenceExpression (thisRef, name);
56 CodePrimitiveExpression prim;
57 prim = new CodePrimitiveExpression (value);
58 return new CodeAssignStatement (prop, prim);
61 protected override void AddStatementsToFrameworkInitialize (CodeMemberMethod method)
63 string responseEncoding = pageParser.ResponseEncoding;
64 if (responseEncoding != null)
65 method.Statements.Add (CreatePropertyAssign ("ResponseEncoding", responseEncoding));
67 int codepage = pageParser.CodePage;
69 method.Statements.Add (CreatePropertyAssign ("CodePage", codepage));
71 string contentType = pageParser.ContentType;
72 if (contentType != null)
73 method.Statements.Add (CreatePropertyAssign ("ContentType", contentType));
75 if (pageParser.OutputCache) {
76 CodeMethodReferenceExpression init = new CodeMethodReferenceExpression (null,
78 CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression (init,
79 OutputCacheParams ());
80 method.Statements.Add (invoke);
83 int lcid = pageParser.LCID;
85 method.Statements.Add (CreatePropertyAssign ("LCID", lcid));
87 string culture = pageParser.Culture;
89 method.Statements.Add (CreatePropertyAssign ("Culture", culture));
91 culture = pageParser.UICulture;
93 method.Statements.Add (CreatePropertyAssign ("UICulture", culture));
95 string errorPage = pageParser.ErrorPage;
96 if (errorPage != null)
97 method.Statements.Add (CreatePropertyAssign ("ErrorPage", errorPage));
99 if (pageParser.Trace) {
100 CodeAssignStatement stmt = new CodeAssignStatement ();
101 stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceEnabled");
102 stmt.Right = new CodePrimitiveExpression (true);
103 method.Statements.Add (stmt);
106 if (pageParser.TraceMode != TraceMode.Default) {
107 CodeAssignStatement stmt = new CodeAssignStatement ();
108 CodeTypeReferenceExpression tm = new CodeTypeReferenceExpression ("System.Web.TraceMode");
109 stmt.Left = new CodePropertyReferenceExpression (thisRef, "TraceModeValue");
110 stmt.Right = new CodeFieldReferenceExpression (tm, pageParser.TraceMode.ToString ());
111 method.Statements.Add (stmt);
115 if (pageParser.ValidateRequest) {
116 CodeMethodInvokeExpression expr = new CodeMethodInvokeExpression ();
117 CodePropertyReferenceExpression prop;
118 prop = new CodePropertyReferenceExpression (thisRef, "Request");
119 expr.Method = new CodeMethodReferenceExpression (prop, "ValidateInput");
120 method.Statements.Add (expr);
124 base.AddStatementsToFrameworkInitialize (method);
127 private CodeExpression[] OutputCacheParams ()
129 return new CodeExpression [] {
130 new CodePrimitiveExpression (pageParser.OutputCacheDuration),
131 new CodePrimitiveExpression (pageParser.OutputCacheVaryByHeader),
132 new CodePrimitiveExpression (pageParser.OutputCacheVaryByCustom),
133 new CodeSnippetExpression (typeof (OutputCacheLocation).ToString () +
134 "." + pageParser.OutputCacheLocation.ToString ()),
135 new CodePrimitiveExpression (pageParser.OutputCacheVaryByParam)
139 protected override void CreateMethods ()
141 base.CreateMethods ();
143 CreateGetTypeHashCode ();
146 public static Type CompilePageType (PageParser pageParser)
148 PageCompiler compiler = new PageCompiler (pageParser);
149 return compiler.GetCompiledType ();