2 // System.Web.UI.WebControls.AdRotator
5 // Ben Maurer <bmaurer@novell.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
31 using System.ComponentModel;
32 using System.Security.Permissions;
34 namespace System.Web.UI.WebControls {
37 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 [DefaultEvent("AdCreated")]
41 [DefaultProperty("AdvertisementFile")]
42 [Designer("System.Web.UI.Design.WebControls.AdRotatorDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
44 [ToolboxData("<{0}:AdRotator runat=\"server\"></{0}:AdRotator>")]
46 [ToolboxData("<{0}:AdRotator runat=\"server\" Height=\"60px\" Width=\"468px\"></{0}:AdRotator>")]
48 public class AdRotator :
56 protected internal override void OnInit (EventArgs e)
67 override void OnPreRender (EventArgs eee)
71 if (ad_file != "" && ad_file != null) {
72 ReadAdsFromFile (Page.MapPath (ad_file));
76 AdCreatedEventArgs e = new AdCreatedEventArgs (ht);
83 [MonoTODO ("Not implemented")]
84 protected internal override void PerformDataBinding (IEnumerable data)
86 throw new NotImplementedException ();
89 [MonoTODO ("Not implemented")]
90 protected override void PerformSelect ()
92 throw new NotImplementedException ();
96 AdCreatedEventArgs createdargs;
103 override void Render (HtmlTextWriter w)
105 AdCreatedEventArgs e = createdargs;
107 base.AddAttributesToRender (w);
109 if (e.NavigateUrl != null)
110 w.AddAttribute (HtmlTextWriterAttribute.Href, ResolveClientUrl (e.NavigateUrl));
111 w.AddAttribute (HtmlTextWriterAttribute.Target, Target);
113 w.RenderBeginTag (HtmlTextWriterTag.A);
115 if (e.NavigateUrl != null)
116 w.AddAttribute (HtmlTextWriterAttribute.Src, ResolveClientUrl (e.ImageUrl));
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
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
133 Hashtable ChooseAd ()
135 // cache for performance
136 string KeywordFilter = this.KeywordFilter;
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;
146 int r = new Random ().Next (total_imp);
148 foreach (Hashtable a in ads) {
149 if (KeywordFilter != "" && KeywordFilter != (string) a ["Keyword"])
151 cur_imp += a ["Impressions"] != null ? int.Parse ((string) a ["Impressions"]) : 1;
158 throw new Exception ("I should only get here if no ads matched");
163 ArrayList ads = new ArrayList ();
165 void ReadAdsFromFile (string s)
167 XmlDocument d = new XmlDocument ();
170 } catch (Exception e) {
171 throw new HttpException ("AdRotator could not parse the xml file", e);
176 foreach (XmlNode n in d.DocumentElement.ChildNodes) {
178 Hashtable ad = new Hashtable ();
180 foreach (XmlNode nn in n.ChildNodes)
181 ad.Add (nn.Name, nn.InnerText);
194 [Editor("System.Web.UI.Design.XmlUrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
195 [WebSysDescription("")]
196 [WebCategory("Behavior")]
197 public string AdvertisementFile {
208 [DefaultValue ("AlternateText")]
209 [WebSysDescriptionAttribute ("")]
210 [WebCategoryAttribute ("Behavior")]
211 [MonoTODO ("Not implemented")]
212 public string AlternateTextField
215 throw new NotImplementedException ();
218 throw new NotImplementedException ();
224 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
225 [EditorBrowsable(EditorBrowsableState.Never)]
226 public override FontInfo Font {
233 [DefaultValue ("ImageUrl")]
234 [MonoTODO ("Not implemented")]
235 [WebSysDescriptionAttribute ("")]
236 [WebCategoryAttribute ("Behavior")]
237 public string ImageUrlField
240 throw new NotImplementedException ();
243 throw new NotImplementedException ();
251 [WebSysDescription("")]
252 [WebCategory("Behavior")]
253 public string KeywordFilter {
255 return ViewState.GetString ("KeywordFilter", "");
258 ViewState ["KeywordFilter"] = value;
264 [DefaultValue ("NavigateUrl")]
265 [MonoTODO ("Not implemented")]
266 [WebSysDescriptionAttribute ("")]
267 [WebCategoryAttribute ("Behavior")]
268 public string NavigateUrlField
271 throw new NotImplementedException ();
274 throw new NotImplementedException ();
280 [DefaultValue("_top")]
281 [TypeConverter(typeof(System.Web.UI.WebControls.TargetConverter))]
282 [WebSysDescription("")]
283 [WebCategory("Behavior")]
284 public string Target {
286 return ViewState.GetString ("Target", "_top");
289 ViewState ["Target"] = value;
295 /* all these are listed in corcompare */
296 public override string UniqueID
299 return base.UniqueID;
303 protected override HtmlTextWriterTag TagKey
311 protected virtual void OnAdCreated (AdCreatedEventArgs e)
313 AdCreatedEventHandler h = (AdCreatedEventHandler) Events [AdCreatedEvent];
318 static readonly object AdCreatedEvent = new object ();
320 [WebSysDescription("")]
321 [WebCategory("Action")]
322 public event AdCreatedEventHandler AdCreated {
323 add { Events.AddHandler (AdCreatedEvent, value); }
324 remove { Events.RemoveHandler (AdCreatedEvent, value); }