File: TemplateControlParser.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 (384 lines) | stat: -rw-r--r-- 14,262 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//------------------------------------------------------------------------------
// <copyright file="TemplateControlParser.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

/*
 * Implements the ASP.NET template parser
 *
 * Copyright (c) 1998 Microsoft Corporation
 */

namespace System.Web.UI {

using System.Text;
using System;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Reflection;
using System.Configuration;
using System.Web.Caching;
using System.Web.Util;
using System.Web.Compilation;
using HttpException = System.Web.HttpException;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Security.Permissions;


/*
 * Parser for TemplateControl's (UserControls and Pages)
 */

/// <internalonly/>
/// <devdoc>
///    <para>[To be supplied.]</para>
/// </devdoc>
public abstract class TemplateControlParser : BaseTemplateParser {

    // Attributes in <%@ outputcache ... %> directive
    private IDictionary _outputCacheDirective;

    private OutputCacheParameters _outputCacheSettings;
    internal OutputCacheParameters OutputCacheParameters { get { return _outputCacheSettings; } }

    internal bool FAutoEventWireup { get { return !flags[noAutoEventWireup]; } }

    internal override bool RequiresCompilation {
        get { return flags[requiresCompilation] || CompilationMode == CompilationMode.Always; }
    }

    // Get default settings from config
    internal override void ProcessConfigSettings() {
        base.ProcessConfigSettings();

        if (PagesConfig != null) {
            flags[noAutoEventWireup] = !PagesConfig.AutoEventWireup;

            // If config has a non-default EnableViewState value, set it in the main directive
            if (PagesConfig.EnableViewState != Control.EnableViewStateDefault)
                _mainDirectiveConfigSettings["enableviewstate"] = Util.GetStringFromBool(PagesConfig.EnableViewState);

            CompilationMode = PagesConfig.CompilationMode;
        }

        // if there is a filter, ask it if it wants to use a different mode
        if (_pageParserFilter != null)
            CompilationMode = _pageParserFilter.GetCompilationMode(CompilationMode);
    }

    internal override void ProcessDirective(string directiveName, IDictionary directive) {

        if (StringUtil.EqualsIgnoreCase(directiveName, "outputcache")) {

            // Ignore the OutputCache directive in design mode (VSWhidbey 470314)
            if (FInDesigner)
                return;

            if (_outputCacheSettings == null) {
                _outputCacheSettings = new OutputCacheParameters();
            }

            // Make sure the outputcache directive was not already specified
            if (_outputCacheDirective != null) {
                throw new HttpException(
                    SR.GetString(SR.Only_one_directive_allowed, directiveName));
            }

            ProcessOutputCacheDirective(directiveName, directive);

            _outputCacheDirective = directive;
        }
        else if (StringUtil.EqualsIgnoreCase(directiveName, "reference")) {

            // Ignore the OutputCache directive in design mode (VSWhidbey 517783)
            if (FInDesigner) {
                return;
            }

            // Even though this only makes sense for compiled pages, Sharepoint needs us to
            // ignore instead of throw when the page in non-compiled.


            // For historical reasons, the virtual path can be specified by 3 different attributes:
            // virtualpath, page and control.  They all do the same, and virtualpath is the recommended
            // one (the other two are deprecated).

            // Make sure that no more than one is specified.

            VirtualPath virtualPath = Util.GetAndRemoveVirtualPathAttribute(directive, "virtualpath");

            bool enforcePage = false;
            bool enforceControl = false;

            VirtualPath tmp = Util.GetAndRemoveVirtualPathAttribute(directive, "page");
            if (tmp != null) {
                if (virtualPath != null) {
                    ProcessError(SR.GetString(SR.Invalid_reference_directive));
                    return;
                }
                virtualPath = tmp;
                enforcePage = true;
            }

            tmp = Util.GetAndRemoveVirtualPathAttribute(directive, "control");
            if (tmp != null) {
                if (virtualPath != null) {
                    ProcessError(SR.GetString(SR.Invalid_reference_directive));
                    return;
                }
                virtualPath = tmp;
                enforceControl = true;
            }

            // If we didn't get a virtual path, fail
            if (virtualPath == null) {
                ProcessError(SR.GetString(SR.Invalid_reference_directive));
                return;
            }

            Type t = GetReferencedType(virtualPath);

            if (t == null) {
                ProcessError(SR.GetString(SR.Invalid_reference_directive_attrib, virtualPath));
            }

            // If the 'page' attribute was used, make sure it's indeed a Page
            if (enforcePage && !typeof(Page).IsAssignableFrom(t)) {
                ProcessError(SR.GetString(SR.Invalid_reference_directive_attrib, virtualPath));
            }

            // If the 'control' attribute was used, make sure it's indeed a UserControl
            if (enforceControl && !typeof(UserControl).IsAssignableFrom(t)) {
                ProcessError(SR.GetString(SR.Invalid_reference_directive_attrib, virtualPath));
            }

            // If there are some attributes left, fail
            Util.CheckUnknownDirectiveAttributes(directiveName, directive);
        }
        else {
            base.ProcessDirective(directiveName, directive);
        }
    }

