KnowDotNet

Generate Classes From a Document

Code Generation Using Visual Studio Macros

by Les Smith

How can I create code from an existing table in a document.  For example, I have numerous input descriptions in Word Tables.  This article shows you how to use the table data to create input classes automatically.

Recently, I needed to create approximately 40  input classes from tables in a Word document.  The input file to the client application had about 40 different formats for the various types of input data lines.  Obviously, I could have created the classes  by hand, but that would have been very time consuming and error prone.  Since I had done something like this before I quickly determined to create a Macro in Visual Studio to do the code generation for me.  All of the classes will be alike in that they will be self populating data classes.  Their only method of note will be the Parse method, which will be called by the constructor.  The Parse method will parse the input line using a GetField method, also generated in the class, to pull the data from the input data line.  The input data line is passed to the constructor of the object when it is called by the client application.

Since I have developed the application in C#, I will generate the code for the classes in C#.  However, you will notice that macro is coded in VB.NET because that's the code in which Visual Studio Macros must be developed.  If you are a VB.NET developer, it would be a fairly simple job to change the code being generated to VB.NET.  If you do not understand C# that well, you can create the C# classes and then use a
converter to convert the C# code to VB.NET code.

There are two ways that the macro can get the name of the class to be created.  First, you can create a class in your IDE and select all of the code, which would only be the code shown below.

Figure 1 - Class Definition Selected

Empty class

If you select all of the code shown above when the macro is invoked, then the macro will automatically pick up the name "NameLine" from the selection.  If you do not do this, and just place the cursor in a a code window where you want the new class to be inserted, the macro will display an input box for you to enter the name of the new class.

Now, I will show you an example input document with two tables describing two input lines.  For my application, I had approximately 40 tables describing 40 different input line types.  You will notice that I have selected the just the first two data columns in the table and copied them to the Clipboard.  The macro will retrieve the data and use it to generate the code.

Figure 2 - Table Definition of Input Line Format

Table Definition

Having selected the class definition shown in Figure 1, I will double click on the desired macro in the Macro Explorer to invoke the macro, which generates the code shown below in Figure 3.

Figure 3 - Input Class Generated by the Macro

   public class NameLine
   {
      #region Class Constructor
      
// calling the constructor will populate the object
      public NameLine(string line)
      {
         Parse(line);
      }
      #endregion // constructor

      #region private methods
      
private void Parse(string line)
      {
         RecordId = GetField(line, 1, 3);
         RecordType = GetField(line, 4, 6);
         LastName = GetField(line, 10, 25);
         MiddleInitial = GetField(line, 35, 15);
         LastName = GetField(line, 50, 25);
      }

      
private string GetField(string dataLine, int stChar, int len)
      {
        
if(dataLine.Length < 74) dataLine += Space(74 - dataLine.Length);
        
return dataLine.Substring(stChar - 1, len).Trim();
      }

      
// Replacement for VB.NET Space Function
      public string Space(int spaceCount)
      {
        
return String.Empty.PadLeft(spaceCount);
      }
// method: Space
      #endregion // private methods

      #region public properties
      
public string RecordId {get; set; }
      
public string RecordType {get; set; }
      
public string LastName {get; set; }
      
public string MiddleInitial {get; set; }
      
public string LastName {get; set; }
      #endregion // public properties

   }

Note that there is a Space function created in the class, which will be repeated in each class.  Although it is only a one line function, if you have a file of utilities, you could remove its generation from the macro and place a single copy of it in your utility file.  The space function is needed to ensure that the input line is at least as long as the data line definition, otherwise the GetField function will throw an exception because the SubString method will fail.  Also note that the GetField function must be generated in each class because it it generated with variables in it that match the length of the data definition being processed.

Next, comes the Macro code itself.  You will see that it calls a GetClipboard method that is described in another article on this site.  Click here to see that article.  Obviously, I could have used the CodeDom to create the code, but for a quick and dirty solution for creating code, macros are much easier to write than writing code genertion using the CodeDom.  And...when the macro is written and debugged, the code will work without fail.

Figure 4 - The Code Generation Macro

    Public Sub CreateCSInputObjectFromClipboardTwoInputFields()
        
Dim ts As TextSelection = DTE.ActiveDocument.Selection
        
Dim s2 As String = ts.Text
        
Dim mName As Match = _
           Regex.Match(s2,
"\s*(private)*\s*class\s+(?<name>\w+)")
        
Dim name As String = String.Empty

        
' get name from code window if extant
        If mName.Success Then
            name = mName.Groups("name").Value
        
Else
            name = InputBox("Enter name for new Input Object", _
                  
"Enter Object Name", "")
        
End If

        If String.IsNullOrEmpty(name) Then Exit Sub

        Dim s As String = GetClipboardData()
        
If s Is Nothing OrElse s.Trim.Length = 0 Then
            MsgBox("Nothing retrieved from clipboard.")
            
