XML technologies / XSLT / XSLT Kochbuch / Code generation / Generating constant definitions
(Excerpt from "XSLT Kochbuch" by Sal Mangano)
Problem
You want to generate a source file containing all message names as constants equivalent to their message IDs.
Solution
You can build a single transformation targeting C++ by default but easily adaptable to C, C# or Java:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<!-- Der Name der Ausgabe-Quellcodedatei. -->
<xsl:param name="file" select=" 'MESSAGE_IDS.h' "/>
<!-- Standardmäßig werden Konstanten im C++-Stil generiert -->
<xsl:variable name="constants-type" select=" 'const int' "/>
<!-- Der normale C++-Zuweisungsoperator -->
<xsl:variable name="assignment" select=" ' = ' "/>
<!-- Das normale C++-Anweisungsterminierungszeichen -->
<xsl:variable name="terminator" select=" ';' "/>
<!-- Transformiere Repository in eine Sequenz von Konstanten für die Nachrichtendefinitionen -->
<xsl:template match="MessageRepository">
<xsl:call-template name="constants-start"/>
<xsl:apply-templates select="Messages/Message"/>
<xsl:call-template name="constants-end"/>
</xsl:template>
<!-- Aus jeder Nachricht wird ein Kommentar und eine Konstantendefinition -->
<xsl:template match="Message">
<xsl:apply-templates select="." mode="doc" />
<xsl:apply-templates select="." mode="constant" />
</xsl:template>
<!-- C++-Headerdateien beginnen mit einem Include-Wächter -->
<xsl:template name="constants-start">
<xsl:variable name="guard" select="translate($file,'.','_')"/>
<xsl:text>#ifndef </xsl:text>
<xsl:value-of select="$guard"/>
<xsl:text>
</xsl:text>
<xsl:text>#define </xsl:text>
<xsl:value-of select="$guard"/>
<xsl:text>


</xsl:text>
</xsl:template>
<!-- C++-Headerdateien enden mit dem Abschluss des obersten Include-Wächter -->
<xsl:template name="constants-end">
<xsl:variable name="guard" select="translate($file,'.','_')"/>
<xsl:text>


#endif /* </xsl:text>
<xsl:value-of select="$guard"/>
<xsl:text> */
</xsl:text>
</xsl:template>
<!-- Jeder Konstantendefinition geht ein Kommentar voraus, der die zugehörige Nachricht beschreibt. -->
<xsl:template match="Message" mode="doc">
/*
* Zweck: <xsl:call-template name="format-comment"><xsl:with-param name="text" select="Documentation"/></xsl:call-template>
* Datenformat: <xsl:value-of select="DataTypeName"/>
* Von: <xsl:apply-templates select="Senders" mode="doc"/>
* An: <xsl:apply-templates select="Receivers" mode="doc"/>
*/
</xsl:template>
<!-- Wird bei der Generierung von Nachrichtendokumentation benutzt. Listet Sender- oder Empfängerprozesse auf. -->
<xsl:template match="Senders|Receivers" mode="doc">
<xsl:for-each select="ProcessRef">
<xsl:value-of select="."/>
<xsl:if test="position( ) != last( )">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- Dieses Hilfsmittel umbricht Kommentare bei 40 Zeichen -->
<xsl:template name="format-comment">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="string-length($text)<40">
<xsl:value-of select="$text"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($text,1,39)"/>
<xsl:text>*
</xsl:text>
<xsl:call-template name="format-comment">
<xsl:with-param name="text" select="substring($text,40)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Aus jedem Nachrichtennamen wird eine Konstante, deren Wert die Nachrichten-ID ist. -->
<xsl:template match="Message" mode="constant">
<xsl:value-of select="$constants-type"/>
<xsl:text> </xsl:text>
<xsl:value-of select="Name"/>
<xsl:value-of select="$assignment"/>
<xsl:value-of select="MsgId"/>
<xsl:value-of select="$terminator"/>
<xsl:text>
</xsl:text>
</xsl:template>
<!-- Ignoriere Textknoten, die nicht explizit von den obigen Templates behandelt werden. -->
<xsl:template match="text( )"/>
</xsl:stylesheet>
When run on your repository, this transformation generates the following code:
#ifndef MESSAGE_IDS_h
#define MESSAGE_IDS_h
/*
* Zweck: Hinzufügen einer neuen Order.
* Datenformat: AddStockOrderData
* Von: StockClient
* An: StockServer
*/
const int ADD_STOCK_ORDER_ID = 1;
/*
* Zweck: Bestätigung der Order-Buchung.
* Datenformat: AddStockOrderAckData
* Von: StockServer
* An: StockClient
*/
const int ADD_STOCK_ORDER_ACK_ID = 2;
/*
* Zweck: Fehler bei der Order-Buchung. Vielleicht
* verletzt sie eine Regel.
* Datenformat: AddStockOrderNackData
* Von: StockServer
* An: StockClient
*/
const int ADD_STOCK_ORDER_NACK_ID = 3;
// usw ...
#endif /* MESSAGE_IDS_h */Discussion
To support code generation in several languages, I use a stylesheet more complex than a single language would require. Even so, it has not been fully generalized in this chapter. For example, its comment conventions assume a C-like language, and the comments may not match your preferred style. When creating your own code-generation templates, you should make the following adaptations anyway:
Encode language-specific constructs in top-level parameters or variables so imported stylesheets can override them, or, for parameters, values can be supplied when running the stylesheet.
Split the generated components into separate templates that imported stylesheets can override individually.
With this design, minor changes let your transformation generate C-style #define constants:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="msgIds.xslt"/>
<xsl:variable name="constants-type" select=" '#define ' "/>
<xsl:variable name="assignment" select=" ' ' "/>
<xsl:variable name="terminator" select=" '' "/>
</xsl:stylesheet>Java requires everything to be inside a class, but this too can be achieved:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="msgIds.xslt"/>
<xsl:variable name="constants-type" select=" 'public static final int' "/>
<xsl:template name="constants-start">
<xsl:text>final public class MESSAGE_IDS 
</xsl:text>
<xsl:text>{
</xsl:text>
</xsl:template>
<xsl:template name="constants-end">
<xsl:text>

}
</xsl:text>
</xsl:template>
</xsl:stylesheet>
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from "XSLT Kochbuch" is subject to the same terms as the printed edition: the work and all its parts are protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de