2 * Namespace: System.Web.UI.WebControls
\r
5 * Author: Gaurav Vaish
\r
6 * Contact: <gvaish@iitk.ac.in>
\r
9 * (C) Gaurav Vaish (2001)
\r
14 using System.Collections;
\r
15 using System.Collections.Specialized;
\r
17 using System.Web.UI;
\r
20 namespace System.Web.UI.WebControls
\r
22 public class AdRotator: WebControl
\r
25 private string advertisementFile;
\r
26 private string keywordFilter;
\r
27 private string target;
\r
28 private static readonly object AdCreatedEvent = new object();
\r
30 // Will be set values during (On)PreRender-ing
\r
31 private string alternateText;
\r
32 private string imageUrl;
\r
33 private string navigateUrl;
\r
35 private string fileDirctory;
\r
37 private static class AdRecord
\r
39 public IDictionary adProps;
\r
40 public int hits; // or impressions or clicks
\r
41 public string keyword;
\r
47 public void Initialize(IDictionary adProps)
\r
49 this.adProps = adProps;
\r
54 * Loading / Saving data from/to ad file and all the manipulations wrt to the URL...
\r
55 * are incorporated by the following functions.
\r
57 * LoadAdFile(string)
\r
58 * IsAdMatching(AdRecord)
\r
59 * ResolveAdUrl(string)
\r
61 * The exact control flow will be detailed. Let me first write the functions
\r
64 private AdRecord[] LoadAdFile(string file)
\r
66 //TODO: Implement me
\r
70 XmlDocument document;
\r
71 XmlNode topNode, innerNode;
\r
72 IDictionary hybridDict;
\r
73 AdRecord[] adsArray = null;
\r
76 fStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
\r
77 } catch(Exception e)
\r
79 throw new HttpException("AdRotator: Unable to open file");
\r
83 list = new ArrayList();
\r
84 reader = new XmlTextReader(fStream);
\r
85 document = new XmlDocument();
\r
86 document.Load(reader);
\r
87 if(document.DocumentElement!=null)
\r
89 if(docuent.DocumentElement.LocalName=="Advertisements")
\r
91 topNode = documentElement.FirstChild;
\r
92 while(topNode!=null)
\r
94 if(topNode.LocalName=="Ad")
\r
96 innerNode = topNode.FirstChild;
\r
97 while(innerNode!=null)
\r
99 if(innerNode.NodeType==NodeType.Element)
\r
101 if(hybridDic==null)
\r
103 hybirdDict = new HybridDictionary();
\r
105 hybridDic.Add(innerNode.LocalName, innerNode.InnerText);
\r
107 innerNode = innerNode.NextSibling;
\r
109 if(hybridDict!=null)
\r
110 list.Add(hybridDict);
\r
112 topNode = topNode.NextSibling;
\r
118 adsArray = new AdRecord[list.Count];
\r
119 for(int i=0; i < list.Count; i++)
\r
121 adsArray[i] = new AdRecord((IDictionary)list.Item[i]);
\r
124 } catch(Excetion e)
\r
126 //TODO: Write error. Parsing error has occured
\r
127 throw new HttpException("AdRotator: Unable to parse file" + file);
\r
132 if(adsArray == null)
\r
134 throw new HttpException("AdRotator: No Advertisements Fount");
\r
139 private AdRecord[] GetData(string file)
\r
141 //TODO: Implement me
\r
142 fileDirectory = TemplateSourceDirectory + MapPathSecure(file);
\r
143 //TODO: Do I need to check caching?
\r
146 private IDictionary SelectAd()
\r
148 //TODO: Implement Me
\r
151 private bool IsAdMatching(AdRecord currAd)
\r
153 //TODO: Implement me
\r
156 private string ResolveAdUrl(string)
\r
158 //TODO: Implement me
\r
161 public event AdCreatedEventHandler AdCreated
\r
165 Events.AddHandler(AdCreatedEvent, value);
\r
169 Events.RemoveHandler(AdCreatedEvent, value);
\r
176 advertisementFile = string.Empty;
\r
177 keywordFilter = string.Empty;
\r
178 target = string.Empty;
\r
181 public string AdvertisementFile
\r
185 return advertisementFile;
\r
189 advertisementFile = value;
\r
193 public string KeywordFilter
\r
197 return keywordFilter;
\r
201 keywordFilter = value;
\r
205 public string Target
\r
217 protected override ControlCollection CreateControlCollection()
\r
219 return new EmptyControlCollection(this);
\r
222 protected virtual void OnAdCreated(AdCreatedEventArgs e)
\r
226 AdCreatedEventHandler aceh = (AdCreatedEventHandler)(Events[AdCreatedEvent]);
\r
232 protected override void OnPreRender(EventArgs e)
\r
234 if(AdvertisementFile!=String.Empty)
\r
236 AdCreatedEventArgs acea = new AdCreatedEventArgs(SelectAd());
\r
237 imageUrl = acea.ImageUrl;
\r
238 navigateUrl = acea.NavigateUrl;
\r
239 alternateText = acea.AlternateText;
\r
243 protected override void Render(HtmlTextWriter writer)
\r
245 HyperLink hLink = new HyperLink();
\r
248 AttributeCollection attributeColl = base.Attributes;
\r
249 ICollection keys = attributeColl.Keys;
\r
250 IEnumerator iterator = keys.GetEnumerator();
\r