Click here to Skip to main content
12,354,716 members (55,024 online)
Click here to Skip to main content
Articles » Languages » C# » Utilities » Downloads

Stats

57.2K views
3.2K downloads
110 bookmarked
Posted

Template Based Code Generator

, 20 Nov 2007 CPOL
A template based, command-line oriented .NET code generator
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
CGen.exe
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
CGen.exe
CGen.exe
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
Arebis.CodeGeneration
Properties
Arebis.CodeGeneration.VisualStudio
Properties
Arebis.CodeGenerator
Extensions
Parsing
Properties
Resources
Templated
backup_v1.zip
_ClassDiagram.cd
CGen
Properties
Resources
backup_v1.zip
_ClassDiagram.cd
ClassDiagram.cd
Arebis.CodeGenerator.csproj.user
_ClassDiagram.cd
backup_v1.zip
Syntax
bin
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
CGen.exe
BuildClasses.cst
Class.cst
using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom.Compiler;
using Arebis.Utils;
using TemplateInfoClass = Arebis.CodeGenerator.Templated.TemplateInfo;

namespace Arebis.CodeGenerator.Templated
{
	[System.Diagnostics.DebuggerStepThrough]
	public class CSCodeBuilder : BaseCodeBuilder
	{
		protected override void AppendConstructorParam(StringBuilder constructorparams, string name, string type)
		{
			constructorparams.Append(", ");
			constructorparams.Append(type);
			constructorparams.Append(" ");
			constructorparams.Append(name);
		}

		protected override void AppendField(StringBuilder fields, string name, string type)
		{
			fields.Append("private ");
			fields.Append(type);
			fields.Append(' ');
			fields.Append(name);
			fields.AppendLine(";");
		}

		protected override void AppendFieldInitializer(StringBuilder fieldinits, string name, string type)
		{
			fieldinits.Append("this.");
			fieldinits.Append(name);
			fieldinits.Append(" = ");
			fieldinits.Append(name);
			fieldinits.AppendLine(";");
		}

		protected override void AppendImport(StringBuilder imports, string nameSpace, string alias)
		{
			imports.Append("using ");
			if (alias != null)
			{
				imports.Append(alias);
				imports.Append("=");
			}
			imports.Append(nameSpace);
			imports.AppendLine(";");
		}

		protected override void AppendLinePragmaBegin(StringBuilder code, string filename, int line)
		{
			code.AppendLine(String.Format("#line {1} \"{0}\"", filename, line));
		}

		protected override void AppendLinePragmaEnd(StringBuilder code)
		{ }
		
		protected override CodeDomProvider GetCodeDomProvider()
		{
			return CodeDomProvider.CreateProvider("c#");
		}

		protected override string GetCodeTemplate()
		{
			return Properties.Resources.CSCodeTemplate;
		}

		protected override string LineToWriteCall(string line)
		{
			StringBuilder result = new StringBuilder(line.Length * 2);

			string[] parts = StringUtils.SplitByTokens(line, TemplateInfoClass.ExpressionTokens, false, true, StringComparison.InvariantCulture);

			result.Append("this.WriteObjects(\"");
			bool isExpression = false;
			foreach (string part in parts)
			{
				if (isExpression)
				{
					result.Append("\", ");
					result.Append(part);
					result.Append(", \"");
				}
				else
				{
					string str = part;
					str = str.Replace("\\", "\\\\");
					str = str.Replace("\"", "\\\"");
					str = str.Replace("\r", "\\\r");
					str = str.Replace("\n", "\\\n");
					result.Append(str);
				}
				isExpression = !isExpression;
			}
			result.Append("\");");

			return result.ToString();
		}
	}
}

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)

Share

About the Author

Rudi Breedenraedt
Architect Wolters Kluwer Belgium
Belgium Belgium
Rudi is a Software Architect at Wolters Kluwer Belgium.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 20 Nov 2007
Article Copyright 2007 by Rudi Breedenraedt
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid