AdRotator.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // System.Web.UI.WebControls.AdRotator
  3. //
  4. // Author:
  5. // Ben Maurer <[email protected]>
  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. using System.Xml;
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.Security.Permissions;
  32. using System.Web.Util;
  33. namespace System.Web.UI.WebControls {
  34. // CAS
  35. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. // attributes
  38. [DefaultEvent("AdCreated")]
  39. [DefaultProperty("AdvertisementFile")]
  40. [Designer("System.Web.UI.Design.WebControls.AdRotatorDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  41. #if NET_2_0
  42. [ToolboxData("<{0}:AdRotator runat=\"server\"></{0}:AdRotator>")]
  43. #else
  44. [ToolboxData("<{0}:AdRotator runat=\"server\" Height=\"60px\" Width=\"468px\"></{0}:AdRotator>")]
  45. #endif
  46. public class AdRotator :
  47. #if NET_2_0
  48. DataBoundControl
  49. #else
  50. WebControl
  51. #endif
  52. {
  53. #if NET_2_0
  54. protected internal override void OnInit (EventArgs e)
  55. {
  56. base.OnInit(e);
  57. }
  58. #endif
  59. #if NET_2_0
  60. protected internal
  61. #else
  62. protected
  63. #endif
  64. override void OnPreRender (EventArgs eee)
  65. {
  66. Hashtable ht = null;
  67. if (ad_file != "" && ad_file != null) {
  68. ReadAdsFromFile (Page.MapPath (ad_file));
  69. ht = ChooseAd ();
  70. }
  71. AdCreatedEventArgs e = new AdCreatedEventArgs (ht);
  72. OnAdCreated (e);
  73. createdargs = e;
  74. }
  75. #if NET_2_0
  76. [MonoTODO ("Not implemented")]
  77. protected internal override void PerformDataBinding (IEnumerable data)
  78. {
  79. throw new NotImplementedException ();
  80. }
  81. [MonoTODO ("Not implemented")]
  82. protected override void PerformSelect ()
  83. {
  84. throw new NotImplementedException ();
  85. }
  86. #endif
  87. AdCreatedEventArgs createdargs;
  88. #if NET_2_0
  89. protected internal
  90. #else
  91. protected
  92. #endif
  93. override void Render (HtmlTextWriter w)
  94. {
  95. AdCreatedEventArgs e = createdargs;
  96. base.AddAttributesToRender (w);
  97. if (e.NavigateUrl != null && e.NavigateUrl.Length > 0)
  98. w.AddAttribute (HtmlTextWriterAttribute.Href, ResolveAdUrl (e.NavigateUrl));
  99. if (Target != null && Target.Length > 0)
  100. w.AddAttribute (HtmlTextWriterAttribute.Target, Target);
  101. w.RenderBeginTag (HtmlTextWriterTag.A);
  102. if (e.NavigateUrl != null && e.NavigateUrl.Length > 0)
  103. w.AddAttribute (HtmlTextWriterAttribute.Src, ResolveAdUrl (e.ImageUrl));
  104. w.AddAttribute (HtmlTextWriterAttribute.Alt, e.AlternateText == null ? "" : e.AlternateText);
  105. w.AddAttribute (HtmlTextWriterAttribute.Border, "0");
  106. w.RenderBeginTag (HtmlTextWriterTag.Img);
  107. w.RenderEndTag (); // img
  108. w.RenderEndTag (); // a
  109. }
  110. string ResolveAdUrl (string url)
  111. {
  112. string path = url;
  113. if (AdvertisementFile != null && AdvertisementFile.Length > 0 && path [0] != '/' && path [0] != '~')
  114. return UrlUtils.Combine (UrlUtils.GetDirectory (ResolveUrl (AdvertisementFile)), path);
  115. return ResolveUrl (path);
  116. }
  117. //
  118. // We take all the ads in the ad file and add up their
  119. // impression weight. We then take a random number [0,
  120. // TotalImpressions). We then choose the ad that is
  121. // that number or less impressions for the beginning
  122. // of the file. This lets us respect the weights
  123. // given.
  124. //
  125. Hashtable ChooseAd ()
  126. {
  127. // cache for performance
  128. string KeywordFilter = this.KeywordFilter;
  129. int total_imp = 0;
  130. int cur_imp = 0;
  131. foreach (Hashtable a in ads) {
  132. if (KeywordFilter == "" || KeywordFilter == (string) a ["Keyword"])
  133. total_imp += a ["Impressions"] != null ? int.Parse ((string) a ["Impressions"]) : 1;
  134. }
  135. int r = new Random ().Next (total_imp);
  136. foreach (Hashtable a in ads) {
  137. if (KeywordFilter != "" && KeywordFilter != (string) a ["Keyword"])
  138. continue;
  139. cur_imp += a ["Impressions"] != null ? int.Parse ((string) a ["Impressions"]) : 1;
  140. if (cur_imp > r)
  141. return a;
  142. }
  143. if (total_imp != 0)
  144. throw new Exception ("I should only get here if no ads matched");
  145. return null;
  146. }
  147. ArrayList ads = new ArrayList ();
  148. void ReadAdsFromFile (string s)
  149. {
  150. XmlDocument d = new XmlDocument ();
  151. try {
  152. d.Load (s);
  153. } catch (Exception e) {
  154. throw new HttpException ("AdRotator could not parse the xml file", e);
  155. }
  156. ads.Clear ();
  157. foreach (XmlNode n in d.DocumentElement.ChildNodes) {
  158. Hashtable ad = new Hashtable ();
  159. foreach (XmlNode nn in n.ChildNodes)
  160. ad.Add (nn.Name, nn.InnerText);
  161. ads.Add (ad);
  162. }
  163. }
  164. string ad_file = "";
  165. #if NET_2_0
  166. [UrlProperty]
  167. #endif
  168. [Bindable(true)]
  169. [DefaultValue("")]
  170. [Editor("System.Web.UI.Design.XmlUrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
  171. [WebSysDescription("")]
  172. [WebCategory("Behavior")]
  173. public string AdvertisementFile {
  174. get {
  175. return ad_file;
  176. }
  177. set {
  178. ad_file = value;
  179. }
  180. }
  181. #if NET_2_0
  182. [DefaultValue ("AlternateText")]
  183. [WebSysDescriptionAttribute ("")]
  184. [WebCategoryAttribute ("Behavior")]
  185. [MonoTODO ("Not implemented")]
  186. public string AlternateTextField
  187. {
  188. get {
  189. throw new NotImplementedException ();
  190. }
  191. set {
  192. throw new NotImplementedException ();
  193. }
  194. }
  195. #endif
  196. [Browsable(false)]
  197. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  198. [EditorBrowsable(EditorBrowsableState.Never)]
  199. public override FontInfo Font {
  200. get {
  201. return base.Font;
  202. }
  203. }
  204. #if NET_2_0
  205. [DefaultValue ("ImageUrl")]
  206. [MonoTODO ("Not implemented")]
  207. [WebSysDescriptionAttribute ("")]
  208. [WebCategoryAttribute ("Behavior")]
  209. public string ImageUrlField
  210. {
  211. get {
  212. throw new NotImplementedException ();
  213. }
  214. set {
  215. throw new NotImplementedException ();
  216. }
  217. }
  218. #endif
  219. [Bindable(true)]
  220. [DefaultValue("")]
  221. [WebSysDescription("")]
  222. [WebCategory("Behavior")]
  223. public string KeywordFilter {
  224. get {
  225. return ViewState.GetString ("KeywordFilter", "");
  226. }
  227. set {
  228. ViewState ["KeywordFilter"] = value;
  229. }
  230. }
  231. #if NET_2_0
  232. [DefaultValue ("NavigateUrl")]
  233. [MonoTODO ("Not implemented")]
  234. [WebSysDescriptionAttribute ("")]
  235. [WebCategoryAttribute ("Behavior")]
  236. public string NavigateUrlField
  237. {
  238. get {
  239. throw new NotImplementedException ();
  240. }
  241. set {
  242. throw new NotImplementedException ();
  243. }
  244. }
  245. #endif
  246. [Bindable(true)]
  247. [DefaultValue("_top")]
  248. [TypeConverter(typeof(System.Web.UI.WebControls.TargetConverter))]
  249. [WebSysDescription("")]
  250. [WebCategory("Behavior")]
  251. public string Target {
  252. get {
  253. return ViewState.GetString ("Target", "_top");
  254. }
  255. set {
  256. ViewState ["Target"] = value;
  257. }
  258. }
  259. #if NET_2_0
  260. /* all these are listed in corcompare */
  261. public override string UniqueID
  262. {
  263. get {
  264. return base.UniqueID;
  265. }
  266. }
  267. protected override HtmlTextWriterTag TagKey
  268. {
  269. get {
  270. return base.TagKey;
  271. }
  272. }
  273. #endif
  274. protected virtual void OnAdCreated (AdCreatedEventArgs e)
  275. {
  276. AdCreatedEventHandler h = (AdCreatedEventHandler) Events [AdCreatedEvent];
  277. if (h != null)
  278. h (this, e);
  279. }
  280. static readonly object AdCreatedEvent = new object ();
  281. [WebSysDescription("")]
  282. [WebCategory("Action")]
  283. public event AdCreatedEventHandler AdCreated {
  284. add { Events.AddHandler (AdCreatedEvent, value); }
  285. remove { Events.RemoveHandler (AdCreatedEvent, value); }
  286. }
  287. }
  288. }