XML technologies / XSLT / XSLT Cookbook / Code generation / Generate data wrappers

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You want classes wrapping message data with a type-safe interface.

Solution

There are two modes: a message-name parameter generates a wrapper for that message only; without it, wrappers are generated for all messages.

<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>
  <!-- Die Nachricht, für die Daten generiert werden, '*' für alle. -->
  <xsl:param name="message" select=" '*' "/>
  <!-- Das Zielverzeichnis des generierten Codes -->
  <xsl:param name="generationDir" select=" 'src/' "/>
  <!-- Die gewünschte Erweiterung der C++-Headerdateien -->
  <xsl:param name="headerExt" select=" '.h' "/>
  <!-- Die gewünschte Erweiterung der C++-Quelldateien -->
  <xsl:param name="sourceExt" select=" '.C' "/>
  <!-- Ein Schlüssel, um Datentypen über den Namen zu finden -->
  <xsl:key name="dataTypes" match="Structure" use="Name" />
  <xsl:key name="dataTypes" match="Primitive" use="Name" />
  <xsl:key name="dataTypes" match="Array" use="Name" />
  <xsl:key name="dataTypes" match="Enumeration" use="Name" />
  <!-- Das oberste Template bestimmt, welche Nachrichten verarbeitet werden. -->
  <xsl:template match="/">
    <xsl:choose>
      <xsl:when test="$message = '*'">
        <xsl:apply-templates select="*/Messages/*"/>
      </xsl:when>
      <xsl:when test="*/Messages/Message[Name=$message]">
        <xsl:apply-templates select="*/Messages/Message[Name=$message]"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message terminate="yes">No such message name [<xsl:value-of select="$message"/>]</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <!-- Falls der Datentyp der Nachricht im Repository enthalten ist, dann generiere Header- und Quelldatei für einen Daten-Wrapper dafür. -->
  <xsl:template match="Message">
    <xsl:choose>
      <xsl:when test="key('dataTypes',DataTypeName)">
        <xsl:apply-templates select="key('dataTypes',DataTypeName)" mode="header"/>
        <xsl:apply-templates select="key('dataTypes',DataTypeName)" mode="source"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message>Message name [<xsl:value-of select="Name"/>] uses data [<xsl:value-of select="DataTypeName"/>] that is not defined in the repository.</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <!-- Wir generieren nur dann Headerdateien, wenn der Nachrichten-Datentyp eine Struktur ist. Der einzig andere typische Nachrichten-Datentyp ist XML. Für XML-Inhalte generieren wir keine Wrapper. -->
  <xsl:template match="Structure" mode="header">
    <xsl:document href="{concat($generationDir,Name,$headerExt)}">
    #include &lt;primitives/primitives.h&gt;
    class <xsl:value-of select="Name"/>
    {
    public:<xsl:text>&#xa;&#xa;</xsl:text>
      <xsl:for-each select="Members/Member">
        <xsl:text>    </xsl:text>
        <xsl:apply-templates select="key('dataTypes',DataTypeName)" mode="returnType"/>
        get_<xsl:value-of select="Name"/>( ) const ;<xsl:text/>
        <xsl:text>&#xa;</xsl:text>
      </xsl:for-each>
      <xsl:text>&#xa;</xsl:text>
      private:<xsl:text>&#xa;&#xa;</xsl:text>
        <xsl:for-each select="Members/Member">
          <xsl:text>    </xsl:text>
          <xsl:apply-templates select="key('dataTypes',DataTypeName)" mode="data"/>  m_
          <xsl:value-of select="Name"/> ;<xsl:text/>
          <xsl:text>&#xa;</xsl:text>
        </xsl:for-each>
    } ;
    </xsl:document>
  </xsl:template>
  <!-- Wir generieren nur dann Quelldateien, wenn der Nachrichten-Datentyp eine Struktur ist. Der einzig andere typische Nachrichten-Datentyp ist XML. Für XML-Inhalte generieren wir keine Wrapper. -->
  <xsl:template match="Structure" mode="source">
    <xsl:document href="{concat($generationDir,Name,$sourceExt)}">
    #include "<xsl:value-of select="Name"/><xsl:value-of select="$headerExt"/>"
      <xsl:text/>
      <xsl:for-each select="Members/Member">
        <xsl:apply-templates select="key('dataTypes',DataTypeName)" mode="returnType"/>
        <xsl:text>  </xsl:text>
        <xsl:value-of select="../../Name"/>::get_<xsl:value-of select="Name"/>( ) const
        <xsl:text>&#xa;</xsl:text>
        <xsl:text>{&#xa;</xsl:text>
        <xsl:text>    return m_</xsl:text><xsl:value-of select="Name"/>
        <xsl:text>;&#xa;</xsl:text>
        <xsl:text>}&#xa;&#xa;</xsl:text>
      </xsl:for-each>
    </xsl:document>
  </xsl:template>
  <!-- Wir nehmen an, dass Member, die ihrerseits Strukturen sind, als Referenz zurückgegeben werden. -->
  <xsl:template match="Structure" mode="returnType">const <xsl:value-of select="Name"/>&amp;<xsl:text/></xsl:template>
  <!-- Wir bilden Primitive, die durch native C++-Typen dargestellt werden können, auf diese nativen Typen ab. Sonst nehmen wir an, dass das Primitiv extern definiert ist. -->
  <xsl:template match="Primitive" mode="returnType">
    <xsl:choose>
      <xsl:when test="Name='Integer' ">int</xsl:when>
      <xsl:when test="Name='Real' ">double</xsl:when>
      <xsl:otherwise><xsl:value-of select="Name"/></xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="*" mode="returnType">
    <xsl:value-of select="Name"/>
  </xsl:template>
  <xsl:template match="Primitive" mode="data">
    <xsl:choose>
      <xsl:when test="Name='Integer' ">int</xsl:when>
      <xsl:when test="Name='Real' ">double</xsl:when>
      <xsl:otherwise><xsl:value-of select="Name"/></xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="*" mode="data">
    <xsl:value-of select="Name"/>
  </xsl:template>
