Greenator Articles
Greenator is a code generator, integrated into Visual Studio as Add-In. It generates text output stored in hidden project items relative to your metadata everytime you change and save your metadata or code templates that are defined in an associated ruleset.
Greenator uses rules to define generation tasks. Rules are basic concept of Greenator. As you have seen in first steps, a rule consists of five properties: Generator, MetaDataFileEnding, Name, ResultFileEnding and TemplateFullName.
| Name: |
Name of the rule |
| MetaDataFileEnding: |
The rule targets all project items which file names ends with MetaDataFileEnding. All metadata following the same pattern should use a common file ending. (e.g. .meta.xml, .data.xml, .myowntyp) |
| ResultFileEnding |
The created item is saved as sub project item with the name of the metadata file added with the ResultFileEnding. (e.g. .cs for Csharb, .vb Visual Basic, .htm, .aspx.....) |
| Generator: |
Choose between Simple, XSLT or External. |
| TemplateFullName |
Path to the fitting template. |
Saves to metadata and templates launches the code generation
A rules covers all metadata files ending with the same name. Each time you save your metadata, the corresponding sub project items are generated overwriting the old ones. Each time you save your code generation template, the rule is applied to all suited metadata.

Multiple rules per metadata pattern
XSLT as code generator is missing the ability to output to multiple files. Greenator ignores that drawback. Greenator allows to connect more than one rule to each MetaDataFileEnding.
Consider generating a general ASP.NET form asking for appointment confirmation. You want to generate an ASPX Webpage, a CodeBehind Class and a master template for sending the content. Greenator allows you to create specialized rules. One rule perhaps uses XSLT to generate the ASPX page and two other rules creates the corresponding CodeBehind and Mail templates. Now you are lucky. Every time you have to create a new always slightly different form, you place a new metadata file with the same file ending in your project. Saving the metadata raise an event all your rules are lucky subscribed to.
Rule chain
It is possible to build up a rule chains. I will explain against the last example dealing with appointment confirmation forms. The meta data will look like
<?
xml version="1.0" encoding="utf-8" ?><
form> <block name="person"> <element name="Name" /> <element name="FirstName" /> <element name="Bithday" /> </block> <block name="address"> <element name="address line1" /> <element name="postal code" /> <element name="city" /> ...
</block> <block name="event"> <element name="unseen before" /> <element name="never ever used but now" /> ...
</block></
form>
Next time they ask you to build up a different form:
<?
xml version="1.0" encoding="utf-8" ?>
<form>
<block name="nownew">
<element name="foo" />
<element name="bar" />
....
</block>
<block name="person">
<element name="Name" />
<element name="FirstName" />
<element name="Bithday" />
....
</block>
<block name="address">
<element name="address line1" />
<element name="postal code" />
<element name="city" />
....
</block>
<block name="otherevent">
<element name="date" />
....
</block>
</form>
You will recognize a repeating pattern in your meta data. Wouldn't it be better to write down your meta data this way?
<?
xml version="1.0" encoding="utf-8" ?><
form> <block name="nownew"> <element name="foo"/> <element name="bar"/> ....
</block> <block_person /> <block_adress /> <block name="otherevent"> <element name="date"/> ....
</block></
form>
You can solve that in integrating more <xsl:template>s into your XSLT Stylesheet. Other way is to define a new rule.

