XML technologies / XSLT / XSLT Cookbook / Code generation / Generate a web CGI for test input
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You want to test a process in isolation, taking text data from a form and converting it into a physical message sent to the process under test.
Solution
This server generator accompanies the client generator in recipe Generate a web client for entering test data. Only part of the solution appears here; the discussion provides more details.
The generated server is a C++ CGI program. C++ or C is required because form input becomes a binary message laid out according to C’s memory model. The consuming C++ processes are assumed to expect binary data. The generated CGI uses LibCGI to simplify common CGI tasks such as parsing query strings:
<!DOCTYPE xslt [
<!-- Wird für die Steuerung der Code-Einrückung benutzt -->
<!ENTITY INDENT " ">
]>
<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 Zeilverzeichnis 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="source">
<xsl:with-param name="msg" select="Name"/>
</xsl:apply-templates>
</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 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:param name="msg"/>
<xsl:document href="{concat($generationDir,Name,'CGI',$sourceExt)}">
<xsl:text>
#include <stdio.h>
#include "cgi.h"
#include "</xsl:text>
<xsl:value-of select="Name"/><xsl:value-of select="$headerExt"/>
<xsl:text>"
void cgi_main(cgi_info *cgi)
{
</xsl:text>
<xsl:value-of select="Name"/>
<xsl:text> data ;
form_entry* form_data = get_form_entries(cgi) ;
</xsl:text>
<xsl:for-each select="Members/Member">
<xsl:apply-templates select="key('dataTypes',DataTypeName)" mode="variables">
<xsl:with-param name="field" select="concat($msg,'_',Name)"/>
<xsl:with-param name="var" select="Name"/>
</xsl:apply-templates>
</xsl:for-each>
<xsl:for-each select="Members/Member">
<xsl:apply-templates select="key('dataTypes',DataTypeName)" mode="load">
<xsl:with-param name="field" select="concat('data.',Name)"/>
<xsl:with-param name="var" select="Name"/>
</xsl:apply-templates>
</xsl:for-each>
<xsl:text>
&INDENT;// Daten an die Schlange des getesteten Prozesses übergeben
&INDENT;enqueData(data) ;
}</xsl:text>
</xsl:document>
</xsl:template>
<!-- Deklariere und initialisiere Variablen für alle Felder -->
<xsl:template match="Structure" mode="variables">
<xsl:param name="field"/>
<xsl:param name="var"/>
<xsl:for-each select="Members/Member">
<xsl:apply-templates select="key('dataTypes',DataTypeName)" mode="variables">
<xsl:with-param name="field" select="concat($field,'_',Name)"/>
<xsl:with-param name="var" select="$var"/>
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>
<xsl:template match="*" mode="variables">
<xsl:param name="field"/>
<xsl:param name="var"/>
<xsl:text>&INDENT;const char * </xsl:text>
<xsl:value-of select="$var"/>
<xsl:text> = parmval(form_data, "</xsl:text>
<xsl:value-of select="$field"/>
<xsl:text>");
</xsl:text>
</xsl:template>
<!-- Initialisiere Daten aus dem konvertierten Wert -->
<xsl:template match="Structure" mode="load">
<xsl:param name="field"/>
<xsl:param name="var"/>
<xsl:for-each select="Members/Member">
<xsl:apply-templates select="key('dataTypes',DataTypeName)" mode="load">
<xsl:with-param name="field" select="concat($field,'.',Name)"/>
<xsl:with-param name="var" select="concat($field,'_',Name)"/>
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>
<xsl:template match="Primitive" mode="load">
<xsl:param name="field"/>
<xsl:param name="var"/>
<xsl:text>&INDENT;</xsl:text>
<xsl:value-of select="$field"/>
<xsl:text> = </xsl:text>
<xsl:value-of select="Name"/>
<xsl:text>(</xsl:text>
<xsl:value-of select="$var"/>
<xsl:text>);
</xsl:text>
</xsl:template>
<xsl:template match="Enumeration" mode="load">
<xsl:param name="field"/>
<xsl:param name="var"/>
<xsl:text>&INDENT;</xsl:text>
<xsl:value-of select="$field"/>
<xsl:text> = Enum</xsl:text>
<xsl:value-of select="Name"/>
<xsl:text>NameToVal(</xsl:text>
<xsl:value-of select="$var"/>
<xsl:text>");
</xsl:text>
</xsl:template>
</xsl:stylesheet>Applied to our repository, this transformation generates a cgi program for each message, for example:
#include <stdio.h>
#include "cgi.h"
#include "msg_ids.h"
#include "AddStockOrderData.h"
void cgi_main(cgi_info *cgi)
{
AddStockOrderData data ;
form_entry* form_data = get_form_entries(cgi) ;
const char * symbol = parmval(form_data, "ADD_STOCK_ORDER_symbol");
const char * quantity = parmval(form_data, "ADD_STOCK_ORDER_quantity");
const char * side = parmval(form_data, "ADD_STOCK_ORDER_side");
const char * type = parmval(form_data, "ADD_STOCK_ORDER_type");
const char * price = parmval(form_data, "ADD_STOCK_ORDER_price");
data.symbol = StkSymbol(symbol);
data.quantity = Shares(quantity);
data.side = EnumBuyOrSellNameToVal(side);
data.type = EnumOrderTypeNameToVal(type);
data.price = Real(price);
// Daten an die Warteschlange des getesteten Prozesses übergeben
enqueData(ADD_STOCK_ORDER,data) ;
}Discussion
This solution makes several assumptions. First, primitive types, such as Shares, are classes whose constructors convert strings to the internal type representation, such as int. Second, functions must convert symbolic enumeration names to integral values. These functions can readily be generated from the repository; this is left as an exercise. Third, the generated code assumes a function named enqueData that sends a message to the right process under test. Its interaction with middleware and selection of the correct queue depend on the implementation.
Adapt this generator to your own needs. Once a sufficiently rich metadata repository exists, however, substantial automation becomes possible. The functionality automated here and in the preceding recipe could occupy a full-time programmer, especially when messages change continually.
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 conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights 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