File: BrowserDefinition.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (592 lines) | stat: -rw-r--r-- 22,580 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//------------------------------------------------------------------------------
// <copyright file="BrowserDefinition.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.Configuration {
    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Configuration;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web.Compilation;
    using System.Web.UI;
    using System.Web.Util;
    using System.Xml;
    using System.Globalization;

    //
    //
    //    <browsers>
    //    <browser id="XXX" parentID="YYY">
    //         <identification>
    //              <userAgent match="xxx" />
    //              <header name="HTTP_X_JPHONE_DISPLAY" match="xxx" />
    //              <capability name="majorVersion" match="^6$" />
    //         </identification>
    //         <capture>
    //              <header name="HTTP_X_UP_DEVCAP_NUMSOFTKEYS" match="?'softkeys'\d+)" />
    //         </capture>
    //         <capabilities>
    //              <mobileDeviceManufacturer>OpenWave</mobileDeviceManufacturer>
    //              <numberOfSoftKeys>$(softkeys)</numberOfSoftKeys>
    //         </capabilities>
    //         <controlAdapters>
    //              <adapter controlType="System.Web.UI.WebControls.Image"
    //                       adapterType="System.Web.UI.WebControls.Adapters.Html32ImageAdapter" />
    //         </controlAdapters>
    //    </browser>
    //    </browsers>

    internal class BrowserDefinition {

        internal static string MakeValidTypeNameFromString(string s) {
            if (s == null) {
                return s;
            }

            s = s.ToLower(CultureInfo.InvariantCulture);
            StringBuilder sb = new StringBuilder();
            for (int i=0; i<s.Length; i++) {
                // To be CLS-compliant (CS3008), public method name cannot starts with _{digit}
                if (i == 0) {
                    if(Char.IsDigit(s[0])) {
                        sb.Append("N");
                    }
                    else if(Char.IsLetter(s[0])) {
                        sb.Append(s.Substring(0, 1).ToUpper(CultureInfo.InvariantCulture));
                        continue;
                    }
                }

                if (Char.IsLetterOrDigit(s[i]) || s[i] == '_') {
                    sb.Append(s[i]);
                }
                else {
                    //
                    sb.Append('A');
                }
            }
            return sb.ToString();
        }

        // _idHeaderChecks are the header name and match string for <identification>
        private ArrayList _idHeaderChecks;
        //_idCapabilityChecks are the capability name and match string for <identification>
        private ArrayList _idCapabilityChecks;
        //_captureHeaderChecks are the header name and match string for <capture>
        private ArrayList _captureHeaderChecks;
        //_captureCapabilityChecks are the capability name and match string for <capture>
        private ArrayList _captureCapabilityChecks;
        private AdapterDictionary _adapters;
        private string _id;
        private string _parentID;
        private string _name;
        private string _parentName;
        private NameValueCollection _capabilities;
        private BrowserDefinitionCollection _browsers;
        private BrowserDefinitionCollection _gateways;
        private BrowserDefinitionCollection _refBrowsers;
        private BrowserDefinitionCollection _refGateways;
        private XmlNode _node;
        private bool _isRefID = false;
        private bool _isDeviceNode;
        private bool _isDefaultBrowser;
        private string _htmlTextWriterString;
        private int _depth = 0;

        internal BrowserDefinition(XmlNode node) : this(node, false) {
        }

        internal BrowserDefinition(XmlNode node, bool isDefaultBrowser) {
            if (node == null)
                throw new ArgumentNullException("node");

            _capabilities = new NameValueCollection();
            _idHeaderChecks = new ArrayList();
            _idCapabilityChecks = new ArrayList();
            _captureHeaderChecks = new ArrayList();
            _captureCapabilityChecks = new ArrayList();
            _adapters = new AdapterDictionary();
            _browsers = new BrowserDefinitionCollection();
            _gateways = new BrowserDefinitionCollection();
            _refBrowsers = new BrowserDefinitionCollection();
            _refGateways = new BrowserDefinitionCollection();
            _node = node;
            _isDefaultBrowser = isDefaultBrowser;

            string refID = null;

            HandlerBase.GetAndRemoveNonEmptyStringAttribute(node, "id", ref _id);
            HandlerBase.GetAndRemoveNonEmptyStringAttribute(node, "refID", ref refID);

            if((refID != null) && (_id != null)) {
                throw new ConfigurationErrorsException(SR.GetString(SR.Browser_mutually_exclusive_attributes, "id", "refID"), node);
            }

            if (_id != null) {
                if (!System.CodeDom.Compiler.CodeGenerator.IsValidLanguageIndependentIdentifier(_id)) {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Browser_InvalidID, "id", _id), node);
                }
            }
            else {
                if (refID == null) {
                    if (this is GatewayDefinition) {
                        throw new ConfigurationErrorsException(SR.GetString(SR.Browser_attributes_required, "gateway", "refID", "id"), node);
                    }

                    throw new ConfigurationErrorsException(SR.GetString(SR.Browser_attributes_required, "browser", "refID", "id"), node);
                }
                else {
                    if (!System.CodeDom.Compiler.CodeGenerator.IsValidLanguageIndependentIdentifier(refID)) {
                        throw new ConfigurationErrorsException(SR.GetString(SR.Browser_InvalidID, "refID", refID), node);
                    }
                }

                _parentID = refID;
                _isRefID = true;
                _id = refID;

                if (this is GatewayDefinition) {
                    _name = "refgatewayid$";
                }
                else {
                    _name = "refbrowserid$";
                }

                String parentID = null;
                HandlerBase.GetAndRemoveNonEmptyStringAttribute(node, "parentID", ref parentID);
                if ((parentID != null) && (parentID.Length != 0)) {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Browser_mutually_exclusive_attributes, "parentID", "refID"), node);
                }
            }

            _name = MakeValidTypeNameFromString(_id + _name);

            if(!_isRefID) {
                // Not a default browser definition
                if (!("Default".Equals(_id))) {
                    HandlerBase.GetAndRemoveNonEmptyStringAttribute(node, "parentID", ref _parentID);
                }
                // Make sure parentID is not specified on default browser
                else {
                    HandlerBase.GetAndRemoveNonEmptyStringAttribute(node, "parentID", ref _parentID);
                    if (_parentID != null)
                        throw new ConfigurationErrorsException(
                            SR.GetString(SR.Browser_parentID_applied_to_default), node);
                }
            }

            _parentName = MakeValidTypeNameFromString(_parentID);

            if(_id.IndexOf(" ", StringComparison.Ordinal) != -1) {
                throw new ConfigurationErrorsException(SR.GetString(SR.Space_attribute, "id " + _id), node);
            }

            foreach(XmlNode child in node.ChildNodes) {
                if(child.NodeType != XmlNodeType.Element) {
                    continue;
                }

                switch (child.Name) {
                    case "identification":
                        // refID nodes do not allow <identification>
                        if (_isRefID) {
                            throw new ConfigurationErrorsException(SR.GetString(SR.Browser_refid_prohibits_identification), node);
                        }

                        this.ProcessIdentificationNode(child, BrowserCapsElementType.Identification);
                        break;

                    case "capture":
                        this.ProcessCaptureNode(child, BrowserCapsElementType.Capture);
                        break;

                    case "capabilities":
                        this.ProcessCapabilitiesNode(child);
                        break;

                    case "controlAdapters":
                        this.ProcessControlAdaptersNode(child);
                        break;

                    case "sampleHeaders":
                        break;

                    default:
                        throw new ConfigurationErrorsException(SR.GetString(SR.Browser_invalid_element, child.Name), node);
                }
            }
        }

        public bool IsDefaultBrowser {
            get {
                return _isDefaultBrowser;
            }
        }

        public BrowserDefinitionCollection Browsers {
            get {
                return _browsers;
            }
        }

        public BrowserDefinitionCollection RefBrowsers {
            get {
                return _refBrowsers;
            }
        }

        public BrowserDefinitionCollection RefGateways {
            get {
                return _refGateways;
            }
        }

        public BrowserDefinitionCollection Gateways {
            get {
                return _gateways;
            }
        }

        public string ID {
            get {
                return _id;
            }
        }

        public string Name {
            get {
                return _name;
            }
        }

        public string ParentName {
            get {
                return _parentName;
            }
        }

        // Indicate whether this node represents a real device, ie. all ancestor nodes are browser deinitions.
        internal bool IsDeviceNode {
            get {
                return _isDeviceNode;
            }
            set {
                _isDeviceNode = value;
            }
        }

        internal int Depth {
            get {
                return _depth;
            }
            set {
                _depth = value;
            }
        }

        public string ParentID {
            get {
                return _parentID;
            }
        }

        internal bool IsRefID {
            get {
                return _isRefID;
            }
        }

        public NameValueCollection Capabilities {
            get {
                return _capabilities;
            }
        }

        public ArrayList IdHeaderChecks {
            get {
                return _idHeaderChecks;
            }
        }

        public ArrayList CaptureHeaderChecks {
            get {
                return _captureHeaderChecks;
            }
        }

        public ArrayList IdCapabilityChecks {
            get {
                return _idCapabilityChecks;
            }
        }

        public ArrayList CaptureCapabilityChecks {
            get {
                return _captureCapabilityChecks;
            }
        }

        public AdapterDictionary Adapters {
            get {
                return _adapters;
            }
        }

        internal XmlNode XmlNode {
            get {
                return _node;
            }
        }

        public string HtmlTextWriterString {
            get {
                return _htmlTextWriterString;
            }
        }

        private void DisallowNonMatchAttribute(XmlNode node) {
            string check = null;
            HandlerBase.GetAndRemoveStringAttribute(node, "nonMatch", ref check);
            if(check != null) {
                throw new ConfigurationErrorsException(SR.GetString(SR.Browser_mutually_exclusive_attributes, "match", "nonMatch"), node);
            }
        }

        private void HandleMissingMatchAndNonMatchError(XmlNode node) {
            throw new ConfigurationErrorsException(
                SR.GetString(SR.Missing_required_attributes, "match", "nonMatch", node.Name),
                node);
        }

        /*
        /* 
        <identification>
        <userAgent match="xxx" />
        <header name="HTTP_X_JPHONE_DISPLAY" match="xxx" />
        <capability name="majorVersion" match="^6$" />
        </identification>
        <capture>
        <header name="HTTP_X_UP_DEVCAP_NUMSOFTKEYS" match="?'softkeys'\d+)" />
        </capture>
        */
        internal void ProcessIdentificationNode(XmlNode node, BrowserCapsElementType elementType) {
            string match = null;
            string header = null;
            bool nonMatch;
            bool emptyIdentification = true;

            foreach(XmlNode child in node.ChildNodes) {
                match = String.Empty;
                nonMatch = false;
                if(child.NodeType != XmlNodeType.Element) {
                    continue;
                }

                switch (child.Name) {
                    case "userAgent":
                        emptyIdentification = false;

                        //match the user agent
                        HandlerBase.GetAndRemoveNonEmptyStringAttribute(child, "match", ref match);
                        if (String.IsNullOrEmpty(match)) {
                            HandlerBase.GetAndRemoveNonEmptyStringAttribute(child, "nonMatch", ref match);

                            if (String.IsNullOrEmpty(match)) {
                                HandleMissingMatchAndNonMatchError(child);
                            }

                            nonMatch = true;
                        }
                        _idHeaderChecks.Add(new CheckPair("User-Agent", match, nonMatch));
                        if (nonMatch == false) {
                            DisallowNonMatchAttribute(child);
                        }

                        break;

                    case "header":
                        emptyIdentification = false;

                        //match some arbitrary header
                        HandlerBase.GetAndRemoveRequiredNonEmptyStringAttribute(child, "name", ref header);
                        HandlerBase.GetAndRemoveNonEmptyStringAttribute(child, "match", ref match);
                        if (String.IsNullOrEmpty(match)) {
                            HandlerBase.GetAndRemoveNonEmptyStringAttribute(child, "nonMatch", ref match);

                            if (String.IsNullOrEmpty(match)) {
                                HandleMissingMatchAndNonMatchError(child);
                            }

                            nonMatch = true;
                        }
                        _idHeaderChecks.Add(new CheckPair(header, match, nonMatch));
                        if (nonMatch == false) {
                            DisallowNonMatchAttribute(child);
                        }
                        break;

                    case "capability":
                        emptyIdentification = false;

                        //match against an already set capability
                        HandlerBase.GetAndRemoveRequiredNonEmptyStringAttribute(child, "name", ref header);
                        HandlerBase.GetAndRemoveNonEmptyStringAttribute(child, "match", ref match);
                        if (String.IsNullOrEmpty(match)) {
                            HandlerBase.GetAndRemoveNonEmptyStringAttribute(child, "nonMatch", ref match);

                            if (String.IsNullOrEmpty(match)) {
                                HandleMissingMatchAndNonMatchError(child);
                            }

                            nonMatch = true;
                        }
                        _idCapabilityChecks.Add(new CheckPair(header, match, nonMatch));
                        //verify that match and nonMatch are not both specified
                        if (nonMatch == false) {
                            DisallowNonMatchAttribute(child);
                        }
                        break;
                    default:
                        throw new ConfigurationErrorsException(SR.GetString(SR.Config_invalid_element, child.ToString()), child);
                }
            }

            if (emptyIdentification) {
                throw new ConfigurationErrorsException(SR.GetString(SR.Browser_empty_identification), node);
            }

            return;
        }

        internal void ProcessCaptureNode(XmlNode node, BrowserCapsElementType elementType) {
            string match = null;
            string header = null;
            foreach(XmlNode child in node.ChildNodes) {
                if(child.NodeType != XmlNodeType.Element) {
                    continue;
                }
                switch(child.Name) {
                case "userAgent":
                    //match the user agent
                    HandlerBase.GetAndRemoveRequiredNonEmptyStringAttribute(child, "match", ref match);
                    _captureHeaderChecks.Add(new CheckPair("User-Agent", match));
                    break;
                case "header":
                    //match some arbitrary header
                    HandlerBase.GetAndRemoveRequiredNonEmptyStringAttribute(child, "name", ref header);
                    HandlerBase.GetAndRemoveRequiredNonEmptyStringAttribute(child, "match", ref match);
                    _captureHeaderChecks.Add(new CheckPair(header, match));
                    break;
                case "capability":
                    //match against an already set capability
                    HandlerBase.GetAndRemoveRequiredNonEmptyStringAttribute(child, "name", ref header);
                    HandlerBase.GetAndRemoveRequiredNonEmptyStringAttribute(child, "match", ref match);
                    _captureCapabilityChecks.Add(new CheckPair(header, match));
                    break;
                default:
                    throw new ConfigurationErrorsException(SR.GetString(SR.Config_invalid_element, child.ToString()), child);
                }
            }
            return;
        }


        /*
        <capabilities>
             <capability name="mobileDeviceManufacturer" value="OpenWave"</capability>
             <capability name="numberOfSoftKeys" value="$(softkeys)"</capability>
        </capabilities>
        */

        internal void ProcessCapabilitiesNode(XmlNode node) {
            foreach(XmlNode child in node.ChildNodes) {
                if(child.NodeType != XmlNodeType.Element) {
                    continue;
                }
                if (child.Name != "capability") {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Config_base_unrecognized_element), child);
                }
                string capabilityName = null;
                string capabilityValue = null;
                HandlerBase.GetAndRemoveRequiredNonEmptyStringAttribute(child, "name", ref capabilityName);
                HandlerBase.GetAndRemoveRequiredStringAttribute(child, "value", ref capabilityValue);
                _capabilities[capabilityName] = capabilityValue;
            }
            return;
        }

        /*
        <controlAdapters>
             <adapter controlType="System.Web.UI.WebControls.Image"
                      adapterType="System.Web.UI.WebControls.Adapters.Html32ImageAdapter" />
        </controlAdapters>
        */
        internal void ProcessControlAdaptersNode(XmlNode node) {
            HandlerBase.GetAndRemoveStringAttribute(node, "markupTextWriterType", ref _htmlTextWriterString);
            foreach(XmlNode child in node.ChildNodes) {
                if(child.NodeType != XmlNodeType.Element) {
                    continue;
                }
                if(child.Name != "adapter") {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Config_base_unrecognized_element), child);
                }
                XmlAttributeCollection nodeAttributes = child.Attributes;
                string controlString = null;
                string adapterString = null;
                HandlerBase.GetAndRemoveRequiredNonEmptyStringAttribute(child, "controlType", ref controlString);
                HandlerBase.GetAndRemoveRequiredStringAttribute(child, "adapterType", ref adapterString);

                Type type = CheckType(controlString, typeof(Control), child);

                // Normalize control type name
                controlString = type.AssemblyQualifiedName;

                if (!String.IsNullOrEmpty(adapterString)) {
                    CheckType(adapterString, typeof(System.Web.UI.Adapters.ControlAdapter), child);
                }

                _adapters[controlString] = adapterString;
            }
            return;
        }

        private static Type CheckType(string typeName, Type baseType, XmlNode child) {
            // Use BuildManager to verify control types.
            // Note for machine level browser files, this will only check assemblies in GAC.
            Type type = ConfigUtil.GetType(typeName, child, true /*ignoreCase*/);

            if (!baseType.IsAssignableFrom(type)) {
                throw new ConfigurationErrorsException(
                    SR.GetString(SR.Type_doesnt_inherit_from_type, typeName, 
                        baseType.FullName), child);
            }

            if (!HttpRuntime.IsTypeAllowedInConfig(type)) {
                throw new ConfigurationErrorsException(
                    SR.GetString(SR.Type_from_untrusted_assembly, typeName), child);
            }

            return type;
        }

        internal void MergeWithDefinition(BrowserDefinition definition) {
            Debug.Assert(definition.IsRefID);

            // Copy the capabilities
            foreach (String key in definition.Capabilities.Keys) {
                this._capabilities[key] = definition.Capabilities[key];
            }

            // Copy the adapter definition
            foreach (String key in definition.Adapters.Keys) {
                this._adapters[key] = definition.Adapters[key];
            }

            this._htmlTextWriterString = definition.HtmlTextWriterString;
        }
    }
}