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 (string name
, object value)
90 CodePropertyReferenceExpression prop
;
91 prop
= new CodePropertyReferenceExpression (thisRef
, name
);
92 CodePrimitiveExpression prim
;
93 prim
= new CodePrimitiveExpression (value);
94 return new CodeAssignStatement (prop
, prim
);
97 protected override void AddStatementsToFrameworkInitialize (CodeMemberMethod method
)
99 string responseEncoding
= pageParser
.ResponseEncoding
;
100 if (responseEncoding
!= null)
101 method
.Statements
.Add (CreatePropertyAssign ("ResponseEncoding", responseEncoding
));
103 int codepage
= pageParser
.CodePage
;
105 method
.Statements
.Add (CreatePropertyAssign ("CodePage", codepage
));
107 string contentType
= pageParser
.ContentType
;
108 if (contentType
!= null)
109 method
.Statements
.Add (CreatePropertyAssign ("ContentType", contentType
));
111 if (pageParser
.OutputCache
) {
112 CodeMethodReferenceExpression init
= new CodeMethodReferenceExpression (null,
114 CodeMethodInvokeExpression invoke
= new CodeMethodInvokeExpression (init
,
115 OutputCacheParams ());
116 method
.Statements
.Add (invoke
);
119 int lcid
= pageParser
.LCID
;
121 method
.Statements
.Add (CreatePropertyAssign ("LCID", lcid
));
123 string culture
= pageParser
.Culture
;
125 method
.Statements
.Add (CreatePropertyAssign ("Culture", culture
));
127 culture
= pageParser
.UICulture
;
129 method
.Statements
.Add (CreatePropertyAssign ("UICulture", culture
));
131 string errorPage
= pageParser
.ErrorPage
;
132 if (errorPage
!= null)
133 method
.Statements
.Add (CreatePropertyAssign ("ErrorPage", errorPage
));
135 if (pageParser
.HaveTrace
) {
136 CodeAssignStatement stmt
= new CodeAssignStatement ();
137 stmt
.Left
= new CodePropertyReferenceExpression (thisRef
, "TraceEnabled");
138 stmt
.Right
= new CodePrimitiveExpression (pageParser
.Trace
);
139 method
.Statements
.Add (stmt
);
142 if (pageParser
.TraceMode
!= TraceMode
.Default
) {
143 CodeAssignStatement stmt
= new CodeAssignStatement ();
144 CodeTypeReferenceExpression tm
= new CodeTypeReferenceExpression ("System.Web.TraceMode");
145 stmt
.Left
= new CodePropertyReferenceExpression (thisRef
, "TraceModeValue");
146 stmt
.Right
= new CodeFieldReferenceExpression (tm
, pageParser
.TraceMode
.ToString ());
147 method
.Statements
.Add (stmt
);
150 if (pageParser
.NotBuffer
) {
151 CodeAssignStatement stmt
= new CodeAssignStatement ();
152 stmt
.Left
= new CodePropertyReferenceExpression (thisRef
, "Buffer");
153 stmt
.Right
= new CodePrimitiveExpression (false);
154 method
.Statements
.Add (stmt
);
158 if (pageParser
.ValidateRequest
) {
159 CodeMethodInvokeExpression expr
= new CodeMethodInvokeExpression ();
160 CodePropertyReferenceExpression prop
;
161 prop
= new CodePropertyReferenceExpression (thisRef
, "Request");
162 expr
.Method
= new CodeMethodReferenceExpression (prop
, "ValidateInput");
163 method
.Statements
.Add (expr
);
167 base.AddStatementsToFrameworkInitialize (method
);
170 private CodeExpression
[] OutputCacheParams ()
172 return new CodeExpression
[] {
173 new CodePrimitiveExpression (pageParser
.OutputCacheDuration
),
174 new CodePrimitiveExpression (pageParser
.OutputCacheVaryByHeader
),
175 new CodePrimitiveExpression (pageParser
.OutputCacheVaryByCustom
),
176 new CodeSnippetExpression (typeof (OutputCacheLocation
).ToString () +
177 "." + pageParser
.OutputCacheLocation
.ToString ()),
178 new CodePrimitiveExpression (pageParser
.OutputCacheVaryByParam
)
182 protected override void CreateMethods ()
184 base.CreateMethods ();
186 CreateGetTypeHashCode ();
189 public static Type
CompilePageType (PageParser pageParser
)
191 PageCompiler compiler
= new PageCompiler (pageParser
);
192 return compiler
.GetCompiledType ();