65.9K
CodeProject is changing. Read more.
Home

File2SB - StringBuilder generator

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.46/5 (11 votes)

Dec 1, 2004

3 min read

viewsIcon

52631

downloadIcon

759

File2SB creates StringBuilder functions from text files.

Sample Image - File2SB.jpg

Introduction

Template generation is all the rage these days, especially amongst developers who frequently code similar blocks and functions with only minor differences between them. There are lots of good template based programs out there, but sometimes you need to code something specific for your own internal process.

The Basics of Coding for Templates

Template based code generation goes something like this - first, you create a template. A typical template is a text file, with the fields you need to replace marked in some special way. For example:

Private Function <functionname> As String
  Return "This is Function <functionname>!"
End Function

OK, yes - it is admittedly a stupid example. Anyway, you would typically read the template into a string variable, and then perform a simple Replace() as required, like this:

Dim MyTemplate As String = GetTemplate(filename)
' GetTemplate is some function you wrote that reads and returns the template
Dim FinishedProduct as String = MyTemplate.Replace("<functionname>","HelloWorld")

which would return:

Private Function HelloWorld As String
  Return "This is Function HelloWorld!"
End Function

The Problems with Templates

There are two ways of "getting" templates. The first is read in a text file from somewhere that has already had the "fields" added by hand. This is the most common method of dealing with templates, as the end user can simply edit the templates with any given text editor if they need to change them.

The problem is, sometimes you don't want to allow the end user to change the templates. For example, you might have a program that generates legal forms or corporate documents, that need to remain unchanged except for the fields you designate. In such a case, reading in the text files would be bad, as a malicious end user could alter the template and render the results invalid or unwanted. Or, you wish to offer the option to regenerate the templates to their original status as a program feature.

The answer to this is to use the second method of "getting" templates - generating them internally from code. Using the above example, this would look something like this:

Imports System.Text
...
Function GetTemplate() As String
  Dim sb As New StringBuilder
  sb.Append ("Private Function <functionname> As String")
  sb.Append ("Return ""This is Function <functionname>!""")
  sb.Append ("End Function")
  Return sb.ToString()
End Function

Now, you can guarantee that the template you want will be exactly what you intended it to be.

The Problem Expands

Let's take this one step further. Let's say that your program needs to generate not just one legal document, but hundreds, and they are lengthy technical documents. Writing all the sb.Append() statements to accomplish this would take weeks or even months. Plus, the process can be made more difficult when encountering things such as double quotes - see the special notation that was required above?

The Solution

This is where File2SB steps in. File2SB will read in a given text document, and convert that document into a Function that returns the template as a string. It writes all the required lines for the StringBuilder, and corrects for things like the double quotes. File2SB can write the function in either VB.NET or C# notation as well. Simply click File | Open to read in the template and convert it. From there, you can either cut and paste the converted file into your application, or save it to a text file. At that point, a simple Search and Replace can usually assist you in adding "fields" to your templates.

If you would like to see how File2SB was used to create another tool, please see my other project, PACT.

Notes

I'm a VB.NET developer by trade, so the C# coding is really untested. If you have any problems with it, please let me know so that I can fix the problem and re-release the code. I hope you enjoy File2SB.