New test.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / AdRotator.cs
1 // 
2 // System.Web.UI.WebControls.AdRotator
3 //
4 // Author:
5 //        Ben Maurer <bmaurer@novell.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.Xml;
30 using System.Collections;
31 using System.ComponentModel;
32 using System.Security.Permissions;
33
34 namespace System.Web.UI.WebControls {
35
36         // CAS
37         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         // attributes
40         [DefaultEvent("AdCreated")]
41         [DefaultProperty("AdvertisementFile")]
42         [Designer("System.Web.UI.Design.WebControls.AdRotatorDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43 #if NET_2_0
44         [ToolboxData("<{0}:AdRotator runat=\"server\"></{0}:AdRotator>")]
45 #else
46         [ToolboxData("<{0}:AdRotator runat=\"server\" Height=\"60px\" Width=\"468px\"></{0}:AdRotator>")]
47 #endif          
48         public class AdRotator :
49 #if NET_2_0
50         DataBoundControl
51 #else           
52         WebControl
53 #endif  
54         {
55 #if NET_2_0
56                 protected internal override void OnInit (EventArgs e)
57                 {
58                         base.OnInit(e);
59                 }
60 #endif
61                 
62 #if NET_2_0
63                 protected internal
64 #else           
65                 protected
66 #endif          
67                 override void OnPreRender (EventArgs eee)
68                 {
69                         Hashtable ht = null;
70                         
71                         if (ad_file != "" && ad_file != null) {
72                                 ReadAdsFromFile (Page.MapPath (ad_file));
73                                 ht = ChooseAd ();
74                         }
75
76                         AdCreatedEventArgs e = new AdCreatedEventArgs (ht);
77                         OnAdCreated (e);
78                         createdargs = e;
79                         
80                 }
81
82 #if NET_2_0
83                 [MonoTODO ("Not implemented")]
84                 protected internal override void PerformDataBinding (IEnumerable data)
85                 {
86                         throw new NotImplementedException ();
87                 }
88
89                 [MonoTODO ("Not implemented")]
90                 protected override void PerformSelect ()
91                 {
92                         throw new NotImplementedException ();
93                 }
94 #endif          
95
96                 AdCreatedEventArgs createdargs;
97
98 #if NET_2_0
99                 protected internal
100 #else           
101                 protected
102 #endif          
103                 override void Render (HtmlTextWriter w)
104                 {
105                         AdCreatedEventArgs e = createdargs;
106
107                         base.AddAttributesToRender (w);
108                         
109                         if (e.NavigateUrl != null)
110                                 w.AddAttribute (HtmlTextWriterAttribute.Href, ResolveClientUrl (e.NavigateUrl));
111                         w.AddAttribute (HtmlTextWriterAttribute.Target, Target);
112                         
113                         w.RenderBeginTag (HtmlTextWriterTag.A);
114
115                         if (e.NavigateUrl != null)
116                                 w.AddAttribute (HtmlTextWriterAttribute.Src, ResolveClientUrl (e.ImageUrl));
117                         
118                         w.AddAttribute (HtmlTextWriterAttribute.Alt, e.AlternateText == null ? "" : e.AlternateText);
119                         w.AddAttribute (HtmlTextWriterAttribute.Border, "0");
120                         w.RenderBeginTag (HtmlTextWriterTag.Img);
121                         w.RenderEndTag (); // img
122                         w.RenderEndTag (); // a
123                 }
124
125                 //
126                 // We take all the ads in the ad file and add up their
127                 // impression weight. We then take a random number [0,
128                 // TotalImpressions). We then choose the ad that is
129                 // that number or less impressions for the beginning
130                 // of the file. This lets us respect the weights
131                 // given.
132                 //
133                 Hashtable ChooseAd ()
134                 {
135                         // cache for performance
136                         string KeywordFilter = this.KeywordFilter;
137                         
138                         int total_imp = 0;
139                         int cur_imp = 0;
140                         
141                         foreach (Hashtable a in ads) {
142                                 if (KeywordFilter == "" || KeywordFilter == (string) a ["Keyword"])
143                                         total_imp += a ["Impressions"] != null ? int.Parse ((string) a ["Impressions"]) : 1;
144                         }
145
146                         int r = new Random ().Next (total_imp);
147
148                         foreach (Hashtable a in ads) {
149                                 if (KeywordFilter != "" && KeywordFilter != (string) a ["Keyword"])
150                                         continue;
151                                 cur_imp += a ["Impressions"] != null ? int.Parse ((string) a ["Impressions"]) : 1;
152                                 
153                                 if (cur_imp > r)
154                                         return a;
155                         }
156
157                         if (total_imp != 0)
158                                 throw new Exception ("I should only get here if no ads matched");
159                         
160                         return null;
161                 }
162
163                 ArrayList ads = new ArrayList ();
164                 
165                 void ReadAdsFromFile (string s)
166                 {
167                         XmlDocument d = new XmlDocument ();
168                         try {
169                                 d.Load (s);
170                         } catch (Exception e) {
171                                 throw new HttpException ("AdRotator could not parse the xml file", e);
172                         }
173                         
174                         ads.Clear ();
175                         
176                         foreach (XmlNode n in d.DocumentElement.ChildNodes) {
177
178                                 Hashtable ad = new Hashtable ();
179                                 
180                                 foreach (XmlNode nn in n.ChildNodes)
181                                         ad.Add (nn.Name, nn.InnerText);
182                                 
183                                 ads.Add (ad);
184                         }
185                 }
186                 
187                 string ad_file = "";
188
189 #if NET_2_0
190                 [UrlProperty]
191 #endif          
192                 [Bindable(true)]
193                 [DefaultValue("")]
194                 [Editor("System.Web.UI.Design.XmlUrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
195                 [WebSysDescription("")]
196                 [WebCategory("Behavior")]
197                 public string AdvertisementFile {
198                         get {
199                                 return ad_file;
200                         }
201                         set {
202                                 ad_file = value;
203                         }
204                         
205                 }
206
207 #if NET_2_0
208                 [DefaultValue ("AlternateText")]
209                 [WebSysDescriptionAttribute ("")]
210                 [WebCategoryAttribute ("Behavior")]
211                 [MonoTODO ("Not implemented")]
212                 public string AlternateTextField 
213                 {
214                         get {
215                                 throw new NotImplementedException ();
216                         }
217                         set {
218                                 throw new NotImplementedException ();
219                         }
220                 }
221 #endif          
222                 
223                 [Browsable(false)]
224                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
225                 [EditorBrowsable(EditorBrowsableState.Never)]
226                 public override FontInfo Font {
227                         get {
228                                 return base.Font;
229                         }
230                 }
231
232 #if NET_2_0
233                 [DefaultValue ("ImageUrl")]
234                 [MonoTODO ("Not implemented")]
235                 [WebSysDescriptionAttribute ("")]
236                 [WebCategoryAttribute ("Behavior")]
237                 public string ImageUrlField 
238                 {
239                         get {
240                                 throw new NotImplementedException ();
241                         }
242                         set {
243                                 throw new NotImplementedException ();
244                         }
245                 }
246 #endif          
247                 
248
249                 [Bindable(true)]
250                 [DefaultValue("")]
251                 [WebSysDescription("")]
252                 [WebCategory("Behavior")]
253                 public string KeywordFilter {
254                         get {
255                                 return ViewState.GetString ("KeywordFilter", "");
256                         }
257                         set {
258                                 ViewState ["KeywordFilter"] = value;
259                         }
260                         
261                 }
262
263 #if NET_2_0
264                 [DefaultValue ("NavigateUrl")]
265                 [MonoTODO ("Not implemented")]
266                 [WebSysDescriptionAttribute ("")]
267                 [WebCategoryAttribute ("Behavior")]
268                 public string NavigateUrlField 
269                 {
270                         get {
271                                 throw new NotImplementedException ();
272                         }
273                         set {
274                                 throw new NotImplementedException ();
275                         }
276                 }
277 #endif          
278                 
279                 [Bindable(true)]
280                 [DefaultValue("_top")]
281                 [TypeConverter(typeof(System.Web.UI.WebControls.TargetConverter))]
282                 [WebSysDescription("")]
283                 [WebCategory("Behavior")]
284                 public string Target {
285                         get {
286                                 return ViewState.GetString ("Target", "_top");
287                         }
288                         set {
289                                 ViewState ["Target"] = value;
290                         }
291                         
292                 }
293
294 #if NET_2_0
295                 /* all these are listed in corcompare */
296                 public override string UniqueID
297                 {
298                         get {
299                                 return base.UniqueID;
300                         }
301                 }
302
303                 protected override HtmlTextWriterTag TagKey 
304                 {
305                         get {
306                                 return base.TagKey;
307                         }
308                 }
309 #endif          
310         
311                 protected virtual void OnAdCreated (AdCreatedEventArgs e)
312                 {
313                         AdCreatedEventHandler h = (AdCreatedEventHandler) Events [AdCreatedEvent];
314                         if (h != null)
315                                 h (this, e);
316                 }
317
318                 static readonly object AdCreatedEvent = new object ();
319
320                 [WebSysDescription("")]
321                 [WebCategory("Action")]
322                 public event AdCreatedEventHandler AdCreated {
323                         add { Events.AddHandler (AdCreatedEvent, value); }
324                         remove { Events.RemoveHandler (AdCreatedEvent, value); }
325                 }
326         }
327 }