* TagAttribute.cs: attributes can be stored as encoded html so we
[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 using System;
10 using System.CodeDom;
11 using System.IO;
12 using System.Reflection;
13 using System.Text;
14 using System.Web.UI;
15 using System.Web.SessionState;
16 using System.Web.Util;
17
18 namespace System.Web.Compilation
19 {
20         class PageCompiler : TemplateControlCompiler
21         {
22                 PageParser pageParser;
23                 static CodeTypeReference intRef = new CodeTypeReference (typeof (int));
24
25                 public PageCompiler (PageParser pageParser)
26                         : base (pageParser)
27                 {
28                         this.pageParser = pageParser;
29                 }
30
31                 protected override void AddInterfaces () 
32                 {
33                         base.AddInterfaces ();
34                         if (pageParser.EnableSessionState)
35                                 mainClass.BaseTypes.Add (new CodeTypeReference (typeof(IRequiresSessionState)));
36
37                         if (pageParser.ReadOnlySessionState)
38                                 mainClass.BaseTypes.Add (new CodeTypeReference (typeof (IReadOnlySessionState)));
39                 }
40
41                 void CreateGetTypeHashCode () 
42                 {
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);
50                 }
51
52                 static CodeAssignStatement CreatePropertyAssign (string name, object value)
53                 {
54                         CodePropertyReferenceExpression prop;
55                         prop = new CodePropertyReferenceExpression (thisRef, name);
56                         CodePrimitiveExpression prim;
57                         prim = new CodePrimitiveExpression (value);
58                         return new CodeAssignStatement (prop, prim);
59                 }
60
61                 protected override void AddStatementsToFrameworkInitialize (CodeMemberMethod method)
62                 {
63                         string responseEncoding = pageParser.ResponseEncoding;
64                         if (responseEncoding != null)
65                                 method.Statements.Add (CreatePropertyAssign ("ResponseEncoding", responseEncoding));
66                         
67                         int codepage = pageParser.CodePage;
68                         if (codepage != -1)
69                                 method.Statements.Add (CreatePropertyAssign ("CodePage", codepage));
70
71                         string contentType = pageParser.ContentType;
72                         if (contentType != null)
73                                 method.Statements.Add (CreatePropertyAssign ("ContentType", contentType));
74
75                         if (pageParser.OutputCache) {
76                                 CodeMethodReferenceExpression init = new CodeMethodReferenceExpression (null,
77                                                 "InitOutputCache");
78                                 CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression (init,
79                                                 OutputCacheParams ());
80                                 method.Statements.Add (invoke);
81                         }
82                         
83                         int lcid = pageParser.LCID;
84                         if (lcid != -1)
85                                 method.Statements.Add (CreatePropertyAssign ("LCID", lcid));
86
87                         string culture = pageParser.Culture;
88                         if (culture != null)
89                                 method.Statements.Add (CreatePropertyAssign ("Culture", culture));
90
91                         culture = pageParser.UICulture;
92                         if (culture != null)
93                                 method.Statements.Add (CreatePropertyAssign ("UICulture", culture));
94
95                         string errorPage = pageParser.ErrorPage;
96                         if (errorPage != null)
97                                 method.Statements.Add (CreatePropertyAssign ("ErrorPage", errorPage));
98
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);
104                         }
105
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);
112                         }
113
114 #if NET_1_1
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);
121                         }
122 #endif
123                         
124                         base.AddStatementsToFrameworkInitialize (method);
125                 }
126
127                 private CodeExpression[] OutputCacheParams ()
128                 {
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)
136                                 };
137                 }
138                 
139                 protected override void CreateMethods ()
140                 {
141                         base.CreateMethods ();
142
143                         CreateGetTypeHashCode ();
144                 }
145
146                 public static Type CompilePageType (PageParser pageParser)
147                 {
148                         PageCompiler compiler = new PageCompiler (pageParser);
149                         return compiler.GetCompiledType ();
150                 }
151         }
152 }
153