XML technologies / XSLT / XSLT Cookbook / Code generation / Generate code stubs for message handlers

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You want to generate a skeleton for your message handlers.

Solution

This stylesheet generates a simple skeleton containing the appropriate code stub for a given process name, or stub code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <!-- Gibt an, für welchen Prozess Handler generiert werden sollen -->
  <xsl:param name="process"/>
  <!-- Gibt an, für welche Message Handler generiert werden sollen. Der Spezialwert %ALL% bezeichnet alle Messages. -->
  <xsl:param name="message" select=" '%ALL%' "/>
  <!-- Das Zielverzeichnis -->
  <xsl:variable name="message-dir" select=" 'messages' "/>
  <xsl:variable name="directory-sep" select=" '/' "/>
  <xsl:variable name="include-ext" select=" '.h' "/>
  <xsl:template match="MessageRepository">
    <xsl:choose>
      <xsl:when test="$message='%ALL%'"  >
        <xsl:apply-templates select="Messages/Message[Receivers/ProcessRef = $process]"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="Messages/Message[Receivers/ProcessRef = $process and Name=$message]"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="Message">
    <xsl:document href="{concat(Name,'.h')}">
      <xsl:call-template name="makeHeader"/>
    </xsl:document>
    <xsl:document href="{concat(Name,'.cpp')}">
      <xsl:call-template name="makeSource"/>
    </xsl:document>
  </xsl:template>
  <xsl:template name="makeHeader">
  #ifndef <xsl:value-of select="Name"/>_h
  #define <xsl:value-of select="Name"/>_h

  #include &lt;transport/MessageHandler.h&gt;

  // Vorwärtsdeklarationen
  class <xsl:value-of select="DataTypeName"/> ;
  /*!TODO:  Hier weitere Vorwärtsdeklarationen einfügen.*/

  class <xsl:value-of select="Name"/> : public MessageHandler
  {
  public:
    <xsl:value-of select="Name"/>(const <xsl:value-of select="DataTypeName"/>& data) ;
    bool process( ) ;
  private:

    const <xsl:value-of select="DataTypeName"/>& m_Data ;
  } ;

  #endif
  </xsl:template>
  <xsl:template name="makeSource">
  #include &lt;messages/<xsl:value-of select="Name"/>.h&gt;

  /*!TODO:  Hier weitere Includes einfügen. */

    <xsl:value-of select="Name"/>::<xsl:value-of select="Name"/>(const
        <xsl:value-of select="DataTypeName"/>& data)
      : m_Data(data)
    {
    }
    bool <xsl:value-of select="Name"/>::process( )
    {
        /*!TODO:  Hier Nachrichten-Handler-Code einfügen. */
        return true;
    }
  </xsl:template>
</xsl:stylesheet>

It generates a header and source file for every message processed, as the next two examples show.

Example: AddStockOrder.h

#ifndef ADD_STOCK_ORDER_h
#define ADD_STOCK_ORDER_h

#include <transport/MessageHandler.h>

// Vorwärtsdeklarationen
class AddStockOrderData ;
/*!TODO:  Insert addition forward declarations here.*/

class ADD_STOCK_ORDER : public MessageHandler
{
public:
    ADD_STOCK_ORDER(const AddStockOrderData& data) ;
    bool process() ;
private:

    const AddStockOrderData& m_Data ;
} ;

#endif

Example: AddStockOrder.cpp

#include <messages/ADD_STOCK_ORDER.h>

/*!TODO:  Hier weitere Includes einfügen.*/

ADD_STOCK_ORDER::ADD_STOCK_ORDER(const AddStockOrderData& data)
  : m_Data(data)
{
}

bool ADD_STOCK_ORDER::process()
{
  /*!TODO:  Hier Nachrichten-Handler-Code einfügen. */
  return true;
}

Discussion

Much development work repeats in structure while varying in substance. We write large amounts of boilerplate specialized for each context. Repetition leads to boredom, boredom to distraction, and distraction to errors. Generating the repetitive code lets you concentrate on the important parts.

Tools generating code with TODO sections are often called wizards. This message-handler wizard is a simple example. Some commercial wizards generate entire application structures. It is unclear whether XSLT scales to wizards of that size. For simple stub generators with XML input, however, XSLT is preferable to other languages, including Perl. Many Perl enthusiasts disagree, viewing XML processing as another form of text processing. We all know Perl’s reputation as king of text-processing languages. But XML processing is really processing trees containing text nodes, and XSLT transforms such trees more nimbly than Perl. Nevertheless, Perl and XSLT can work together, each contributing its strengths, as chapter Extend and embed XSLT demonstrates. It also includes an XSLT extension that removes the cumbersome verbosity of pure-XSLT generators.

  

 

 

 

back to top