Utils.cs :  » RSS-RDF » GtkSharpRSS » Atom » Utils » 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 » RSS RDF » GtkSharpRSS 
GtkSharpRSS » Atom » Utils » Utils.cs
/* 
    * Utils.cs
  * [ part of Atom.NET library: http://atomnet.sourceforge.net ]
  * Author: Lawrence Oluyede
  * License: BSD-License (see below)
    
  Copyright (c) 2003, 2004 Lawrence Oluyede
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
    * this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
    * notice, this list of conditions and the following disclaimer in the
    * documentation and/or other materials provided with the distribution.
    * Neither the name of the copyright owner nor the names of its
    * contributors may be used to endorse or promote products derived from
    * this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/
using Atom.Core;
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Atom.Utils{
  /// <summary>
  /// A class with some useful methods.
  /// </summary>
  public sealed class Utils
  {
    private Utils() {}
    
    #region Public methods

    /// <summary>
    /// Escapes the given <see cref="String"/>.
    /// </summary>
    /// <param name="text">The <see cref="String"/> to escape.</param>
    /// <returns>The escaped <see cref="String"/>.</returns>
    public static string Escape(string text)
    {
      return HttpUtility.HtmlEncode(text);
    }

    /// <summary>
    /// Unescapes the given <see cref="String"/>.
    /// </summary>
    /// <param name="text">The <see cref="String"/> to unescape.</param>
    /// <returns>The unescaped <see cref="String"/>.</returns>
    public static string Unescape(string text)
    {
      return HttpUtility.HtmlDecode(text);
    }

    /// <summary>
    /// Base64-encodes the given byte array.
    /// </summary>
    /// <param name="array">The byte array to encode.</param>
    /// <returns>A base64-encoded string.</returns>
    public static string Base64Encode(byte[] array)
    {
      return Convert.ToBase64String(array);
    }

    /// <summary>
    /// Base64-encodes the given byte array from the given offset to "len" number of bytes.
    /// </summary>
    /// <param name="array">The byte array to encode.</param>
    /// <param name="offset">The offset from which the encoding starts.</param>
    /// <param name="length">The number of bytes from the offset to encode.</param>
    /// <returns>A base64-encoded string.</returns>
    public static string Base64Encode(byte[] array, int offset, int length)
    {
      return Convert.ToBase64String(array, offset, length);
    }

    /// <summary>
    /// Base64-encodes the given <see cref="String"/>.
    /// </summary>
    /// <param name="text">The string to encode.</param>
    /// <returns>A base64-encoded string.</returns>
    public static string Base64Encode(string text)
    {
      byte[] arr = Encoding.ASCII.GetBytes(text);
      return Base64Encode(arr);
    }

    /// <summary>
    /// Base64-decodes the given byte array.
    /// </summary>
    /// <param name="array">The byte array to decode.</param>
    /// <returns>A base64-decoded array of bytes.</returns>
    public static byte[] Base64Decode(char[] array)
    {
      return Convert.FromBase64CharArray(array, 0, array.Length);
    }

    /// <summary>
    /// Base64-decodes the given byte array from the given offset to "len" number of bytes.
    /// </summary>
    /// <param name="array">The byte array to decode.</param>
    /// <param name="offset">The offset from which the decoding starts.</param>
    /// <param name="length">The number of bytes from the offset to decode.</param>
    /// <returns>A base64-encoded array of bytes.</returns>
    public static byte[] Base64Decode(char[] array, int offset, int length)
    {
      return Convert.FromBase64CharArray(array, offset, length);
    }

    /// <summary>
    /// Base64-decodes the given <see cref="String"/>.
    /// </summary>
    /// <param name="text">The string to decode.</param>
    /// <returns>A base64-decoded array of bytes.</returns>
    public static byte[] Base64Decode(string text)
    {
      return Convert.FromBase64String(text);
    }

    /// <summary>
    /// Checks if the given string representation of a date matches the ISO 8601 format.
    /// </summary>
    /// <param name="theDate">The datetime to check.</param>
    /// <returns>true if the given date is in ISO 8601 format, false otherwise.</returns>
    public static bool IsIso8601Date(string theDate)
    {
      string regExp = @"\d\d\d\d(-\d\d(-\d\d(T\d\d:\d\d(:\d\d(\.\d*)?)?(Z|([+-]\d\d:\d\d))?)?)?)?$";

            Regex rx = new Regex(regExp, RegexOptions.IgnoreCase);
      if(rx.IsMatch(theDate))
        return true;

      return false;
    }

    /// <summary>
    /// Checks if the given string representation of a date matches the ISO 8601 format with the UTC time zone.
    /// </summary>
    /// <param name="theDate">The datetime to check.</param>
    /// <returns>true if the given date is in ISO 8601 format with the UTC time zone, false otherwise.</returns>
    public static bool IsIso8601DateTZ(string theDate)
    {
      if(IsIso8601Date(theDate))
      {
        string regExp = @"Z|([+-]\d\d:\d\d)$";

        Regex rx = new Regex(regExp, RegexOptions.IgnoreCase | RegexOptions.Multiline);
        if(rx.IsMatch(theDate))
          return true;
        else if(theDate.IndexOf('Z') != -1)
          return true;
      }

      return false;
    }

    /// <summary>
    /// Checks if the given string representation of a date matches the ISO 8601 format without the UTC time zone.
    /// </summary>
    /// <param name="theDate">The datetime to check.</param>
    /// <returns>true if the given date is in ISO 8601 format with the UTC time zone, false otherwise.</returns>
    public static bool IsIso8601DateLocalNoTZ(string theDate)
    {
      bool val = IsIso8601DateTZ(theDate);
      
      return !val;
    }

    /// <summary>
    /// Checks if the given string representation of a date matches the ISO 8601 format with a local time zone.
    /// </summary>
    /// <param name="theDate">The datetime to check.</param>
    /// <returns>true if the given date is in ISO 8601 format with a local time zone, false otherwise.</returns>
    public static bool IsIso8601DateLocal(string theDate)
    {
      if(IsIso8601Date(theDate))
      {
        if(theDate.IndexOf('Z') != -1)
          return false;

        return true;
      }

      return false;
    }

    /// <summary>
    /// Checks if the given parameter is a valid email address.
    /// </summary>
    /// <param name="email">The email to check.</param>
    /// <returns>true if the given email is valid, false otherwise.</returns>
    public static bool IsEmail(string email)
    {
      string regExp = @"([a-zA-Z0-9_\-\+\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

      Regex rx = new Regex(regExp, RegexOptions.IgnoreCase | RegexOptions.Multiline);
      if(rx.IsMatch(email))
        return true;

      return false;
    }

    #endregion

    #region Internal methods

    /// <summary>
    /// Parses the <see cref="Relationship"/> enum.
    /// </summary>
    /// <param name="rel">The relationship value to parse.</param>
    /// <returns>The string representation of the relationship value.</returns>
    internal static string ParseRelationship(Relationship rel)
    {
      string val = String.Empty;

      switch(rel)
      {
        case Relationship.Alternate:
          val = "alternate";
          break;

        case Relationship.Next:
          val = "next";
          break;

        case Relationship.Prev:
          val = "prev";
          break;

        case Relationship.ServiceEdit:
          val = "service.edit";
          break;

        case Relationship.ServiceFeed:
          val = "service.feed";
          break;

        case Relationship.ServicePost:
          val = "service.post";
          break;

        case Relationship.Start:
          val = "start";
          break;
      }
      
      return val;
    }

    /// <summary>
    /// Parses a rel attribute.
    /// </summary>
    /// <param name="rel">The relationship value to parse.</param>
    /// <returns>The enum value of the given relationship string.</returns>
    internal static Relationship ParseRelationship(string rel)
    {
      switch(rel)
      {
        case "alternate":
          return Relationship.Alternate;

        case "next":
          return Relationship.Next;

        case "prev":
          return Relationship.Prev;

        case "service.edit":
          return Relationship.ServiceEdit;

        case "service.feed":
          return Relationship.ServiceFeed;

        case "service.post":
          return Relationship.ServicePost;

        case "start":
          return Relationship.Start;

        default:
          return Relationship.Alternate;
      }
    }

    /// <summary>
    /// Parses the <see cref="MediaType"/> enum.
    /// </summary>
    /// <param name="type">The media type to parse.</param>
    /// <returns>The string representation of the media type.</returns>
    internal static string ParseMediaType(MediaType type)
    {
      string val = String.Empty;

      switch(type)
      {
        case MediaType.ApplicationAtomXml:
          val = "application/atom+xml";
          break;

        case MediaType.ApplicationXAtomXml:
          val = "application/x.atom+xml";
          break;

        case MediaType.ApplicationXhtmlXml:
          val = "application/xhtml+xml";
          break;

        case MediaType.ApplicationXml:
          val = "application/xml";
          break;

        case MediaType.MultipartAlternative:
          val = "multipart/alternative";
          break;

        case MediaType.TextHtml:
          val = "text/html";
          break;

        case MediaType.TextPlain:
          val = "text/plain";
          break;

        case MediaType.TextXml:
          val = "text/xml";
          break;

        case MediaType.UnknownType:
          val = "text/plain";
          break;
      }
      
      return val;
    }

    /// <summary>
    /// Parses a media type string.
    /// </summary>
    /// <param name="type">The media type to parse.</param>
    /// <returns>The enum value of the given media type.</returns>
    internal static MediaType ParseMediaType(string type)
    {
      switch(type)
      {
        case "application/atom+xml":
          return MediaType.ApplicationAtomXml;

        case "application/x.atom+xml":
          return MediaType.ApplicationXAtomXml;

        case "application/xhtml+xml":
          return MediaType.ApplicationXhtmlXml;

        case "application/xml":
          return MediaType.ApplicationXml;

        case "multipart/alternative":
          return MediaType.MultipartAlternative;

        case "text/html":
          return MediaType.TextHtml;

        case "text/plain":
          return MediaType.TextPlain;

        case "text/xml":
          return MediaType.TextXml;

        default:
          return MediaType.UnknownType;
      }
    }

    /// <summary>
    /// Parses the <see cref="Language"/> enum.
    /// </summary>
    /// <param name="lang">The language to parse.</param>
    /// <returns>The string representation of the language.</returns>
    internal static string ParseLanguage(Language lang)
    {
      if(lang == Language.UnknownLanguage)
        return String.Empty;

      return lang.ToString().Replace("_", "-");
    }

    /// <summary>
    /// Parses the string representation of a language
    /// </summary>
    /// <param name="lang">The language to parse.</param>
    /// <returns>The <see cref="Language"/> enum value.</returns>
    internal static Language ParseLanguage(string lang)
    {
      string temp = lang.Replace("-", "_");
      return (Language) Enum.Parse(typeof(Language), temp, true);
    }

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