File: AdRotator.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 (974 lines) | stat: -rw-r--r-- 35,777 bytes parent folder | download | duplicates (6)
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
//------------------------------------------------------------------------------
// <copyright file="AdRotator.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.UI.WebControls {
    using System.IO;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI;
    using System.Web.Caching;
    using System.Web;
    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Drawing.Design;
    using System.Xml;
    using System.Globalization;
    using System.Web.Util;
    using System.Reflection;
    using System.Text;


    /// <devdoc>
    ///    <para>Displays a randomly selected ad banner on a page.</para>
    /// </devdoc>
    [
    DefaultEvent("AdCreated"),
    DefaultProperty("AdvertisementFile"),
    Designer("System.Web.UI.Design.WebControls.AdRotatorDesigner, " + AssemblyRef.SystemDesign),
    ToolboxData("<{0}:AdRotator runat=\"server\"></{0}:AdRotator>")
    ]
    public class AdRotator : DataBoundControl {

        private static readonly object EventAdCreated = new object();

        private const string XmlDocumentTag = "Advertisements";
        private const string XmlDocumentRootXPath = "/" + XmlDocumentTag;
        private const string XmlAdTag = "Ad";

        private const string KeywordProperty = "Keyword";
        private const string ImpressionsProperty = "Impressions";

        // static copy of the Random object. This is a pretty hefty object to
        // initialize, so you don't want to create one each time.
        private static Random _random;

        private String _baseUrl;
        private string _advertisementFile;
        private AdCreatedEventArgs _adCreatedEventArgs;

        private AdRec [] _adRecs;
        private bool _isPostCacheAdHelper;
        private string _uniqueID;

        private static readonly Type _adrotatorType = typeof(AdRotator);
        private static readonly Type[] _AdCreatedParameterTypes = {typeof(AdCreatedEventArgs)};


        /// <devdoc>
        /// <para>Initializes a new instance of the <see cref='System.Web.UI.WebControls.AdRotator'/> class.</para>
        /// </devdoc>
        public AdRotator() {
        }


        /// <devdoc>
        ///    <para>Gets or sets the path to the XML file that contains advertisement data.</para>
        /// </devdoc>
        [
        Bindable(true),
        WebCategory("Behavior"),
        DefaultValue(""),
        Editor("System.Web.UI.Design.XmlUrlEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
        UrlProperty(),
        WebSysDescription(SR.AdRotator_AdvertisementFile)
        ]
        public string AdvertisementFile {
            get {
                return((_advertisementFile == null) ? String.Empty : _advertisementFile);
            }
            set {
                _advertisementFile = value;
            }
        }


        [
        WebCategory("Behavior"),
        DefaultValue(AdCreatedEventArgs.AlternateTextElement),
        WebSysDescription(SR.AdRotator_AlternateTextField)
        ]
        public String AlternateTextField {
            get {
                String s = (String) ViewState["AlternateTextField"];
                return((s != null) ? s : AdCreatedEventArgs.AlternateTextElement);
            }
            set {
                ViewState["AlternateTextField"] = value;
            }
        }

        /// <devdoc>
        ///   The base url corresponds for mapping of other url elements such as
        ///   imageUrl and navigateUrl.
        /// </devdoc>
        internal String BaseUrl {
            get {
                if (_baseUrl == null) {
                    // Deal with app relative syntax (e.g. ~/foo)
                    string tplSourceDir = TemplateControlVirtualDirectory.VirtualPathString;

                    // For the AdRotator, use the AdvertisementFile directory as the base, and fall back to the
                    // page/user control location as the base.
                    String absoluteFile = null;
                    String fileDirectory = null;
                    if (!String.IsNullOrEmpty(AdvertisementFile)) {
                        absoluteFile = UrlPath.Combine(tplSourceDir, AdvertisementFile);
                        fileDirectory = UrlPath.GetDirectory(absoluteFile);
                    }

                    _baseUrl = string.Empty;
                    if (fileDirectory != null) {
                        _baseUrl = fileDirectory;
                    }
                    if (_baseUrl.Length == 0) {
                        _baseUrl = tplSourceDir;
                    }
                }
                return _baseUrl;
            }
        }

        /// <internalonly/>
        /// <devdoc>
        ///    Font property. Has no effect on this control, so hide it.
        /// </devdoc>
        [
        Browsable(false),
        EditorBrowsableAttribute(EditorBrowsableState.Never),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        ]
        public override FontInfo Font {
            get {
                return base.Font;
            }
        }


        [
        WebCategory("Behavior"),
        DefaultValue(AdCreatedEventArgs.ImageUrlElement),
        WebSysDescription(SR.AdRotator_ImageUrlField)
        ]
        public String ImageUrlField {
            get {
                String s = (String) ViewState["ImageUrlField"];
                return((s != null) ? s : AdCreatedEventArgs.ImageUrlElement);
            }
            set {
                ViewState["ImageUrlField"] = value;
            }
        }

        private bool IsTargetSet {
            get {
                return (ViewState["Target"] != null);
            }
        }

        internal bool IsPostCacheAdHelper {
            get {
                return _isPostCacheAdHelper;
            }
            set {
                _isPostCacheAdHelper = value;
            }
        }


        /// <devdoc>
        ///    <para>Gets or sets a category keyword used for matching related advertisements in the advertisement file.</para>
        /// </devdoc>
        [
        Bindable(true),
        WebCategory("Behavior"),
        DefaultValue(""),
        WebSysDescription(SR.AdRotator_KeywordFilter)
        ]
        public string KeywordFilter {
            get {
                string s = (string)ViewState["KeywordFilter"];
                return((s == null) ? String.Empty : s);
            }
            set {
                // trim the filter value
                if (String.IsNullOrEmpty(value)) {
                    ViewState.Remove("KeywordFilter");
                }
                else {
                    ViewState["KeywordFilter"] = value.Trim();
                }
            }
        }


        [
        WebCategory("Behavior"),
        DefaultValue(AdCreatedEventArgs.NavigateUrlElement),
        WebSysDescription(SR.AdRotator_NavigateUrlField)
        ]
        public String NavigateUrlField {
            get {
                String s = (String) ViewState["NavigateUrlField"];
                return((s != null) ? s : AdCreatedEventArgs.NavigateUrlElement);
            }
            set {
                ViewState["NavigateUrlField"] = value;
            }
        }


        private AdCreatedEventArgs SelectedAdArgs {
            get {
                return _adCreatedEventArgs;
            }
            set {
                _adCreatedEventArgs = value;
            }
        }


        /// <devdoc>
        ///    <para>Gets
        ///       or sets the name of the browser window or frame to display the advertisement.</para>
        /// </devdoc>
        [
        Bindable(true),
        WebCategory("Behavior"),
        DefaultValue("_top"),
        WebSysDescription(SR.AdRotator_Target),
        TypeConverter(typeof(TargetConverter))
        ]
        public string Target {
            get {
                string s = (string)ViewState["Target"];
                return((s == null) ? "_top" : s);
            }
            set {
                ViewState["Target"] = value;
            }
        }


        protected override HtmlTextWriterTag TagKey {
            get {
                return HtmlTextWriterTag.A;
            }
        }

        public override string UniqueID {
            get {
                if (_uniqueID == null) {
                    _uniqueID = base.UniqueID;
                }
                return _uniqueID;
            }
        }


        /// <devdoc>
        ///    <para>Occurs once per round trip after the creation of the
        ///       control before the page is rendered. </para>
        /// </devdoc>
        [
        WebCategory("Action"),
        WebSysDescription(SR.AdRotator_OnAdCreated)
        ]
        public event AdCreatedEventHandler AdCreated {
            add {
                Events.AddHandler(EventAdCreated, value);
            }
            remove {
                Events.RemoveHandler(EventAdCreated, value);
            }
        }

        private void CheckOnlyOneDataSource() {
            int numOfDataSources = ((AdvertisementFile.Length > 0) ? 1 : 0);
            numOfDataSources += ((DataSourceID.Length > 0) ? 1 : 0);
            numOfDataSources += ((DataSource != null) ? 1 : 0);

            if (numOfDataSources > 1) {
                throw new HttpException(SR.GetString(SR.AdRotator_only_one_datasource, ID));
            }
        }

        // Currently this is designed to be called when PostCache Substitution is being initialized
        internal void CopyFrom(AdRotator adRotator) {
            _adRecs = adRotator._adRecs;

            AccessKey = adRotator.AccessKey;
            AlternateTextField = adRotator.AlternateTextField;
            Enabled = adRotator.Enabled;
            ImageUrlField = adRotator.ImageUrlField;
            NavigateUrlField = adRotator.NavigateUrlField;
            TabIndex = adRotator.TabIndex;
            Target = adRotator.Target;
            ToolTip = adRotator.ToolTip;

            string id = adRotator.ID;
            if (!String.IsNullOrEmpty(id)) {
                ID = adRotator.ClientID;
            }

            // Below are properties that need to be handled specially and saved
            // to private variables.
            _uniqueID = adRotator.UniqueID;
            _baseUrl = adRotator.BaseUrl;

            // Special copy to properties that cannot be assigned directly
            if (adRotator.HasAttributes) {
                foreach(string key in adRotator.Attributes.Keys) {
                    Attributes[key] = adRotator.Attributes[key];
                }
            }

            if (adRotator.ControlStyleCreated) {
                ControlStyle.CopyFrom(adRotator.ControlStyle);
            }
        }


        private ArrayList CreateAutoGeneratedFields(IEnumerable dataSource) {
            if (dataSource == null) {
                return null;
            }

            ArrayList generatedFields = new ArrayList();
            PropertyDescriptorCollection propertyDescriptors = null;

            if (dataSource is ITypedList) {
                propertyDescriptors =
                    ((ITypedList)dataSource).GetItemProperties(new PropertyDescriptor[0]);
            }

            if (propertyDescriptors == null) {

                IEnumerator enumerator = dataSource.GetEnumerator();
                if (enumerator.MoveNext()) {

                    Object sampleItem = enumerator.Current;
                    if (IsBindableType(sampleItem.GetType())) {
                        // Raise error since we are expecting some record
                        // containing multiple data values.
                        throw new HttpException(SR.GetString(SR.AdRotator_expect_records_with_advertisement_properties,
                                ID, sampleItem.GetType()));
                    }
                    else {
                        propertyDescriptors = TypeDescriptor.GetProperties(sampleItem);
                    }
                }
            }
            if (propertyDescriptors != null && propertyDescriptors.Count > 0) {

                foreach (PropertyDescriptor pd in propertyDescriptors) {
                    if (IsBindableType(pd.PropertyType)) {
                        generatedFields.Add(pd.Name);
                    }
                }
            }

            return generatedFields;
        }


        // 








        internal bool DoPostCacheSubstitutionAsNeeded(HtmlTextWriter writer) {
            if (!IsPostCacheAdHelper && SelectedAdArgs == null &&
                Page.Response.HasCachePolicy &&
                (int)Page.Response.Cache.GetCacheability() != (int)HttpCacheabilityLimits.None) {

                // The checking of the cacheability is to see if the page is output cached
                AdPostCacheSubstitution adPostCacheSubstitution = new AdPostCacheSubstitution(this);
                adPostCacheSubstitution.RegisterPostCacheCallBack(Context, Page, writer);
                return true;
            }
            return false;
        }

        /// <devdoc>
        ///     <para>Select an ad from ad records and create the event
        ///     argument object.</para>
        /// </devdoc>
        private AdCreatedEventArgs GetAdCreatedEventArgs() {
            IDictionary adInfo = SelectAdFromRecords();
            AdCreatedEventArgs adArgs =
                new AdCreatedEventArgs(adInfo,
                                       ImageUrlField,
                                       NavigateUrlField,
                                       AlternateTextField);
           return adArgs;
        }


        private AdRec [] GetDataSourceData(IEnumerable dataSource) {

            ArrayList fields = CreateAutoGeneratedFields(dataSource);

            ArrayList adDicts = new ArrayList();
            IEnumerator enumerator = dataSource.GetEnumerator();
            while(enumerator.MoveNext()) {
                IDictionary dict = null;
                foreach (String field in fields){
                    if (dict == null) {
                        dict = new HybridDictionary();
                    }
                    dict.Add(field, DataBinder.GetPropertyValue(enumerator.Current, field));
                }

                if (dict != null) {
                    adDicts.Add(dict);
                }
            }

            return SetAdRecs(adDicts);
        }


        /// <devdoc>
        ///   Gets the ad data for the given file by loading the file, or reading from the
        ///   application-level cache.
        /// </devdoc>
        private AdRec [] GetFileData(string fileName) {

            // VSWhidbey 208626: Adopting similar code from xml.cs to support virtual path provider

            // First, figure out if it's a physical or virtual path
            VirtualPath virtualPath;
            string physicalPath;
            ResolvePhysicalOrVirtualPath(fileName, out virtualPath, out physicalPath);

            // try to get it from the ASP.NET cache
            string fileKey = CacheInternal.PrefixAdRotator + ((!String.IsNullOrEmpty(physicalPath)) ?
                physicalPath : virtualPath.VirtualPathString);
            CacheStoreProvider cacheInternal = System.Web.HttpRuntime.Cache.InternalCache;
            AdRec[] adRecs = cacheInternal.Get(fileKey) as AdRec[];

            if (adRecs == null) {
                // Otherwise load it
                CacheDependency dependency;
                try {
                    using (Stream stream = OpenFileAndGetDependency(virtualPath, physicalPath, out dependency)) {
                        adRecs = LoadStream(stream);
                        Debug.Assert(adRecs != null);
                    }
                }
                catch (Exception e) {
                    if (!String.IsNullOrEmpty(physicalPath) && HttpRuntime.HasPathDiscoveryPermission(physicalPath)) {
                        // We want to catch the error message, but not propage the inner exception. Otherwise we can throw up
                        // logon prompts through IE;
                        throw new HttpException(SR.GetString(SR.AdRotator_cant_open_file, ID, e.Message));
                    }
                    else {
                        throw new HttpException(SR.GetString(SR.AdRotator_cant_open_file_no_permission, ID));
                    }
                }

                // Cache it, but only if we got a dependency
                if (dependency != null) {
                    using (dependency) {
                        // and store it in the cache, dependent on the file name
                        cacheInternal.Insert(fileKey, adRecs, new CacheInsertOptions() { Dependencies = dependency });
                    }
                }
            }
            return adRecs;
        }

        private static int GetRandomNumber(int maxValue) {
            if (_random == null) {
                _random = new Random();
            }
            return _random.Next(maxValue) + 1;
        }

        private AdRec [] GetXmlDataSourceData(XmlDataSource xmlDataSource) {
            Debug.Assert(xmlDataSource != null);

            XmlDocument doc = xmlDataSource.GetXmlDocument();
            if (doc == null) {
                return null;
            }
            return LoadXmlDocument(doc);
        }

        private bool IsBindableType(Type type) {
            return(type.IsPrimitive ||
                   (type == typeof(String)) ||
                   (type == typeof(DateTime)) ||
                   (type == typeof(Decimal)));
        }

        private bool IsOnAdCreatedOverridden() {
            bool result = false;
            Type type = this.GetType();
            if (type != _adrotatorType) {
                MethodInfo methodInfo = type.GetMethod("OnAdCreated",
                                                       BindingFlags.NonPublic | BindingFlags.Instance,
                                                       null,
                                                       _AdCreatedParameterTypes,
                                                       null);
                if (methodInfo.DeclaringType != _adrotatorType) {
                    result = true;
                }
            }
            return result;
        }

        private AdRec [] LoadFromXmlReader(XmlReader reader) {
            ArrayList adDicts = new ArrayList();

            while (reader.Read()) {
                if (reader.Name == "Advertisements") {
                    if (reader.Depth != 0) {
                        return null;
                    }
                    break;
                }
            }

            while (reader.Read()) {
                if (reader.NodeType == XmlNodeType.Element && reader.Name == "Ad" && reader.Depth == 1) {

                    IDictionary dict = null;
                    reader.Read();
                    while (!(reader.NodeType == XmlNodeType.EndElement)) {
                        if (reader.NodeType == XmlNodeType.Element && !reader.IsEmptyElement) {
                            if (dict == null) {
                                dict = new HybridDictionary();
                            }
                            dict.Add(reader.LocalName, reader.ReadString());
                        }
                        reader.Skip();
                    }

                    if (dict != null) {
                        adDicts.Add(dict);
                    }
                }
            }

            AdRec [] adRecs = SetAdRecs(adDicts);
            return adRecs;
        }

        /// <devdoc>
        ///   Loads the given XML stream into an array of AdRec structures
        /// </devdoc>
        private AdRec [] LoadStream(Stream stream) {

            AdRec [] adRecs = null;
            try {
                // Read the XML stream into an array of dictionaries
                XmlReader reader = XmlUtils.CreateXmlReader(stream);

                // Perf: We use LoadFromXmlReader instead of LoadXmlDocument to
                // do the text parsing only once
                adRecs = LoadFromXmlReader(reader);
            }
            catch (Exception e) {
                throw new HttpException(
                    SR.GetString(SR.AdRotator_parse_error, ID, e.Message), e);
            }

            if (adRecs == null) {
                throw new HttpException(
                    SR.GetString(SR.AdRotator_no_advertisements, ID, AdvertisementFile));
            }

            return adRecs;
        }

        private AdRec [] LoadXmlDocument(XmlDocument doc) {
            // Read the XML data into an array of dictionaries
            ArrayList adDicts = new ArrayList();

            if (doc.DocumentElement != null &&
                doc.DocumentElement.LocalName == XmlDocumentTag) {

                XmlNode elem = doc.DocumentElement.FirstChild;

                while (elem != null) {
                    IDictionary dict = null;
                    if (elem.LocalName.Equals(XmlAdTag)) {
                        XmlNode prop = elem.FirstChild;
                        while (prop != null) {
                            if (prop.NodeType == XmlNodeType.Element) {
                                if (dict == null) {
                                    dict = new HybridDictionary();
                                }
                                dict.Add(prop.LocalName, prop.InnerText);
                            }
                            prop = prop.NextSibling;
                        }
                    }
                    if (dict != null) {
                        adDicts.Add(dict);
                    }
                    elem = elem.NextSibling;
                }
            }

            AdRec [] adRecs = SetAdRecs(adDicts);
            return adRecs;
        }

        /// <devdoc>
        ///   Used to determine if the advertisement meets current criteria. Does a comparison with
        ///   KeywordFilter if it is set.
        /// </devdoc>
        private bool MatchingAd(AdRec adRec, string keywordFilter) {
            Debug.Assert(keywordFilter != null && keywordFilter.Length > 0);
            return(String.Equals(keywordFilter, adRec.keyword, StringComparison.OrdinalIgnoreCase));
        }


        /// <devdoc>
        /// <para>Raises the <see cref='System.Web.UI.WebControls.AdRotator.AdCreated'/> event for an <see cref='System.Web.UI.WebControls.AdRotator'/>.</para>
        /// </devdoc>
        protected virtual void OnAdCreated(AdCreatedEventArgs e) {
            AdCreatedEventHandler handler = (AdCreatedEventHandler)Events[EventAdCreated];
            if (handler != null) handler(this, e);
        }


        protected internal override void OnInit(EventArgs e) {
            base.OnInit(e);

            // VSWhidbey 419600: We just always need binding data every time since
            // AdRotator doesn't store the entire Ad data in ViewState for selecting
            // Ad during postbacks.  It's too big for storing in ViewState.
            RequiresDataBinding = true;
        }

        /// <internalonly/>
        /// <devdoc>
        ///    <para>Gets the advertisement information for rendering in its parameter, then calls
        ///     the OnAdCreated event to render the ads.</para>
        /// </devdoc>
        protected internal override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);

            // If after PreRender (which would call DataBind if DataSource or DataSourceID available)
            // and no _adRecs created, it must be the normal v1 behavior which uses ad file.
            if (_adRecs == null && AdvertisementFile.Length > 0) {
                PerformAdFileBinding();
            }

            // If handler is specified, we don't do any post-cache
            // substitution because the handler code would not be executed.
            //
            // VSWhidbey 213759: We also don't want any post-cache substitution
            // if OnAdCreated has been overridden
            if (Events[EventAdCreated] != null || IsOnAdCreatedOverridden()) {
                // Fire the user event for further customization
                SelectedAdArgs = GetAdCreatedEventArgs();
                OnAdCreated(SelectedAdArgs);
            }
        }

        private void PerformAdFileBinding() {
            // Getting ad data from physical file is V1 way which is not supported
            // by the base class DataBoundControl so we had above code to handle
            // this case.  However, we need to support DataBound control events
            // in Whidbey and since above code doesn't go through the event
            // raising in the base class DataBoundControl, here we mimic them.
            OnDataBinding(EventArgs.Empty);

            // get the ads from the file or app cache
            _adRecs = GetFileData(AdvertisementFile);

            OnDataBound(EventArgs.Empty);
        }

        protected internal override void PerformDataBinding(IEnumerable data) {
            if (data != null) {
                // We retrieve ad data from xml format in a specific way.
                XmlDataSource xmlDataSource = null;
                object dataSource = DataSource;
                if (dataSource != null) {
                    xmlDataSource = dataSource as XmlDataSource;
                }
                else { // DataSourceID case, we know that only one source is available
                    xmlDataSource = GetDataSource() as XmlDataSource;
                }

                if (xmlDataSource != null) {
                    _adRecs = GetXmlDataSourceData(xmlDataSource);
                }
                else {
                    _adRecs = GetDataSourceData(data);
                }
            }
        }

        protected override void PerformSelect() {
            // VSWhidbey 141362
            CheckOnlyOneDataSource();

            if (AdvertisementFile.Length > 0) {
                PerformAdFileBinding();
            }
            else {
                base.PerformSelect();
            }
        }

        // 
        internal AdCreatedEventArgs PickAd() {
            AdCreatedEventArgs adArgs = SelectedAdArgs;
            if (adArgs == null) {
                adArgs = GetAdCreatedEventArgs();
            }
            adArgs.ImageUrl = ResolveAdRotatorUrl(BaseUrl, adArgs.ImageUrl);
            adArgs.NavigateUrl = ResolveAdRotatorUrl(BaseUrl, adArgs.NavigateUrl);
            return adArgs;
        }


        /// <internalonly/>
        /// <devdoc>
        /// <para>Displays the <see cref='System.Web.UI.WebControls.AdRotator'/> on the client.</para>
        /// </devdoc>
        protected internal override void Render(HtmlTextWriter writer) {
            if (!DesignMode && !IsPostCacheAdHelper &&
                DoPostCacheSubstitutionAsNeeded(writer)) {
                return;
            }

            AdCreatedEventArgs adArgs = PickAd();
            RenderLink(writer, adArgs);
        }

        private void RenderLink(HtmlTextWriter writer, AdCreatedEventArgs adArgs) {
            Debug.Assert(writer != null);
            Debug.Assert(adArgs != null);

            HyperLink bannerLink = new HyperLink();

            bannerLink.NavigateUrl = adArgs.NavigateUrl;
            bannerLink.Target = Target;

            if (HasAttributes) {
                foreach(string key in Attributes.Keys) {
                    bannerLink.Attributes[key] = Attributes[key];
                }
            }

            string id = ID;
            if (!String.IsNullOrEmpty(id)) {
                bannerLink.ID = ClientID;
            }

            if (!Enabled) {
                bannerLink.Enabled = false;
            }

            // WebControl's properties use a private flag to determine if a
            // property is set and does not return the value unless the flag is
            // marked.  So here we access those properites (inherited from WebControl)
            // directly from the ViewState bag because if ViewState bag reference
            // was copied to the helper class in the optimized case during the
            // Initialize() method, the flags of the properties wouldn't be set
            // in the helper class.
            string accessKey = (string) ViewState["AccessKey"];
            if (!String.IsNullOrEmpty(accessKey)) {
                bannerLink.AccessKey = accessKey;
            }

            object o = ViewState["TabIndex"];
            if (o != null) {
                short tabIndex = (short) o;
                if (tabIndex != (short) 0) {
                    bannerLink.TabIndex = tabIndex;
                }
            }

            bannerLink.RenderBeginTag(writer);

            // create inner Image
            Image bannerImage = new Image();
            // apply styles to image
            if (ControlStyleCreated) {
                bannerImage.ApplyStyle(ControlStyle);
            }

            string alternateText = adArgs.AlternateText;
            if (!String.IsNullOrEmpty(alternateText)) {
                bannerImage.AlternateText = alternateText;
            }
            else {
                // 25914 Do not render empty 'alt' attribute if <AlternateText> tag is never specified
                IDictionary adProps = adArgs.AdProperties;
                string altTextKey = (AlternateTextField.Length != 0)
                                        ? AlternateTextField : AdCreatedEventArgs.AlternateTextElement;
                string altText = (adProps == null) ? null : (string) adProps[altTextKey];
                if (altText != null && altText.Length == 0) {
                    bannerImage.GenerateEmptyAlternateText = true;
                }
            }

            // Perf work: AdRotator should have resolved the NavigateUrl and
            // ImageUrl when assigning them and have UrlResolved set properly.
            bannerImage.UrlResolved = true;
            string imageUrl = adArgs.ImageUrl;
            if (!String.IsNullOrEmpty(imageUrl)) {
                bannerImage.ImageUrl = imageUrl;
            }

            if (adArgs.HasWidth) {
                bannerImage.ControlStyle.Width = adArgs.Width;
            }

            if (adArgs.HasHeight) {
                bannerImage.ControlStyle.Height = adArgs.Height;
            }

            string toolTip = (string) ViewState["ToolTip"];
            if (!String.IsNullOrEmpty(toolTip)) {
                bannerImage.ToolTip = toolTip;
            }

            bannerImage.RenderControl(writer);
            bannerLink.RenderEndTag(writer);
        }

        private string ResolveAdRotatorUrl(string baseUrl, string relativeUrl) {

            if ((relativeUrl == null) ||
                (relativeUrl.Length == 0) ||
                (UrlPath.IsRelativeUrl(relativeUrl) == false) ||
                (baseUrl == null) ||
                (baseUrl.Length == 0)) {
                return relativeUrl;
            }

            // make it absolute
            return UrlPath.Combine(baseUrl, relativeUrl);
        }

        /// <devdoc>
        ///     <para>Selects an advertisement from the a list of records based
        ///     on different factors.</para>
        /// </devdoc>
        private IDictionary SelectAdFromRecords() {
            if (_adRecs == null || _adRecs.Length == 0) {
                return null;
            }

            string keywordFilter = KeywordFilter;
            bool noKeywordFilter = String.IsNullOrEmpty(keywordFilter);
            if (!noKeywordFilter) {
                // do a lower case comparison
                keywordFilter = keywordFilter.ToLower(CultureInfo.InvariantCulture);
            }

            // sum the matching impressions
            int totalImpressions = 0;
            for (int i = 0; i < _adRecs.Length; i++) {
                if (noKeywordFilter || MatchingAd(_adRecs[i], keywordFilter)) {
                    totalImpressions += _adRecs[i].impressions;
                }
            }

            if (totalImpressions == 0) {
                return null;
            }

            // select one using a random number between 1 and totalImpressions
            int selectedImpression = GetRandomNumber(totalImpressions);
            int impressionCounter = 0;
            int selectedIndex = -1;
            for (int i = 0; i < _adRecs.Length; i++) {
                // Is this the ad?
                if (noKeywordFilter || MatchingAd(_adRecs[i], keywordFilter)) {
                    impressionCounter += _adRecs[i].impressions;
                    if (selectedImpression <= impressionCounter) {
                        selectedIndex = i;
                        break;
                    }
                }
            }
            Debug.Assert(selectedIndex >= 0 && selectedIndex < _adRecs.Length, "Index not found");

            return _adRecs[selectedIndex].adProperties;
        }

        private AdRec [] SetAdRecs(ArrayList adDicts) {
            if (adDicts == null || adDicts.Count == 0) {
                return null;
            }

            // Create an array of AdRec structures from the dictionaries, removing blanks
            AdRec [] adRecs = new AdRec[adDicts.Count];
            int iRec = 0;
            for (int i = 0; i < adDicts.Count; i++) {
                if (adDicts[i] != null) {
                    adRecs[iRec].Initialize((IDictionary) adDicts[i]);
                    iRec++;
                }
            }
            Debug.Assert(iRec == adDicts.Count, "Record count did not match non-null entries");

            return adRecs;
        }



        /// <devdoc>
        ///   Structure to store ads in memory for fast selection by multiple instances of adrotator
        ///   Stores the dictionary and caches some values for easier selection.
        /// </devdoc>
        private struct AdRec {
            public string keyword;
            public int impressions;
            public IDictionary adProperties;


            /// <devdoc>
            ///   Initialize the stuct based on a dictionary containing the advertisement properties
            /// </devdoc>
            public void Initialize(IDictionary adProperties) {

                // Initialize the values we need to keep for ad selection
                Debug.Assert(adProperties != null, "Required here");
                this.adProperties = adProperties;

                // remove null and trim keyword for easier comparisons.
                // VSWhidbey 114634: Be defensive and only retrieve the keyword
                // value if it is in string type
                object keywordValue = adProperties[KeywordProperty];
                if (keywordValue != null && keywordValue is string) {
                    keyword = ((string) keywordValue).Trim();
                }
                else {
                    keyword = string.Empty;
                }

                // get the impressions, but be defensive: let the schema enforce the rules. Default to 1.
                string impressionsString = adProperties[ImpressionsProperty] as string;
                if (String.IsNullOrEmpty(impressionsString) ||
                    !int.TryParse(impressionsString, NumberStyles.Integer,
                                   CultureInfo.InvariantCulture, out impressions)) {
                    impressions = 1;
                }
                if (impressions < 0) {
                    impressions = 1;
                }
            }
        }
    }
}