Click here to Skip to main content
12,945,927 members (37,094 online)
Click here to Skip to main content
Articles » Languages » C# » Utilities » Downloads

Stats

62.1K views
3.4K downloads
111 bookmarked
Posted 2 Nov 2007

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 RealDolmen
Belgium Belgium
Rudi is a Software Architect at RealDolmen.

You may also be interested in...

Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170518.1 | Last Updated 20 Nov 2007
Article Copyright 2007 by Rudi Breedenraedt
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid