[ You are here: XTF -> Programming -> Calling Command-line Tools ]

Calling Command-line Tools

It can be very handy to utilize external (even non-Java) applications within your stylesheets. To facilitate this, XTF provides a mechanism for your stylesheet to call any command-line program. The stylesheet can send input and receive output in either plain text or XML.

This is accomplished using XTF's built-in exec:run XSL extension element. It can call any command-line tool installed on the same machine on which the servlet container is running. For example, here's some XSL code that will pause for 10 seconds on a UNIX machine:
<xsl:stylesheet xmlns:exec="java:/org.cdlib.xtf.saxonExt.Exec">
...
  <exec:run command="sleep" xsl:extension-element-prefixes="exec">
    <exec:arg>10</exec:arg>
  </exec:run>
Now let's look at a more complicated example, in which was pass in some XML data to the UNIX sed tool. We ask it to perform a simple text replacement and return the result, which was gather in XML form into an XSLT variable.
<xsl:variable name="myText">
  <exec:run command="sed" xsl:extension-element-prefixes="exec">
    <exec:arg>s/floor/chimney/</exec:arg>
    <exec:input>
      <job>I work as a floor-sweep.</job>
    </exec:input>
  </exec:run>
</xsl:variable>
After this runs, the variable myText will contain the XML element <job>I work as a chimney-sweep.</job>

XTF automatically determines if the data you pass to an external program is XML and serializes it as text. If it's just a string, that string is passed verbatim to the external program. Likewise, XTF parses the output from the external program, and if the output looks like XML then XTF will parse it as such; otherwise, the results will come back in the form of a string. In this way, you have the flexibility to work with any command-line tool whether or not it understands XML.

The <exec:run> element is also capable of killing the external tool if it takes too long to finish. For details, see the XTF Tag Reference section.