PageThemeCompiler.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //
  2. // System.Web.Compilation.PageThemeCompiler
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. //
  7. // (C) 2006-2009 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. using System;
  30. using System.CodeDom;
  31. using System.Collections;
  32. using System.Collections.Specialized;
  33. using System.Reflection;
  34. using System.Text;
  35. using System.Web.UI;
  36. using System.Web.SessionState;
  37. using System.Web.Util;
  38. namespace System.Web.Compilation
  39. {
  40. class PageThemeCompiler : TemplateControlCompiler
  41. {
  42. PageThemeParser parser;
  43. public PageThemeCompiler (PageThemeParser parser)
  44. : base (parser)
  45. {
  46. this.parser = parser;
  47. }
  48. protected internal override void CreateMethods ()
  49. {
  50. CodeMemberField fld;
  51. CodeMemberProperty prop;
  52. /* override the following abstract PageTheme properties:
  53. protected abstract string AppRelativeTemplateSourceDirectory { get; }
  54. protected abstract IDictionary ControlSkins { get; }
  55. protected abstract string[] LinkedStyleSheets { get; }
  56. */
  57. /* ControlSkins */
  58. fld = new CodeMemberField (typeof (HybridDictionary), "__controlSkins");
  59. fld.Attributes = MemberAttributes.Private;
  60. fld.InitExpression = new CodeObjectCreateExpression (typeof (HybridDictionary));
  61. mainClass.Members.Add (fld);
  62. prop = new CodeMemberProperty ();
  63. prop.Name = "ControlSkins";
  64. prop.Attributes = MemberAttributes.Family | MemberAttributes.Override;
  65. prop.Type = new CodeTypeReference (typeof (IDictionary));
  66. prop.GetStatements.Add (new CodeMethodReturnStatement (new CodeVariableReferenceExpression ("__controlSkins")));
  67. mainClass.Members.Add (prop);
  68. /* LinkedStyleSheets */
  69. fld = new CodeMemberField (typeof (string[]), "__linkedStyleSheets");
  70. fld.Attributes = MemberAttributes.Private;
  71. fld.InitExpression = CreateLinkedStyleSheets ();
  72. mainClass.Members.Add (fld);
  73. prop = new CodeMemberProperty ();
  74. prop.Name = "LinkedStyleSheets";
  75. prop.Attributes = MemberAttributes.Family | MemberAttributes.Override;
  76. prop.Type = new CodeTypeReference (typeof (string[]));
  77. prop.GetStatements.Add (new CodeMethodReturnStatement (new CodeVariableReferenceExpression ("__linkedStyleSheets")));
  78. mainClass.Members.Add (prop);
  79. /* AppRelativeTemplateSourceDirectory */
  80. prop = new CodeMemberProperty ();
  81. prop.Name = "AppRelativeTemplateSourceDirectory";
  82. prop.Attributes = MemberAttributes.Family | MemberAttributes.Override;
  83. prop.Type = new CodeTypeReference (typeof (string));
  84. prop.GetStatements.Add (new CodeMethodReturnStatement (
  85. new CodePrimitiveExpression (
  86. VirtualPathUtility.ToAbsolute (parser.BaseVirtualDir))));
  87. mainClass.Members.Add (prop);
  88. ControlBuilder builder = parser.RootBuilder;
  89. if (builder.Children != null) {
  90. foreach (object o in builder.Children) {
  91. if (! (o is ControlBuilder))
  92. continue;
  93. if (o is CodeRenderBuilder)
  94. continue;
  95. ControlBuilder b = (ControlBuilder) o;
  96. CreateControlSkinMethod (b);
  97. }
  98. }
  99. }
  100. CodeExpression CreateLinkedStyleSheets ()
  101. {
  102. string [] lss = parser.LinkedStyleSheets;
  103. if (lss == null)
  104. return new CodePrimitiveExpression (null);
  105. CodeExpression [] initializers = new CodeExpression [lss.Length];
  106. for (int i = 0; i < lss.Length; i++)
  107. initializers[i] = new CodePrimitiveExpression (lss[i]);
  108. return new CodeArrayCreateExpression (typeof (string), initializers);
  109. }
  110. protected override string HandleUrlProperty (string str, MemberInfo member)
  111. {
  112. if (str.StartsWith ("~", StringComparison.Ordinal))
  113. return str;
  114. return "~/App_Themes/" + UrlUtils.Combine (
  115. System.IO.Path.GetFileName (parser.InputFile), str);
  116. }
  117. void CreateControlSkinMethod (ControlBuilder builder)
  118. {
  119. if (builder.ControlType == null)
  120. return;
  121. EnsureID (builder);
  122. CodeMemberMethod method = new CodeMemberMethod ();
  123. method.Name = "__BuildControl_" + builder.ID;
  124. method.Parameters.Add (new CodeParameterDeclarationExpression (typeof (Control), "ctrl"));
  125. mainClass.Members.Add (method);
  126. builder.Method = method;
  127. builder.MethodStatements = method.Statements;
  128. method.ReturnType = new CodeTypeReference (typeof (Control));
  129. // _ctrl = ($controlType)(ctrl);
  130. //
  131. CodeCastExpression castExpr = new CodeCastExpression (builder.ControlType, new CodeVariableReferenceExpression ("ctrl"));
  132. method.Statements.Add (new CodeVariableDeclarationStatement (builder.ControlType, "__ctrl"));
  133. CodeAssignStatement assign = new CodeAssignStatement ();
  134. assign.Left = ctrlVar;
  135. assign.Right = castExpr;
  136. method.Statements.Add (assign);
  137. CreateAssignStatementsFromAttributes (builder);
  138. if (builder.Children != null) {
  139. foreach (object o in builder.Children) {
  140. if (! (o is ControlBuilder))
  141. continue;
  142. ControlBuilder b = (ControlBuilder) o;
  143. if (b.ControlType == null)
  144. continue;
  145. if (b is CollectionBuilder) {
  146. PropertyInfo itemsProp = null;
  147. try {
  148. itemsProp = b.GetType().GetProperty ("Items");
  149. } catch (Exception) {}
  150. if (itemsProp != null) {
  151. /* emit a prop.Clear call before populating the collection */;
  152. CodePropertyReferenceExpression prop = new CodePropertyReferenceExpression (ctrlVar,
  153. b.TagName);
  154. CodePropertyReferenceExpression items = new CodePropertyReferenceExpression (prop,
  155. "Items");
  156. method.Statements.Add (new CodeMethodInvokeExpression (items, "Clear"));
  157. }
  158. }
  159. CreateControlTree (b, false, builder.ChildrenAsProperties);
  160. AddChildCall (builder, b);
  161. }
  162. }
  163. builder.Method.Statements.Add (new CodeMethodReturnStatement (ctrlVar));
  164. }
  165. protected override void AddClassAttributes ()
  166. {
  167. base.AddClassAttributes ();
  168. }
  169. protected override void CreateStaticFields ()
  170. {
  171. base.CreateStaticFields ();
  172. ControlBuilder builder = parser.RootBuilder;
  173. if (builder.Children != null) {
  174. foreach (object o in builder.Children) {
  175. if (o is string) /* literal stuff gets ignored */
  176. continue;
  177. if (o is CodeRenderBuilder)
  178. continue;
  179. ControlBuilder b = (ControlBuilder) o;
  180. EnsureID (b);
  181. Type controlType = b.ControlType;
  182. if (controlType == null)
  183. continue;
  184. string id = b.ID;
  185. string skinId = b.Attributes != null ? b.Attributes["skinid"] as string : null;
  186. if (skinId == null)
  187. skinId = "";
  188. // private static object __BuildControl__$id_skinKey = System.Web.UI.PageTheme.CreateSkinKey(typeof($controlType), "$skinID")
  189. //
  190. CodeMemberField fld = new CodeMemberField (typeof (object), "__BuildControl_" + id + "_skinKey");
  191. fld.Attributes = MemberAttributes.Private | MemberAttributes.Static;
  192. fld.InitExpression = new CodeMethodInvokeExpression (
  193. new CodeTypeReferenceExpression (typeof (PageTheme)),
  194. "CreateSkinKey",
  195. new CodeTypeOfExpression (controlType),
  196. new CodePrimitiveExpression (skinId));
  197. mainClass.Members.Add (fld);
  198. }
  199. }
  200. }
  201. protected override void CreateConstructor (CodeStatementCollection localVars,
  202. CodeStatementCollection trueStmt)
  203. {
  204. ControlBuilder builder = parser.RootBuilder;
  205. if (builder.Children != null) {
  206. foreach (object o in builder.Children) {
  207. if (o is string) /* literal stuff gets ignored */
  208. continue;
  209. if (o is CodeRenderBuilder)
  210. continue;
  211. ControlBuilder b = (ControlBuilder) o;
  212. Type controlType = b.ControlType;
  213. if (controlType == null)
  214. continue;
  215. string id = b.ID;
  216. if (localVars == null)
  217. localVars = new CodeStatementCollection ();
  218. // this.__controlSkins[__BuildControl_$id_skinKey] = new System.Web.UI.ControlSkin(typeof ($controlType), this.__BuildControl__$id)
  219. //
  220. localVars.Add (new CodeAssignStatement (new CodeIndexerExpression (new CodePropertyReferenceExpression (thisRef, "__controlSkins"),
  221. new CodeVariableReferenceExpression ("__BuildControl_" + id + "_skinKey")),
  222. new CodeObjectCreateExpression (typeof (ControlSkin),
  223. new CodeTypeOfExpression (controlType),
  224. new CodeDelegateCreateExpression (new CodeTypeReference (typeof (ControlSkinDelegate)),
  225. thisRef, "__BuildControl_" + id))));
  226. }
  227. base.CreateConstructor (localVars, trueStmt);
  228. }
  229. }
  230. }
  231. }