2 * Namespace: System.Web.UI.WebControls
\r
5 * Author: Gaurav Vaish
\r
6 * Maintainer: gvaish@iitk.ac.in
\r
7 * Implementation: yes
\r
8 * Contact: <gvaish@iitk.ac.in>
\r
11 * (C) Gaurav Vaish (2001)
\r
16 using System.Collections;
\r
17 using System.Collections.Specialized;
\r
19 using System.Web.UI;
\r
21 using System.Web.Utils;
\r
23 namespace System.Web.UI.WebControls
\r
25 public class AdRotator: WebControl
\r
28 private string advertisementFile;
\r
29 private string keywordFilter;
\r
30 private string target;
\r
31 private static readonly object AdCreatedEvent = new object();
\r
33 // Will be set values during (On)PreRender-ing
\r
34 private string alternateText;
\r
35 private string imageUrl;
\r
36 private string navigateUrl;
\r
38 private string fileDirctory;
\r
40 private static class AdRecord
\r
42 public IDictionary adProps;
\r
43 public int hits; // or impressions or clicks
\r
44 public string keyword;
\r
50 public void Initialize(IDictionary adProps)
\r
52 this.adProps = adProps;
\r
57 * Loading / Saving data from/to ad file and all the manipulations wrt to the URL...
\r
58 * are incorporated by the following functions.
\r
60 * LoadAdFile(string)
\r
61 * IsAdMatching(AdRecord)
\r
62 * ResolveAdUrl(string)
\r
64 * The exact control flow will be detailed. Let me first write the functions
\r
67 private AdRecord[] LoadAdFile(string file)
\r
72 XmlDocument document;
\r
73 XmlNode topNode, innerNode;
\r
74 IDictionary hybridDict;
\r
75 AdRecord[] adsArray = null;
\r
78 fStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
\r
79 } catch(Exception e)
\r
81 throw new HttpException("AdRotator: Unable to open file");
\r
85 list = new ArrayList();
\r
86 reader = new XmlTextReader(fStream);
\r
87 document = new XmlDocument();
\r
88 document.Load(reader);
\r
89 if(document.DocumentElement!=null)
\r
91 if(docuent.DocumentElement.LocalName=="Advertisements")
\r
93 topNode = documentElement.FirstChild;
\r
94 while(topNode!=null)
\r
96 if(topNode.LocalName=="Ad")
\r
98 innerNode = topNode.FirstChild;
\r
99 while(innerNode!=null)
\r
101 if(innerNode.NodeType==NodeType.Element)
\r
103 if(hybridDic==null)
\r
105 hybirdDict = new HybridDictionary();
\r
107 hybridDic.Add(innerNode.LocalName, innerNode.InnerText);
\r
109 innerNode = innerNode.NextSibling;
\r
111 if(hybridDict!=null)
\r
112 list.Add(hybridDict);
\r
114 topNode = topNode.NextSibling;
\r
120 adsArray = new AdRecord[list.Count];
\r
121 for(int i=0; i < list.Count; i++)
\r
123 adsArray[i] = new AdRecord((IDictionary)list.Item[i]);
\r
126 } catch(Excetion e)
\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 throw new NotImplementedException();
\r
145 //TODO: Do I need to check caching?
\r
149 private IDictionary SelectAd()
\r
151 //TODO: I have to select a Random Ad from the file
\r
152 AdRecord[] records = GetFileData(AdvertisementFile);
\r
155 if(records.Length > 0)
\r
157 //TODO: Implement Me
\r
160 throw new NotImplementedException();
\r
164 private bool IsAdMatching(AdRecord currAd)
\r
166 if(KeywordFilter!=String.Empty)
\r
168 /* Perform a case-insensitive keyword filtering
\r
169 * From the documentation, it looks like there's only one keyword attached.
\r
171 if(currAd.keyword.ToLower() == KeywordFilter.ToLower())
\r
177 private string ResolveAdUrl(string relativeUrl)
\r
179 throw new NotImplementedException();
\r
180 //TODO: Implement me
\r
181 //TODO: Get the full Url from the relativeUrl
\r
184 public event AdCreatedEventHandler AdCreated
\r
188 Events.AddHandler(AdCreatedEvent, value);
\r
192 Events.RemoveHandler(AdCreatedEvent, value);
\r
199 advertisementFile = string.Empty;
\r
200 keywordFilter = string.Empty;
\r
201 target = string.Empty;
\r
204 public string AdvertisementFile
\r
208 return advertisementFile;
\r
212 advertisementFile = value;
\r
216 public string KeywordFilter
\r
220 return keywordFilter;
\r
224 keywordFilter = value;
\r
228 public string Target
\r
240 protected override ControlCollection CreateControlCollection()
\r
242 return new EmptyControlCollection(this);
\r
245 protected virtual void OnAdCreated(AdCreatedEventArgs e)
\r
249 AdCreatedEventHandler aceh = (AdCreatedEventHandler)(Events[AdCreatedEvent]);
\r
255 protected override void OnPreRender(EventArgs e)
\r
257 if(AdvertisementFile!=String.Empty)
\r
259 AdCreatedEventArgs acea = new AdCreatedEventArgs(SelectAd());
\r
260 imageUrl = acea.ImageUrl;
\r
261 navigateUrl = acea.NavigateUrl;
\r
262 alternateText = acea.AlternateText;
\r
266 protected override void Render(HtmlTextWriter writer)
\r
268 HyperLink hLink = new HyperLink();
\r
271 AttributeCollection attributeColl = base.Attributes;
\r
272 ICollection keys = attributeColl.Keys;
\r
273 IEnumerator iterator = keys.GetEnumerator();
\r
274 throw new NotImplementedException();
\r