    internal override void ProcessMainDirective(IDictionary mainDirective) {

        // We want to make sure that we process the compilationmode attribute before any
        // of the other main directive attributes, since its presence can cause other
        // attributes to be illegal.  So we handle it instead of in ProcessMainDirectiveAttribute.
        object tmpObj = null;

        try {
            tmpObj = Util.GetAndRemoveEnumAttribute(mainDirective,
                typeof(CompilationMode), "compilationmode");
        }
        // catch the exception here so we can continue to process the rest of the main directive
        // when called from CBM
        catch (Exception ex) {
            ProcessError(ex.Message);
        }

        if (tmpObj != null) {
            CompilationMode = (CompilationMode) tmpObj;

            // if there is a filter, ask it if it wants to use a different mode
            if (_pageParserFilter != null)
                CompilationMode = _pageParserFilter.GetCompilationMode(CompilationMode);
        }

        base.ProcessMainDirective(mainDirective);
    }

    internal override bool ProcessMainDirectiveAttribute(string deviceName, string name,
        string value, IDictionary parseData) {

        switch (name) {

        // Ignore 'targetschema' attribute (ASURT 85670)
        case "targetschema":
            break;

        case "autoeventwireup":
            // This only makes sense for compiled pages
            OnFoundAttributeRequiringCompilation(name);

            flags[noAutoEventWireup] = !Util.GetBooleanAttribute(name, value);
            break;

        case "enabletheming":
            // Return false to let the generic attribute processing continue
            // which will cause the EnableTheming property to be set on the
            // TemplateControl instance.
            return false;

        case CodeFileBaseClassAttributeName:
            // Remember the base class for post processing
            parseData[name] = Util.GetNonEmptyAttribute(name, value);
            break;

        default:
            // We didn't handle the attribute.  Try the base class
            return base.ProcessMainDirectiveAttribute(deviceName, name, value, parseData);
        }

        // The attribute was handled

        // Make sure no device filter or resource expression was specified
        ValidateBuiltInAttribute(deviceName, name, value);

        return true;
    }

    internal override void ProcessUnknownMainDirectiveAttribute(string filter, string attribName, string value) {

        // Don't allow the id to be specified on the directive, even though it is
        // a public member of the control class (VSWhidbey 85384)
        if (attribName == "id") {
            base.ProcessUnknownMainDirectiveAttribute(filter, attribName, value);
            return;
        }

        // Process unknown attributes as regular control attribute, hence allowing
        // arbitrary properties of the base class to be set.
        // But turn off IAttributeAccessor support, otherwise any bad string on a
        // user control's directive won't be caught.

        try {
            RootBuilder.PreprocessAttribute(filter, attribName, value, true /*mainDirectiveMode*/);
        }
        catch (Exception e) {
            ProcessError(SR.GetString(SR.Attrib_parse_error, attribName, e.Message));
        }
    }

    /*
     * Add assembly dependencies for a collection of static objects
     */
    private void AddStaticObjectAssemblyDependencies(HttpStaticObjectsCollection staticObjects) {
        if (staticObjects == null || staticObjects.Objects == null) return;

        IDictionaryEnumerator en = staticObjects.Objects.GetEnumerator();
        while (en.MoveNext()) {
            HttpStaticObjectsEntry entry = (HttpStaticObjectsEntry)en.Value;

            AddTypeDependency(entry.ObjectType);
        }
    }

