Apml.cs :  » Bloggers » BlogEngine.NET » BlogEngine.Core » Web » HttpHandlers » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » BlogEngine.NET 
BlogEngine.NET » BlogEngine.Core » Web » HttpHandlers » Apml.cs
#region Using

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Web;
using System.Web.Security;
using System.IO;
using System.Security;
using System.Security.Cryptography;

#endregion

namespace BlogEngine.Core.Web.HttpHandlers{
  /// <summary>
  /// Based on John Dyer's (http://johndyer.name/) extension.
  /// </summary>
  public class Apml : IHttpHandler
  {

    #region IHttpHandler Members

    /// <summary>
    /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"></see> instance.
    /// </summary>
    /// <value></value>
    /// <returns>true if the <see cref="T:System.Web.IHttpHandler"></see> instance is reusable; otherwise, false.</returns>
    public bool IsReusable
    {
      get { return false; }
    }

    /// <summary>
    /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
    /// </summary>
    /// <param name="context">An <see cref="T:System.Web.HttpContext"></see> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/xml";
      WriteApmlDocument(context.Response.OutputStream);
    }

    #endregion

    #region Write XML

    private XmlWriter GetWriter(Stream stream)
    {
      XmlWriterSettings settings = new XmlWriterSettings();
      settings.Encoding = Encoding.UTF8;
      settings.Indent = true;
      XmlWriter xmlWriter = XmlWriter.Create(stream, settings);

      xmlWriter.WriteStartDocument();
      xmlWriter.WriteStartElement("APML");

      return xmlWriter;
    }

    private void CloseWriter(XmlWriter xmlWriter)
    {
      xmlWriter.WriteEndElement(); // APML
      xmlWriter.WriteEndDocument();
      xmlWriter.Close();
    }

    private void WriteApmlDocument(Stream stream)
    {
      XmlWriter writer = GetWriter(stream);

      // HEAD
      writer.WriteStartElement("Head");
      writer.WriteElementString("Title", "APML data for " + BlogSettings.Instance.Name + " - " + BlogSettings.Instance.Description);
      writer.WriteElementString("Generator", "BlogEngine.NET " + BlogSettings.Instance.Version());
      writer.WriteElementString("UserEmail", "");
      writer.WriteElementString("DateCreated", DateTime.Now.ToString());
      writer.WriteEndElement();  // Head

      // BODY
      writer.WriteStartElement("Body");
      writer.WriteAttributeString("defaultProfile", "tags");

      // tags
      writer.WriteStartElement("Profile");
      writer.WriteAttributeString("name", "tags");
      writer.WriteStartElement("ImplicitData");
      writer.WriteStartElement("Concepts");

      List<Concept> tagList = CreateTagList();
      foreach (Concept key in tagList)
      {
        writer.WriteStartElement("Concept");
        writer.WriteAttributeString("key", key.Title);
        writer.WriteAttributeString("value", key.Score.ToString());
        writer.WriteAttributeString("from", Utils.AbsoluteWebRoot.ToString());
        writer.WriteAttributeString("updated", key.LastUpdated.ToString());
        writer.WriteEndElement();  // Concept
      }

      writer.WriteEndElement();  // Concepts
      writer.WriteEndElement();  // ImplicitData
      writer.WriteEndElement();  // Profile

      // categories
      //writer.WriteStartElement("Profile");
      //writer.WriteAttributeString("name", "categories");
      //writer.WriteStartElement("ImplicitData");
      //writer.WriteStartElement("Concepts");

      //Dictionary<string, Concept> catList = CreateCategoryList();
      //foreach (string key in catList.Keys)
      //{
      //  writer.WriteStartElement("Concept");
      //  writer.WriteAttributeString("key", key);
      //  writer.WriteAttributeString("value", catList[key].Score.ToString());
      //  writer.WriteAttributeString("from", Utils.AbsoluteWebRoot.ToString());
      //  writer.WriteAttributeString("updated", catList[key].LastUpdated.ToString());
      //  writer.WriteEndElement();  // Concept
      //}

      //writer.WriteEndElement();  // Concepts
      //writer.WriteEndElement();  // ImplicitData
      //writer.WriteEndElement();  // Profile


      // links
      writer.WriteStartElement("Profile");
      writer.WriteAttributeString("name", "links");
      writer.WriteStartElement("ExplicitData");
      writer.WriteStartElement("Concepts");

      Dictionary<string, string> links = CreateLinkList();
      if (links != null)
      {
        foreach (string title in links.Keys)
        {
          string website = links[title];
          writer.WriteStartElement("Source");
          writer.WriteAttributeString("key", website);
          writer.WriteAttributeString("name", title);
          writer.WriteAttributeString("value", "1.0");
          writer.WriteAttributeString("type", "text/html");
          writer.WriteAttributeString("from", Utils.AbsoluteWebRoot.ToString());
          writer.WriteAttributeString("updated", DateTime.Now.ToString());

          writer.WriteStartElement("Author");
          writer.WriteAttributeString("key", title);
          writer.WriteAttributeString("value", "1.0");
          writer.WriteAttributeString("from", Utils.AbsoluteWebRoot.ToString());
          writer.WriteAttributeString("updated", DateTime.Now.ToString());
          writer.WriteEndElement();  // Author

          writer.WriteEndElement();  // Source
        }
      }

      writer.WriteEndElement();  // Concepts
      writer.WriteEndElement();  // ImplicitData
      writer.WriteEndElement();  // Profile      

      writer.WriteEndElement();  // Body      

      CloseWriter(writer);
    }

