File: PageThemeCodeDomTreeGenerator.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (342 lines) | stat: -rw-r--r-- 16,303 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//------------------------------------------------------------------------------
// <copyright file="PageThemeCodeDomTreeGenerator.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;
using System.ComponentModel;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Globalization;
using System.IO;
using System.Web.UI;
using System.Web.Util;
using Debug=System.Web.Util.Debug;

namespace System.Web.Compilation {

    internal class PageThemeCodeDomTreeGenerator : BaseTemplateCodeDomTreeGenerator {

        private Hashtable _controlSkinTypeNameCollection = new Hashtable();
        private ArrayList _controlSkinBuilderEntryList = new ArrayList();

        private int _controlCount = 0;
        private CodeTypeReference _controlSkinDelegateType = new CodeTypeReference(typeof(ControlSkinDelegate));
        private CodeTypeReference _controlSkinType = new CodeTypeReference(typeof(ControlSkin));

        private PageThemeParser _themeParser;
        private const string _controlSkinsVarName = "__controlSkins";
        private const string _controlSkinsPropertyName = "ControlSkins";
        private const string _linkedStyleSheetsVarName = "__linkedStyleSheets";
        private const string _linkedStyleSheetsPropertyName = "LinkedStyleSheets";

        internal PageThemeCodeDomTreeGenerator(PageThemeParser parser) : base(parser) {
            _themeParser = parser;
        }

        private void AddMemberOverride(string name, Type type, CodeExpression expr) {
            CodeMemberProperty member = new CodeMemberProperty();
            member.Name = name;
            member.Attributes = MemberAttributes.Override | MemberAttributes.Family;
            member.Type = new CodeTypeReference(type.FullName);
            CodeMethodReturnStatement returnStmt = new CodeMethodReturnStatement(expr);
            member.GetStatements.Add(returnStmt);
            _sourceDataClass.Members.Add(member);
        }

        private void BuildControlSkins(CodeStatementCollection statements) {
            foreach (ControlSkinBuilderEntry entry in _controlSkinBuilderEntryList) {
                string skinID = entry.SkinID;
                ControlBuilder builder = entry.Builder;
                statements.Add(BuildControlSkinAssignmentStatement(builder, skinID));
            }
        }

        private CodeStatement BuildControlSkinAssignmentStatement(
            ControlBuilder builder, string skinID) {

            Type controlType = builder.ControlType;

            string keyVarName = GetMethodNameForBuilder(buildMethodPrefix, builder) + "_skinKey";

            // e.g.
            // private static object __BuildControl__control3_skinKey = PageTheme.CreateSkinKey(typeof({controlType}), {skinID});
            CodeMemberField field = new CodeMemberField(typeof(object), keyVarName);
            field.Attributes = MemberAttributes.Static | MemberAttributes.Private;
            CodeMethodInvokeExpression cmie = new CodeMethodInvokeExpression();
            cmie.Method = new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(PageTheme)), "CreateSkinKey");
            cmie.Parameters.Add(new CodeTypeOfExpression(controlType));
            cmie.Parameters.Add(new CodePrimitiveExpression(skinID));
            field.InitExpression = cmie;
            _sourceDataClass.Members.Add(field);

            // e.g. this.__namedControlSkins[keyVarName] =
            //          new System.Web.UI.ControlSkin(typeof(System.Web.UI.WebControls.Label),
            //          new System.Web.UI.ControlSkinDelegate(this.__BuildControl__control3));
            CodeFieldReferenceExpression varExpr = new CodeFieldReferenceExpression(
                new CodeThisReferenceExpression(),
                _controlSkinsVarName);

            CodeIndexerExpression indexerExpr = new CodeIndexerExpression(
                varExpr,
                new CodeExpression[] {
                    new CodeVariableReferenceExpression(keyVarName)
                }
            );

            CodeDelegateCreateExpression del = new CodeDelegateCreateExpression(
                                                            _controlSkinDelegateType,
                                                            new CodeThisReferenceExpression(),
                                                            GetMethodNameForBuilder(buildMethodPrefix, builder));
            CodeObjectCreateExpression valueExpr = new CodeObjectCreateExpression(_controlSkinType);
            valueExpr.Parameters.Add(new CodeTypeOfExpression(controlType));
            valueExpr.Parameters.Add(del);

