Generating Switching Code

Problem

You want to generate the switching code that will route incoming messages to their message handlers.

Solution

The message repository stores information about which processes receive which messages. Therefore, given a process name, you can generate a message switch that routes inbound messages to handlers:

<?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:param name="process" select=" '*' "/> <xsl:variable name="message-dir" select=" 'messages' "/> <xsl:variable name="directory-sep" select=" '/' "/> <xsl:variable name="include-ext" select=" '.h' "/> <xsl:template match="MessageRepository"> <!-- Generate source file preliminaries --> <xsl:call-template name="file-start"/> <!-- Generate includes for messages this process recives --> <xsl:apply-templates select="Messages/ Message[Receivers/ ProcessRef = $process or $process = '*']" mode="includes"/> <!-- Generate message switch preliminaries --> <xsl:call-template name="switch-start"/> <!-- Generate switch body --> <xsl:apply-templates select="Messages/ Message[Receivers/ ProcessRef = $process or $process = '*']" mode="switch"/> <!-- Generate switch end --> <xsl:call-template name="switch-end"/> <!-- Generate file end --> <xsl:call-template name="file-end"/> </xsl:template> <!-- Generate an include for each message --> <xsl:template match="Message" mode="includes"> <xsl:text>#include &lt;</xsl:text> ...

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.