PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Web/System.Web.Compilation/PageThemeCompiler.cs

https://github.com/cschlote/mono
C# | 283 lines | 191 code | 50 blank | 42 comment | 35 complexity | 1c1aea1cb8e7971807b54287beb4d153 MD5 | raw file
  1. //
  2. // System.Web.Compilation.PageThemeCompiler
  3. //
  4. // Authors:
  5. // Chris Toshok (toshok@ximian.com)
  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 (
  86. new CodePrimitiveExpression (
  87. VirtualPathUtility.ToAbsolute (parser.BaseVirtualDir))));
  88. mainClass.Members.Add (prop);
  89. ControlBuilder builder = parser.RootBuilder;
  90. if (builder.Children != null) {
  91. foreach (object o in builder.Children) {
  92. if (! (o is ControlBuilder))
  93. continue;
  94. if (o is CodeRenderBuilder)
  95. continue;
  96. ControlBuilder b = (ControlBuilder) o;
  97. CreateControlSkinMethod (b);
  98. }
  99. }
  100. }
  101. CodeExpression CreateLinkedStyleSheets ()
  102. {
  103. string [] lss = parser.LinkedStyleSheets;
  104. if (lss == null)
  105. return new CodePrimitiveExpression (null);
  106. CodeExpression [] initializers = new CodeExpression [lss.Length];
  107. for (int i = 0; i < lss.Length; i++)
  108. initializers[i] = new CodePrimitiveExpression (lss[i]);
  109. return new CodeArrayCreateExpression (typeof (string), initializers);
  110. }
  111. protected override string HandleUrlProperty (string str, MemberInfo member)
  112. {
  113. if (str.StartsWith ("~", StringComparison.Ordinal))
  114. return str;
  115. return "~/App_Themes/" + UrlUtils.Combine (
  116. System.IO.Path.GetFileName (parser.InputFile), str);
  117. }
  118. void CreateControlSkinMethod (ControlBuilder builder)
  119. {
  120. if (builder.ControlType == null)
  121. return;
  122. EnsureID (builder);
  123. CodeMemberMethod method = new CodeMemberMethod ();
  124. method.Name = "__BuildControl_" + builder.ID;
  125. method.Parameters.Add (new CodeParameterDeclarationExpression (typeof (Control), "ctrl"));
  126. mainClass.Members.Add (method);
  127. builder.Method = method;
  128. builder.MethodStatements = method.Statements;
  129. method.ReturnType = new CodeTypeReference (typeof (Control));
  130. // _ctrl = ($controlType)(ctrl);
  131. //
  132. CodeCastExpression castExpr = new CodeCastExpression (builder.ControlType, new CodeVariableReferenceExpression ("ctrl"));
  133. method.Statements.Add (new CodeVariableDeclarationStatement (builder.ControlType, "__ctrl"));
  134. CodeAssignStatement assign = new CodeAssignStatement ();
  135. assign.Left = ctrlVar;
  136. assign.Right = castExpr;
  137. method.Statements.Add (assign);
  138. CreateAssignStatementsFromAttributes (builder);
  139. if (builder.Children != null) {
  140. foreach (object o in builder.Children) {
  141. if (! (o is ControlBuilder))
  142. continue;
  143. ControlBuilder b = (ControlBuilder) o;
  144. if (b.ControlType == null)
  145. continue;
  146. if (b is CollectionBuilder) {
  147. PropertyInfo itemsProp = null;
  148. try {
  149. itemsProp = b.GetType().GetProperty ("Items");
  150. } catch (Exception) {}
  151. if (itemsProp != null) {
  152. /* emit a prop.Clear call before populating the collection */;
  153. CodePropertyReferenceExpression prop = new CodePropertyReferenceExpression (ctrlVar,
  154. b.TagName);
  155. CodePropertyReferenceExpression items = new CodePropertyReferenceExpression (prop,
  156. "Items");
  157. method.Statements.Add (new CodeMethodInvokeExpression (items, "Clear"));
  158. }
  159. }
  160. CreateControlTree (b, false, builder.ChildrenAsProperties);
  161. AddChildCall (builder, b);
  162. }
  163. }
  164. builder.Method.Statements.Add (new CodeMethodReturnStatement (ctrlVar));
  165. }
  166. protected override void AddClassAttributes ()
  167. {
  168. base.AddClassAttributes ();
  169. }
  170. protected override void CreateStaticFields ()
  171. {
  172. base.CreateStaticFields ();
  173. ControlBuilder builder = parser.RootBuilder;
  174. if (builder.Children != null) {
  175. foreach (object o in builder.Children) {
  176. if (o is string) /* literal stuff gets ignored */
  177. continue;
  178. if (o is CodeRenderBuilder)
  179. continue;
  180. ControlBuilder b = (ControlBuilder) o;
  181. EnsureID (b);
  182. Type controlType = b.ControlType;
  183. if (controlType == null)
  184. continue;
  185. string id = b.ID;
  186. string skinId = b.Attributes != null ? b.Attributes["skinid"] as string : null;
  187. if (skinId == null)
  188. skinId = "";
  189. // private static object __BuildControl__$id_skinKey = System.Web.UI.PageTheme.CreateSkinKey(typeof($controlType), "$skinID")
  190. //
  191. CodeMemberField fld = new CodeMemberField (typeof (object), "__BuildControl_" + id + "_skinKey");
  192. fld.Attributes = MemberAttributes.Private | MemberAttributes.Static;
  193. fld.InitExpression = new CodeMethodInvokeExpression (
  194. new CodeTypeReferenceExpression (typeof (PageTheme)),
  195. "CreateSkinKey",
  196. new CodeTypeOfExpression (controlType),
  197. new CodePrimitiveExpression (skinId));
  198. mainClass.Members.Add (fld);
  199. }
  200. }
  201. }
  202. protected override void CreateConstructor (CodeStatementCollection localVars,
  203. CodeStatementCollection trueStmt)
  204. {
  205. ControlBuilder builder = parser.RootBuilder;
  206. if (builder.Children != null) {
  207. foreach (object o in builder.Children) {
  208. if (o is string) /* literal stuff gets ignored */
  209. continue;
  210. if (o is CodeRenderBuilder)
  211. continue;
  212. ControlBuilder b = (ControlBuilder) o;
  213. Type controlType = b.ControlType;
  214. if (controlType == null)
  215. continue;
  216. string id = b.ID;
  217. if (localVars == null)
  218. localVars = new CodeStatementCollection ();
  219. // this.__controlSkins[__BuildControl_$id_skinKey] = new System.Web.UI.ControlSkin(typeof ($controlType), this.__BuildControl__$id)
  220. //
  221. localVars.Add (new CodeAssignStatement (new CodeIndexerExpression (new CodePropertyReferenceExpression (thisRef, "__controlSkins"),
  222. new CodeVariableReferenceExpression ("__BuildControl_" + id + "_skinKey")),
  223. new CodeObjectCreateExpression (typeof (ControlSkin),
  224. new CodeTypeOfExpression (controlType),
  225. new CodeDelegateCreateExpression (new CodeTypeReference (typeof (ControlSkinDelegate)),
  226. thisRef, "__BuildControl_" + id))));
  227. }
  228. base.CreateConstructor (localVars, trueStmt);
  229. }
  230. }
  231. }
  232. }
  233. #endif