AdRotator.cs 6.0 KB

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