AdRotator.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: AdRotator
  4. *
  5. * Author: Gaurav Vaish
  6. * Contact: <[email protected]>
  7. * Status: 10??%
  8. *
  9. * (C) Gaurav Vaish (2001)
  10. */
  11. using System;
  12. using System.IO;
  13. using System.Collections;
  14. using System.Collections.Specialized;
  15. using System.Web;
  16. using System.Web.UI;
  17. using System.Xml;
  18. // Namespace with internal classes, in System.Web assembly
  19. using System.Web.WebUtils;
  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. //TODO: Write error. Parsing error has occured
  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. //TODO: Do I need to check caching?
  134. }
  135. private IDictionary SelectAd()
  136. {
  137. //TODO: I have to select a Random Ad from the file
  138. AdRecord[] records = GetFileData(AdvertisementFile);
  139. if(records!=null)
  140. {
  141. if(records.Length > 0)
  142. {
  143. //TODO: Implement Me
  144. }
  145. }
  146. return null;
  147. }
  148. private bool IsAdMatching(AdRecord currAd)
  149. {
  150. if(KeywordFilter!=String.Empty)
  151. {
  152. /* Perform a case-insensitive keyword filtering
  153. * From the documentation, it looks like there's only one keyword attached.
  154. */
  155. if(currAd.keyword.ToLower() == KeywordFilter.ToLower())
  156. return false;
  157. }
  158. return true;
  159. }
  160. private string ResolveAdUrl(string relativeUrl)
  161. {
  162. //TODO: Implement me
  163. //TODO: Get the full Url from the relativeUrl
  164. }
  165. public event AdCreatedEventHandler AdCreated
  166. {
  167. add
  168. {
  169. Events.AddHandler(AdCreatedEvent, value);
  170. }
  171. remove
  172. {
  173. Events.RemoveHandler(AdCreatedEvent, value);
  174. }
  175. }
  176. public AdRotator()
  177. {
  178. base();
  179. advertisementFile = string.Empty;
  180. keywordFilter = string.Empty;
  181. target = string.Empty;
  182. }
  183. public string AdvertisementFile
  184. {
  185. get
  186. {
  187. return advertisementFile;
  188. }
  189. set
  190. {
  191. advertisementFile = value;
  192. }
  193. }
  194. public string KeywordFilter
  195. {
  196. get
  197. {
  198. return keywordFilter;
  199. }
  200. set
  201. {
  202. keywordFilter = value;
  203. }
  204. }
  205. public string Target
  206. {
  207. get
  208. {
  209. return target;
  210. }
  211. set
  212. {
  213. target = value;
  214. }
  215. }
  216. protected override ControlCollection CreateControlCollection()
  217. {
  218. return new EmptyControlCollection(this);
  219. }
  220. protected virtual void OnAdCreated(AdCreatedEventArgs e)
  221. {
  222. if(Events!=null)
  223. {
  224. AdCreatedEventHandler aceh = (AdCreatedEventHandler)(Events[AdCreatedEvent]);
  225. if(aceh!=null)
  226. aceh(this, e);
  227. }
  228. }
  229. protected override void OnPreRender(EventArgs e)
  230. {
  231. if(AdvertisementFile!=String.Empty)
  232. {
  233. AdCreatedEventArgs acea = new AdCreatedEventArgs(SelectAd());
  234. imageUrl = acea.ImageUrl;
  235. navigateUrl = acea.NavigateUrl;
  236. alternateText = acea.AlternateText;
  237. }
  238. }
  239. protected override void Render(HtmlTextWriter writer)
  240. {
  241. HyperLink hLink = new HyperLink();
  242. Image image;
  243. AttributeCollection attributeColl = base.Attributes;
  244. ICollection keys = attributeColl.Keys;
  245. IEnumerator iterator = keys.GetEnumerator();
  246. }
  247. }
  248. }