Greenantor is partly based on "RealTypedDataSetAddin" by Ralf Westphal, which he has demonstrated at MSDN TechTalk in autumn 2003. The real subject of this TechTalk was about integrating compiling into application using reflection.emit and CodeDOM. Ralf wrote the core parser which I have extented with a few functions. I named this parser "Simple" in my Greenator project.
"Simples" parses and transform XML. It has only a few command tags. The examples will you following metadata:.
XML to parse:
<?xml version="1.0" encoding="utf-8" ?>
<DocRoot name="myDoc">
<Element foo="Foo" />
<Element foo="Bar">
<SubElement something="Special"/>
</Element>
</DocRoot>
<template>
The Simpe template has to be valid XML. I suggest to use <template> as root element using the Namespace:
Template:
<?
xml version="1.0" encoding="utf-8" ?>
<template xmlns="http://greenator.com/intellisense/simple">
Hello World
</template>
is transformed to
Hello World
<value>
The value tag extracts the content out of the metadata. Syntax is
Template:
<?xml version="1.0" encoding="utf-8" ?>
<template xmlns="http://greenator.com/intellisense/simple">
Name of DocRoot: <value name ="DocRoot/@name" />
second Element's fo <value name ="DocRoot/Element[2]/@foo" />
</template>
is transformed to
Name of DocRoot: myDoc
second Element's fo Bar
<foreach>
Foreach traveres the XML nodes. Alle Values are caled relativ to the current position:
Template:
<?xml version="1.0" encoding="utf-8" ?>
<template xmlns="http://greenator.com/intellisense/simple">
<foreach name ="SubElement">No Subelement, no Text</foreach>
<foreach name="DocRoot/Element">
fo <value name="@foo"/>
<foreach name ="SubElement">
Sub Element exists
</foreach>
</foreach>
</template>
is transformed to
fo Foo
fo Bar
Sub Element exists
<if>
"if" if there are nodes.
Template:
<?xml version="1.0" encoding="utf-8" ?>
<template xmlns="http://greenator.com/intellisense/simple">
<if notNull="DocRoot/Element">There are Elements</if>
<if notNull="DocRoot/Element[@foo='FooBar']">No Text</if>
<if notNull='DocRoot/Element[@foo="Foo"]'>Element exists</if>
</template>
is transformed to
There are Elements
Element exists
<toLower><toUpper><capitalize><decapitalize>
String functions doing what you expect.
Template:
<?xml version="1.0" encoding="utf-8" ?>
<template xmlns="http://greenator.com/intellisense/simple">
<capitalize>i </capitalize>
<decapitalize>Like </decapitalize>
<toUpper>xslt</toUpper>,
<toLower>YOU TOO?</toLower>
</template>
is transformed
I like XSLT, you too?
<replace>
Template:
<?xml version="1.0" encoding="utf-8" ?>
<template xmlns="http://greenator.com/intellisense/simple">
<replace new="Simple" old="XSLT">I prefer "XSLT"</replace>
</template>
is transformed
I like "Simple"
Greenator is a code generator, integrated into Visual Studio as Add-In. It generates text output stored in hidden project items relative to your metadata everytime you change and save your metadata or code templates that are defined in an associated ruleset.
The Greenator project is hosted at http://workspaces.gotdotnet.com/greenator. It is published under the BSD licence. You are all welcome to participate in the progress of the project.
Content:
Tips:
Documention is work in progress.
1. Install Greenator and open Visual Studio
2. Create an empty C# Project (or VB if you prefer)
3. Write your first Class:
public
class Person
{
public Person(){}
public System.String Name;
public System.String FirstName;
}
4. Write a second Class
public
class Product
{
public Product (){}
public System.String Name;
public System.String Description;
public System.String Notice;
}
5. Stop and look at that code.
9 lines of Code and you started to repeat a pattern. You recognize that you are writing boring code. Hey, and you know that there are a lot more such stupid classes waiting for you.
Wouldn’t it be nice to just write down your intention? Let’s try that and write that down in XML:
<
Container name="Person">
<Field name="Name" />
<Field name="FirstName" />
</Container>
<
Container name="Product">
<Field name="Name" />
<Field name="Description" />
<Field name="Notice" />
</Container>
Save the two containers into separate XML files. Name one XML file Person.meta.xml and the second one product.meta.xml.
Congratulation, you just created your first metadata.
6. Write a template using Simple
Create a new xml file in VS Studio.
<?
xml version="1.0" encoding="utf-8" ?>
Type in <template xmlns="
Recognize that Intellisense proposes to choose the namespace http://greenator.com/intellisense/simple. Choose it and close the XML Tag.
The File will look like
<?
xml version="1.0" encoding="utf-8" ?>
<template xmlns="http://greenator.com/intellisense/simple">
</
template>
Paste the content of the first class inside the template tags.
<?
xml version="1.0" encoding="utf-8" ?>
<template xmlns="http://greenator.com/intellisense/simple">
public class Person
{
public Person(){}
public System.String Name;
public System.String FirstName;
}
</template>
In the class definition and the constructor you will sure recognize that for a template “person” has to be replaced with a more generic term. Later it has to be replaced with the value of the name in your container. Do you know Xpath? You can ask the value with Container/@name out of your metadata. That it:
<?
xml version="1.0" encoding="utf-8" ?>
<template x�lns="http://greenator.com/intellisense/simple">
public class <value name ="Container/@name" />
{
public <value name ="Container/@name" />(){}
public System.String Name;
public System.String FirstName;
}
</template>
Ok, but how do to manage the string fields? Person has 2 strings in class, 2 fields inside the container in xml, Product has 3 strings in class and 3 fields inside the container in xml. Each of the strings suits to a field. Lets write it down.
<?xml
version="1.0" encoding="utf-8" ?>
<template xmlns="http://greenator.com/intellisense/simple">
public class <value name ="Container/@name" />
{
public <value name ="Container/@name" />(){}
<foreach name ="Container/Field">
public System.String <value name ="@name" />
</foreach>
}
</template>
Save this file as firsttemplate.xml.
7. Start Greenator (Extras->Greenator)

Edit the ruleItems Collection:

Add following rule:
Generator: Simple
MetaDataFileEnding: .meta.xml
Name: firstGen
ResultFileEnding: .cs
TemplateFullName: path to your firsttemplate.xml
Close all Greenator forms.
8. Use it.
Rename the file extensions of the 2 c# classes or delete them, you don’t need them any more.
Press the “save” button of your template file. The two cs-files are generated as project items hidden under the meta xml files.

Every change to meta file or template recreates these classes.
9. More to come
“simple” is a very small template language. It has only 8 commands:

Try them. You will notice that the most of the generation tasks are just done with the 2 commands foreach and value.
If you need more, combine this technique wirh XPATH. Learn XPATH.
10. More Power
XSLT is built in! Extern Generator is prepared to use an installed CodeSmith.
Generate Now. Simple!