*/\r
\r
using System;\r
+using System.IO;\r
using System.Collections;\r
+using System.Collections.Specialized;\r
+using System.Web;\r
+using System.Web.UI;\r
+using System.Xml;\r
\r
namespace System.Web.UI.WebControls\r
{\r
private string advertisementFile;\r
private string keywordFilter;\r
private string target;\r
+ private static readonly object AdCreatedEvent = new object();\r
\r
- public event AdCreatedEventHandler AdCreated;\r
+ // Will be set values during (On)PreRender-ing\r
+ private string alternateText;\r
+ private string imageUrl;\r
+ private string navigateUrl;\r
\r
+ private string fileDirctory;\r
+\r
+ private static class AdRecord\r
+ {\r
+ public IDictionary adProps;\r
+ public int hits; // or impressions or clicks\r
+ public string keyword;\r
+\r
+ public AdRecord()\r
+ {\r
+ }\r
+ \r
+ public void Initialize(IDictionary adProps)\r
+ {\r
+ this.adProps = adProps;\r
+ }\r
+ }\r
+\r
+/*\r
+ * Loading / Saving data from/to ad file and all the manipulations wrt to the URL...\r
+ * are incorporated by the following functions.\r
+ * GetData(string)\r
+ * LoadAdFile(string)\r
+ * IsAdMatching(AdRecord)\r
+ * ResolveAdUrl(string)\r
+ * SelectAd()\r
+ * The exact control flow will be detailed. Let me first write the functions\r
+ */\r
+\r
+ private AdRecord[] LoadAdFile(string file)\r
+ {\r
+ //TODO: Implement me\r
+ Stream fSream;\r
+ ArrayList list;\r
+ XmlReader reader;\r
+ XmlDocument document;\r
+ XmlNode topNode, innerNode;\r
+ IDictionary hybridDict;\r
+ AdRecord[] adsArray = null;\r
+ try\r
+ {\r
+ fStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);\r
+ } catch(Exception e)\r
+ {\r
+ throw new HttpException("AdRotator: Unable to open file");\r
+ }\r
+ try\r
+ {\r
+ list = new ArrayList();\r
+ reader = new XmlTextReader(fStream);\r
+ document = new XmlDocument();\r
+ document.Load(reader);\r
+ if(document.DocumentElement!=null)\r
+ {\r
+ if(docuent.DocumentElement.LocalName=="Advertisements")\r
+ {\r
+ topNode = documentElement.FirstChild;\r
+ while(topNode!=null)\r
+ {\r
+ if(topNode.LocalName=="Ad")\r
+ {\r
+ innerNode = topNode.FirstChild;\r
+ while(innerNode!=null)\r
+ {\r
+ if(innerNode.NodeType==NodeType.Element)\r
+ {\r
+ if(hybridDic==null)\r
+ {\r
+ hybirdDict = new HybridDictionary();\r
+ }\r
+ hybridDic.Add(innerNode.LocalName, innerNode.InnerText);\r
+ }\r
+ innerNode = innerNode.NextSibling;\r
+ }\r
+ if(hybridDict!=null)\r
+ list.Add(hybridDict);\r
+ }\r
+ topNode = topNode.NextSibling;\r
+ }\r
+ }\r
+ }\r
+ if(list.Count>0)\r
+ {\r
+ adsArray = new AdRecord[list.Count];\r
+ for(int i=0; i < list.Count; i++)\r
+ {\r
+ adsArray[i] = new AdRecord((IDictionary)list.Item[i]);\r
+ }\r
+ }\r
+ } catch(Excetion e)\r
+ {\r
+ //TODO: Write error. Parsing error has occured\r
+ throw new HttpException("AdRotator: Unable to parse file" + file);\r
+ } finally\r
+ {\r
+ fStream.close();\r
+ }\r
+ if(adsArray == null)\r
+ {\r
+ throw new HttpException("AdRotator: No Advertisements Fount");\r
+ }\r
+ return adsArray;\r
+ }\r
+ \r
+ private AdRecord[] GetData(string file)\r
+ {\r
+ //TODO: Implement me\r
+ fileDirectory = TemplateSourceDirectory + MapPathSecure(file);\r
+ //TODO: Do I need to check caching?\r
+ }\r
+ \r
+ private IDictionary SelectAd()\r
+ {\r
+ //TODO: Implement Me\r
+ }\r
+ \r
+ private bool IsAdMatching(AdRecord currAd)\r
+ {\r
+ //TODO: Implement me\r
+ }\r
+ \r
+ private string ResolveAdUrl(string)\r
+ {\r
+ //TODO: Implement me\r
+ }\r
+ \r
+ public event AdCreatedEventHandler AdCreated\r
+ {\r
+ add\r
+ {\r
+ Events.AddHandler(AdCreatedEvent, value);\r
+ }\r
+ remove\r
+ {\r
+ Events.RemoveHandler(AdCreatedEvent, value);\r
+ }\r
+ }\r
+ \r
public AdRotator()\r
{\r
+ base();\r
advertisementFile = string.Empty;\r
keywordFilter = string.Empty;\r
target = string.Empty;\r
}\r
}\r
\r
+ protected override ControlCollection CreateControlCollection()\r
+ {\r
+ return new EmptyControlCollection(this);\r
+ }\r
+ \r
+ protected virtual void OnAdCreated(AdCreatedEventArgs e)\r
+ {\r
+ if(Events!=null)\r
+ {\r
+ AdCreatedEventHandler aceh = (AdCreatedEventHandler)(Events[AdCreatedEvent]);\r
+ if(aceh!=null)\r
+ aceh(this, e);\r
+ }\r
+ }\r
+\r
+ protected override void OnPreRender(EventArgs e)\r
+ {\r
+ if(AdvertisementFile!=String.Empty)\r
+ {\r
+ AdCreatedEventArgs acea = new AdCreatedEventArgs(SelectAd());\r
+ imageUrl = acea.ImageUrl;\r
+ navigateUrl = acea.NavigateUrl;\r
+ alternateText = acea.AlternateText;\r
+ }\r
+ }\r
+ \r
protected override void Render(HtmlTextWriter writer)\r
{\r
HyperLink hLink = new HyperLink();\r
{\r
//TODO: A list of private members may be incomplete\r
\r
- private HtmlTextWriterTag writerTag;\r
- private string stringTag;\r
- private AttributesCollection attributes;\r
- private StateBag attributeState;\r
- private Style controlStyle;\r
+ private HtmlTextWriterTag writerTag;\r
+ private string stringTag;\r
+ private AttributeCollection attributes;\r
+ private StateBag attributeState;\r
+ private Style controlStyle;\r
+ private bool enabled;\r
+ private HtmlTextWriterTag tagKey;\r
+ private string tagName;\r
\r
// TODO: The constructors definitions\r
protected WebControl()\r
{\r
- //todo: what now?\r
- controlStyle = null;\r
+ //todo: what now? To be rendered as SPAN tag!\r
+ base();\r
+ Initialize();\r
}\r
\r
public WebControl(HtmlTextWriterTag tag)\r
{\r
- //TODO: am i right?\r
+ //FIXME: am i right?\r
+ base();\r
writerTag = tag;\r
- stringTag = null;\r
- controlStyle = null;\r
+ //stringTag = null;\r
+ Initialize();\r
}\r
\r
protected WebControl(string tag)\r
{\r
- //TODO: am i right?\r
+ //FIXME: am i right?\r
+ base();\r
stringTag = tag;\r
- writerTag = null;\r
- controlStyle = null;\r
+ //writerTag = null;\r
+ Initialize();\r
+ }\r
+ \r
+ private void Initialize()\r
+ {\r
+ controlStyle = null;\r
+ enabled = true;\r
+ tagName = null;\r
+ attributeState = null;\r
}\r
\r
public virtual string AccessKey\r
\r
public AttributeCollection Attributes\r
{\r
- if(attributes==null)\r
+ get\r
{\r
- //TODO: From where to get StateBag and how? I think this method is OK!\r
- if(attributeState == null)\r
+ if(attributes==null)\r
{\r
- attributeState = new StateBag(true);\r
- if(attributeState.IsTrackingViewState)\r
+ //TODO: From where to get StateBag and how? I think this method is OK!\r
+ if(attributeState == null)\r
{\r
- attributeState.TrackViewState();\r
+ attributeState = new StateBag(true);\r
+ //FIXME: Uncomment the following in the final release\r
+ // commented because of the assembly problem.\r
+ //The function TrackViewState() is internal\r
+ /*\r
+ if(IsTrackingViewState)\r
+ {\r
+ attributeState.TrackViewState();\r
+ }\r
+ */\r
}\r
+ attributes = new AttributeCollection(attributeState);\r
}\r
- attributes = new AttributeCollection(attributes);\r
+ return attributes;\r
}\r
- return attributes;\r
}\r
\r
public Style ControlStyle \r
if(controlStyle == null)\r
{\r
controlStyle = CreateControlStyle();\r
+ //FIXME: Uncomment the following in the final release\r
+ // commented because of the assembly problem.\r
+ //The functions TrackViewState() and LoadViewState() are internal\r
+ /*\r
if(IsTrackingViewState)\r
{\r
controlStyle.TrackViewState();\r
}\r
controlStyle.LoadViewState(null);\r
+ */\r
}\r
return controlStyle;\r
}\r
{\r
get\r
{\r
- if(ControlStyleCreated)\r
- return controlStyle.CssClass;\r
+ return ControlStyle.CssClass;\r
+ }\r
+ set\r
+ {\r
+ ControlStyle.CssClass = value;\r
+ }\r
+ }\r
+ \r
+ public virtual bool Enabled\r
+ {\r
+ get\r
+ {\r
+ return enabled;\r
+ }\r
+ set\r
+ {\r
+ enabled = value;\r
+ }\r
+ }\r
+\r
+ public virtual FontInfo Font\r
+ {\r
+ get\r
+ {\r
+ return ControlStyle.Font;\r
+ }\r
+ }\r
+ \r
+ public virtual Color ForeColor\r
+ {\r
+ get\r
+ {\r
+ return ControlStyle.ForeColor;\r
+ }\r
+ set\r
+ {\r
+ ControlStyle.ForeColor = value;\r
+ }\r
+ }\r
+ \r
+ public virtual Unit Height\r
+ {\r
+ get\r
+ {\r
+ return ControlStyle.Height;\r
+ }\r
+ set\r
+ {\r
+ ControlStyle.Height = value;\r
+ }\r
+ }\r
+ \r
+ public CssStyleCollection Style\r
+ {\r
+ get\r
+ {\r
+ return Attributes.CssStyle;\r
+ }\r
+ }\r
+ \r
+ public virtual short TabIndex\r
+ {\r
+ get\r
+ {\r
+ object o = ViewState["TabIndex"];\r
+ if(o!=null)\r
+ return (short)o;\r
+ return 0;\r
+ }\r
+ set\r
+ {\r
+ if(value < -32768 || value > 32767)\r
+ throw new ArgumentException();\r
+ ViewState["TabIndex"] = value;\r
+ }\r
+ }\r
+ \r
+ public virtual string ToolTip\r
+ {\r
+ get\r
+ {\r
+ object o = ViewState["ToolTip"];\r
+ if(o!=null)\r
+ return (string)o;\r
return String.Empty;\r
}\r
+ set\r
+ {\r
+ ViewState["ToolTip"] = value;\r
+ }\r
}\r
\r
- public \r
+ public virtual Unit Width\r
+ {\r
+ get\r
+ {\r
+ return ControlStyle.Width;\r
+ }\r
+ set\r
+ {\r
+ ControlStyle.Width = value;\r
+ }\r
+ }\r
+ \r
+ public void ApplyStyle(Style s)\r
+ {\r
+ /* FIXME: Again internal problem\r
+ if(!ControlStyle.IsEmpty)\r
+ {\r
+ */\r
+ ControlStyle.CopyFrom(s);\r
+ //}\r
+ }\r
+ \r
+ public void CopyBaseAttributes(WebControl controlSrc)\r
+ {\r
+ //TODO: tocopy\r
+ /*\r
+ * AccessKey, Enabled, ToolTip, TabIndex, Attributes\r
+ */\r
+ AccessKey = controlSrc.AccessKey;\r
+ Enabled = controlSrc.Enabled;\r
+ ToolTip = controlSrc.ToolTip;\r
+ TabIndex = controlSrc.TabIndex;\r
+ Attributes = controlSrc.Attributes;\r
+ }\r
+ \r
+ public void MergeStyle(Style s)\r
+ {\r
+ ControlStyle.MergeWith(s);\r
+ }\r
+ \r
+ public virtual void RenderBeginTag(HtmlTextWriter writer)\r
+ {\r
+ AddAttributesToRender(writer);\r
+ if(TagKey!=null)\r
+ {\r
+ writer.RenderBeginTag(TagKey);\r
+ }\r
+ writer.RenderBeginTag(tagName);\r
+ }\r
+ \r
+ public virtual void RenderEndTag(HtmlTextWriter writer)\r
+ {\r
+ writer.RenderEndTag();\r
+ }\r
+ \r
+ protected virtual HtmlTextWriterTag TagKey\r
+ {\r
+ get\r
+ {\r
+ //FIXME: should I do new HtmlTextWriter()?\r
+ return tagKey;\r
+ }\r
+ }\r
+ \r
+ protected virtual string TagName\r
+ {\r
+ get\r
+ {\r
+ if(tagName==null)\r
+ {\r
+ if(tagKey == null)\r
+ {\r
+ //FIXME: If it is null, is this the right way? I don't think\r
+ tagKey = new HtmlTextWriter();\r
+ }\r
+ tagName = Enum.Format(typeof(tagKey), tagKey, "G").ToLower();\r
+ }\r
+ return tagName;\r
+ }\r
+ }\r
+ \r
+ protected virtual void AddAttributesToRender(HtmlTextWriter writer)\r
+ {\r
+ if(ID!=null)\r
+ {\r
+ writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);\r
+ }\r
+ if(AccessKey.Length>0)\r
+ {\r
+ writer.AddAttribute(HtmlTextWriterAttribute.AccessKey, AccessKey);\r
+ }\r
+ if(!Enabled)\r
+ {\r
+ writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");\r
+ }\r
+ if(ToolTip.Length>0)\r
+ {\r
+ writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);\r
+ }\r
+ if(TabIndex != 0)\r
+ {\r
+ writer.AddAttribute(HtmlTextWriterAttribute.TabIndex, TabIndex.ToString());\r
+ }\r
+ if(ControlStyleCreated)\r
+ {\r
+ if(!ControlStyle.IsEmpty)\r
+ {\r
+ ControlStyle.AddAttributesToRender(writer, this);\r
+ }\r
+ }\r
+ if(attributeState!=null)\r
+ {\r
+ IEnumerator ie = Attributes.Keys.GetEnumerator();\r
+ do\r
+ {\r
+ writer.AddAttribute((string)ie.Current, Attributes.Item[(string)ie.Current]);\r
+ } while(ie.MoveNext());\r
+ }\r
+ }\r
\r
protected virtual Style CreateControlStyle()\r
{\r
- return new Style(ViewState); // from parent class Control\r
+ return new Style(ViewState);\r
+ }\r
+ \r
+ protected override void LoadViewState(object savedState)\r
+ {\r
+ //TODO: Load viewStates\r
+ /*\r
+ * May be will have to first look at Control::LoadViewState \r
+ */\r
+ }\r
+ \r
+ protected override void Render(HtmlTextWriter writer)\r
+ {\r
+ RenderBeginTag(writer);\r
+ RenderContents(writer);\r
+ RenderEngTags(writer);\r
+ }\r
+ \r
+ protected override void RenderContents(HtmlTextWriter writer)\r
+ {\r
+ base.RenderContents(writer);\r
+ }\r
+ \r
+ protected override object SaveViewState()\r
+ {\r
+ //TODO: Implement me!\r
+ // THE LOST WORLD\r
+ }\r
+ \r
+ protected override void TrackViewState()\r
+ {\r
+ base.TrackViewState();\r
+ if(ControlStyleCreated)\r
+ {\r
+ ControlStyle.TrackViewState();\r
+ }\r
+ if(attributeState!=null)\r
+ {\r
+ attributeState.TrackViewState();\r
+ }\r
}\r
\r
// Implemented procedures\r
+ //TODO: The scope of the functions - is public valid. Test thru Reflection\r
public string GetAttribute(string key)\r
{\r
- return "";\r
+ if(Attributes!=null)\r
+ return (string)Attributes[key];\r
}\r
\r
public void SetAttribute(string key, string val)\r
- { \r
+ {\r
+ Attributes.Item[key] = value;\r
}\r
\r
}\r