    internal Type GetDirectiveType(IDictionary directive, string directiveName) {
        string typeName = Util.GetAndRemoveNonEmptyNoSpaceAttribute(directive, "typeName");
        VirtualPath virtualPath = Util.GetAndRemoveVirtualPathAttribute(directive, "virtualPath");

        Type resultType = null;

        // If neither or both are specified, fail
        if ((typeName == null) == (virtualPath == null)) {
            throw new HttpException(SR.GetString(SR.Invalid_typeNameOrVirtualPath_directive, directiveName));
        }

        if (typeName != null) {
            resultType = GetType(typeName);
            AddTypeDependency(resultType);
        }
        else {
            resultType = GetReferencedType(virtualPath);
        }

        // If there are some attributes left, fail
        Util.CheckUnknownDirectiveAttributes(directiveName, directive);

        return resultType;
    }

    internal override void HandlePostParse() {
        base.HandlePostParse();

        if (!FInDesigner) {
            // Omit AutoEventWireup if there can't possibly be any events defined (ASURT 97772)
            if (ScriptList.Count == 0 && BaseType == DefaultBaseType && CodeFileVirtualPath == null)
                flags[noAutoEventWireup] = true;

            _applicationObjects = HttpApplicationFactory.ApplicationState.StaticObjects;
            AddStaticObjectAssemblyDependencies(_applicationObjects);

            _sessionObjects = HttpApplicationFactory.ApplicationState.SessionStaticObjects;
            AddStaticObjectAssemblyDependencies(_sessionObjects);
        }
    }

    /*
     * Process the contents of the <%@ OutputCache ... %> directive
     */
    internal virtual void ProcessOutputCacheDirective(string directiveName, IDictionary directive) {
        int duration = 0;   // Unit is second
        string varyByParams;
        string varyByCustom;
        string outputCacheProfile = null;
        string varyByControls;

        bool fHasDuration = Util.GetAndRemovePositiveIntegerAttribute(directive, "duration", ref duration);
        if (fHasDuration) {
            OutputCacheParameters.Duration = duration;
        }

        // 
        if (this is PageParser) {
            outputCacheProfile = Util.GetAndRemoveNonEmptyAttribute(directive, "cacheProfile");
            if (outputCacheProfile != null) {
                OutputCacheParameters.CacheProfile = outputCacheProfile;
            }
        }

        if (!fHasDuration && (outputCacheProfile == null || outputCacheProfile.Length == 0) && FDurationRequiredOnOutputCache)
            throw new HttpException(SR.GetString(SR.Missing_attr, "duration"));

        varyByCustom = Util.GetAndRemoveNonEmptyAttribute(directive, "varybycustom");
        if (varyByCustom != null) {
            OutputCacheParameters.VaryByCustom = varyByCustom;
        }

        varyByControls = Util.GetAndRemoveNonEmptyAttribute(directive, "varybycontrol");
        if (varyByControls != null) {
            OutputCacheParameters.VaryByControl = varyByControls;
        }

        varyByParams = Util.GetAndRemoveNonEmptyAttribute(directive, "varybyparam");
        if (varyByParams != null) {
            OutputCacheParameters.VaryByParam = varyByParams;
        }

        // VaryByParams is required (ASURT 76763)
        if (varyByParams == null && 
            varyByControls == null && 
            (outputCacheProfile == null || outputCacheProfile.Length == 0) && 
            FVaryByParamsRequiredOnOutputCache)
            throw new HttpException(SR.GetString(SR.Missing_varybyparam_attr));

        // If it's "none", set it to null
        if (StringUtil.EqualsIgnoreCase(varyByParams, "none"))
            OutputCacheParameters.VaryByParam = null;

        if (StringUtil.EqualsIgnoreCase(varyByControls, "none"))
            OutputCacheParameters.VaryByControl = null;

        // If there are some attributes left, fail
        Util.CheckUnknownDirectiveAttributes(directiveName, directive, UnknownOutputCacheAttributeError);
    }

    internal virtual bool FDurationRequiredOnOutputCache {
        get { return true; }
    }

    internal virtual bool FVaryByParamsRequiredOnOutputCache {
        get { return true; }
    }

    internal abstract string UnknownOutputCacheAttributeError {
        get;
    }
}
}