PageRenderTime 86ms CodeModel.GetById 67ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/BlogEngine/DotNetSlave.BusinessLogic/Web/HttpHandlers/BlogMLExportHandler.cs

#
C# | 485 lines | 305 code | 64 blank | 116 comment | 21 complexity | d4ed3dde9b4d18bea44c10651e7a6942 MD5 | raw file
  1namespace BlogEngine.Core.Web.HttpHandlers
  2{
  3    using System;
  4    using System.Globalization;
  5    using System.Linq;
  6    using System.Text;
  7    using System.Threading;
  8    using System.Web;
  9    using System.Web.Security;
 10    using System.Xml;
 11
 12    /// <summary>
 13    /// Exports all posts to the BlogML xml format.
 14    /// </summary>
 15    public class BlogMLExportHandler : IHttpHandler
 16    {
 17        #region Properties
 18
 19        /// <summary>
 20        ///     Gets a value indicating whether another request can use the <see cref = "T:System.Web.IHttpHandler"></see> instance.
 21        /// </summary>
 22        /// <value></value>
 23        /// <returns>true if the <see cref = "T:System.Web.IHttpHandler"></see> instance is reusable; otherwise, false.</returns>
 24        public bool IsReusable
 25        {
 26            get
 27            {
 28                return false;
 29            }
 30        }
 31
 32        #endregion
 33
 34        #region Implemented Interfaces
 35
 36        #region IHttpHandler
 37
 38        /// <summary>
 39        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
 40        /// </summary>
 41        /// <param name="context">
 42        /// An <see cref="T:System.Web.HttpContext"></see> object that provides references to the intrinsic server 
 43        ///     objects (for example, Request, Response, Session, and Server) used to service HTTP requests.
 44        /// </param>
 45        public void ProcessRequest(HttpContext context)
 46        {
 47            if (Security.IsAdministrator)
 48            {
 49                context.Response.ContentType = "text/xml";
 50                context.Response.AppendHeader("Content-Disposition", "attachment; filename=BlogML.xml");
 51                WriteXml(context);
 52            }
 53            else
 54            {
 55                context.Response.StatusCode = 403;
 56            }
 57        }
 58
 59        #endregion
 60
 61        #endregion
 62
 63        #region Methods
 64
 65        /// <summary>
 66        /// Add authors.
 67        /// </summary>
 68        /// <param name="writer">
 69        /// The writer.
 70        /// </param>
 71        private static void AddAuthors(XmlWriter writer)
 72        {
 73            writer.WriteStartElement("authors");
 74
 75            foreach (MembershipUser user in Membership.GetAllUsers())
 76            {
 77                writer.WriteStartElement("author");
 78
 79                writer.WriteAttributeString("id", user.UserName);
 80                writer.WriteAttributeString(
 81                    "date-created", user.CreationDate.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture));
 82                writer.WriteAttributeString(
 83                    "date-modified", user.CreationDate.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture));
 84                writer.WriteAttributeString("approved", "true");
 85                writer.WriteAttributeString("email", user.Email);
 86
 87                writer.WriteStartElement("title");
 88                writer.WriteAttributeString("type", "text");
 89                writer.WriteCData(user.UserName);
 90                writer.WriteEndElement();
 91
 92                writer.WriteEndElement();
 93            }
 94
 95            writer.WriteEndElement();
 96        }
 97
 98        /// <summary>
 99        /// Add categories.
