Page 168 - The Ultimate Palm Robot
P. 168
Color profile: Generic CMYK printer profile
Composite Default screen
Bots /The Ultimate Palm Robot/ Mukhar & Johnson / 222880-6 / Chapter 6
Chapter 6 The Palm Robot Programmer (PRP) 151
The translate(Program) method is a little more involved. To perform the
translation, your code will call the doTransform(Program, String, String)
method. This method is implemented in the AbstractTranslator class. The
first string parameter is the extension to add to the translated source code file
(including the leading dot). After a program is translated, the PRP displays a
dialog box in which the name and location of the translated source code file is
displayed. If you want to include an additional message with that dialog box,
you can define that message in the translate(Program) method and pass the
message as the second String parameter of the doTransform(Program, String,
String) method. If you do not have an additional message, pass an empty
string rather than a null as the message.
The XSLT Style Sheet
The second step to providing a new translator is to create an XSLT style sheet
that is used by the translator code to convert the XML file to the source code
file. For every program component in the PRP palette, the style sheet needs to
convert the corresponding XML tags created by the component into the ap-
propriate code in the new language.
To complete this step, you already need to know how to create an XSLT
style sheet—teaching you how to write an XSLT style-sheet is beyond the
scope of this book. In simple terms, an XSLT style sheet provides a translation
for every element in an XMLfile. For example, here are two entries of the XSLT
style sheet for the TEA language (which you can find in the PRP installation
directory as tea.xslt):
<xsl:template match="ServoStop">
aServo_Stop(<xsl:value-of select="@servoNumber"/>);
</xsl:template>
<xsl:template match="Sleep">
aCore_Sleep(<xsl:value-of select="number(@sleepSeconds) * 10000"/>);
</xsl:template>
The first entry is the template for the ServoStop element. Whenever the tag
<ServoStop> is found in the XMLfile, the XSLT converter applies the template
to the tag. If you look at the XML listing, you should see a ServoStop tag that
looks like this:
<ServoStop servoNumber="1"/>
P:\010Comp\Bots\880-6\ch06.vp
Monday, May 12, 2003 1:06:27 PM