Read our list of questions and answers on Java XML
WML TroubleTicketViewer Application
Our last step is to build an application that uses the translet and source XML to generate WML.
You should be able to compile this code with the ant build.xml file available at the Wrox Press web site along with all source code (see Appendix A). Some of this code we've already seen.
import org.apache.xalan.xsltc.*;
import org.apache.xalan.xsltc.dom.DOMImpl;
import org.apache.xalan.xsltc.runtime.TextOutput;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;
import java.io.FileReader;
public class TroubleTicketViewer {
public TroubleTicketViewer(String inputfile)
throws Exception { //load and create the translet
Class cls = Class.forName("TroubleTicket");
Translet xlet = (Translet)cls.newInstance();
DOMImpl dom = new DOMImpl(); //will contain the
parsed source XML
//create SAX 2.0 parser & get the XMLReader
uses object it
SAXParserFactory factory =
SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
//Set the DOM's builder as the XMLReader's SAX 2.0
content handler reader.setContentHandler(dom.getBuilder());
//parse
reader.parse(new InputSource(new
FileReader(inputfile)));
Now that we've parsed the source XML and have it in DOM, let's tell the translet to do the translation. We'll have the translet put the translated document into another DOMImpl object, wmlDOM, although we could have passed it a SAX 1.0 handler to receive callbacks instead.
DOMImpl wmlDOM = new DOMImpl(); //implements
sax.DocumentHandler
TextOutput txtOutput; //implements
TransletOutputHandler
txtOutput = new TextOutput(wmlDOM.getBuilder());
//pass the translet the source XML and a handler
xlet.transform(dom, txtOutput);
Finally, let's output the WML to stdout:
wmlDOM.print(1, 1); //print the root and its children
}
public static void main(String[] args) throws Exception
{
TroubleTicketViewer ttv = new
TroubleTicketViewer(args[0]);
}
}
Run the application:
> java TroubleTicketViewer TroubleTicket.xml
You should get this WML output. It's not pretty, but it's what we expect:
<wml><card
title="Ticket:T746284"
id="T746284"><p><b>Installation failed</b>
<br></br>
<br></br>
Int'l Steel<br></br>
Ann McKinsey<br></br>
303-781-7777<br></br>
SteelPlant2001</p>
</card>
</wml>
The Future
As XSLTC begins to benefit from the open source development cycle at Apache, expect new initiatives for transformation in the information appliance arena. Translets are ideally placed to foster a viable peer-to-peer environment on small devices, bringing with them the power of universal transformation to the growing support for XML on these appliances. Keep an eye on the xalan-dev@xml.apache.org mailing list!
SOAP On Lightweight Clients
The potential for SOAP on lightweight clients is big. SOAP is notoriously slow and resource-intensive compared to RPC, CORBA, and JNI calls, but with more powerful information appliances becoming available each day, this hopefully won't be an issue soon. Simple and relatively infrequent calls, such as requests for weather or stock data from HTTP SOAP servers, are certainly feasible with today's information appliances.
There are currently two lightweight Java SOAP libraries that are worth mentioning:
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