2
0

AdRotator.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: AdRotator
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Implementation: yes
  8. * Contact: <[email protected]>
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.IO;
  15. using System.Collections;
  16. using System.Collections.Specialized;
  17. using System.Web;
  18. using System.Web.Caching;
  19. using System.Web.UI;
  20. using System.Xml;
  21. using System.Web.Utils;
  22. using System.ComponentModel;
  23. namespace System.Web.UI.WebControls
  24. {
  25. [DefaultEvent("AdCreated")]
  26. [DefaultProperty("AdvertisementFile")]
  27. //TODO: [Designer("??")]
  28. [ToolboxData("<{0}:AdRotator runat=\"server\" Height=\"60px\" "
  29. + "Width=\"468\"></{0}:AdRotator>")]
  30. public class AdRotator: WebControl
  31. {
  32. private string advertisementFile;
  33. private static readonly object AdCreatedEvent = new object();
  34. // Will be set values during (On)PreRender-ing
  35. private string alternateText;
  36. private string imageUrl;
  37. private string navigateUrl;
  38. private string fileDirectory;
  39. class AdRecord
  40. {
  41. public IDictionary adProps;
  42. public int hits; // or impressions or clicks
  43. public string keyword;
  44. public AdRecord(IDictionary adProps)
  45. {
  46. this.adProps = adProps;
  47. hits = 0;
  48. keyword = String.Empty;
  49. }
  50. }
  51. /*
  52. * Loading / Saving data from/to ad file and all the manipulations wrt to the URL...
  53. * are incorporated by the following functions.
  54. * GetData(string)
  55. * LoadAdFile(string)
  56. * IsAdMatching(AdRecord)
  57. * ResolveAdUrl(string)
  58. * SelectAd()
  59. * The exact control flow will be detailed. Let me first write the functions
  60. */
  61. private AdRecord[] LoadAdFile(string file)
  62. {
  63. Stream fStream;
  64. ArrayList list;
  65. XmlReader reader;
  66. XmlDocument document;
  67. XmlNode topNode, innerNode;
  68. IDictionary hybridDict = null;
  69. AdRecord[] adsArray = null;
  70. try
  71. {
  72. fStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
  73. } catch(Exception e)
  74. {
  75. throw new HttpException("AdRotator: Unable to open file");
  76. }
  77. try
  78. {
  79. list = new ArrayList();
  80. reader = new XmlTextReader(fStream);
  81. document = new XmlDocument();
  82. document.Load(reader);
  83. if(document.DocumentElement!=null)
  84. {
  85. if(document.DocumentElement.LocalName=="Advertisements")
  86. {
  87. topNode = document.DocumentElement.FirstChild;
  88. while(topNode!=null)
  89. {
  90. if(topNode.LocalName=="Ad")
  91. {
  92. innerNode = topNode.FirstChild;
  93. while(innerNode!=null)
  94. {
  95. if(innerNode.NodeType==XmlNodeType.Element)
  96. {
  97. if(hybridDict==null)
  98. {
  99. hybridDict = new HybridDictionary();
  100. }
  101. hybridDict.Add(innerNode.LocalName, innerNode.InnerText);
  102. }
  103. innerNode = innerNode.NextSibling;
  104. }
  105. if (hybridDict!=null) {
  106. list.Add (hybridDict);
  107. hybridDict = null;
  108. }
  109. }
  110. topNode = topNode.NextSibling;
  111. }
  112. }
  113. }
  114. if(list.Count>0)
  115. {
  116. adsArray = new AdRecord[list.Count];
  117. for(int i=0; i < list.Count; i++)
  118. {
  119. adsArray[i] = new AdRecord((IDictionary)list[i]);
  120. }
  121. }
  122. } catch(Exception e)
  123. {
  124. throw new HttpException("AdRotator_Parse_Error" + file);
  125. } finally
  126. {
  127. fStream.Close();
  128. }
  129. if(adsArray == null)
  130. {
  131. throw new HttpException("AdRotator_No_Advertisements_Found");
  132. }
  133. return adsArray;
  134. }
  135. [MonoTODO("CacheDependency does nothing")]
  136. private AdRecord[] GetData(string file)
  137. {
  138. string physPath = MapPathSecure(file);
  139. string AdKey = "AdRotatorCache: " + physPath;
  140. fileDirectory = UrlUtils.GetDirectory(UrlUtils.Combine(TemplateSourceDirectory, file));
  141. Cache cache = HttpRuntime.Cache;
  142. AdRecord[] records = (AdRecord[])cache[AdKey];
  143. if(records==null)
  144. {
  145. records = LoadAdFile(physPath);
  146. if(records==null)
  147. {
  148. return null;
  149. }
  150. cache.Insert(AdKey, records, new CacheDependency(physPath));
  151. }
  152. return records;
  153. }
  154. private IDictionary SelectAd()
  155. {
  156. AdRecord[] records = GetData(AdvertisementFile);
  157. if(records!=null && records.Length!=0)
  158. {
  159. int impressions = 0;
  160. for(int i=0 ; i < records.Length; i++)
  161. {
  162. if(IsAdMatching(records[i]))
  163. impressions += records[i].hits;
  164. }
  165. if(impressions!=0)
  166. {
  167. int rnd = (new Random()).Next(impressions) + 1;
  168. int counter = 0;
  169. int index = 0;
  170. for(int i=0; i < records.Length; i++)
  171. {
  172. if(IsAdMatching(records[i]))
  173. {
  174. if(rnd <= (counter + records[i].hits))
  175. {
  176. index = i;
  177. break;
  178. }
  179. counter += records[i].hits;
  180. }
  181. }
  182. return records[index].adProps;
  183. }
  184. //FIXME: the line below added. Where should i init hits to make
  185. //impressions not be 0?
  186. return records [0].adProps;
  187. }
  188. return null;
  189. }
  190. private bool IsAdMatching(AdRecord currAd)
  191. {
  192. if (KeywordFilter != String.Empty)
  193. return (0 == String.Compare (currAd.keyword, KeywordFilter, true));
  194. return true;
  195. }
  196. private string ResolveAdUrl(string relativeUrl)
  197. {
  198. if(relativeUrl.Length==0 || !UrlUtils.IsRelativeUrl(relativeUrl))
  199. return relativeUrl;
  200. string fullUrl = String.Empty;
  201. if(fileDirectory != null)
  202. fullUrl = fileDirectory;
  203. if(fullUrl.Length == 0)
  204. fullUrl = TemplateSourceDirectory;
  205. if(fullUrl.Length == 0)
  206. return relativeUrl;
  207. return (fullUrl + relativeUrl);
  208. }
  209. [WebCategory("Action")]
  210. [WebSysDescription("AdRotator_OnAdCreated")]
  211. public event AdCreatedEventHandler AdCreated
  212. {
  213. add
  214. {
  215. Events.AddHandler(AdCreatedEvent, value);
  216. }
  217. remove
  218. {
  219. Events.RemoveHandler(AdCreatedEvent, value);
  220. }
  221. }
  222. public AdRotator(): base()
  223. {
  224. advertisementFile = string.Empty;
  225. fileDirectory = null;
  226. }
  227. [Bindable(true)]
  228. [DefaultValue("")]
  229. //[Editor("??")]
  230. [WebCategory("Behaviour")]
  231. [WebSysDescription("AdRotator_AdvertisementFile")]
  232. public string AdvertisementFile
  233. {
  234. get
  235. {
  236. return advertisementFile;
  237. }
  238. set
  239. {
  240. advertisementFile = value;
  241. }
  242. }
  243. public override FontInfo Font
  244. {
  245. get
  246. {
  247. return Font;
  248. }
  249. }
  250. [Bindable(true)]
  251. [DefaultValue("")]
  252. [WebCategory("Behaviour")]
  253. [WebSysDescription("AdRotator_KeywordFilter")]
  254. public string KeywordFilter
  255. {
  256. get
  257. {
  258. object o = ViewState["KeywordFilter"];
  259. if(o!=null)
  260. return (string)o;
  261. return String.Empty;
  262. }
  263. set
  264. {
  265. if(value!=null)
  266. ViewState["KeywordFilter"] = value.Trim();
  267. }
  268. }
  269. [Bindable(true)]
  270. [DefaultValue("")]
  271. [TypeConverter(typeof(TargetConverter))]
  272. [WebCategory("Behaviour")]
  273. [WebSysDescription("AdRotator_Target")]
  274. public string Target
  275. {
  276. get
  277. {
  278. object o = ViewState["Target"];
  279. if(o!=null)
  280. return (string)o;
  281. return String.Empty;
  282. }
  283. set
  284. {
  285. ViewState["Target"] = value;
  286. }
  287. }
  288. protected override ControlCollection CreateControlCollection()
  289. {
  290. return new EmptyControlCollection(this);
  291. }
  292. protected virtual void OnAdCreated(AdCreatedEventArgs e)
  293. {
  294. if(Events!=null)
  295. {
  296. AdCreatedEventHandler aceh = (AdCreatedEventHandler)(Events[AdCreatedEvent]);
  297. if(aceh!=null)
  298. aceh(this, e);
  299. }
  300. }
  301. protected override void OnPreRender(EventArgs e)
  302. {
  303. if(AdvertisementFile!=String.Empty)
  304. {
  305. AdCreatedEventArgs acea = new AdCreatedEventArgs(SelectAd());
  306. imageUrl = acea.ImageUrl;
  307. navigateUrl = acea.NavigateUrl;
  308. alternateText = acea.AlternateText;
  309. }
  310. }
  311. protected override void Render(HtmlTextWriter writer)
  312. {
  313. HyperLink hLink = new HyperLink();
  314. Image adImage = new Image();
  315. foreach(IEnumerable current in Attributes.Keys)
  316. {
  317. hLink.Attributes[(string)current] = Attributes[(string)current];
  318. }
  319. if(ID != null && ID.Length > 0)
  320. hLink.ID = ID;
  321. hLink.Target = Target;
  322. hLink.AccessKey = AccessKey;
  323. hLink.Enabled = Enabled;
  324. hLink.TabIndex = TabIndex;
  325. if (navigateUrl != null && navigateUrl.Length != 0)
  326. hLink.NavigateUrl = ResolveAdUrl (navigateUrl);
  327. hLink.RenderBeginTag(writer);
  328. if(ControlStyleCreated)
  329. {
  330. adImage.ApplyStyle(ControlStyle);
  331. }
  332. if(imageUrl!=null && imageUrl.Length > 0)
  333. adImage.ImageUrl = ResolveAdUrl(imageUrl);
  334. adImage.AlternateText = alternateText;
  335. adImage.ToolTip = ToolTip;
  336. adImage.RenderControl(writer);
  337. hLink.RenderEndTag(writer);
  338. }
  339. }
  340. }