Utilities.cs :  » Bloggers » SubText » Subtext » Web » Admin » 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 » SubText 
SubText » Subtext » Web » Admin » Utilities.cs
#region Disclaimer/Info

///////////////////////////////////////////////////////////////////////////////////////////////////
// Subtext WebLog
// 
// Subtext is an open source weblog system that is a fork of the .TEXT
// weblog system.
//
// For updated news and information please visit http://subtextproject.com/
// Subtext is hosted at Google Code at http://code.google.com/p/subtext/
// The development mailing list is at subtext@googlegroups.com 
//
// This project is licensed under the BSD license.  See the License.txt file for more information.
///////////////////////////////////////////////////////////////////////////////////////////////////

#endregion

using System;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using Subtext.Framework.Components;
using Subtext.Framework.Web;
using Subtext.Web.Properties;

namespace Subtext.Web.Admin{
    public static class Utilities
    {
        public static string ResourcePath
        {
            get { return HttpHelper.CombineWebPaths(HttpContext.Current.Request.ApplicationPath, "/admin/"); }
        }

        internal static string AbsolutePath(string path)
        {
            const string ADMIN_DEFAULT = "/admin";

            if(null != HttpContext.Current)
            {
                if(null != path && path.StartsWith("~"))
                {
                    string relativePath = path.Substring(1, path.Length - 1);
                    string appPath = HttpContext.Current.Request.ApplicationPath;

                    if(relativePath.StartsWith(ADMIN_DEFAULT, StringComparison.InvariantCultureIgnoreCase))
                    {
                        relativePath = relativePath.Replace(ADMIN_DEFAULT,
                                                            "admin");
                    }

                    if(relativePath.StartsWith("/"))
                    {
                        relativePath = relativePath.Substring(1, relativePath.Length - 1);
                    }

                    return string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", appPath,
                                         appPath.EndsWith("/") ? "" : "/",
                                         relativePath);
                }
                else
                {
                    return path;
                }
            }
            else
            {
                return path;
            }
        }

        internal static bool IsNullorEmpty(String test)
        {
            return (test == null || test.Length <= 0);
        }

        internal static string Truncate(string value, int numChars)
        {
            return Truncate(value, numChars, true);
        }

        internal static string Truncate(string value, int numChars, bool useSuffix)
        {
            if(value.Length > numChars)
            {
                if(useSuffix && (value.Length > numChars - Constants.TRUNCATE_SUFFIX.Length))
                {
                    return value.Substring(0, numChars - Constants.TRUNCATE_SUFFIX.Length)
                           + Constants.TRUNCATE_SUFFIX;
                }
                else
                {
                    return value.Substring(0, numChars);
                }
            }
            else
            {
                return value;
            }
        }

        internal static LinkButton CreateLinkButton(string text)
        {
            var result = new LinkButton();
            result.Text = text;
            return result;
        }

        internal static HyperLink CreateHyperLink(string text, string url)
        {
            var result = new HyperLink();
            result.Text = text;
            result.NavigateUrl = url;
            return result;
        }

        internal static WebControl CopyStyles(WebControl control, NameValueCollection styles)
        {
            foreach(string key in styles.Keys)
            {
                control.Style.Add(key, styles[key]);
            }

            return control;
        }

        internal static Encoding GetEncoding(string filepath)
        {
            Encoding result;
            FileStream file = null;
            try
            {
                file = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);

                if(file.CanSeek)
                {
                    // get the bom, if there is one
                    var bom = new byte[4];
                    file.Read(bom, 0, 4);

                    // utf-8
                    if(bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf)
                    {
                        result = Encoding.UTF8;
                    }
                        // ucs-2le, ucs-4le, ucs-16le, utf-16, ucs-2, ucs-4
                    else if((bom[0] == 0xff && bom[1] == 0xfe) ||
                            (bom[0] == 0xfe && bom[1] == 0xff) ||
                            (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff))
                    {
                        result = Encoding.Unicode;
                    }
                        // else ascii
                    else
                    {
                        result = Encoding.ASCII;
                    }
                }
                else
                {
                    // can't detect, set to default
                    result = Encoding.ASCII;
                }

                return result;
            }
            finally
            {
                if(null != file)
                {
                    file.Close();
                }
            }
        }
    }

    internal static class Keys
    {
        internal const string QRYSTR_CATEGORYID = "catid";
        internal const string QRYSTR_CATEGORYTYPE = "catType";
        internal const string QRYSTR_IMAGEID = "imgid";
        internal const string QRYSTR_PAGEINDEX = "pg";
        internal const string SESSION_COOKIETEST = "AdminPageCookieTest";
    }

    internal static class Constants
    {
        internal const bool ALWAYS_EXPAND_DEFAULT = false;
        internal const bool CREATE_ISACTIVE_DEFAULT = true;
        internal const string CSSSTYLE_HIDDEN = "display: none;";

        internal const CategoryType DEFAULT_CATEGORYTYPE = CategoryType.PostCollection;

        internal const int PAGE_SIZE_DEFAULT = 10;
        internal const string TRUNCATE_SUFFIX = "...";

        internal const string URL_CONFIRM = "Confirm.aspx";
        internal const string URL_EDITCATEGORIES = "EditCategories.aspx";
        internal const string URL_EDITGALLERIES = "EditGalleries.aspx";

        internal static readonly string RES_EXCEPTION = Resources.Message_GenericError;

        internal static readonly string RES_FAILURECATEGORYUPDATE = Resources.Message_CategoryUpdateFailed;
        internal static readonly string RES_FAILUREEDIT = Resources.Message_PostOperationFailed;
        internal static readonly string RES_SUCCESSCATEGORYUPDATE = Resources.Message_CategoriesUpdatedSuccessful;
        internal static string RES_SUCCESSEDIT = Resources.Message_UpdateSuccessful;
        internal static string RES_SUCCESSNEW = Resources.Message_PostedSuccessful;
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.