Part 3
Java XML Programmers Reference
Chapter 11: XML Tools for Information Appliances
October 8, 2001
Brought to you by:
Check It Out!
|
Usage and Examples
So now let's look at some code, which transforms XML and notifies us of the new (transformed) document via SAX.
First, recall the signature of Translet.transform():
public void transform(DOM Document,
transletOutputHandler handler)
throws TransletException
And here's our code:
//load and create the translet
Class cls = Class.forName("MyClass");
Translet xlet = (Translet)cls.newInstance();
DOMImpl dom = new DOMImpl(); //will contain the parsed
//source XML
//build DOM tree from source XML into the dom object (not
// shown)
Handler saxHandler; //implements sax.DocumentHandler
TextOutput textOutput; //implements TransletOutputHandler
try {
saxHandler= new Handler();
textOutput = new TextOutput(saxHandler);
//pass the translet the source XML and a handler
xlet.transform(dom, textOutput);
}
catch (Exception e) {
e.printStackTrace();
}
This code performs the following steps:
- Loads the translet with Java reflection
- 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:
/jaxp-1.1/jaxp.jar
/xalan-j_2_1_0/bin/xsltc.jar
/xalan-j_2_1_0/bin/runtime.jar
/xalan-j_2_1_0/bin/BCEL.jar
/xerces-1_4_0/xerces.jar
> java org.apache.xalan.xsltc.compiler.XSLTC TroubleTicket.xsl
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.
|