AdRotator.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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: 75%
  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.UI;
  19. using System.Xml;
  20. using System.Web.Utils;
  21. namespace System.Web.UI.WebControls
  22. {
  23. public class AdRotator: WebControl
  24. {
  25. private string advertisementFile;
  26. private string keywordFilter;
  27. private string target;
  28. private static readonly object AdCreatedEvent = new object();
  29. // Will be set values during (On)PreRender-ing
  30. private string alternateText;
  31. private string imageUrl;
  32. private string navigateUrl;
  33. private string fileDirctory;
  34. private class AdRecord
  35. {
  36. public IDictionary adProps;
  37. public int hits; // or impressions or clicks
  38. public string keyword;
  39. public AdRecord()
  40. {
  41. }
  42. public void Initialize(IDictionary adProps)
  43. {
  44. this.adProps = adProps;
  45. }
  46. }
  47. /*
  48. * Loading / Saving data from/to ad file and all the manipulations wrt to the URL...
  49. * are incorporated by the following functions.
  50. * GetData(string)
  51. * LoadAdFile(string)
  52. * IsAdMatching(AdRecord)
  53. * ResolveAdUrl(string)
  54. * SelectAd()
  55. * The exact control flow will be detailed. Let me first write the functions
  56. */
  57. private AdRecord[] LoadAdFile(string file)
  58. {
  59. Stream fSream;
  60. ArrayList list;
  61. XmlReader reader;
  62. XmlDocument document;
  63. XmlNode topNode, innerNode;
  64. IDictionary hybridDict;
  65. AdRecord[] adsArray = null;
  66. try
  67. {
  68. fStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
  69. } catch(Exception e)
  70. {
  71. throw new HttpException("AdRotator: Unable to open file");
  72. }
  73. try
  74. {
  75. list = new ArrayList();
  76. reader = new XmlTextReader(fStream);
  77. document = new XmlDocument();
  78. document.Load(reader);
  79. if(document.DocumentElement!=null)
  80. {
  81. if(docuent.DocumentElement.LocalName=="Advertisements")
  82. {
  83. topNode = documentElement.FirstChild;
  84. while(topNode!=null)
  85. {
  86. if(topNode.LocalName=="Ad")
  87. {
  88. innerNode = topNode.FirstChild;
  89. while(innerNode!=null)
  90. {
  91. if(innerNode.NodeType==NodeType.Element)
  92. {
  93. if(hybridDic==null)
  94. {
  95. hybirdDict = new HybridDictionary();
  96. }
  97. hybridDic.Add(innerNode.LocalName, innerNode.InnerText);
  98. }
  99. innerNode = innerNode.NextSibling;
  100. }
  101. if(hybridDict!=null)
  102. list.Add(hybridDict);
  103. }
  104. topNode = topNode.NextSibling;
  105. }
  106. }
  107. }
  108. if(list.Count>0)
  109. {
  110. adsArray = new AdRecord[list.Count];
  111. for(int i=0; i < list.Count; i++)
  112. {
  113. adsArray[i] = new AdRecord((IDictionary)list.Item[i]);
  114. }
  115. }
  116. } catch(Excetion e)
  117. {
  118. throw new HttpException("AdRotator: Unable to parse file" + file);
  119. } finally
  120. {
  121. fStream.close();
  122. }
  123. if(adsArray == null)
  124. {
  125. throw new HttpException("AdRotator: No Advertisements Fount");
  126. }
  127. return adsArray;
  128. }
  129. private AdRecord[] GetData(string file)
  130. {
  131. //TODO: Implement me
  132. fileDirectory = UrlUtils.GetDirectory(TemplateSourceDirectory + MapPathSecure(file));
  133. throw new NotImplementedException();
  134. //TODO: Do I need to check caching?
  135. return null;
  136. }
  137. private IDictionary SelectAd()
  138. {
  139. //TODO: I have to select a Random Ad from the file
  140. AdRecord[] records = GetFileData(AdvertisementFile);
  141. if(records!=null)
  142. {
  143. if(records.Length > 0)
  144. {
  145. //TODO: Implement Me
  146. }
  147. }
  148. throw new NotImplementedException();
  149. return null;
  150. }
  151. private bool IsAdMatching(AdRecord currAd)
  152. {
  153. if(KeywordFilter!=String.Empty)
  154. {
  155. /* Perform a case-insensitive keyword filtering
  156. * From the documentation, it looks like there's only one keyword attached.
  157. */
  158. if(currAd.keyword.ToLower() == KeywordFilter.ToLower())
  159. return false;
  160. }
  161. return true;
  162. }
  163. private string ResolveAdUrl(string relativeUrl)
  164. {
  165. throw new NotImplementedException();
  166. //TODO: Implement me
  167. //TODO: Get the full Url from the relativeUrl
  168. }
  169. public event AdCreatedEventHandler AdCreated
  170. {
  171. add
  172. {
  173. Events.AddHandler(AdCreatedEvent, value);
  174. }
  175. remove
  176. {
  177. Events.RemoveHandler(AdCreatedEvent, value);
  178. }
  179. }
  180. public AdRotator()
  181. {
  182. base();
  183. advertisementFile = string.Empty;
  184. keywordFilter = string.Empty;
  185. target = string.Empty;
  186. }
  187. public string AdvertisementFile
  188. {
  189. get
  190. {
  191. return advertisementFile;
  192. }
  193. set
  194. {
  195. advertisementFile = value;
  196. }
  197. }
  198. public string KeywordFilter
  199. {
  200. get
  201. {
  202. return keywordFilter;
  203. }
  204. set
  205. {
  206. keywordFilter = value;
  207. }
  208. }
  209. public string Target
  210. {
  211. get
  212. {
  213. return target;
  214. }
  215. set
  216. {
  217. target = value;
  218. }
  219. }
  220. protected override ControlCollection CreateControlCollection()
  221. {
  222. return new EmptyControlCollection(this);
  223. }
  224. protected virtual void OnAdCreated(AdCreatedEventArgs e)
  225. {
  226. if(Events!=null)
  227. {
  228. AdCreatedEventHandler aceh = (AdCreatedEventHandler)(Events[AdCreatedEvent]);
  229. if(aceh!=null)
  230. aceh(this, e);
  231. }
  232. }
  233. protected override void OnPreRender(EventArgs e)
  234. {
  235. if(AdvertisementFile!=String.Empty)
  236. {
  237. AdCreatedEventArgs acea = new AdCreatedEventArgs(SelectAd());
  238. imageUrl = acea.ImageUrl;
  239. navigateUrl = acea.NavigateUrl;
  240. alternateText = acea.AlternateText;
  241. }
  242. }
  243. protected override void Render(HtmlTextWriter writer)
  244. {
  245. HyperLink hLink = new HyperLink();
  246. Image image;
  247. AttributeCollection attributeColl = base.Attributes;
  248. ICollection keys = attributeColl.Keys;
  249. IEnumerator iterator = keys.GetEnumerator();
  250. throw new NotImplementedException();
  251. }
  252. }
  253. }