PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C# | 179 lines | 112 code | 24 blank | 43 comment | 8 complexity | 48c16970e4cad216725e44e36f7e4d39 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 Category
  21. /// </summary>
  22. /// <param name="category">
  23. /// Must be a valid Category object.
  24. /// </param>
  25. public override void DeleteCategory(Category category)
  26. {
  27. var categories = Category.Categories;
  28. categories.Remove(category);
  29. if (Category.Categories.Contains(category))
  30. {
  31. Category.Categories.Remove(category);
  32. }
  33. this.WriteToFile();
  34. }
  35. /// <summary>
  36. /// Fills an unsorted list of categories.
  37. /// </summary>
  38. /// <returns>
  39. /// A List&lt;Category&gt; of all Categories.
  40. /// </returns>
  41. public override List<Category> FillCategories()
  42. {
  43. var fileName = this.Folder + "categories.xml";
  44. if (!File.Exists(fileName))
  45. {
  46. return null;
  47. }
  48. var doc = new XmlDocument();
  49. doc.Load(fileName);
  50. var categories = new List<Category>();
  51. foreach (XmlNode node in doc.SelectNodes("categories/category"))
  52. {
  53. var category = new Category { Id = new Guid(node.Attributes["id"].InnerText), Title = node.InnerText };
  54. if (node.Attributes["description"] != null)
  55. {
  56. category.Description = node.Attributes["description"].InnerText;
  57. }
  58. else
  59. {
  60. category.Description = string.Empty;
  61. }
  62. if (node.Attributes["parent"] != null)
  63. {
  64. if (String.IsNullOrEmpty(node.Attributes["parent"].InnerText))
  65. {
  66. category.Parent = null;
  67. }
  68. else
  69. {
  70. category.Parent = new Guid(node.Attributes["parent"].InnerText);
  71. }
  72. }
  73. else
  74. {
  75. category.Parent = null;
  76. }
  77. categories.Add(category);
  78. category.MarkOld();
  79. }
  80. return categories;
  81. }
  82. /// <summary>
  83. /// Inserts a Category
  84. /// </summary>
  85. /// <param name="category">
  86. /// Must be a valid Category object.
  87. /// </param>
  88. public override void InsertCategory(Category category)
  89. {
  90. var categories = Category.Categories;
  91. categories.Add(category);
  92. categories.Sort();
  93. this.WriteToFile();
  94. }
  95. /// <summary>
  96. /// Gets a Category based on a Guid
  97. /// </summary>
  98. /// <param name="id">
  99. /// The category's Guid.
  100. /// </param>
  101. /// <returns>
  102. /// A matching Category
  103. /// </returns>
  104. public override Category SelectCategory(Guid id)
  105. {
  106. var categories = Category.Categories;
  107. var category = categories.FirstOrDefault(cat => cat.Id == id) ?? new Category();
  108. category.MarkOld();
  109. return category;
  110. }
  111. /// <summary>
  112. /// Updates a Category
  113. /// </summary>
  114. /// <param name="category">
  115. /// Must be a valid Category object.
  116. /// </param>
  117. public override void UpdateCategory(Category category)
  118. {
  119. var categories = Category.Categories;
  120. categories.Remove(category);
  121. categories.Add(category);
  122. categories.Sort();
  123. this.WriteToFile();
  124. }
  125. #endregion
  126. #region Methods
  127. /// <summary>
  128. /// Saves the Categories to disk.
  129. /// </summary>
  130. private void WriteToFile()
  131. {
  132. var categories = Category.Categories;
  133. var fileName = string.Format("{0}categories.xml", this.Folder);
  134. using (var writer = new XmlTextWriter(fileName, Encoding.UTF8))
  135. {
  136. writer.Formatting = Formatting.Indented;
  137. writer.Indentation = 4;
  138. writer.WriteStartDocument(true);
  139. writer.WriteStartElement("categories");
  140. foreach (var cat in categories)
  141. {
  142. writer.WriteStartElement("category");
  143. writer.WriteAttributeString("id", cat.Id.ToString());
  144. writer.WriteAttributeString("description", cat.Description);
  145. writer.WriteAttributeString("parent", cat.Parent.ToString());
  146. writer.WriteValue(cat.Title);
  147. writer.WriteEndElement();
  148. cat.MarkOld();
  149. }
  150. writer.WriteEndElement();
  151. }
  152. }
  153. #endregion
  154. }
  155. }