Exit Sub
        End If

        ' remove extraneous characters
        s = s.Replace(" ", "")
        s = s.Replace(
"*", "")
        s = s.Replace(
"-", "_")
        s = s.Replace(
"/", "_")
        s = s.Replace(
".", "")

        
Dim mc As MatchCollection = _
            Regex.Matches(s,
"^(?<id>\w*)\t(?<len>(\d\d\d|\d\d|\d))", _
            RegexOptions.Multiline)
        
Dim cnt As Integer = 0
        
Dim stPtr As Integer = 1
        
Dim sb As New Text.StringBuilder(5000)
        
Dim sbprop As New Text.StringBuilder(5000)
        
Dim sbClass As New Text.StringBuilder(5000)
        
Dim reservedCnt As Integer = 1

        
If name.Length > 0 Then
            sbClass.Append("   public class " & name & vbCrLf)
            sbClass.Append(
"   {" & vbCrLf)

            sb.Append(
"      #region Class Constructor" & vbCrLf)

            
' build constructor
            sb.Append("      // calling the constructor will populate the object" & _
               vbCrLf)
            sb.Append(
"      public " & name & "(string line)" & vbCrLf)
            sb.Append(
"      {" & vbCrLf)
            sb.Append(
"         Parse(line);" & vbCrLf)
            sb.Append(
"      }" & vbCrLf)
            sb.Append(
"      #endregion // constructor" & vbCrLf & vbCrLf)

            sb.Append(
"      #region private methods" & vbCrLf)
            sb.Append(
"      private void Parse(string line)" & vbCrLf)
            sb.Append(
"      {" & vbCrLf)

            sbprop.Append(
"      #region public properties" & vbCrLf)

            
For Each m As Match In mc
                
Dim id As String = m.Groups("id").Value
                
If id.Equals("Reserved") Then
                    id &= reservedCnt.ToString
                    reservedCnt += 1
                
End If

                Dim len As Integer = CType(m.Groups("len").Value, Integer)
                cnt += len

                
' create the automatic property for each match
                sbprop.Append("      public string " & id & " {get; set; }" & vbCrLf)
                sb.Append(
"         " & id & " = GetField(line, " & _
                   stPtr.ToString &
", " & len & ");" & vbCrLf)
                stPtr += len
            
Next

            sbprop.Append("      #endregion // public properties" & vbCrLf & vbCrLf)

            sb.Append(
"      }" & vbCrLf & vbCrLf) ' end of Parse method

            ' create the GetField method for this object
            Const gf1 As String = _
              
"      private string GetField(string dataLine, int stChar, int len)"
            Const gf2 As String = "      {"
            Dim gf22 As String = "         if(dataLine.Length < " & _
               (stPtr - 1).ToString &
") dataLine += Space(" & _
               (stPtr - 1).ToString &
" - dataLine.Length);"
            Const gf3 As String = _
              
"         return dataLine.Substring(stChar - 1, len).Trim();"
            Const gf4 As String = "      }"
            Const gf40 As String = " "
            Const gf41 As String = "      // Replacement for VB.NET Space Function"

            ' create the equivalent method for VB Space function
            Const gf5 As String = "      public string Space(int spaceCount)"
            Const gf6 As String = "      {"
            Const gf7 As String = "         return String.Empty.PadLeft(spaceCount);"
            Const gf11 As String = "      } // method: Space"

            sb.Append(gf1 & vbCrLf)
            sb.Append(gf2 & vbCrLf)
            sb.Append(gf22 & vbCrLf)
            sb.Append(gf3 & vbCrLf)
            sb.Append(gf4 & vbCrLf)
            sb.Append(gf40 & vbCrLf)
            sb.Append(gf41 & vbCrLf)
            sb.Append(gf5 & vbCrLf)
            sb.Append(gf6 & vbCrLf)
            sb.Append(gf7 & vbCrLf)
            sb.Append(gf11 & vbCrLf)
            sb.Append(
"      #endregion // private methods" & vbCrLf & vbCrLf)

            
' concatenate all of the string builders to create the new class
            sbClass.Append(sb.ToString())
            sbClass.Append(sbpriv.ToString())
            sbClass.Append(sbprop.ToString())
            sbClass.Append(
"   }" & vbCrLf)

            
' put the new class into the code window
            ts.Insert(sbClass.ToString)
        
End If
    End Sub

Macros and add-ins are awesome in their ability to generate code that can be counted on to work the first time you use it.  I hope this example sparks a new interest in their use by you from time to time.  I don't write complex macros like this every day, but when I have a need for them, they are invaluable.

Have you tried our newest product, Visual Class Organizer?  You'll be amazed how easy it is to keep the code in your code windows organized.  TRY IT FREE FOR 30 DAYS BY CLICKING HERE.



If you are developing in C# and haven't tried CSharpCompleter, you are wasting valuable time typing hundreds of braces {} daily needlessly.  Try CSharpCompleter for 30 DAYS FREE.



Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog.
  

You can also email me directly at les@KnowDotNet.com.