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
19 // Namespace with internal classes, in System.Web assembly
\r
20 using System.Web.WebUtils;
\r
22 namespace System.Web.UI.WebControls
\r
24 public class AdRotator: WebControl
\r
27 private string advertisementFile;
\r
28 private string keywordFilter;
\r
29 private string target;
\r
30 private static readonly object AdCreatedEvent = new object();
\r
32 // Will be set values during (On)PreRender-ing
\r
33 private string alternateText;
\r
34 private string imageUrl;
\r
35 private string navigateUrl;
\r
37 private string fileDirctory;
\r
39 private static class AdRecord
\r
41 public IDictionary adProps;
\r
42 public int hits; // or impressions or clicks
\r
43 public string keyword;
\r
49 public void Initialize(IDictionary adProps)
\r
51 this.adProps = adProps;
\r
56 * Loading / Saving data from/to ad file and all the manipulations wrt to the URL...
\r
57 * are incorporated by the following functions.
\r
59 * LoadAdFile(string)
\r
60 * IsAdMatching(AdRecord)
\r
61 * ResolveAdUrl(string)
\r
63 * The exact control flow will be detailed. Let me first write the functions
\r
66 private AdRecord[] LoadAdFile(string file)
\r
71 XmlDocument document;
\r
72 XmlNode topNode, innerNode;
\r
73 IDictionary hybridDict;
\r
74 AdRecord[] adsArray = null;
\r
77 fStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
\r
78 } catch(Exception e)
\r
80 throw new HttpException("AdRotator: Unable to open file");
\r
84 list = new ArrayList();
\r
85 reader = new XmlTextReader(fStream);
\r
86 document = new XmlDocument();
\r
87 document.Load(reader);
\r
88 if(document.DocumentElement!=null)
\r
90 if(docuent.DocumentElement.LocalName=="Advertisements")
\r
92 topNode = documentElement.FirstChild;
\r
93 while(topNode!=null)
\r
95 if(topNode.LocalName=="Ad")
\r
97 innerNode = topNode.FirstChild;
\r
98 while(innerNode!=null)
\r
100 if(innerNode.NodeType==NodeType.Element)
\r
102 if(hybridDic==null)
\r
104 hybirdDict = new HybridDictionary();
\r
106 hybridDic.Add(innerNode.LocalName, innerNode.InnerText);
\r
108 innerNode = innerNode.NextSibling;
\r
110 if(hybridDict!=null)
\r
111 list.Add(hybridDict);
\r
113 topNode = topNode.NextSibling;
\r
119 adsArray = new AdRecord[list.Count];
\r
120 for(int i=0; i < list.Count; i++)
\r
122 adsArray[i] = new AdRecord((IDictionary)list.Item[i]);
\r
125 } catch(Excetion e)
\r
127 //TODO: Write error. Parsing error has occured
\r
128 throw new HttpException("AdRotator: Unable to parse file" + file);
\r
133 if(adsArray == null)
\r
135 throw new HttpException("AdRotator: No Advertisements Fount");
\r
140 private AdRecord[] GetData(string file)
\r
142 //TODO: Implement me
\r
143 fileDirectory = UrlUtils.GetDirectory(TemplateSourceDirectory + MapPathSecure(file));
\r
144 //TODO: Do I need to check caching?
\r
147 private IDictionary SelectAd()
\r
149 //TODO: I have to select a Random Ad from the file
\r
150 AdRecord[] records = GetFileData(AdvertisementFile);
\r
153 if(records.Length > 0)
\r
155 //TODO: Implement Me
\r
161 private bool IsAdMatching(AdRecord currAd)
\r
163 if(KeywordFilter!=String.Empty)
\r
165 /* Perform a case-insensitive keyword filtering
\r
166 * From the documentation, it looks like there's only one keyword attached.
\r
168 if(currAd.keyword.ToLower() == KeywordFilter.ToLower())
\r
174 private string ResolveAdUrl(string relativeUrl)
\r
176 //TODO: Implement me
\r
177 //TODO: Get the full Url from the relativeUrl
\r
180 public event AdCreatedEventHandler AdCreated
\r
184 Events.AddHandler(AdCreatedEvent, value);
\r
188 Events.RemoveHandler(AdCreatedEvent, value);
\r
195 advertisementFile = string.Empty;
\r
196 keywordFilter = string.Empty;
\r
197 target = string.Empty;
\r
200 public string AdvertisementFile
\r
204 return advertisementFile;
\r
208 advertisementFile = value;
\r
212 public string KeywordFilter
\r
216 return keywordFilter;
\r
220 keywordFilter = value;
\r
224 public string Target
\r
236 protected override ControlCollection CreateControlCollection()
\r
238 return new EmptyControlCollection(this);
\r
241 protected virtual void OnAdCreated(AdCreatedEventArgs e)
\r
245 AdCreatedEventHandler aceh = (AdCreatedEventHandler)(Events[AdCreatedEvent]);
\r
251 protected override void OnPreRender(EventArgs e)
\r
253 if(AdvertisementFile!=String.Empty)
\r
255 AdCreatedEventArgs acea = new AdCreatedEventArgs(SelectAd());
\r
256 imageUrl = acea.ImageUrl;
\r
257 navigateUrl = acea.NavigateUrl;
\r
258 alternateText = acea.AlternateText;
\r
262 protected override void Render(HtmlTextWriter writer)
\r
264 HyperLink hLink = new HyperLink();
\r
267 AttributeCollection attributeColl = base.Attributes;
\r
268 ICollection keys = attributeColl.Keys;
\r
269 IEnumerator iterator = keys.GetEnumerator();
\r