PageThemeCompiler.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //
  2. // System.Web.Compilation.PageThemeCompiler
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. //
  7. // (C) 2006 Novell, Inc (http://www.novell.com/)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.CodeDom;
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Reflection;
  35. using System.Text;
  36. using System.Web.UI;
  37. using System.Web.SessionState;
  38. using System.Web.Util;
  39. namespace System.Web.Compilation
  40. {
  41. class PageThemeCompiler : TemplateControlCompiler
  42. {
  43. PageThemeParser parser;
  44. public PageThemeCompiler (PageThemeParser parser)
  45. : base (parser)
  46. {
  47. this.parser = parser;
  48. }
  49. protected internal override void CreateMethods ()
  50. {
  51. CodeMemberField fld;
  52. CodeMemberProperty prop;
  53. /* override the following abstract PageTheme properties:
  54. protected abstract string AppRelativeTemplateSourceDirectory { get; }
  55. protected abstract IDictionary ControlSkins { get; }
  56. protected abstract string[] LinkedStyleSheets { get; }
  57. */
  58. /* ControlSkins */
  59. fld = new CodeMemberField (typeof (HybridDictionary), "__controlSkins");
  60. fld.Attributes = MemberAttributes.Private;
  61. fld.InitExpression = new CodeObjectCreateExpression (typeof (HybridDictionary));
  62. mainClass.Members.Add (fld);
  63. prop = new CodeMemberProperty ();
  64. prop.Name = "ControlSkins";
  65. prop.Attributes = MemberAttributes.Family | MemberAttributes.Override;
  66. prop.Type = new CodeTypeReference (typeof (IDictionary));
  67. prop.GetStatements.Add (new CodeMethodReturnStatement (new CodeVariableReferenceExpression ("__controlSkins")));
  68. mainClass.Members.Add (prop);
  69. /* LinkedStyleSheets */
  70. fld = new CodeMemberField (typeof (string[]), "__linkedStyleSheets");
  71. fld.Attributes = MemberAttributes.Private;
  72. fld.InitExpression = CreateLinkedStyleSheets ();
  73. mainClass.Members.Add (fld);
  74. prop = new CodeMemberProperty ();
  75. prop.Name = "LinkedStyleSheets";
  76. prop.Attributes = MemberAttributes.Family | MemberAttributes.Override;
  77. prop.Type = new CodeTypeReference (typeof (string[]));
  78. prop.GetStatements.Add (new CodeMethodReturnStatement (new CodeVariableReferenceExpression ("__linkedStyleSheets")));
  79. mainClass.Members.Add (prop);
  80. /* AppRelativeTemplateSourceDirectory */
  81. prop = new CodeMemberProperty ();
  82. prop.Name = "AppRelativeTemplateSourceDirectory";
  83. prop.Attributes = MemberAttributes.Family | MemberAttributes.Override;
  84. prop.Type = new CodeTypeReference (typeof (string));
  85. prop.GetStatements.Add (new CodeMethodReturnStatement (new CodePrimitiveExpression (parser.BaseVirtualDir)));
  86. mainClass.Members.Add (prop);
  87. ControlBuilder builder = parser.RootBuilder;
  88. if (builder.Children != null) {
  89. foreach (object o in builder.Children) {
  90. if (! (o is ControlBuilder))
  91. continue;
  92. ControlBuilder b = (ControlBuilder) o;
  93. CreateControlSkinMethod (b);
  94. }
  95. }
  96. }
  97. private CodeExpression CreateLinkedStyleSheets ()
  98. {
  99. string [] lss = parser.LinkedStyleSheets;
  100. if (lss == null)
  101. return new CodePrimitiveExpression (null);
  102. CodeExpression [] initializers = new CodeExpression [lss.Length];
  103. for (int i = 0; i < lss.Length; i++)
  104. initializers[i] = new CodePrimitiveExpression (lss[i]);
  105. return new CodeArrayCreateExpression (typeof (string), initializers);
  106. }
  107. protected override string HandleUrlProperty (string str, MemberInfo member)
  108. {
  109. if (str.StartsWith ("~", StringComparison.Ordinal))
  110. return str;
  111. return "~/App_Themes/" + UrlUtils.Combine (
  112. System.IO.Path.GetFileName (parser.InputFile), str);
  113. }
  114. void CreateControlSkinMethod (ControlBuilder builder)
  115. {
  116. EnsureID (builder);
  117. CodeMemberMethod method = new CodeMemberMethod ();
  118. method.Name = "__BuildControl_" + builder.ID;
  119. method.Parameters.Add (new CodeParameterDeclarationExpression (typeof (Control), "ctrl"));
  120. mainClass.Members.Add (method);
  121. builder.method = method;
  122. builder.methodStatements = method.Statements;
  123. method.ReturnType = new CodeTypeReference (typeof (Control));
  124. // _ctrl = ($controlType)(ctrl);
  125. //
  126. CodeCastExpression castExpr = new CodeCastExpression (builder.ControlType, new CodeVariableReferenceExpression ("ctrl"));
  127. method.Statements.Add (new CodeVariableDeclarationStatement (builder.ControlType, "__ctrl"));
  128. CodeAssignStatement assign = new CodeAssignStatement ();
  129. assign.Left = ctrlVar;
  130. assign.Right = castExpr;
  131. method.Statements.Add (assign);
  132. CreateAssignStatementsFromAttributes (builder);
  133. if (builder.Children != null) {
  134. foreach (object o in builder.Children) {
  135. if (! (o is ControlBuilder))
  136. continue;
  137. ControlBuilder b = (ControlBuilder) o;
  138. if (b is CollectionBuilder) {
  139. PropertyInfo itemsProp = null;
  140. try {
  141. itemsProp = b.GetType().GetProperty ("Items");
  142. } catch (Exception) {}
  143. if (itemsProp != null) {
  144. /* emit a prop.Clear call before populating the collection */;
  145. CodePropertyReferenceExpression prop = new CodePropertyReferenceExpression (ctrlVar,
  146. b.TagName);
  147. CodePropertyReferenceExpression items = new CodePropertyReferenceExpression (prop,
  148. "Items");
  149. method.Statements.Add (new CodeMethodInvokeExpression (items, "Clear"));
  150. }
  151. }
  152. CreateControlTree (b, false, builder.ChildrenAsProperties);
  153. AddChildCall (builder, b);
  154. }
  155. }
  156. builder.method.Statements.Add (new CodeMethodReturnStatement (ctrlVar));
  157. }
  158. protected override void AddClassAttributes ()
  159. {
  160. base.AddClassAttributes ();
  161. }
  162. protected override void CreateStaticFields ()
  163. {
  164. base.CreateStaticFields ();
  165. ControlBuilder builder = parser.RootBuilder;
  166. if (builder.Children != null) {
  167. foreach (object o in builder.Children) {
  168. if (o is string) /* literal stuff gets ignored */
  169. continue;
  170. ControlBuilder b = (ControlBuilder) o;
  171. EnsureID (b);
  172. string id = b.ID;
  173. string skinId = b.attribs["skinid"] as string;
  174. Type controlType = b.ControlType;
  175. if (skinId == null) skinId = "";
  176. // private static object __BuildControl__$id_skinKey = System.Web.UI.PageTheme.CreateSkinKey(typeof($controlType), "$skinID")
  177. //
  178. CodeMemberField fld = new CodeMemberField (typeof (object), "__BuildControl_" + id + "_skinKey");
  179. fld.Attributes = MemberAttributes.Private | MemberAttributes.Static;
  180. fld.InitExpression = new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof (PageTheme)),
  181. "CreateSkinKey",
  182. new CodeTypeOfExpression (controlType),
  183. new CodePrimitiveExpression (skinId));
  184. mainClass.Members.Add (fld);
  185. }
  186. }
  187. }
  188. protected override void CreateConstructor (CodeStatementCollection localVars,
  189. CodeStatementCollection trueStmt)
  190. {
  191. ControlBuilder builder = parser.RootBuilder;
  192. if (builder.Children != null) {
  193. foreach (object o in builder.Children) {
  194. if (o is string) /* literal stuff gets ignored */
  195. continue;
  196. ControlBuilder b = (ControlBuilder) o;
  197. string id = b.ID;
  198. Type controlType = b.ControlType;
  199. if (localVars == null)
  200. localVars = new CodeStatementCollection ();
  201. // this.__controlSkins[__BuildControl_$id_skinKey] = new System.Web.UI.ControlSkin(typeof ($controlType), this.__BuildControl__$id)
  202. //
  203. localVars.Add (new CodeAssignStatement (new CodeIndexerExpression (new CodePropertyReferenceExpression (thisRef, "__controlSkins"),
  204. new CodeVariableReferenceExpression ("__BuildControl_" + id + "_skinKey")),
  205. new CodeObjectCreateExpression (typeof (ControlSkin),
  206. new CodeTypeOfExpression (controlType),
  207. new CodeDelegateCreateExpression (new CodeTypeReference (typeof (ControlSkinDelegate)),
  208. thisRef, "__BuildControl_" + id))));
  209. }
  210. base.CreateConstructor (localVars, trueStmt);
  211. }
  212. }
  213. }
  214. }
  215. #endif