Creates a DOM tree from source XML (this part has been removed, but we will demonstrate how to do this in the section Example: Compiling and Using A Translet)
Creates an object which implements interface org.xml.sax.DocumentHandler (saxHandler)
Creates a TextOutput object and passes it the saxHandler
Starts the transformation by calling transform()
We can clearly see in the bolded line how TextOutput maps its implementation of TransletOutputHandler to DocumentHandler.
Class TransletException
org.apache.xalan.xsltc
public class TransletException
extends java.lang.Exception
This is the exception class thrown by Translet.transform() and all of the methods in interface TransletOutputHandler. Since you probably will use the SAX interface via the TextOutput wrapper (never directly implementing TransletOutputHandler), you won't need to catch TransletExceptions except when calling Translet.transform().
There aren't any special methods in this class. You should handle TransletException objects in the same manner that you treat other Throwable classes extending java.lang.Exception.
Now let's take a look at a creating a translet and an application which uses it.
Example: Compiling and Using a Translet
In this example, we have a trouble ticket system to which our client connects. The client can add, update, and view trouble tickets. For this example, however, we'll concern ourselves only with viewing trouble tickets already in the system. Here are the steps our client application will take:
Read an XML document, representing a single existing trouble ticket. To simplify matters, we'll read the document from persistent storage instead of from a network
Invoke a translet to convert the TroubleTicket document into Wireless Markup Language (WML). You don't need to know WML to understand this example, but if you do, we'll translate the single trouble ticket into a single card in one WML deck. A more advanced system might be able to query and collate multiple trouble tickets into multiple cards within the same deck to save network trips
If we passed the WML to a browser at this point, or wrote our own browser within the application, we could view the WML. However, for simplicity, we'll just write the WML to stdout
Before we write the application, we will need to compile a translet from a "TroubleTicket to WML" XSL stylesheet. So, here's how we'll present this example:
Examine a document instance of a trouble ticket document class
Present an XSL stylesheet that transforms <TroubleTicket/> documents into WML
Compile a translet from the XSL stylesheet
Write the client application that uses the translet and a trouble ticket document instance to produce an instance of a WML document
Sample TroubleTicket.xml
So let's begin by taking a look at TroubleTicket.xml, a TroubleTicket document instance:
This is pretty straightforward so we won't go into it much. This, and documents of this class, will be the source XML to our translet. For brevity's sake, the DTD for this document class has been omitted.
Sample TroubleTicket.xsl
Now let's take a look at the guts of the application: the XSL stylesheet that converts TroubleTicket document instances into WML. We'll call this TroubleTicket.xsl:
An XSLT processor, using the stylesheet above, will produce the following WML from the TroubleTicket document instance in The TroubleTicket Document:
Compiling a Translet
Now let's compile the XSL stylesheet from the previous section into a translet (Java class file). The compiler is class org.apache.xalan.xsltc.compiler.XSLTC, and you will need to set your classpath to include the following JAR files:
Make sure that TroubleTicket.xsl is in the current directory, or provide its full path on the command-line.
You should now have a class file called TroubleTicket.class. It resides in the directory from which you ran XSLTC, unless the d <directory> argument is used. Note that a build script to build this with Apache's ant tool is available from http://www.wrox.com/. See Appendix A.
REPORTS
Analyize In-Line NAC strategies and products.
ANALYTICS Plan and design your enterprise blade server deployments
InformationWeek U.S. IT Salary Survey 2008
Salaries for business technology professionals are falling. Here's what you need to know in order to make good hiring decisions and personal career choices. Download Today