    #endregion

    #region Create tag-, categorie- and link lists

    // modified version of what's happening in the tag cloud
    private List<Concept> CreateTagList()
    {
      // get all the tags and count the number of usages
      Dictionary<string, Concept> dic = new Dictionary<string, Concept>();
      foreach (Post post in Post.Posts)
      {
        if (post.IsVisible)
        {
          foreach (string tag in post.Tags)
          {
            if (dic.ContainsKey(tag))
            {
              Concept concept = dic[tag];
              concept.Score++;
              if (post.DateModified > concept.LastUpdated)
                concept.LastUpdated = post.DateModified;
              dic[tag] = concept;
            }
            else
            {
              dic[tag] = new Concept(post.DateModified, 1, tag);
            }
          }
        }
      }

      return FindMax(dic);
    }

    private static List<Concept> FindMax(Dictionary<string, Concept> dic)
    {
      float max = 0;
      foreach (Concept concept in dic.Values)
      {
        if (concept.Score > max)
          max = concept.Score;
      }

      List<Concept> list = new List<Concept>();

      // reset values as a percentage of the max
      foreach (string key in dic.Keys)
      {
        list.Add(new Concept(dic[key].LastUpdated, dic[key].Score / max, key));
      }

      list.Sort(delegate(Concept c1, Concept c2) { return c2.Score.CompareTo(c1.Score); });
      
      return list;
    }

    //// modified version of what's happening in the tag cloud
    //private Dictionary<string, Concept> CreateCategoryList()
    //{
    //  // get all the tags and count the number of usages
    //  Dictionary<string, Concept> dic = new Dictionary<string, Concept>();
    //  foreach (Post post in Post.Posts)
    //  {
    //    if (post.IsVisible)
    //    {
    //      foreach (Category cat in post.Categories)
    //      {
    //        if (dic.ContainsKey(cat.Title))
    //        {
    //          Concept concept = dic[cat.Title];
    //          concept.Score++;
    //          if (post.DateModified > concept.LastUpdated)
    //            concept.LastUpdated = post.DateModified;
    //          dic[cat.Title] = concept;
    //        }
    //        else
    //        {
    //          dic[cat.Title] = new Concept(post.DateModified, 1);
    //        }
    //      }
    //    }
    //  }

    //  return FindMax(dic);
    //}

    private Dictionary<string, string> CreateLinkList()
    {
      Dictionary<string, string> dic = new Dictionary<string, string>();

            foreach (BlogRollItem br in BlogRollItem.BlogRolls)
            {
                string title = br.Title;
        string website = br.BlogUrl.ToString();
        dic.Add(title, website);
      }

      return dic;
    }

    #endregion

    private class Concept
    {
      public Concept(DateTime lastUpdated, float score, string title)
      {
        LastUpdated = lastUpdated;
        Score = score;
        Title = title;
      }
      public DateTime LastUpdated;
      public float Score;
      public string Title;
    }

  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.