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;
33 using System.Web.Util;
35 namespace System.Web.UI.WebControls {
38 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 [DefaultEvent("AdCreated")]
42 [DefaultProperty("AdvertisementFile")]
43 [Designer("System.Web.UI.Design.WebControls.AdRotatorDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
45 [ToolboxData("<{0}:AdRotator runat=\"server\"></{0}:AdRotator>")]
47 [ToolboxData("<{0}:AdRotator runat=\"server\" Height=\"60px\" Width=\"468px\"></{0}:AdRotator>")]
49 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);
84 [MonoTODO ("Not implemented")]
85 protected internal override void PerformDataBinding (IEnumerable data)
87 throw new NotImplementedException ();
90 [MonoTODO ("Not implemented")]
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 && e.NavigateUrl.Length > 0)
111 w.AddAttribute (HtmlTextWriterAttribute.Href, ResolveAdUrl (e.NavigateUrl));
112 if (Target != null && Target.Length > 0)
113 w.AddAttribute (HtmlTextWriterAttribute.Target, Target);
115 w.RenderBeginTag (HtmlTextWriterTag.A);
117 if (e.ImageUrl != null && e.ImageUrl.Length > 0)
118 w.AddAttribute (HtmlTextWriterAttribute.Src, ResolveAdUrl (e.ImageUrl));
120 w.AddAttribute (HtmlTextWriterAttribute.Alt, e.AlternateText == null ? "" : e.AlternateText);
121 w.AddAttribute (HtmlTextWriterAttribute.Border, "0");
122 w.RenderBeginTag (HtmlTextWriterTag.Img);
123 w.RenderEndTag (); // img
124 w.RenderEndTag (); // a
127 string ResolveAdUrl (string url)
131 if (AdvertisementFile != null && AdvertisementFile.Length > 0 && path [0] != '/' && path [0] != '~')
136 return UrlUtils.Combine (UrlUtils.GetDirectory (ResolveUrl (AdvertisementFile)), path);
139 return ResolveUrl (path);
143 // We take all the ads in the ad file and add up their
144 // impression weight. We then take a random number [0,
145 // TotalImpressions). We then choose the ad that is
146 // that number or less impressions for the beginning
147 // of the file. This lets us respect the weights
150 Hashtable ChooseAd ()
152 // cache for performance
153 string KeywordFilter = this.KeywordFilter;
158 foreach (Hashtable a in ads) {
159 if (KeywordFilter == "" || KeywordFilter == (string) a ["Keyword"])
160 total_imp += a ["Impressions"] != null ? int.Parse ((string) a ["Impressions"]) : 1;
163 int r = new Random ().Next (total_imp);
165 foreach (Hashtable a in ads) {
166 if (KeywordFilter != "" && KeywordFilter != (string) a ["Keyword"])
168 cur_imp += a ["Impressions"] != null ? int.Parse ((string) a ["Impressions"]) : 1;
175 throw new Exception ("I should only get here if no ads matched");
180 ArrayList ads = new ArrayList ();
182 void ReadAdsFromFile (string s)
184 XmlDocument d = new XmlDocument ();
187 } catch (Exception e) {
188 throw new HttpException ("AdRotator could not parse the xml file", e);
193 foreach (XmlNode n in d.DocumentElement.ChildNodes) {
195 Hashtable ad = new Hashtable ();
197 foreach (XmlNode nn in n.ChildNodes)
198 ad.Add (nn.Name, nn.InnerText);
211 [Editor("System.Web.UI.Design.XmlUrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
212 [WebSysDescription("")]
213 [WebCategory("Behavior")]
214 public string AdvertisementFile {
225 [DefaultValue ("AlternateText")]
226 [WebSysDescriptionAttribute ("")]
227 [WebCategoryAttribute ("Behavior")]
228 [MonoTODO ("Not implemented")]
229 public string AlternateTextField
232 throw new NotImplementedException ();
235 throw new NotImplementedException ();
241 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
242 [EditorBrowsable(EditorBrowsableState.Never)]
243 public override FontInfo Font {
250 [DefaultValue ("ImageUrl")]
251 [MonoTODO ("Not implemented")]
252 [WebSysDescriptionAttribute ("")]
253 [WebCategoryAttribute ("Behavior")]
254 public string ImageUrlField
257 throw new NotImplementedException ();
260 throw new NotImplementedException ();
268 [WebSysDescription("")]
269 [WebCategory("Behavior")]
270 public string KeywordFilter {
272 return ViewState.GetString ("KeywordFilter", "");
275 ViewState ["KeywordFilter"] = value;
281 [DefaultValue ("NavigateUrl")]
282 [MonoTODO ("Not implemented")]
283 [WebSysDescriptionAttribute ("")]
284 [WebCategoryAttribute ("Behavior")]
285 public string NavigateUrlField
288 throw new NotImplementedException ();
291 throw new NotImplementedException ();
297 [DefaultValue("_top")]
298 [TypeConverter(typeof(System.Web.UI.WebControls.TargetConverter))]
299 [WebSysDescription("")]
300 [WebCategory("Behavior")]
301 public string Target {
303 return ViewState.GetString ("Target", "_top");
306 ViewState ["Target"] = value;
312 /* all these are listed in corcompare */
313 public override string UniqueID
316 return base.UniqueID;
320 protected override HtmlTextWriterTag TagKey
328 protected virtual void OnAdCreated (AdCreatedEventArgs e)
330 AdCreatedEventHandler h = (AdCreatedEventHandler) Events [AdCreatedEvent];
335 static readonly object AdCreatedEvent = new object ();
337 [WebSysDescription("")]
338 [WebCategory("Action")]
339 public event AdCreatedEventHandler AdCreated {
340 add { Events.AddHandler (AdCreatedEvent, value); }
341 remove { Events.RemoveHandler (AdCreatedEvent, value); }