Click here to Skip to main content
15,881,687 members
Articles / Programming Languages / XSLT

XSL Transform Code Generator for Visual Studio .NET

Rate me:
Please Sign up or sign in to vote.
4.88/5 (14 votes)
13 Feb 2021CPOL3 min read 145.1K   2.4K   75  
A custom tool for Visual Studio .NET which runs an XSL transformation to generate code
/*
 * Copyright (C) 2006 Chris Stefano
 *       cnjs@mweb.co.za
 */
namespace TransformCodeGenerator
{
  using System;
  using System.IO;
  using System.Text;
  using Microsoft.Win32;
  using System.Runtime.InteropServices;
  using CustomToolGenerator;

  /// <summary>
  /// Inheriter must supply the GuidAttribute and CustomToolAttribute 
  /// </summary>
  public abstract class CustomToolBase: BaseCodeGeneratorWithSite
  {

    #region Static Section

    internal static Guid CSharpCategoryGuid = new Guid("FAE04EC1-301F-11D3-BF4B-00C04F79EFBC");

    /// <summary>
    /// 
    /// </summary>
    /// <param name="t"></param>
    [ComRegisterFunction]
    public static void RegisterClass(Type t)
    {
      GuidAttribute guidAttribute = getGuidAttribute(t);
      CustomToolAttribute customToolAttribute = getCustomToolAttribute(t);
      using( RegistryKey key = Registry.LocalMachine.CreateSubKey(GetKeyName(CSharpCategoryGuid, customToolAttribute.Name)))
      {
        key.SetValue("", customToolAttribute.Description);
        key.SetValue("CLSID", "{" + guidAttribute.Value + "}");
        key.SetValue("GeneratesDesignTimeSource", 1);
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="t"></param>
    [ComUnregisterFunction]
    public static void UnregisterClass(Type t)
    {
      CustomToolAttribute customToolAttribute = getCustomToolAttribute(t);
      Registry.LocalMachine.DeleteSubKey(GetKeyName(CSharpCategoryGuid, customToolAttribute.Name), false);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="t"></param>
    /// <returns></returns>
    internal static GuidAttribute getGuidAttribute(Type t)
    {
      return (GuidAttribute)getAttribute(t, typeof(GuidAttribute));
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="t"></param>
    /// <returns></returns>
    internal static CustomToolAttribute getCustomToolAttribute(Type t)
    {
      return (CustomToolAttribute)getAttribute(t, typeof(CustomToolAttribute));
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="t"></param>
    /// <param name="attributeType"></param>
    /// <returns></returns>
    internal static Attribute getAttribute(Type t, Type attributeType)
    {
      object[] attributes = t.GetCustomAttributes(attributeType, /* inherit */ true);
      if(attributes.Length==0)
        throw new Exception(String.Format("Class '{0}' does not provide a '{1}' attribute.", t.FullName, attributeType.FullName));
      return (Attribute)attributes[0];
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="categoryGuid"></param>
    /// <param name="toolName"></param>
    /// <returns></returns>
    internal static string GetKeyName(Guid categoryGuid, string toolName)
    {
      return String.Format("SOFTWARE\\Microsoft\\VisualStudio\\7.1\\Generators\\{{{0}}}\\{1}\\", categoryGuid.ToString(), toolName);
    }

    #endregion

    /// <summary>
    /// 
    /// </summary>
    protected CustomToolBase()
    {
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="inputFileName"></param>
    /// <param name="inputFileContent"></param>
    /// <returns></returns>
    protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
    {

      // get the generated code
      string returnString = this.DoGenerateCode(inputFileName, inputFileContent);

      // return the generated code
      return System.Text.Encoding.ASCII.GetBytes(returnString);

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="inputFileName"></param>
    /// <param name="inputFileContent"></param>
    /// <returns></returns>
    public abstract string DoGenerateCode(string inputFileName, string inputFileContent);

  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
Systems builder and developer, DotNet C#, Ruby, Golang, Linux, Cloud, Open Source contributor.

Comments and Discussions