AdRotator.cs 7.9 KB

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