Developers can create custom code generators and register them with WebORB. WebORB invokes a code generator when user selects a class in the Management Console's service browser or runs the command line code generation utility. A code generator is an XSLT stylesheet (or a collection of XSLT stylesheets) which takes an intermediary XML file created by WebORB and produces final result. The XML file created by WebORB contains information about the selected service (class), all its methods, method arguments, return value and all other referenced types and enumerations. The result produced by code generator may be source code, documentation or any other artifact created based on the information available through the input XML.
Developing Code Generator
The intermediary XML file conforms to the code generation XML schema. XSLT stylesheets must be designed to extract data from the incoming XML files. The stylesheets may reuse the existing code generation XSLT framework designed by Midnight Coders. Consider a sample XSLT stylesheet below:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="codegen.xslt"/>
<xsl:template name="comment.service">
//////////////////////////////////////////////////////
This is a demo code generator which displays a
list of all types referenced by the selected service.
//////////////////////////////////////////////////////
</xsl:template>
<xsl:template name="codegen.vo.folder">
</xsl:template>
<xsl:template name="codegen.service">
<file name="{@name}Service.txt"
<xsl:call-template name="codegen.code" />
</file>
</xsl:template>
<xsl:template name="codegen.code">
<xsl:call-template name="comment.service" />
Service class:
<xsl:value-of select="@fullname" />
Methods:
<xsl:for-each select="method">
<xsl:value-of select="@name"/>()<xsl:if test="count(arg) != 0">
args:
<xsl:for-each select="arg">
<xsl:value-of select="@name"/>:<xsl:value-of select="@type"/><xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:if><xsl:text>
</xsl:text>
</xsl:for-each>
<xsl:if test="count(//datatype) != 0">
Data types:
<xsl:for-each select="//datatype">
<xsl:value-of select="@fullname" />
fields and properties:
<xsl:for-each select="field">
<xsl:value-of select="@name"/>:<xsl:value-of select="@fulltype"/><xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
The stylesheet imports codegen.xslt, thus plugs itself into the Midnight Coders code generation framework.
<xsl:import href="codegen.xslt"/>
The framework starts execution of the code generation by running the "codegen.service" template:
<xsl:template name="codegen.service">
The output of the stylesheet is a single file named after the selected service with the .txt extension using the following elements:
<file name="{@name}Service.txt"
<xsl:call-template name="codegen.code" />
</file>
The "codegen.code" template contains the main generation logic. First it outputs the name of the service:
Service class:
<xsl:value-of select="@fullname" />
The cycles through all the methods and outputs the names of the methods and their arguments:
Methods:
<xsl:for-each select="method">
<xsl:value-of select="@name"/>()<xsl:if test="count(arg) != 0">
args:
<xsl:for-each select="arg">
<xsl:value-of select="@name"/>:<xsl:value-of select="@type"/><xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:if><xsl:text>
</xsl:text>
</xsl:for-each>
Finally, the generator adds to the output a list of all the data types in the same namespace as the service itself:
<xsl:if test="count(//datatype) != 0">
Data types:
<xsl:for-each select="//datatype">
<xsl:value-of select="@fullname" />
fields and properties:
<xsl:for-each select="field">
<xsl:value-of select="@name"/>:<xsl:value-of select="@fulltype"/><xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:if>
Registering Code Generator
To register a code generator, copy the generator's XSLT stylesheets to the [WEBORB HOME]/weborbassets/codegen folder. , locate the <codeGenerator> element in weborb.config - WebORB configuration file. The <formats> element contains a list of all available code generators. Add a line for your code generator following the format below:
<codegenFormat
id="XX" - Must be a unique numeric value
name="XX" - Name of your code generator. This is the name which will be shown in the Management Console
xslt="XX.xslt" - Name of the main XSLT stylesheet for your code generator
excludedServiceBrowsers="XX" - Optional element, if present must list the service browsers for which the code generator is not applicable
/>
To test your code generator, open Management Console and select the Services tab. Browser to a class in the service browser and select your code generator from the available options in the panel on the right.
Deploying and Running Sample
Follow the steps below to deploy and register a code generator with WebORB:
| 1. | Select the sample XSLT stylesheet shown above and save the contents in a file named demogen.xml in [WEBORB HOME]/weborbassets/codegen/. |
| 2. | Open weborb.config from the root of your WebORB installation in a text editor. |
| 3. | Locate the <codeGenerator> node In the configuration file and add the following element to the <formats> section: |
<codegenFormat id="100" name="Sample CodeGen" xslt="demogen.xslt" />
| 4. | Save the configuration file and open WebORB Management Console (if you already had the console open, refresh the page). |
| 5. | Select the Services tab and navigate to .NET Assemblies > weborb.dll > Weborb > Examples > IdentityService |
| 6. | Select the "Sample CodeGen" radio button in the upper right panel. You should see the following output: |