</xsl:stylesheet>

This generator produces only a get interface, but can easily be extended to generate set functions or other functions too. Here is a generated header:

#include <primitives/primitives.h>

class AddStockOrderData
{
public:

    StkSymbol get_symbol( ) const ;
    Shares get_quantity( ) const ;
    BuyOrSell get_side( ) const ;
    OrderType get_type( ) const ;
    double get_price( ) const ;


private:

    StkSymbol  m_symbol ;
    Shares  m_quantity ;
    BuyOrSell  m_side ;
    OrderType  m_type ;
    double  m_price ;

} ;

And here is a cpp file:

#include "AddStockOrderData.h"

StkSymbol  AddStockOrderData::get_symbol( ) const
{
    return m_symbol;
}

Shares  AddStockOrderData::get_quantity( ) const
{
    return m_quantity;
}

BuyOrSell  AddStockOrderData::get_side( ) const
{
    return m_side;
}

OrderType  AddStockOrderData::get_type( ) const
{
    return m_type;
}

double  AddStockOrderData::get_price( ) const
{
    return m_price;
}

Discussion

Here, wrapper means a class providing an object-oriented interface to data otherwise represented as a plain C struct. On one project I hand-coded every message wrapper. It was tedious but worthwhile. A message can contain prices, quantities and dates, all represented by integers, allowing accidental swaps the compiler cannot detect. Wrappers instead expose class-based primitives such as Price, Qty and Date. Generated wrappers provide the same benefit with less work.

A message repository and XSLT generator automate wrapper production. Wrappers may include additional intelligence, requiring more metadata to generate that behavior. Arrays are a common example: a separate field often records the number of stored elements. A hand-written function adding an element must consult that field to find the next free position and increment it after insertion. Such code can be generated only if the repository knows the relationship between the array-length field and the array itself.

  

 

 

 

back to top