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 :
57 protected internal override void OnInit (EventArgs e)
68 override void OnPreRender (EventArgs eee)
72 if (ad_file != "" && ad_file != null) {
73 ReadAdsFromFile (Page.MapPath (ad_file));
77 AdCreatedEventArgs e = new AdCreatedEventArgs (ht);
85 protected internal override void PerformDataBinding (IEnumerable data)
87 throw new NotImplementedException ();
91 protected override void PerformSelect ()
93 throw new NotImplementedException ();
97 AdCreatedEventArgs createdargs;
104 override void Render (HtmlTextWriter w)
106 AdCreatedEventArgs e = createdargs;
108 base.AddAttributesToRender (w);
110 if (e.NavigateUrl != null)
111 w.AddAttribute (HtmlTextWriterAttribute.Href, ResolveUrl (e.NavigateUrl));
112 w.AddAttribute (HtmlTextWriterAttribute.Target, Target);
114 w.RenderBeginTag (HtmlTextWriterTag.A);
116 if (e.NavigateUrl != null)
117 w.AddAttribute (HtmlTextWriterAttribute.Src, ResolveUrl (e.ImageUrl));
119 w.AddAttribute (HtmlTextWriterAttribute.Alt, e.AlternateText == null ? "" : e.AlternateText);
120 w.AddAttribute (HtmlTextWriterAttribute.Border, "0");
121 w.RenderBeginTag (HtmlTextWriterTag.Img);
122 w.RenderEndTag (); // img
123 w.RenderEndTag (); // a
127 // We take all the ads in the ad file and add up their
128 // impression weight. We then take a random number [0,
129 // TotalImpressions). We then choose the ad that is
130 // that number or less impressions for the beginning
131 // of the file. This lets us respect the weights
134 Hashtable ChooseAd ()
136 // cache for performance
137 string KeywordFilter = this.KeywordFilter;
142 foreach (Hashtable a in ads) {
143 if (KeywordFilter == "" || KeywordFilter == (string) a ["Keyword"])
144 total_imp += a ["Impressions"] != null ? int.Parse ((string) a ["Impressions"]) : 1;
147 int r = new Random ().Next (total_imp);
149 foreach (Hashtable a in ads) {
150 if (KeywordFilter != "" && KeywordFilter != (string) a ["Keyword"])
152 cur_imp += a ["Impressions"] != null ? int.Parse ((string) a ["Impressions"]) : 1;
159 throw new Exception ("I should only get here if no ads matched");
164 ArrayList ads = new ArrayList ();
166 void ReadAdsFromFile (string s)
168 XmlDocument d = new XmlDocument ();
171 } catch (Exception e) {
172 throw new HttpException ("AdRotator could not parse the xml file", e);
177 foreach (XmlNode n in d.DocumentElement.ChildNodes) {
179 Hashtable ad = new Hashtable ();
181 foreach (XmlNode nn in n.ChildNodes)
182 ad.Add (nn.Name, nn.InnerText);
195 [Editor("System.Web.UI.Design.XmlUrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
196 [WebSysDescription("")]
197 [WebCategory("Behavior")]
198 public string AdvertisementFile {
209 [DefaultValue ("AlternateText")]
210 [WebSysDescriptionAttribute ("")]
211 [WebCategoryAttribute ("Behavior")]
213 public string AlternateTextField
216 throw new NotImplementedException ();
219 throw new NotImplementedException ();
225 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
226 [EditorBrowsable(EditorBrowsableState.Never)]
227 public override FontInfo Font {
234 [DefaultValue ("ImageUrl")]
236 [WebSysDescriptionAttribute ("")]
237 [WebCategoryAttribute ("Behavior")]
238 public string ImageUrlField
241 throw new NotImplementedException ();
244 throw new NotImplementedException ();
252 [WebSysDescription("")]
253 [WebCategory("Behavior")]
254 public string KeywordFilter {
256 return ViewState.GetString ("KeywordFilter", "");
259 ViewState ["KeywordFilter"] = value;
265 [DefaultValue ("NavigateUrl")]
267 [WebSysDescriptionAttribute ("")]
268 [WebCategoryAttribute ("Behavior")]
269 public string NavigateUrlField
272 throw new NotImplementedException ();
275 throw new NotImplementedException ();
281 [DefaultValue("_top")]
282 [TypeConverter(typeof(System.Web.UI.WebControls.TargetConverter))]
283 [WebSysDescription("")]
284 [WebCategory("Behavior")]
285 public string Target {
287 return ViewState.GetString ("Target", "_top");
290 ViewState ["Target"] = value;
296 /* all these are listed in corcompare */
298 public override string UniqueID
301 return base.UniqueID;
306 protected override HtmlTextWriterTag TagKey
314 protected virtual void OnAdCreated (AdCreatedEventArgs e)
316 AdCreatedEventHandler h = (AdCreatedEventHandler) Events [AdCreatedEvent];
321 static readonly object AdCreatedEvent = new object ();
323 [WebSysDescription("")]
324 [WebCategory("Action")]
325 public event AdCreatedEventHandler AdCreated {
326 add { Events.AddHandler (AdCreatedEvent, value); }
327 remove { Events.RemoveHandler (AdCreatedEvent, value); }