Click here to Skip to main content
15,892,839 members
Articles / Programming Languages / C#

DSL Tools

Rate me:
Please Sign up or sign in to vote.
4.73/5 (13 votes)
19 Jan 200711 min read 101.7K   655   74  
DSL Tools enables the construction of custom graphical designers and the generation of source code using domain-specific diagrammatic notations.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.OLE.Interop;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;
using System.Diagnostics;
using EnvDTE;
using System.IO;

namespace Company.Smpl
{
    [Guid("D8760704-A993-40ee-89B9-FB77764D99AF")]
    public class SmplGenerator : IVsSingleFileGenerator, IObjectWithSite
    {
        private ServiceProvider _serviceProvider;
        private object _site;

        public string GetDefaultExtension()
        {
            return ".txt";
        }

        public virtual void SetSite(object site)
        {
            _site = site;
            _serviceProvider = null;
        }

        private ServiceProvider SiteServiceProvider
        {
            get
            {
                if (_serviceProvider == null)
                {
                    Microsoft.VisualStudio.OLE.Interop.IServiceProvider provider = _site as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
                    _serviceProvider = new ServiceProvider(provider);
                }
                return _serviceProvider;
            }
        }

        public virtual void GetSite(ref Guid riid, out IntPtr ppvSite)
        {
            if (_site == null)
            {
                throw new COMException("ObjectNotSited", -2147467259);
            }
            IntPtr ptr1 = Marshal.GetIUnknownForObject(_site);
            IntPtr ptr2 = IntPtr.Zero;
            Marshal.QueryInterface(ptr1, ref riid, out ptr2);
            if (ptr2 == IntPtr.Zero)
            {
                throw new COMException("SiteInterfaceNotSupported", -2147467262);
            }
            ppvSite = ptr2;
        }

        public void Generate(
            string inputFilePath,
            string inputFileContents,
            string defaultNamespace,
            out System.IntPtr outputFileContents,
            out int outputLength,
            IVsGeneratorProgress generateProgress)
        {
            ProjectItem item = SiteServiceProvider.GetService(typeof(EnvDTE.ProjectItem)) as ProjectItem;

            outputLength = 0;
            byte[] bytes = null;

            bytes = GenerateCode(inputFilePath, inputFileContents, item, out outputLength);

            if (bytes == null)
            {
                outputFileContents = IntPtr.Zero;
                outputLength = 0;
            }
            else
            {
                outputFileContents = Marshal.AllocCoTaskMem(outputLength);
                Marshal.Copy(bytes, 0, outputFileContents, outputLength);
            }
        }

        private byte[] GenerateCode(string inputFilePath, string inputFileContents, ProjectItem item, out int length)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (StreamWriter writer = new StreamWriter(ms))
                {
                    writer.WriteLine("This line was generated by our SmplGenerator.");
                    writer.Flush();
                    length = (int)ms.Length;
                    return ms.GetBuffer();
                }
            }
            
        }
    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer SoftFluent
France France
Carl Anderson is a .NET consultant at SoftFluent. You can contact him at carl "dot" anderson "at" softfluent "dot" com.

Comments and Discussions