PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 167 lines | 115 code | 22 blank | 30 comment | 15 complexity | 7dcda71aeb980c672ed508aa456289a8 MD5 | raw file
  1namespace BlogEngine.Core.Providers
  2{
  3    using System;
  4    using System.Collections.Generic;
  5    using System.Globalization;
  6    using System.IO;
  7    using System.Linq;
  8    using System.Xml;
  9
 10    /// <summary>
 11    /// A storage provider for BlogEngine that uses XML files.
 12    ///     <remarks>
 13    /// To build another provider, you can just copy and modify
 14    ///         this one. Then add it to the web.config's BlogEngine section.
 15    ///     </remarks>
 16    /// </summary>
 17    public partial class XmlBlogProvider : BlogProvider
 18    {
 19        #region Public Methods
 20
 21        /// <summary>
 22        /// Deletes a Page from the data store specified by the provider.
 23        /// </summary>
 24        /// <param name="page">The page to delete.</param>
 25        public override void DeletePage(Page page)
 26        {
 27            var fileName = string.Format("{0}pages{1}{2}.xml", this.Folder, Path.DirectorySeparatorChar, page.Id);
 28            if (File.Exists(fileName))
 29            {
 30                File.Delete(fileName);
 31            }
 32
 33            if (Page.Pages.Contains(page))
 34            {
 35                Page.Pages.Remove(page);
 36            }
 37        }
 38
 39        /// <summary>
 40        /// Retrieves all pages from the data store
 41        /// </summary>
 42        /// <returns>
 43        /// List of Pages
 44        /// </returns>
 45        public override List<Page> FillPages()
 46        {
 47            var folder = string.Format("{0}pages{1}", this.Folder, Path.DirectorySeparatorChar);
 48
 49            return (from file in Directory.GetFiles(folder, "*.xml", SearchOption.TopDirectoryOnly)
 50                    select new FileInfo(file)
 51                    into info
 52                    select info.Name.Replace(".xml", string.Empty)
 53                    into id 
 54                    select Page.Load(new Guid(id))).ToList();
 55        }
 56
 57        /// <summary>
 58        /// Inserts a new Page into the data store specified by the provider.
 59        /// </summary>
 60        /// <param name="page">The page to insert.</param>
 61        public override void InsertPage(Page page)
 62        {
 63            if (!Directory.Exists(string.Format("{0}pages", this.Folder)))
 64            {
 65                Directory.CreateDirectory(string.Format("{0}pages", this.Folder));
 66            }
 67
 68            var fileName = string.Format("{0}pages{1}{2}.xml", this.Folder, Path.DirectorySeparatorChar, page.Id);
 69            var settings = new XmlWriterSettings { Indent = true };
 70
 71            using (var writer = XmlWriter.Create(fileName, settings))
 72            {
 73                writer.WriteStartDocument(true);
 74                writer.WriteStartElement("page");
 75
 76                writer.WriteElementString("title", page.Title);
 77                writer.WriteElementString("description", page.Description);
 78                writer.WriteElementString("content", page.Content);
 79                writer.WriteElementString("keywords", page.Keywords);
 80                writer.WriteElementString("slug", page.Slug);
 81                writer.WriteElementString("parent", page.Parent.ToString());
 82                writer.WriteElementString("isfrontpage", page.IsFrontPage.ToString());
 83                writer.WriteElementString("showinlist", page.ShowInList.ToString());
 84                writer.WriteElementString("ispublished", page.IsPublished.ToString());
 85                writer.WriteElementString("isdeleted", page.IsDeleted.ToString());
 86                writer.WriteElementString(
 87                    "datecreated", 
 88                    page.DateCreated.AddHours(-BlogSettings.Instance.Timezone).ToString(
 89                        "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));
 90                writer.WriteElementString(
 91                    "datemodified", 
 92                    page.DateModified.AddHours(-BlogSettings.Instance.Timezone).ToString(
 93                        "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));
 94
 95                writer.WriteEndElement();
 96            }
 97        }
 98
 99        /// <summary>
100        /// Retrieves a Page from the provider based on the specified id.
101        /// </summary>
102        /// <param name="id">The Page id.</param>
103        /// <returns>The Page object.</returns>
104        public override Page SelectPage(Guid id)
105        {
106            var fileName = string.Format("{0}pages{1}{2}.xml", this.Folder, Path.DirectorySeparatorChar, id);
107            var doc = new XmlDocument();
108            doc.Load(fileName);
109
110            var page = new Page
111                {
112                    Title = doc.SelectSingleNode("page/title").InnerText,
113                    Description = doc.SelectSingleNode("page/description").InnerText,
114                    Content = doc.SelectSingleNode("page/content").InnerText,
115                    Keywords = doc.SelectSingleNode("page/keywords").InnerText
116                };
117
118            if (doc.SelectSingleNode("page/slug") != null)
119            {
120                page.Slug = doc.SelectSingleNode("page/slug").InnerText;
121            }
122
123            if (doc.SelectSingleNode("page/parent") != null)
124            {
125                page.Parent = new Guid(doc.SelectSingleNode("page/parent").InnerText);
126            }
127
128            if (doc.SelectSingleNode("page/isfrontpage") != null)
129            {
130                page.IsFrontPage = bool.Parse(doc.SelectSingleNode("page/isfrontpage").InnerText);
131            }
132
133            if (doc.SelectSingleNode("page/showinlist") != null)
134            {
135                page.ShowInList = bool.Parse(doc.SelectSingleNode("page/showinlist").InnerText);
136            }
137
138            if (doc.SelectSingleNode("page/ispublished") != null)
139            {
140                page.IsPublished = bool.Parse(doc.SelectSingleNode("page/ispublished").InnerText);
141            }
142
143            if (doc.SelectSingleNode("page/isdeleted") != null)
144            {
145                page.IsDeleted = bool.Parse(doc.SelectSingleNode("page/isdeleted").InnerText);
146            }
147
148            page.DateCreated = DateTime.Parse(
149                doc.SelectSingleNode("page/datecreated").InnerText, CultureInfo.InvariantCulture);
150            page.DateModified = DateTime.Parse(
151                doc.SelectSingleNode("page/datemodified").InnerText, CultureInfo.InvariantCulture);
152
153            return page;
154        }
155
156        /// <summary>
157        /// Updates an existing Page in the data store specified by the provider.
158        /// </summary>
159        /// <param name="page">The page to update.</param>
160        public override void UpdatePage(Page page)
161        {
162            InsertPage(page);
163        }
164
165        #endregion
166    }
167}