            return new CodeAssignStatement(indexerExpr, valueExpr);
        }

        private void BuildControlSkinMember() {
            // e.g.
            // private System.Collections.Specialized.HybridDictionary __cssFileList =
            //     new System.Collections.Specialized.HybridDictionary(2);
            int initialSize = _controlSkinBuilderEntryList.Count;
            CodeMemberField field = new CodeMemberField(typeof(HybridDictionary).FullName, _controlSkinsVarName);
            CodeObjectCreateExpression expr = new CodeObjectCreateExpression(typeof(HybridDictionary));
            expr.Parameters.Add(new CodePrimitiveExpression(initialSize));
            field.InitExpression = expr;
            _sourceDataClass.Members.Add(field);
        }

        private void BuildControlSkinProperty() {
            // e.g.
            //  protected override System.Collections.IDictionary ControlSkins {
            //       get { return this.__controlSkins; }
            //  }
            CodeFieldReferenceExpression accessExpr = new CodeFieldReferenceExpression(
                                                            new CodeThisReferenceExpression(),
                                                            _controlSkinsVarName);

            AddMemberOverride(_controlSkinsPropertyName, typeof(IDictionary), accessExpr);
        }

        private void BuildLinkedStyleSheetMember() {

            // e.g.
            // private System.String[] __linkedStyleSheets = new String[] {
            //     "linkedStyleSheet1 vdirs",
            //     "linkedStyleSheet2 vdirs",
            // }
            CodeMemberField field = new CodeMemberField(typeof(String[]), _linkedStyleSheetsVarName);

            if (_themeParser.CssFileList != null && _themeParser.CssFileList.Count > 0) {
                CodeExpression[] cssFiles = new CodeExpression[_themeParser.CssFileList.Count];
                int i = 0;
                foreach(String cssFile in _themeParser.CssFileList) {
                    cssFiles[i++] = new CodePrimitiveExpression(cssFile);
                }
                
                CodeArrayCreateExpression initExpr = new CodeArrayCreateExpression(typeof(String), cssFiles);
                field.InitExpression = initExpr;
            }
            else {
                field.InitExpression = new CodePrimitiveExpression(null);
            }
            _sourceDataClass.Members.Add(field);
        }

        private void BuildLinkedStyleSheetProperty() {
            // e.g.
            //  protected override String[] LinkedStyleSheets {
            //       get { return this.__linkedStyleSheets; }
            //  }
            CodeFieldReferenceExpression accessExpr = new CodeFieldReferenceExpression(
                                                            new CodeThisReferenceExpression(),
                                                            _linkedStyleSheetsVarName);

            AddMemberOverride(_linkedStyleSheetsPropertyName, typeof(String[]), accessExpr);
        }

        protected override void BuildInitStatements(CodeStatementCollection trueStatements, CodeStatementCollection topLevelStatements) {
            base.BuildInitStatements(trueStatements, topLevelStatements);
            BuildControlSkins(topLevelStatements);
        }

        protected override void BuildMiscClassMembers() {
            base.BuildMiscClassMembers();

            AddMemberOverride(templateSourceDirectoryName, typeof(String),
                new CodePrimitiveExpression(_themeParser.VirtualDirPath.VirtualPathString));

            BuildSourceDataTreeFromBuilder(_themeParser.RootBuilder,
                false /*fInTemplate*/, false /*topLevelControlInTemplate*/, null /*pse*/);

            BuildControlSkinMember();
            BuildControlSkinProperty();
            BuildLinkedStyleSheetMember();
            BuildLinkedStyleSheetProperty();
        }

        protected override void BuildSourceDataTreeFromBuilder(ControlBuilder builder,
                                                bool fInTemplate, bool topLevelControlInTemplate,
                                                PropertyEntry pse) {

            // Don't do anything for code blocks
            if (builder is CodeBlockBuilder)
                return;

            // Is the current builder for a template?
            bool fTemplate = (builder is TemplateBuilder);

            // Is the current builder the root builder?
            bool fRootBuilder = (builder == _themeParser.RootBuilder);

            // Is this a control theme?
            bool fControlSkin = !fInTemplate && !fTemplate && topLevelControlInTemplate;

            // Ignore the ID attribute, always auto generate ID.
            _controlCount++;
            builder.ID = "__control" + _controlCount.ToString(NumberFormatInfo.InvariantInfo);
            builder.IsGeneratedID = true;

            // Check for the SkinID property.
            if (fControlSkin && !(builder is DataBoundLiteralControlBuilder)) {

                Type ctrlType = builder.ControlType;
                Debug.Assert(typeof(Control).IsAssignableFrom(ctrlType));
                Debug.Assert(ThemeableAttribute.IsTypeThemeable(ctrlType));

                string skinID = builder.SkinID;
                object skinKey = PageTheme.CreateSkinKey(builder.ControlType, skinID);

                if (_controlSkinTypeNameCollection.Contains(skinKey)) {
                    if (String.IsNullOrEmpty(skinID)) {
                        throw new HttpParseException(SR.GetString(SR.Page_theme_default_theme_already_defined,
                            builder.ControlType.FullName), null, builder.VirtualPath, null, builder.Line);
                    }
                    else {
                        throw new HttpParseException(SR.GetString(SR.Page_theme_skinID_already_defined, skinID),
                            null, builder.VirtualPath, null, builder.Line);
                    }
                }

                _controlSkinTypeNameCollection.Add(skinKey, true);
                _controlSkinBuilderEntryList.Add(new ControlSkinBuilderEntry(builder, skinID));
            }

            // Process the children
            // only root builders and template builders are processed.
            if (builder.SubBuilders != null) {
                foreach (object child in builder.SubBuilders) {
                    if (child is ControlBuilder) {
                        bool isTopLevelCtrlInTemplate = fTemplate && typeof(Control).IsAssignableFrom(((ControlBuilder)child).ControlType);
                        BuildSourceDataTreeFromBuilder((ControlBuilder)child, fInTemplate, isTopLevelCtrlInTemplate, null);
                    }
                }
            }

            foreach (TemplatePropertyEntry entry in builder.TemplatePropertyEntries) {
                BuildSourceDataTreeFromBuilder(((TemplatePropertyEntry)entry).Builder, true, false /*topLevelControlInTemplate*/, entry);
            }

            foreach (ComplexPropertyEntry entry in builder.ComplexPropertyEntries) {
                if (!(entry.Builder is StringPropertyBuilder)) {
                    BuildSourceDataTreeFromBuilder(((ComplexPropertyEntry)entry).Builder, fInTemplate, false /*topLevelControlInTemplate*/, entry);
                }
            }

            // Build a Build method for the control
            // fControlSkin indicates whether the method is a theme build method.
            if (!fRootBuilder) {
                BuildBuildMethod(builder, fTemplate, fInTemplate, topLevelControlInTemplate, pse,
                    fControlSkin);
            }

            // Build a Render method for the control, unless it has no code
            if (!fControlSkin && builder.HasAspCode) {
                BuildRenderMethod(builder, fTemplate);
            }

            // Build a method to extract values from the template
            BuildExtractMethod(builder);

            // Build a property binding method for the control
            BuildPropertyBindingMethod(builder, fControlSkin);
        }

        internal override CodeExpression BuildStringPropertyExpression(PropertyEntry pse) {
            // Make the UrlProperty based on virtualDirPath for control themes.
            if (pse.PropertyInfo != null) {
                UrlPropertyAttribute urlAttrib = Attribute.GetCustomAttribute(pse.PropertyInfo, typeof(UrlPropertyAttribute)) as UrlPropertyAttribute;

                if (urlAttrib != null) {
                    if (pse is SimplePropertyEntry) {
                        SimplePropertyEntry spse = (SimplePropertyEntry)pse;

                        string strValue = (string)spse.Value;
                        if (UrlPath.IsRelativeUrl(strValue) && !UrlPath.IsAppRelativePath(strValue)) {
                            spse.Value = UrlPath.MakeVirtualPathAppRelative(UrlPath.Combine(_themeParser.VirtualDirPath.VirtualPathString, strValue));
                        }
                    }
                    else {
                        Debug.Assert(pse is ComplexPropertyEntry);
                        ComplexPropertyEntry cpe = (ComplexPropertyEntry)pse;
                        StringPropertyBuilder builder = (StringPropertyBuilder)cpe.Builder;

                        string strValue = (string)builder.BuildObject();
                        if (UrlPath.IsRelativeUrl(strValue) && !UrlPath.IsAppRelativePath(strValue)) {
                            cpe.Builder = new StringPropertyBuilder(UrlPath.MakeVirtualPathAppRelative(UrlPath.Combine(_themeParser.VirtualDirPath.VirtualPathString, strValue)));
                        }
                    }
                }
            }

            return base.BuildStringPropertyExpression(pse);
        }

        protected override CodeAssignStatement BuildTemplatePropertyStatement(CodeExpression ctrlRefExpr) {

            // e.g. __ctrl.AppRelativeTemplateSourceDirectory = this.AppRelativeTemplateSourceDirectory;
            CodeAssignStatement assign = new CodeAssignStatement();
            assign.Left = new CodePropertyReferenceExpression(ctrlRefExpr, templateSourceDirectoryName);
            assign.Right = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), templateSourceDirectoryName);
            return assign;
        }

        protected override string GetGeneratedClassName() {
            string className = _themeParser.VirtualDirPath.FileName;
            className = System.Web.UI.Util.MakeValidTypeNameFromString(className);

            return className;
        }

        protected override bool UseResourceLiteralString(string s) {
            // never use resource literal string, page theme does not support the required methods.
            return false;
        }

        // Don't build the Profile property in Theme classes
        protected override bool NeedProfileProperty { get { return false; } }

        private class ControlSkinBuilderEntry {
            private ControlBuilder _builder;
            private string _id;

            public ControlSkinBuilderEntry (ControlBuilder builder, string skinID) {
                _builder = builder;
                _id = skinID;
            }

            public ControlBuilder Builder {
                get { return _builder; }
            }

            public String SkinID {
                get { return _id == null? String.Empty : _id; }
            }
        }
    }
}