100        /// </summary>
101        /// <param name="writer">
102        /// The writer.
103        /// </param>
104        private static void AddCategories(XmlWriter writer)
105        {
106            writer.WriteStartElement("categories");
107
108            foreach (var category in Category.Categories)
109            {
110                writer.WriteStartElement("category");
111
112                writer.WriteAttributeString("id", category.Id.ToString());
113                writer.WriteAttributeString(
114                    "date-created", category.DateCreated.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture));
115                writer.WriteAttributeString(
116                    "date-modified", category.DateModified.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture));
117                writer.WriteAttributeString("approved", "true");
118                writer.WriteAttributeString("parentref", "0");
119
120                if (!String.IsNullOrEmpty(category.Description))
121                {
122                    writer.WriteAttributeString("description", category.Description);
123                }
124
125                writer.WriteStartElement("title");
126                writer.WriteAttributeString("type", "text");
127                writer.WriteCData(category.Title);
128                writer.WriteEndElement();
129
130                writer.WriteEndElement();
131            }
132
133            writer.WriteEndElement();
134        }
135
136        /// <summary>
137        /// Add extended properties.
138        /// </summary>
139        /// <param name="writer">
140        /// The writer.
141        /// </param>
142        private static void AddExtendedProperties(XmlWriter writer)
143        {
144            writer.WriteStartElement("extended-properties");
145
146            writer.WriteStartElement("property");
147            writer.WriteAttributeString("name", "CommentModeration");
148            writer.WriteAttributeString("value", "Anonymous");
149            writer.WriteEndElement();
150
151            writer.WriteStartElement("property");
152            writer.WriteAttributeString("name", "SendTrackback");
153            writer.WriteAttributeString("value", BlogSettings.Instance.EnableTrackBackSend ? "Yes" : "No");
154            writer.WriteEndElement();
155
156            writer.WriteEndElement();
157        }
158
159        /// <summary>
160        /// Add post author.
161        /// </summary>
162        /// <param name="writer">
163        /// The writer.
164        /// </param>
165        /// <param name="post">
166        /// The post to add the author on.
167        /// </param>
168        private static void AddPostAuthor(XmlWriter writer, Post post)
169        {
170            writer.WriteStartElement("authors");
171            writer.WriteStartElement("author");
172            writer.WriteAttributeString("ref", post.Author);
173            writer.WriteEndElement();
174            writer.WriteEndElement();
175        }
176
177        /// <summary>
178        /// Add post categories.
179        /// </summary>
180        /// <param name="writer">
181        /// The writer.
182        /// </param>
183        /// <param name="post">
184        /// The post to add categories to.
185        /// </param>
186        private static void AddPostCategories(XmlWriter writer, Post post)
187        {
188            if (post.Categories.Count == 0)
189            {
190                return;
191            }
192
193            writer.WriteStartElement("categories");
194            foreach (var category in post.Categories)
195            {
196                writer.WriteStartElement("category");
197                writer.WriteAttributeString("ref", category.Id.ToString());
198                writer.WriteEndElement();
199            }
200
201            writer.WriteEndElement();
202        }
203
204        /// <summary>
205        /// Add post comments.
206        /// </summary>
207        /// <param name="writer">
208        /// The writer.
209        /// </param>
210        /// <param name="post">
211        /// The post to add comments to.
212        /// </param>
213        private static void AddPostComments(XmlWriter writer, Post post)
214        {
215            if (post.Comments.Count == 0)
216            {
217                return;
218            }
219
220            writer.WriteStartElement("comments");
221            foreach (var comment in
222                post.Comments.Where(comment => comment.Email != "trackback" && comment.Email != "pingback"))
223            {
224                writer.WriteStartElement("comment");
225                writer.WriteAttributeString("id", comment.Id.ToString());
226                writer.WriteAttributeString("parentid", comment.ParentId.ToString());
227                writer.WriteAttributeString(
228                    "date-created", comment.DateCreated.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture));
229                writer.WriteAttributeString(
230                    "date-modified", comment.DateCreated.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture));
231                writer.WriteAttributeString("approved", comment.IsApproved.ToString().ToLowerInvariant());
232                writer.WriteAttributeString("user-name", comment.Author);
233                writer.WriteAttributeString("user-email", comment.Email);
234                writer.WriteAttributeString("user-ip", comment.IP);
235
236                if (comment.Website != null)
237                {
238                    writer.WriteAttributeString("user-url", comment.Website.ToString());
239                }
240                else
241                {
242                    writer.WriteAttributeString("user-url", string.Empty);
243                }
244
245                writer.WriteStartElement("title");
246                writer.WriteAttributeString("type", "text");
247                writer.WriteCData("re: " + post.Title);
248                writer.WriteEndElement();
249
250                writer.WriteStartElement("content");
251                writer.WriteAttributeString("type", "text");
252                writer.WriteCData(comment.Content);
253                writer.WriteEndElement();
254
255                writer.WriteEndElement();
256            }
257
258            writer.WriteEndElement();
259        }
260
261        /// <summary>
262        /// Add post content.
263        /// </summary>
264        /// <param name="writer">
265        /// The writer.
266        /// </param>
267        /// <param name="post">
268        /// The post to add content to.
269        /// </param>
270        private static void AddPostContent(XmlWriter writer, Post post)
271        {
272            writer.WriteStartElement("content");
273            writer.WriteAttributeString("type", "text");
274            writer.WriteCData(post.Content);
275            writer.WriteEndElement();
276        }
277
278        /// <summary>
279        /// Adds the post excerpt.
280        /// </summary>
281        /// <param name="writer">The writer.</param>
282        /// <param name="post">The post to add the excerpt to.</param>
283        private static void AddPostExcerpt(XmlWriter writer, Post post)
284        {
285            if (String.IsNullOrEmpty(post.Description))
286            {
287                return;
288            }
289
290            writer.WriteStartElement("excerpt");
291            writer.WriteAttributeString("type", "text");
292            writer.WriteCData(post.Description);
293            writer.WriteEndElement();
294        }
295
296        /// <summary>
297        /// The post-name element contains the same as the title.
298        /// </summary>
299        /// <param name="writer">
300        /// The writer.
301        /// </param>
302        /// <param name="post">
303        /// The post to add a name to.
304        /// </param>
305        private static void AddPostName(XmlWriter writer, Post post)
306        {
307            writer.WriteStartElement("post-name");
308            writer.WriteAttributeString("type", "text");
309            writer.WriteCData(post.Title);
310            writer.WriteEndElement();
311        }
312
313        /// <summary>
314        /// Adds the post tags.
315        /// </summary>
316        /// <param name="writer">The writer.</param>
317        /// <param name="post">The post to add tags to.</param>
318        private static void AddPostTags(XmlWriter writer, Post post)
319        {
320            if (post.Tags.Count == 0)
321            {
322                return;
323            }
324
325            writer.WriteStartElement("tags");
326            foreach (var tag in post.Tags)
327            {
328                writer.WriteStartElement("tag");
329                writer.WriteAttributeString("ref", tag);
330                writer.WriteEndElement();
331            }
332
333            writer.WriteEndElement();
334        }
335
336        /// <summary>
337        /// Adds the post title.
338        /// </summary>
339        /// <param name="writer">The writer.</param>
340        /// <param name="post">The post to add the title to.</param>
341        private static void AddPostTitle(XmlWriter writer, Post post)
342        {
343            writer.WriteStartElement("title");
344            writer.WriteAttributeString("type", "text");
345            writer.WriteCData(post.Title);
346            writer.WriteEndElement();
347        }
348
349        /// <summary>
350        /// Adds the post trackbacks.
351        /// </summary>
352        /// <param name="writer">The writer.</param>
353        /// <param name="post">The post to add trackbacks for.</param>
354        private static void AddPostTrackbacks(XmlWriter writer, Post post)
355        {
356            if (post.Comments.Count == 0)
357            {
358                return;
359            }
360
361            writer.WriteStartElement("trackbacks");
362            foreach (var comment in
363                post.Comments.Where(comment => comment.Email == "trackback" || comment.Email == "pingback"))
364            {
365                writer.WriteStartElement("trackback");
366                writer.WriteAttributeString("id", comment.Id.ToString());
367                writer.WriteAttributeString(
368                    "date-created", comment.DateCreated.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture));
369                writer.WriteAttributeString(
370                    "date-modified", comment.DateCreated.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture));
371                writer.WriteAttributeString("approved", comment.IsApproved.ToString().ToLowerInvariant());
372
373                if (comment.Website != null)
374                {
375                    writer.WriteAttributeString("url", comment.Website.ToString());
376                }
377
378                writer.WriteStartElement("title");
379                writer.WriteAttributeString("type", "text");
380                writer.WriteCData(comment.Content);
381                writer.WriteEndElement();
382
383                writer.WriteEndElement();
384            }
385
386            writer.WriteEndElement();
387        }
388
389        /// <summary>
390        /// Adds the posts.
391        /// </summary>
392        /// <param name="writer">The writer.</param>
393        private static void AddPosts(XmlWriter writer)
394        {
395            writer.WriteStartElement("posts");
396
397            foreach (var post in Post.Posts)
398            {
399                writer.WriteStartElement("post");
400
401                writer.WriteAttributeString("id", post.Id.ToString());
402                writer.WriteAttributeString(
403                    "date-created", post.DateCreated.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture));
404                writer.WriteAttributeString(
405                    "date-modified", post.DateModified.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture));
406                writer.WriteAttributeString("approved", "true");
407                writer.WriteAttributeString("post-url", post.RelativeLink);
408                writer.WriteAttributeString("type", "normal");
409                writer.WriteAttributeString(
410                    "hasexcerpt", (!string.IsNullOrEmpty(post.Description)).ToString().ToLowerInvariant());
411                writer.WriteAttributeString("views", "0");
412                writer.WriteAttributeString("is-published", post.IsPublished.ToString());
413
414                AddPostTitle(writer, post);
415                AddPostContent(writer, post);
416                AddPostName(writer, post);
417                AddPostExcerpt(writer, post);
418                AddPostAuthor(writer, post);
419                AddPostCategories(writer, post);
420                AddPostTags(writer, post);
421                AddPostComments(writer, post);
422                AddPostTrackbacks(writer, post);
423
424                writer.WriteEndElement();
425            }
426
427            writer.WriteEndElement();
428        }
429
430        /// <summary>
431        /// Adds the sub title.
432        /// </summary>
433        /// <param name="writer">The writer.</param>
434        private static void AddSubTitle(XmlWriter writer)
435        {
436            writer.WriteStartElement("sub-title");
437            writer.WriteAttributeString("type", "text");
438            writer.WriteCData(BlogSettings.Instance.Description);
439            writer.WriteEndElement();
440        }
441
442        /// <summary>
443        /// Adds the title.
444        /// </summary>
445        /// <param name="writer">The writer.</param>
446        private static void AddTitle(XmlWriter writer)
447        {
448            writer.WriteStartElement("title");
449            writer.WriteAttributeString("type", "text");
450            writer.WriteCData(BlogSettings.Instance.Name);
451            writer.WriteEndElement();
452        }
453
454        /// <summary>
455        /// Writes the BlogML to the output stream.
456        /// </summary>
457        /// <param name="context">
458        /// The context.
459        /// </param>
460        private static void WriteXml(HttpContext context)
461        {
462            var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true };
463
464            using (var writer = XmlWriter.Create(context.Response.OutputStream, settings))
465            {
466                writer.WriteStartElement("blog", "http://www.blogml.com/2006/09/BlogML");
467                writer.WriteAttributeString("root-url", Utils.RelativeWebRoot);
468                writer.WriteAttributeString(
469                    "date-created", DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture));
470                writer.WriteAttributeString("xmlns", "xs", null, "http://www.w3.org/2001/XMLSchema");
471
472                AddTitle(writer);
473                AddSubTitle(writer);
474                AddAuthors(writer);
475                AddExtendedProperties(writer);
476                AddCategories(writer);
477                AddPosts(writer);
478
479                writer.WriteEndElement();
480            }
481        }
482
483        #endregion
484    }
485}