PageRenderTime 82ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Providers/XmlProvider/BlogRoll.cs

#
C# | 181 lines | 112 code | 21 blank | 48 comment | 19 complexity | cff1379f163cdb5248cbc3655df118c9 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace BlogEngine.Core.Providers
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Xml;
  9. /// <summary>
  10. /// A storage provider for BlogEngine that uses XML files.
  11. /// <remarks>
  12. /// To build another provider, you can just copy and modify
  13. /// this one. Then add it to the web.config's BlogEngine section.
  14. /// </remarks>
  15. /// </summary>
  16. public partial class XmlBlogProvider : BlogProvider
  17. {
  18. #region Public Methods
  19. /// <summary>
  20. /// Deletes a BlogRoll
  21. /// </summary>
  22. /// <param name="blogRollItem">
  23. /// The blog Roll Item.
  24. /// </param>
  25. public override void DeleteBlogRollItem(BlogRollItem blogRollItem)
  26. {
  27. var blogRoll = BlogRollItem.BlogRolls;
  28. blogRoll.Remove(blogRollItem);
  29. this.WriteBlogRollFile(blogRoll);
  30. }
  31. /// <summary>
  32. /// Fills an unsorted list of BlogRolls.
  33. /// </summary>
  34. /// <returns>
  35. /// A List&lt;BlogRoll&gt; of all BlogRolls
  36. /// </returns>
  37. public override List<BlogRollItem> FillBlogRoll()
  38. {
  39. var fileName = this.Folder + "blogroll.xml";
  40. if (!File.Exists(fileName))
  41. {
  42. return null;
  43. }
  44. var doc = new XmlDocument();
  45. doc.Load(fileName);
  46. var blogRoll = new List<BlogRollItem>();
  47. var largestSortIndex = -1;
  48. var legacyFormat = false;
  49. var nodes = doc.SelectNodes("blogRoll/item");
  50. if (nodes != null)
  51. {
  52. if (nodes.Count == 0)
  53. {
  54. // legacy file format.
  55. nodes = doc.SelectNodes("opml/body/outline");
  56. legacyFormat = true;
  57. }
  58. foreach (var br in from XmlNode node in nodes
  59. select new BlogRollItem
  60. {
  61. Id = node.Attributes["id"] == null ? Guid.NewGuid() : new Guid(node.Attributes["id"].InnerText),
  62. Title = node.Attributes["title"] == null ? null : node.Attributes["title"].InnerText,
  63. Description = node.Attributes["description"] == null ? null : node.Attributes["description"].InnerText,
  64. BlogUrl = node.Attributes["htmlUrl"] == null ? null : new Uri(node.Attributes["htmlUrl"].InnerText),
  65. FeedUrl = node.Attributes["xmlUrl"] == null ? null : new Uri(node.Attributes["xmlUrl"].InnerText),
  66. Xfn = node.Attributes["xfn"] == null ? null : node.Attributes["xfn"].InnerText,
  67. SortIndex = node.Attributes["sortIndex"] == null ? (blogRoll.Count == 0 ? 0 : largestSortIndex + 1) : int.Parse(node.Attributes["sortIndex"].InnerText)
  68. })
  69. {
  70. if (br.SortIndex > largestSortIndex)
  71. {
  72. largestSortIndex = br.SortIndex;
  73. }
  74. blogRoll.Add(br);
  75. br.MarkOld();
  76. }
  77. }
  78. if (legacyFormat && blogRoll.Count > 0)
  79. {
  80. // if we're upgrading from a legacy format, re-write the file to conform to the new format.
  81. this.WriteBlogRollFile(blogRoll);
  82. }
  83. return blogRoll;
  84. }
  85. /// <summary>
  86. /// Inserts a BlogRoll
  87. /// </summary>
  88. /// <param name="blogRollItem">
  89. /// The blog Roll Item.
  90. /// </param>
  91. public override void InsertBlogRollItem(BlogRollItem blogRollItem)
  92. {
  93. var blogRolls = BlogRollItem.BlogRolls;
  94. blogRolls.Add(blogRollItem);
  95. this.WriteBlogRollFile(blogRolls);
  96. }
  97. /// <summary>
  98. /// Gets a BlogRoll based on a Guid.
  99. /// </summary>
  100. /// <param name="id">
  101. /// The BlogRoll's Guid.
  102. /// </param>
  103. /// <returns>
  104. /// A matching BlogRoll
  105. /// </returns>
  106. public override BlogRollItem SelectBlogRollItem(Guid id)
  107. {
  108. var blogRoll = BlogRollItem.BlogRolls.Find(br => br.Id == id) ?? new BlogRollItem();
  109. blogRoll.MarkOld();
  110. return blogRoll;
  111. }
  112. /// <summary>
  113. /// Updates a BlogRoll
  114. /// </summary>
  115. /// <param name="blogRollItem">
  116. /// The blog Roll Item.
  117. /// </param>
  118. public override void UpdateBlogRollItem(BlogRollItem blogRollItem)
  119. {
  120. var blogRolls = BlogRollItem.BlogRolls;
  121. blogRolls.Remove(blogRollItem);
  122. blogRolls.Add(blogRollItem);
  123. this.WriteBlogRollFile(blogRolls);
  124. }
  125. #endregion
  126. #region Methods
  127. /// <summary>
  128. /// The write blog roll file.
  129. /// </summary>
  130. /// <param name="blogRollItems">
  131. /// The blog roll items.
  132. /// </param>
  133. private void WriteBlogRollFile(List<BlogRollItem> blogRollItems)
  134. {
  135. var fileName = this.Folder + "blogroll.xml";
  136. using (var writer = new XmlTextWriter(fileName, Encoding.UTF8))
  137. {
  138. writer.Formatting = Formatting.Indented;
  139. writer.Indentation = 4;
  140. writer.WriteStartDocument(true);
  141. writer.WriteStartElement("blogRoll");
  142. foreach (var br in blogRollItems)
  143. {
  144. writer.WriteStartElement("item");
  145. writer.WriteAttributeString("id", br.Id.ToString());
  146. writer.WriteAttributeString("title", br.Title);
  147. writer.WriteAttributeString("description", br.Description ?? string.Empty);
  148. writer.WriteAttributeString("htmlUrl", br.BlogUrl != null ? br.BlogUrl.ToString() : string.Empty);
  149. writer.WriteAttributeString("xmlUrl", br.FeedUrl != null ? br.FeedUrl.ToString() : string.Empty);
  150. writer.WriteAttributeString("xfn", br.Xfn ?? string.Empty);
  151. writer.WriteAttributeString("sortIndex", br.SortIndex.ToString());
  152. writer.WriteEndElement();
  153. br.MarkOld();
  154. }
  155. writer.WriteEndElement();
  156. }
  157. }
  158. #endregion
  159. }
  160. }