PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 2ms 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
  1namespace 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
 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 BlogRoll
 23        /// </summary>
 24        /// <param name="blogRollItem">
 25        /// The blog Roll Item.
 26        /// </param>
 27        public override void DeleteBlogRollItem(BlogRollItem blogRollItem)
 28        {
 29            var blogRoll = BlogRollItem.BlogRolls;
 30            blogRoll.Remove(blogRollItem);
 31            this.WriteBlogRollFile(blogRoll);
 32        }
 33
 34        /// <summary>
 35        /// Fills an unsorted list of BlogRolls.
 36        /// </summary>
 37        /// <returns>
 38        /// A List&lt;BlogRoll&gt; of all BlogRolls
 39        /// </returns>
 40        public override List<BlogRollItem> FillBlogRoll()
 41        {
 42            var fileName = this.Folder + "blogroll.xml";
 43            if (!File.Exists(fileName))
 44            {
 45                return null;
 46            }
 47
 48            var doc = new XmlDocument();
 49            doc.Load(fileName);
 50            var blogRoll = new List<BlogRollItem>();
 51
 52            var largestSortIndex = -1;
 53            var legacyFormat = false;
 54            var nodes = doc.SelectNodes("blogRoll/item");
 55            if (nodes != null)
 56            {
 57                if (nodes.Count == 0)
 58                {
 59                    // legacy file format.
 60                    nodes = doc.SelectNodes("opml/body/outline");
 61                    legacyFormat = true;
 62                }
 63
 64                foreach (var br in from XmlNode node in nodes
 65                                   select new BlogRollItem
 66                                       {
 67                                           Id = node.Attributes["id"] == null ? Guid.NewGuid() : new Guid(node.Attributes["id"].InnerText),
 68                                           Title = node.Attributes["title"] == null ? null : node.Attributes["title"].InnerText,
 69                                           Description = node.Attributes["description"] == null ? null : node.Attributes["description"].InnerText,
 70                                           BlogUrl = node.Attributes["htmlUrl"] == null ? null : new Uri(node.Attributes["htmlUrl"].InnerText),
 71                                           FeedUrl = node.Attributes["xmlUrl"] == null ? null : new Uri(node.Attributes["xmlUrl"].InnerText), 
 72                                           Xfn = node.Attributes["xfn"] == null ? null : node.Attributes["xfn"].InnerText,
 73                                           SortIndex = node.Attributes["sortIndex"] == null ? (blogRoll.Count == 0 ? 0 : largestSortIndex + 1) : int.Parse(node.Attributes["sortIndex"].InnerText)
 74                                       })
 75                {
 76                    if (br.SortIndex > largestSortIndex)
 77                    {
 78                        largestSortIndex = br.SortIndex;
 79                    }
 80
 81                    blogRoll.Add(br);
 82                    br.MarkOld();
 83                }
 84            }
 85
 86            if (legacyFormat && blogRoll.Count > 0)
 87            {
 88                // if we're upgrading from a legacy format, re-write the file to conform to the new format.
 89                this.WriteBlogRollFile(blogRoll);
 90            }
 91
 92            return blogRoll;
 93        }
 94
 95        /// <summary>
 96        /// Inserts a BlogRoll
 97        /// </summary>
 98        /// <param name="blogRollItem">
 99        /// The blog Roll Item.
100        /// </param>
101        public override void InsertBlogRollItem(BlogRollItem blogRollItem)
102        {
103            var blogRolls = BlogRollItem.BlogRolls;
104            blogRolls.Add(blogRollItem);
105
106            this.WriteBlogRollFile(blogRolls);
107        }
108
109        /// <summary>
110        /// Gets a BlogRoll based on a Guid.
111        /// </summary>
112        /// <param name="id">
113        /// The BlogRoll's Guid.
114        /// </param>
115        /// <returns>
116        /// A matching BlogRoll
117        /// </returns>
118        public override BlogRollItem SelectBlogRollItem(Guid id)
119        {
120            var blogRoll = BlogRollItem.BlogRolls.Find(br => br.Id == id) ?? new BlogRollItem();
121
122            blogRoll.MarkOld();
123            return blogRoll;
124        }
125
126        /// <summary>
127        /// Updates a BlogRoll
128        /// </summary>
129        /// <param name="blogRollItem">
130        /// The blog Roll Item.
131        /// </param>
132        public override void UpdateBlogRollItem(BlogRollItem blogRollItem)
133        {
134            var blogRolls = BlogRollItem.BlogRolls;
135            blogRolls.Remove(blogRollItem);
136            blogRolls.Add(blogRollItem);
137            this.WriteBlogRollFile(blogRolls);
138        }
139
140        #endregion
141
142        #region Methods
143
144        /// <summary>
145        /// The write blog roll file.
146        /// </summary>
147        /// <param name="blogRollItems">
148        /// The blog roll items.
149        /// </param>
150        private void WriteBlogRollFile(List<BlogRollItem> blogRollItems)
151        {
152            var fileName = this.Folder + "blogroll.xml";
153
154            using (var writer = new XmlTextWriter(fileName, Encoding.UTF8))
155            {
156                writer.Formatting = Formatting.Indented;
157                writer.Indentation = 4;
158                writer.WriteStartDocument(true);
159                writer.WriteStartElement("blogRoll");
160
161                foreach (var br in blogRollItems)
162                {
163                    writer.WriteStartElement("item");
164                    writer.WriteAttributeString("id", br.Id.ToString());
165                    writer.WriteAttributeString("title", br.Title);
166                    writer.WriteAttributeString("description", br.Description ?? string.Empty);
167                    writer.WriteAttributeString("htmlUrl", br.BlogUrl != null ? br.BlogUrl.ToString() : string.Empty);
168                    writer.WriteAttributeString("xmlUrl", br.FeedUrl != null ? br.FeedUrl.ToString() : string.Empty);
169                    writer.WriteAttributeString("xfn", br.Xfn ?? string.Empty);
170                    writer.WriteAttributeString("sortIndex", br.SortIndex.ToString());
171                    writer.WriteEndElement();
172                    br.MarkOld();
173                }
174
175                writer.WriteEndElement();
176            }
177        }
178
179        #endregion
180    }
181}