Generating Data Wrappers

Problem

You want to create classes that wrap the data contained in each message with a type-safe interface.

Solution

The solution works in two modes. If a message name is provided in a parameter, then it generates a wrapper only for that message data. Otherwise, if no message is specified, it generates wrappers 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="*"/> <!--The message to generate data for. '*' for all --> <xsl:param name="message" select=" '*' "/> <!--The directory to generate code --> <xsl:param name="generationDir" select=" 'src/' "/> <!--The C++ header extension to use --> <xsl:param name="headerExt" select=" '.h' "/> <!--The C++ source extension to use --> <xsl:param name="sourceExt" select=" '.C' "/> <!--Key to locate data types by name --> <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" /> <!-- Top level template determines which messages to process --> <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 ...

Get XSLT Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.