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.Caching;
\r
20 using System.Web.UI;
\r
22 using System.Web.Utils;
\r
23 using System.ComponentModel;
\r
25 namespace System.Web.UI.WebControls
\r
27 [DefaultEvent("AdCreated")]
\r
28 [DefaultProperty("AdvertisementFile")]
\r
29 //TODO: [Designer("??")]
\r
30 [ToolboxData("<{0}:AdRotator runat=\"server\" Height=\"60px\""
\r
31 + "Width=\"468\"></{0}:AdRotator>")]
\r
32 public class AdRotator: WebControl
\r
35 private string advertisementFile;
\r
36 private static readonly object AdCreatedEvent = new object();
\r
38 // Will be set values during (On)PreRender-ing
\r
39 private string alternateText;
\r
40 private string imageUrl;
\r
41 private string navigateUrl;
\r
43 private string fileDirectory;
\r
45 private class AdRecord
\r
47 public IDictionary adProps;
\r
48 public int hits; // or impressions or clicks
\r
49 public string keyword;
\r
51 public AdRecord(IDictionary adProps)
\r
53 this.adProps = adProps;
\r
55 keyword = String.Empty;
\r
60 * Loading / Saving data from/to ad file and all the manipulations wrt to the URL...
\r
61 * are incorporated by the following functions.
\r
63 * LoadAdFile(string)
\r
64 * IsAdMatching(AdRecord)
\r
65 * ResolveAdUrl(string)
\r
67 * The exact control flow will be detailed. Let me first write the functions
\r
70 private AdRecord[] LoadAdFile(string file)
\r
75 XmlDocument document;
\r
76 XmlNode topNode, innerNode;
\r
77 IDictionary hybridDict = null;
\r
78 AdRecord[] adsArray = null;
\r
81 fStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
\r
82 } catch(Exception e)
\r
84 throw new HttpException("AdRotator: Unable to open file");
\r
88 list = new ArrayList();
\r
89 reader = new XmlTextReader(fStream);
\r
90 document = new XmlDocument();
\r
91 document.Load(reader);
\r
92 if(document.DocumentElement!=null)
\r
94 if(document.DocumentElement.LocalName=="Advertisements")
\r
96 topNode = document.DocumentElement.FirstChild;
\r
97 while(topNode!=null)
\r
99 if(topNode.LocalName=="Ad")
\r
101 innerNode = topNode.FirstChild;
\r
102 while(innerNode!=null)
\r
104 if(innerNode.NodeType==XmlNodeType.Element)
\r
106 if(hybridDict==null)
\r
108 hybridDict = new HybridDictionary();
\r
110 hybridDict.Add(innerNode.LocalName, innerNode.InnerText);
\r
112 innerNode = innerNode.NextSibling;
\r
114 if(hybridDict!=null)
\r
115 list.Add(hybridDict);
\r
117 topNode = topNode.NextSibling;
\r
123 adsArray = new AdRecord[list.Count];
\r
124 for(int i=0; i < list.Count; i++)
\r
126 adsArray[i] = new AdRecord((IDictionary)list[i]);
\r
129 } catch(Exception e)
\r
131 throw new HttpException("AdRotator_Parse_Error" + file);
\r
136 if(adsArray == null)
\r
138 throw new HttpException("AdRotator_No_Advertisements_Found");
\r
143 private AdRecord[] GetData(string file)
\r
145 string physPath = MapPathSecure(file);
\r
146 string AdKey = "AdRotatorCache: " + physPath;
\r
147 fileDirectory = UrlUtils.GetDirectory(UrlUtils.Combine(TemplateSourceDirectory, file));
\r
148 Cache cache = HttpRuntime.Cache;
\r
149 AdRecord[] records = (AdRecord[])cache[AdKey];
\r
152 records = LoadAdFile(physPath);
\r
157 cache.Insert(AdKey, records, new CacheDependency(physPath));
\r
162 private IDictionary SelectAd()
\r
164 AdRecord[] records = GetData(AdvertisementFile);
\r
165 if(records!=null && records.Length!=0)
\r
167 int impressions = 0;
\r
168 for(int i=0 ; i < records.Length; i++)
\r
170 if(IsAdMatching(records[i]))
\r
171 impressions += records[1].hits;
\r
175 int rnd = (new Random()).Next(impressions) + 1;
\r
178 for(int i=0; i < records.Length; i++)
\r
180 if(IsAdMatching(records[i]))
\r
182 if(rnd <= (counter + records[i].hits))
\r
187 counter += records[i].hits;
\r
190 return records[index].adProps;
\r
196 private bool IsAdMatching(AdRecord currAd)
\r
198 if(KeywordFilter!=String.Empty)
\r
200 if(currAd.keyword.ToLower() == KeywordFilter.ToLower())
\r
206 private string ResolveAdUrl(string relativeUrl)
\r
208 if(relativeUrl.Length==0 || !UrlUtils.IsRelativeUrl(relativeUrl))
\r
209 return relativeUrl;
\r
210 string fullUrl = String.Empty;
\r
211 if(fileDirectory != null)
\r
212 fullUrl = fileDirectory;
\r
213 if(fullUrl.Length == 0)
\r
214 fullUrl = TemplateSourceDirectory;
\r
215 if(fullUrl.Length == 0)
\r
216 return relativeUrl;
\r
217 return (fullUrl + relativeUrl);
\r
220 public event AdCreatedEventHandler AdCreated
\r
224 Events.AddHandler(AdCreatedEvent, value);
\r
228 Events.RemoveHandler(AdCreatedEvent, value);
\r
232 public AdRotator(): base()
\r
234 advertisementFile = string.Empty;
\r
235 fileDirectory = null;
\r
238 public string AdvertisementFile
\r
242 return advertisementFile;
\r
246 advertisementFile = value;
\r
250 public override FontInfo Font
\r
258 public string KeywordFilter
\r
262 object o = ViewState["KeywordFilter"];
\r
265 return String.Empty;
\r
270 ViewState["KeywordFilter"] = value.Trim();
\r
274 public string Target
\r
278 object o = ViewState["Target"];
\r
281 return String.Empty;
\r
285 ViewState["Target"] = value;
\r
289 protected override ControlCollection CreateControlCollection()
\r
291 return new EmptyControlCollection(this);
\r
294 protected virtual void OnAdCreated(AdCreatedEventArgs e)
\r
298 AdCreatedEventHandler aceh = (AdCreatedEventHandler)(Events[AdCreatedEvent]);
\r
304 protected override void OnPreRender(EventArgs e)
\r
306 if(AdvertisementFile!=String.Empty)
\r
308 AdCreatedEventArgs acea = new AdCreatedEventArgs(SelectAd());
\r
309 imageUrl = acea.ImageUrl;
\r
310 navigateUrl = acea.NavigateUrl;
\r
311 alternateText = acea.AlternateText;
\r
315 protected override void Render(HtmlTextWriter writer)
\r
317 HyperLink hLink = new HyperLink();
\r
318 Image adImage = new Image();
\r
319 foreach(IEnumerable current in Attributes.Keys)
\r
321 hLink.Attributes[(string)current] = Attributes[(string)current];
\r
323 if(ID != null && ID.Length > 0)
\r
325 hLink.Target = Target;
\r
326 hLink.AccessKey = AccessKey;
\r
327 hLink.Enabled = Enabled;
\r
328 hLink.TabIndex = TabIndex;
\r
329 hLink.RenderBeginTag(writer);
\r
330 if(ControlStyleCreated)
\r
332 adImage.ApplyStyle(ControlStyle);
\r
334 if(imageUrl!=null && imageUrl.Length > 0)
\r
335 adImage.ImageUrl = ResolveAdUrl(imageUrl);
\r
336 adImage.AlternateText = alternateText;
\r
337 adImage.ToolTip = ToolTip;
\r
338 adImage.RenderControl(writer);
\r
339 hLink.RenderEndTag(writer);
\r