PageRenderTime 85ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/HTCHome2.4/Widgets/NewsWidget/Source.cs

#
C# | 158 lines | 96 code | 21 blank | 41 comment | 6 complexity | e4ce62e41863caf3d947b7e0a4d6ce69 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.ServiceModel.Syndication;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Xml;
  9. using System.Xml.Xsl;
  10. using Argotic.Syndication;
  11. using HTCHome.Core;
  12. using System.Xml.Linq;
  13. using System.Threading;
  14. using System.Windows;
  15. namespace NewsWidget
  16. {
  17. public class Source
  18. {
  19. public string Url { get; set; }
  20. //private DateTimeOffset _lastFeedDate;
  21. private DateTime _lastFeedDate;
  22. public string Title;
  23. public event RefreshFinishedDelegate RefreshFinished;
  24. //public delegate void RefreshFinishedDelegate(Source sender, IEnumerable<SyndicationItem> newItems);
  25. public delegate void RefreshFinishedDelegate(Source sender, IEnumerable<RssItem> newItems);
  26. //public SyndicationFeed Feed;
  27. public RssFeed Feed;
  28. //public List<Feed> Items { get; private set; }
  29. public void Refresh()
  30. {
  31. ThreadStart threadStarter = delegate
  32. {
  33. Feed = RssFeed.Create(new Uri(Url));
  34. if (Feed != null)
  35. {
  36. Title = Feed.Channel.Title;
  37. List<RssItem> newItems =
  38. (from x in Feed.Channel.Items
  39. where x.PublicationDate.CompareTo(_lastFeedDate) == 1
  40. select x).ToList();
  41. if (newItems.Count > 0)
  42. {
  43. foreach (var newItem in newItems)
  44. {
  45. if (newItem.PublicationDate.CompareTo(_lastFeedDate) == 1)
  46. _lastFeedDate = newItem.PublicationDate;
  47. newItem.Source = new RssSource(null, Title);
  48. }
  49. }
  50. RefreshFinished(this, newItems);
  51. }
  52. else
  53. {
  54. RefreshFinished(this, null);
  55. }
  56. //XmlTextReader reader = null;
  57. //try
  58. //{
  59. // string xml = GeneralHelper.GetXml(Url);
  60. // xml = RemoveEmptyTags(xml);
  61. // string[] s = xml.Split(new string[] { "\n" }, StringSplitOptions.None);
  62. // reader = new XmlTextReader(xml, XmlNodeType.Document, null);
  63. // Feed = SyndicationFeed.Load(reader);
  64. //}
  65. //catch (Exception ex)
  66. //{
  67. // HTCHome.Core.Logger.Log("Error while getting news. URL: " + Url + "\n" + ex.ToString());
  68. // RefreshFinished(this, null);
  69. //}
  70. //if (reader != null)
  71. // reader.Close();
  72. //if (Feed != null)
  73. //{
  74. // Title = Feed.Title.Text;
  75. // List<SyndicationItem> newItems = (from x in Feed.Items
  76. // where x.PublishDate.CompareTo(_lastFeedDate) == 1
  77. // select x).ToList();
  78. // if (newItems.Count > 0)
  79. // {
  80. // foreach (var syndicationItem in newItems)
  81. // {
  82. // if (syndicationItem.PublishDate.CompareTo(_lastFeedDate) == 1)
  83. // _lastFeedDate = syndicationItem.PublishDate;
  84. // syndicationItem.SourceFeed = new SyndicationFeed(Feed.Title.Text, null, null);
  85. // }
  86. // }
  87. // RefreshFinished(this, newItems);
  88. //}
  89. //else
  90. // RefreshFinished(this, null);
  91. };
  92. var thread = new Thread(threadStarter);
  93. thread.SetApartmentState(ApartmentState.STA);
  94. thread.Start();
  95. }
  96. public void Clear()
  97. {
  98. //_lastFeedDate = new DateTimeOffset();
  99. _lastFeedDate = new DateTime();
  100. }
  101. public static string RemoveEmptyTags(string sXML)
  102. {
  103. var sb = new StringBuilder();
  104. sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  105. sb.Append("<xsl:stylesheet ");
  106. sb.Append(" version=\"1.0\" ");
  107. sb.Append(" xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\"");
  108. sb.Append(" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">");
  109. sb.Append(" <xsl:output method=\"xml\" version=\"1.0\" encoding=\"UTF-8\"/>");
  110. sb.Append(" <!-- Whenever you match any node or any attribute -->");
  111. sb.Append(" <xsl:template match=\"node()|@*\">");
  112. sb.Append(" <!-- Copy the current node -->");
  113. sb.Append(" <xsl:if test=\"normalize-space(.) != '' or normalize-space(./@*) != '' \">");
  114. sb.Append(" <xsl:copy>");
  115. sb.Append(" <!-- Including any attributes it has and any child nodes -->");
  116. sb.Append(" <xsl:apply-templates select=\"@*|node()\"/>");
  117. sb.Append(" </xsl:copy>");
  118. sb.Append(" </xsl:if>");
  119. sb.Append(" </xsl:template>");
  120. sb.Append("</xsl:stylesheet>");
  121. return transXMLStringThroughXSLTString(sXML, sb.ToString());
  122. }
  123. private static string transXMLStringThroughXSLTString(string sXML, string sXSLT)
  124. {
  125. //This is the logic of the application.
  126. XslCompiledTransform objTransform = new XslCompiledTransform();
  127. StringReader xmlStream = new StringReader(sXML);
  128. XmlReader xmlReader = new XmlTextReader(xmlStream);
  129. StringReader stream = new StringReader(sXSLT);
  130. XmlReader xmlReaderXslt = new XmlTextReader(stream);
  131. objTransform.Load(xmlReaderXslt, null, null);
  132. StringWriter objStream = new StringWriter();
  133. objTransform.Transform(xmlReader, null, objStream);
  134. return objStream.ToString().Replace(@"encoding=""utf-16""?>", @"encoding=""utf-8""?>");
  135. }
  136. }
  137. }