PageRenderTime 69ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Web/System.Web.UI.WebControls/AdRotator.cs

https://github.com/pruiz/mono
C# | 310 lines | 223 code | 48 blank | 39 comment | 32 complexity | a15d49b1ec9170bbefc195c3e0696984 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // System.Web.UI.WebControls.AdRotator
  3. //
  4. // Author:
  5. // Ben Maurer <bmaurer@novell.com>
  6. //
  7. // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Xml;
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.Security.Permissions;
  32. using System.Web.Util;
  33. namespace System.Web.UI.WebControls
  34. {
  35. // CAS
  36. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. // attributes
  39. [DefaultEvent("AdCreated")]
  40. [DefaultProperty("AdvertisementFile")]
  41. [Designer("System.Web.UI.Design.WebControls.AdRotatorDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  42. [ToolboxData("<{0}:AdRotator runat=\"server\"></{0}:AdRotator>")]
  43. public class AdRotator : DataBoundControl
  44. {
  45. AdCreatedEventArgs createdargs;
  46. ArrayList ads = new ArrayList ();
  47. string ad_file = String.Empty;
  48. protected internal override void OnInit (EventArgs e)
  49. {
  50. base.OnInit(e);
  51. }
  52. protected internal override void OnPreRender (EventArgs e)
  53. {
  54. Hashtable ht = null;
  55. if (!String.IsNullOrEmpty (ad_file)) {
  56. ReadAdsFromFile (GetPhysicalFilePath (ad_file));
  57. ht = ChooseAd ();
  58. }
  59. AdCreatedEventArgs ev = new AdCreatedEventArgs (ht);
  60. OnAdCreated (ev);
  61. createdargs = ev;
  62. }
  63. [MonoTODO ("Not implemented")]
  64. protected internal override void PerformDataBinding (IEnumerable data)
  65. {
  66. throw new NotImplementedException ();
  67. }
  68. [MonoTODO ("Not implemented")]
  69. protected override void PerformSelect ()
  70. {
  71. throw new NotImplementedException ();
  72. }
  73. protected internal override void Render (HtmlTextWriter writer)
  74. {
  75. AdCreatedEventArgs e = createdargs;
  76. base.AddAttributesToRender (writer);
  77. if (e.NavigateUrl != null && e.NavigateUrl.Length > 0)
  78. writer.AddAttribute (HtmlTextWriterAttribute.Href, ResolveAdUrl (e.NavigateUrl));
  79. if (Target != null && Target.Length > 0)
  80. writer.AddAttribute (HtmlTextWriterAttribute.Target, Target);
  81. writer.RenderBeginTag (HtmlTextWriterTag.A);
  82. if (e.ImageUrl != null && e.ImageUrl.Length > 0)
  83. writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveAdUrl (e.ImageUrl));
  84. writer.AddAttribute (HtmlTextWriterAttribute.Alt, e.AlternateText == null ? String.Empty : e.AlternateText);
  85. writer.AddAttribute (HtmlTextWriterAttribute.Border, "0", false);
  86. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  87. writer.RenderEndTag (); // img
  88. writer.RenderEndTag (); // a
  89. }
  90. string ResolveAdUrl (string url)
  91. {
  92. string path = url;
  93. if (AdvertisementFile != null && AdvertisementFile.Length > 0 && path [0] != '/' && path [0] != '~')
  94. try {
  95. new Uri (path);
  96. }
  97. catch {
  98. return UrlUtils.Combine (UrlUtils.GetDirectory (ResolveUrl (AdvertisementFile)), path);
  99. }
  100. return ResolveUrl (path);
  101. }
  102. //
  103. // We take all the ads in the ad file and add up their
  104. // impression weight. We then take a random number [0,
  105. // TotalImpressions). We then choose the ad that is
  106. // that number or less impressions for the beginning
  107. // of the file. This lets us respect the weights
  108. // given.
  109. //
  110. Hashtable ChooseAd ()
  111. {
  112. // cache for performance
  113. string KeywordFilter = this.KeywordFilter;
  114. int total_imp = 0;
  115. int cur_imp = 0;
  116. bool keywordFilterEmpty = KeywordFilter.Length == 0;
  117. foreach (Hashtable a in ads) {
  118. if (keywordFilterEmpty || KeywordFilter == (string) a ["Keyword"])
  119. total_imp += a ["Impressions"] != null ? int.Parse ((string) a ["Impressions"]) : 1;
  120. }
  121. int r = new Random ().Next (total_imp);
  122. foreach (Hashtable a in ads) {
  123. if (!keywordFilterEmpty && KeywordFilter != (string) a ["Keyword"])
  124. continue;
  125. cur_imp += a ["Impressions"] != null ? int.Parse ((string) a ["Impressions"]) : 1;
  126. if (cur_imp > r)
  127. return a;
  128. }
  129. if (total_imp != 0)
  130. throw new Exception ("I should only get here if no ads matched");
  131. return null;
  132. }
  133. void ReadAdsFromFile (string s)
  134. {
  135. XmlDocument d = new XmlDocument ();
  136. try {
  137. d.Load (s);
  138. } catch (Exception e) {
  139. throw new HttpException ("AdRotator could not parse the xml file", e);
  140. }
  141. ads.Clear ();
  142. foreach (XmlNode n in d.DocumentElement.ChildNodes) {
  143. Hashtable ad = new Hashtable ();
  144. foreach (XmlNode nn in n.ChildNodes)
  145. ad.Add (nn.Name, nn.InnerText);
  146. ads.Add (ad);
  147. }
  148. }
  149. [UrlProperty]
  150. [Bindable(true)]
  151. [DefaultValue("")]
  152. [Editor("System.Web.UI.Design.XmlUrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
  153. [WebSysDescription("")]
  154. [WebCategory("Behavior")]
  155. public string AdvertisementFile {
  156. get {
  157. return ad_file;
  158. }
  159. set {
  160. ad_file = value;
  161. }
  162. }
  163. [DefaultValue ("AlternateText")]
  164. [WebSysDescriptionAttribute ("")]
  165. [WebCategoryAttribute ("Behavior")]
  166. [MonoTODO ("Not implemented")]
  167. public string AlternateTextField
  168. {
  169. get {
  170. throw new NotImplementedException ();
  171. }
  172. set {
  173. throw new NotImplementedException ();
  174. }
  175. }
  176. [Browsable(false)]
  177. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  178. [EditorBrowsable(EditorBrowsableState.Never)]
  179. public override FontInfo Font {
  180. get {
  181. return base.Font;
  182. }
  183. }
  184. [DefaultValue ("ImageUrl")]
  185. [MonoTODO ("Not implemented")]
  186. [WebSysDescriptionAttribute ("")]
  187. [WebCategoryAttribute ("Behavior")]
  188. public string ImageUrlField
  189. {
  190. get {
  191. throw new NotImplementedException ();
  192. }
  193. set {
  194. throw new NotImplementedException ();
  195. }
  196. }
  197. [Bindable(true)]
  198. [DefaultValue("")]
  199. [WebSysDescription("")]
  200. [WebCategory("Behavior")]
  201. public string KeywordFilter {
  202. get {
  203. return ViewState.GetString ("KeywordFilter", String.Empty);
  204. }
  205. set {
  206. ViewState ["KeywordFilter"] = value;
  207. }
  208. }
  209. [DefaultValue ("NavigateUrl")]
  210. [MonoTODO ("Not implemented")]
  211. [WebSysDescriptionAttribute ("")]
  212. [WebCategoryAttribute ("Behavior")]
  213. public string NavigateUrlField
  214. {
  215. get {
  216. throw new NotImplementedException ();
  217. }
  218. set {
  219. throw new NotImplementedException ();
  220. }
  221. }
  222. [Bindable(true)]
  223. [DefaultValue("_top")]
  224. [TypeConverter(typeof(System.Web.UI.WebControls.TargetConverter))]
  225. [WebSysDescription("")]
  226. [WebCategory("Behavior")]
  227. public string Target {
  228. get {
  229. return ViewState.GetString ("Target", "_top");
  230. }
  231. set {
  232. ViewState ["Target"] = value;
  233. }
  234. }
  235. /* all these are listed in corcompare */
  236. public override string UniqueID
  237. {
  238. get {
  239. return base.UniqueID;
  240. }
  241. }
  242. protected override HtmlTextWriterTag TagKey
  243. {
  244. get {
  245. return base.TagKey;
  246. }
  247. }
  248. protected virtual void OnAdCreated (AdCreatedEventArgs e)
  249. {
  250. AdCreatedEventHandler h = (AdCreatedEventHandler) Events [AdCreatedEvent];
  251. if (h != null)
  252. h (this, e);
  253. }
  254. static readonly object AdCreatedEvent = new object ();
  255. [WebSysDescription("")]
  256. [WebCategory("Action")]
  257. public event AdCreatedEventHandler AdCreated {
  258. add { Events.AddHandler (AdCreatedEvent, value); }
  259. remove { Events.RemoveHandler (AdCreatedEvent, value); }
  260. }
  261. }
  262. }