WAUtilities.cs :  » Development » StyleCop » Microsoft » VisualStudio » Package » Web » 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 » Development » StyleCop 
StyleCop » Microsoft » VisualStudio » Package » Web » WAUtilities.cs
/***************************************************************************

Copyright (c) Microsoft Corporation. All rights reserved.
This code is licensed under the Visual Studio SDK license terms.
THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

***************************************************************************/

using System;
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.Win32;
using IServiceProviderSystem.IServiceProvider;
using IOleServiceProviderMicrosoft.VisualStudio.OLE.Interop.IServiceProvider;

namespace Microsoft.VisualStudio.Package.Web{
  internal class WAUtilities
  {

    ///--------------------------------------------------------------------------------------------
    /// <summary>
    /// Helper to create an instance from the local registry given a CLSID Guid
    /// </summary>
    ///--------------------------------------------------------------------------------------------
    public static InterfaceType CreateInstance<InterfaceType>(IServiceProvider serviceProvider, Guid clsid) where InterfaceType : class
    {
      InterfaceType instance = null;

      if (clsid != Guid.Empty)
      {
        ILocalRegistry localRegistry = GetService<ILocalRegistry>(serviceProvider);
        if (localRegistry != null)
        {
          IntPtr pInstance = IntPtr.Zero;
          Guid iidUnknown = NativeMethods.IID_IUnknown;

          try
          {
            localRegistry.CreateInstance(clsid, null, ref iidUnknown, NativeMethods.CLSCTX_INPROC_SERVER, out pInstance);
          }
          catch { }

          if (pInstance != IntPtr.Zero)
          {
            try
            {
              instance = Marshal.GetObjectForIUnknown(pInstance) as InterfaceType;
            }
            catch { }

            try
            {
              Marshal.Release(pInstance);
            }
            catch { }
          }
        }
      }

      return instance;
    }

    ///--------------------------------------------------------------------------------------------
    /// <summary>
    /// Helper to create an instance from the local registry given a CLSID Guid
    /// </summary>
    ///--------------------------------------------------------------------------------------------
    public static InterfaceType CreateSitedInstance<InterfaceType>(IServiceProvider serviceProvider, Guid clsid) where InterfaceType : class
    {
      InterfaceType instance = CreateInstance<InterfaceType>(serviceProvider, clsid);

      if (instance != null)
      {
        IObjectWithSite sitedObject = instance as IObjectWithSite;
        IOleServiceProvider site = GetService<IOleServiceProvider>(serviceProvider);
        if (sitedObject != null && site != null)
        {
          sitedObject.SetSite(site);
        }
        else
        {
          instance = null; // failed to site
        }
      }

      return instance;
    }
    public static InterfaceType CreateSitedInstance<InterfaceType>(IOleServiceProvider serviceProvider, Guid clsid) where InterfaceType : class
    {
      return CreateSitedInstance<InterfaceType>(new ServiceProvider(serviceProvider), clsid);
    }

    ///--------------------------------------------------------------------------------------------
    /// <summary>
    /// Helper to get a shell service interface
    /// </summary>
    ///--------------------------------------------------------------------------------------------
    public static InterfaceType GetService<InterfaceType>(IServiceProvider serviceProvider) where InterfaceType : class
    {
      InterfaceType service = null;

      try
      {
        service = serviceProvider.GetService(typeof(InterfaceType)) as InterfaceType;
      }
      catch
      {
      }

      return service;
    }

    ///--------------------------------------------------------------------------------------------
    /// <summary>
    ///    Helpers to ensure (or remove) trailing slashes (or back slashes)
    /// </summary>
    ///--------------------------------------------------------------------------------------------
    public static string EnsureTrailingSlash(string str)
    {
      if (str != null && !str.EndsWith("/"))
      {
        str += "/";
      }
      return str;
    }
    public static string EnsureTrailingBackSlash(string str)
    {
      if (str != null && !str.EndsWith("\\"))
      {
        str += "\\";
      }
      return str;
    }

    ///--------------------------------------------------------------------------------------------
    /// <summary>
    ///    Helpers to make relative paths/urls from physical paths
    /// </summary>
    ///--------------------------------------------------------------------------------------------
    public static string MakeRelativePath(string fullPath, string basePath)
    {
      string separator = Path.DirectorySeparatorChar.ToString();
      string tempBasePath = basePath;
      string tempFullPath = fullPath;
      string relativePath = null;

      if (!tempBasePath.EndsWith(separator))
        tempBasePath += separator;
      tempFullPath = tempFullPath.ToLowerInvariant();
      tempBasePath = tempBasePath.ToLowerInvariant();

      while (!string.IsNullOrEmpty(tempBasePath))
      {
        if (tempFullPath.StartsWith(tempBasePath))
        {
          relativePath += fullPath.Remove(0, tempBasePath.Length);
          if (relativePath == separator)
            relativePath = "";
          return relativePath;
        }
        else
        {
          tempBasePath = tempBasePath.Remove(tempBasePath.Length - 1);
          int nLastIndex = tempBasePath.LastIndexOf(separator);
          if (-1 != nLastIndex)
          {
            tempBasePath = tempBasePath.Remove(nLastIndex + 1);
            relativePath += "..";
            relativePath += separator;
          }
          else
            return fullPath;
        }
      }

      return fullPath;
    }
  }


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